blob: b8dff1057f3493b6263060f4020c0839dca71e68 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100238fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240#else
241#error "Check if your OS uses bitfields for fd_sets"
242#endif
243
Willy Tarreau80587432006-12-24 17:47:20 +0100244void init_proto_http()
245{
Willy Tarreau42250582007-04-01 01:30:43 +0200246 int i;
247 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100248 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200249
Willy Tarreau80587432006-12-24 17:47:20 +0100250 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
251 if (!http_err_msgs[msg]) {
252 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
253 abort();
254 }
255
256 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
257 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
258 }
Willy Tarreau42250582007-04-01 01:30:43 +0200259
260 /* initialize the log header encoding map : '{|}"#' should be encoded with
261 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
262 * URL encoding only requires '"', '#' to be encoded as well as non-
263 * printable characters above.
264 */
265 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
266 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200268 for (i = 0; i < 32; i++) {
269 FD_SET(i, hdr_encode_map);
270 FD_SET(i, url_encode_map);
271 }
272 for (i = 127; i < 256; i++) {
273 FD_SET(i, hdr_encode_map);
274 FD_SET(i, url_encode_map);
275 }
276
277 tmp = "\"#{|}";
278 while (*tmp) {
279 FD_SET(*tmp, hdr_encode_map);
280 tmp++;
281 }
282
283 tmp = "\"#";
284 while (*tmp) {
285 FD_SET(*tmp, url_encode_map);
286 tmp++;
287 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200288
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100289 /* initialize the http header encoding map. The draft httpbis define the
290 * header content as:
291 *
292 * HTTP-message = start-line
293 * *( header-field CRLF )
294 * CRLF
295 * [ message-body ]
296 * header-field = field-name ":" OWS field-value OWS
297 * field-value = *( field-content / obs-fold )
298 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
299 * obs-fold = CRLF 1*( SP / HTAB )
300 * field-vchar = VCHAR / obs-text
301 * VCHAR = %x21-7E
302 * obs-text = %x80-FF
303 *
304 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
305 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
306 * "obs-fold" is volontary forgotten because haproxy remove this.
307 */
308 memset(http_encode_map, 0, sizeof(http_encode_map));
309 for (i = 0x00; i <= 0x08; i++)
310 FD_SET(i, http_encode_map);
311 for (i = 0x0a; i <= 0x1f; i++)
312 FD_SET(i, http_encode_map);
313 FD_SET(0x7f, http_encode_map);
314
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200315 /* memory allocations */
316 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100317 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100318}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319
Willy Tarreau53b6c742006-12-17 13:37:46 +0100320/*
321 * We have 26 list of methods (1 per first letter), each of which can have
322 * up to 3 entries (2 valid, 1 null).
323 */
324struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100325 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100326 int len;
327 const char text[8];
328};
329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100330const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331 ['C' - 'A'] = {
332 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
333 },
334 ['D' - 'A'] = {
335 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
336 },
337 ['G' - 'A'] = {
338 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
339 },
340 ['H' - 'A'] = {
341 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
342 },
343 ['P' - 'A'] = {
344 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
345 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
346 },
347 ['T' - 'A'] = {
348 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
349 },
350 /* rest is empty like this :
351 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
352 */
353};
354
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100355const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
356 [HTTP_METH_NONE] = { "", 0 },
357 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
358 [HTTP_METH_GET] = { "GET", 3 },
359 [HTTP_METH_HEAD] = { "HEAD", 4 },
360 [HTTP_METH_POST] = { "POST", 4 },
361 [HTTP_METH_PUT] = { "PUT", 3 },
362 [HTTP_METH_DELETE] = { "DELETE", 6 },
363 [HTTP_METH_TRACE] = { "TRACE", 5 },
364 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
365};
366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100367/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100369 * RFC2616 for those chars.
370 */
371
372const char http_is_spht[256] = {
373 [' '] = 1, ['\t'] = 1,
374};
375
376const char http_is_crlf[256] = {
377 ['\r'] = 1, ['\n'] = 1,
378};
379
380const char http_is_lws[256] = {
381 [' '] = 1, ['\t'] = 1,
382 ['\r'] = 1, ['\n'] = 1,
383};
384
385const char http_is_sep[256] = {
386 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
387 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
388 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
389 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
390 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
391};
392
393const char http_is_ctl[256] = {
394 [0 ... 31] = 1,
395 [127] = 1,
396};
397
398/*
399 * A token is any ASCII char that is neither a separator nor a CTL char.
400 * Do not overwrite values in assignment since gcc-2.95 will not handle
401 * them correctly. Instead, define every non-CTL char's status.
402 */
403const char http_is_token[256] = {
404 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
405 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
406 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
407 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
408 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
409 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
410 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
411 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
412 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
413 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
414 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
415 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
416 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
417 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
418 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
419 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
420 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
421 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
422 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
423 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
424 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
425 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
426 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
427 ['|'] = 1, ['}'] = 0, ['~'] = 1,
428};
429
430
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100431/*
432 * An http ver_token is any ASCII which can be found in an HTTP version,
433 * which includes 'H', 'T', 'P', '/', '.' and any digit.
434 */
435const char http_is_ver_token[256] = {
436 ['.'] = 1, ['/'] = 1,
437 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
438 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
439 ['H'] = 1, ['P'] = 1, ['T'] = 1,
440};
441
442
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100444 * Adds a header and its CRLF at the tail of the message's buffer, just before
445 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100446 * The header is also automatically added to the index <hdr_idx>, and the end
447 * of headers is automatically adjusted. The number of bytes added is returned
448 * on success, otherwise <0 is returned indicating an error.
449 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100450int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100451{
452 int bytes, len;
453
454 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200455 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456 if (!bytes)
457 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100458 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
460}
461
462/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463 * Adds a header and its CRLF at the tail of the message's buffer, just before
464 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100465 * the buffer is only opened and the space reserved, but nothing is copied.
466 * The header is also automatically added to the index <hdr_idx>, and the end
467 * of headers is automatically adjusted. The number of bytes added is returned
468 * on success, otherwise <0 is returned indicating an error.
469 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100470int http_header_add_tail2(struct http_msg *msg,
471 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472{
473 int bytes;
474
Willy Tarreau9b28e032012-10-12 23:49:43 +0200475 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100476 if (!bytes)
477 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100478 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100479 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
480}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200481
482/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100483 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
484 * If so, returns the position of the first non-space character relative to
485 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
486 * to return a pointer to the place after the first space. Returns 0 if the
487 * header name does not match. Checks are case-insensitive.
488 */
489int http_header_match2(const char *hdr, const char *end,
490 const char *name, int len)
491{
492 const char *val;
493
494 if (hdr + len >= end)
495 return 0;
496 if (hdr[len] != ':')
497 return 0;
498 if (strncasecmp(hdr, name, len) != 0)
499 return 0;
500 val = hdr + len + 1;
501 while (val < end && HTTP_IS_SPHT(*val))
502 val++;
503 if ((val >= end) && (len + 2 <= end - hdr))
504 return len + 2; /* we may replace starting from second space */
505 return val - hdr;
506}
507
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200508/* Find the first or next occurrence of header <name> in message buffer <sol>
509 * using headers index <idx>, and return it in the <ctx> structure. This
510 * structure holds everything necessary to use the header and find next
511 * occurrence. If its <idx> member is 0, the header is searched from the
512 * beginning. Otherwise, the next occurrence is returned. The function returns
513 * 1 when it finds a value, and 0 when there is no more. It is very similar to
514 * http_find_header2() except that it is designed to work with full-line headers
515 * whose comma is not a delimiter but is part of the syntax. As a special case,
516 * if ctx->val is NULL when searching for a new values of a header, the current
517 * header is rescanned. This allows rescanning after a header deletion.
518 */
519int http_find_full_header2(const char *name, int len,
520 char *sol, struct hdr_idx *idx,
521 struct hdr_ctx *ctx)
522{
523 char *eol, *sov;
524 int cur_idx, old_idx;
525
526 cur_idx = ctx->idx;
527 if (cur_idx) {
528 /* We have previously returned a header, let's search another one */
529 sol = ctx->line;
530 eol = sol + idx->v[cur_idx].len;
531 goto next_hdr;
532 }
533
534 /* first request for this header */
535 sol += hdr_idx_first_pos(idx);
536 old_idx = 0;
537 cur_idx = hdr_idx_first_idx(idx);
538 while (cur_idx) {
539 eol = sol + idx->v[cur_idx].len;
540
541 if (len == 0) {
542 /* No argument was passed, we want any header.
543 * To achieve this, we simply build a fake request. */
544 while (sol + len < eol && sol[len] != ':')
545 len++;
546 name = sol;
547 }
548
549 if ((len < eol - sol) &&
550 (sol[len] == ':') &&
551 (strncasecmp(sol, name, len) == 0)) {
552 ctx->del = len;
553 sov = sol + len + 1;
554 while (sov < eol && http_is_lws[(unsigned char)*sov])
555 sov++;
556
557 ctx->line = sol;
558 ctx->prev = old_idx;
559 ctx->idx = cur_idx;
560 ctx->val = sov - sol;
561 ctx->tws = 0;
562 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
563 eol--;
564 ctx->tws++;
565 }
566 ctx->vlen = eol - sov;
567 return 1;
568 }
569 next_hdr:
570 sol = eol + idx->v[cur_idx].cr + 1;
571 old_idx = cur_idx;
572 cur_idx = idx->v[cur_idx].next;
573 }
574 return 0;
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Find the end of the header value contained between <s> and <e>. See RFC2616,
578 * par 2.2 for more information. Note that it requires a valid header to return
579 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200580 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100581char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200582{
583 int quoted, qdpair;
584
585 quoted = qdpair = 0;
586 for (; s < e; s++) {
587 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200588 else if (quoted) {
589 if (*s == '\\') qdpair = 1;
590 else if (*s == '"') quoted = 0;
591 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200592 else if (*s == '"') quoted = 1;
593 else if (*s == ',') return s;
594 }
595 return s;
596}
597
598/* Find the first or next occurrence of header <name> in message buffer <sol>
599 * using headers index <idx>, and return it in the <ctx> structure. This
600 * structure holds everything necessary to use the header and find next
601 * occurrence. If its <idx> member is 0, the header is searched from the
602 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100603 * 1 when it finds a value, and 0 when there is no more. It is designed to work
604 * with headers defined as comma-separated lists. As a special case, if ctx->val
605 * is NULL when searching for a new values of a header, the current header is
606 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 */
608int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100609 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200610 struct hdr_ctx *ctx)
611{
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 char *eol, *sov;
613 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200614
Willy Tarreau68085d82010-01-18 14:54:04 +0100615 cur_idx = ctx->idx;
616 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200617 /* We have previously returned a value, let's search
618 * another one on the same line.
619 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200620 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200621 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200623 eol = sol + idx->v[cur_idx].len;
624
625 if (sov >= eol)
626 /* no more values in this header */
627 goto next_hdr;
628
Willy Tarreau68085d82010-01-18 14:54:04 +0100629 /* values remaining for this header, skip the comma but save it
630 * for later use (eg: for header deletion).
631 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200632 sov++;
633 while (sov < eol && http_is_lws[(unsigned char)*sov])
634 sov++;
635
636 goto return_hdr;
637 }
638
639 /* first request for this header */
640 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200642 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200643 while (cur_idx) {
644 eol = sol + idx->v[cur_idx].len;
645
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200646 if (len == 0) {
647 /* No argument was passed, we want any header.
648 * To achieve this, we simply build a fake request. */
649 while (sol + len < eol && sol[len] != ':')
650 len++;
651 name = sol;
652 }
653
Willy Tarreau33a7e692007-06-10 19:45:56 +0200654 if ((len < eol - sol) &&
655 (sol[len] == ':') &&
656 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100657 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 sov = sol + len + 1;
659 while (sov < eol && http_is_lws[(unsigned char)*sov])
660 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100661
Willy Tarreau33a7e692007-06-10 19:45:56 +0200662 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100663 ctx->prev = old_idx;
664 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200665 ctx->idx = cur_idx;
666 ctx->val = sov - sol;
667
668 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200669 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200670 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200671 eol--;
672 ctx->tws++;
673 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200674 ctx->vlen = eol - sov;
675 return 1;
676 }
677 next_hdr:
678 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100679 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200680 cur_idx = idx->v[cur_idx].next;
681 }
682 return 0;
683}
684
685int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100686 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 struct hdr_ctx *ctx)
688{
689 return http_find_header2(name, strlen(name), sol, idx, ctx);
690}
691
Willy Tarreau68085d82010-01-18 14:54:04 +0100692/* Remove one value of a header. This only works on a <ctx> returned by one of
693 * the http_find_header functions. The value is removed, as well as surrounding
694 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100695 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100696 * message <msg>. The new index is returned. If it is zero, it means there is
697 * no more header, so any processing may stop. The ctx is always left in a form
698 * that can be handled by http_find_header2() to find next occurrence.
699 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100700int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100701{
702 int cur_idx = ctx->idx;
703 char *sol = ctx->line;
704 struct hdr_idx_elem *hdr;
705 int delta, skip_comma;
706
707 if (!cur_idx)
708 return 0;
709
710 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200711 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100712 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200713 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100714 http_msg_move_end(msg, delta);
715 idx->used--;
716 hdr->len = 0; /* unused entry */
717 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100718 if (idx->tail == ctx->idx)
719 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100720 ctx->idx = ctx->prev; /* walk back to the end of previous header */
721 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
722 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 return ctx->idx;
725 }
726
727 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200728 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
729 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100730 */
731
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200732 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200733 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200734 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 NULL, 0);
736 hdr->len += delta;
737 http_msg_move_end(msg, delta);
738 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200739 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100740 return ctx->idx;
741}
742
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100743/* This function handles a server error at the stream interface level. The
744 * stream interface is assumed to be already in a closed state. An optional
745 * message is copied into the input buffer, and an HTTP status code stored.
746 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100747 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200749static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100750 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200752 channel_auto_read(si->ob);
753 channel_abort(si->ob);
754 channel_auto_close(si->ob);
755 channel_erase(si->ob);
756 channel_auto_close(si->ib);
757 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100758 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200759 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200760 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200762 if (!(s->flags & SN_ERR_MASK))
763 s->flags |= err;
764 if (!(s->flags & SN_FINST_MASK))
765 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766}
767
Willy Tarreau80587432006-12-24 17:47:20 +0100768/* This function returns the appropriate error location for the given session
769 * and message.
770 */
771
Willy Tarreau783f2582012-09-04 12:19:04 +0200772struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100773{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200774 if (s->be->errmsg[msgnum].str)
775 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100776 else if (s->fe->errmsg[msgnum].str)
777 return &s->fe->errmsg[msgnum];
778 else
779 return &http_err_chunks[msgnum];
780}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781
Willy Tarreau53b6c742006-12-17 13:37:46 +0100782/*
783 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
784 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
785 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100786enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100787{
788 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100789 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100790
791 m = ((unsigned)*str - 'A');
792
793 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100794 for (h = http_methods[m]; h->len > 0; h++) {
795 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100796 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100797 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100798 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100799 };
800 return HTTP_METH_OTHER;
801 }
802 return HTTP_METH_NONE;
803
804}
805
Willy Tarreau21d2af32008-02-14 20:25:24 +0100806/* Parse the URI from the given transaction (which is assumed to be in request
807 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
808 * It is returned otherwise.
809 */
810static char *
811http_get_path(struct http_txn *txn)
812{
813 char *ptr, *end;
814
Willy Tarreau9b28e032012-10-12 23:49:43 +0200815 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100816 end = ptr + txn->req.sl.rq.u_l;
817
818 if (ptr >= end)
819 return NULL;
820
821 /* RFC2616, par. 5.1.2 :
822 * Request-URI = "*" | absuri | abspath | authority
823 */
824
825 if (*ptr == '*')
826 return NULL;
827
828 if (isalpha((unsigned char)*ptr)) {
829 /* this is a scheme as described by RFC3986, par. 3.1 */
830 ptr++;
831 while (ptr < end &&
832 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
833 ptr++;
834 /* skip '://' */
835 if (ptr == end || *ptr++ != ':')
836 return NULL;
837 if (ptr == end || *ptr++ != '/')
838 return NULL;
839 if (ptr == end || *ptr++ != '/')
840 return NULL;
841 }
842 /* skip [user[:passwd]@]host[:[port]] */
843
844 while (ptr < end && *ptr != '/')
845 ptr++;
846
847 if (ptr == end)
848 return NULL;
849
850 /* OK, we got the '/' ! */
851 return ptr;
852}
853
William Lallemand65ad6e12014-01-31 15:08:02 +0100854/* Parse the URI from the given string and look for the "/" beginning the PATH.
855 * If not found, return NULL. It is returned otherwise.
856 */
857static char *
858http_get_path_from_string(char *str)
859{
860 char *ptr = str;
861
862 /* RFC2616, par. 5.1.2 :
863 * Request-URI = "*" | absuri | abspath | authority
864 */
865
866 if (*ptr == '*')
867 return NULL;
868
869 if (isalpha((unsigned char)*ptr)) {
870 /* this is a scheme as described by RFC3986, par. 3.1 */
871 ptr++;
872 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
873 ptr++;
874 /* skip '://' */
875 if (*ptr == '\0' || *ptr++ != ':')
876 return NULL;
877 if (*ptr == '\0' || *ptr++ != '/')
878 return NULL;
879 if (*ptr == '\0' || *ptr++ != '/')
880 return NULL;
881 }
882 /* skip [user[:passwd]@]host[:[port]] */
883
884 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
885 ptr++;
886
887 if (*ptr == '\0' || *ptr == ' ')
888 return NULL;
889
890 /* OK, we got the '/' ! */
891 return ptr;
892}
893
Willy Tarreau71241ab2012-12-27 11:30:54 +0100894/* Returns a 302 for a redirectable request that reaches a server working in
895 * in redirect mode. This may only be called just after the stream interface
896 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
897 * follow normal proxy processing. NOTE: this function is designed to support
898 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100899 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100900void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100901{
902 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100903 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100904 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200905 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906
907 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100908 trash.len = strlen(HTTP_302);
909 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100910
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100911 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100912
Willy Tarreauefb453c2008-10-26 20:49:47 +0100913 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100914 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100915 return;
916
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100917 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100918 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100919 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
920 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100921 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100922
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200923 /* 3: add the request URI. Since it was already forwarded, we need
924 * to temporarily rewind the buffer.
925 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200927 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200928
Willy Tarreauefb453c2008-10-26 20:49:47 +0100929 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200930 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200931
Willy Tarreau9b28e032012-10-12 23:49:43 +0200932 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200933
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934 if (!path)
935 return;
936
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100937 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100938 return;
939
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100940 memcpy(trash.str + trash.len, path, len);
941 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100942
943 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100944 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
945 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100946 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100947 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
948 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100949 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950
951 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200952 si_shutr(si);
953 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100954 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100955 si->state = SI_ST_CLO;
956
957 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200958 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100959
960 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100961 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500962 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963}
964
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100965/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100966 * that the server side is closed. Note that err_type is actually a
967 * bitmask, where almost only aborts may be cumulated with other
968 * values. We consider that aborted operations are more important
969 * than timeouts or errors due to the fact that nobody else in the
970 * logs might explain incomplete retries. All others should avoid
971 * being cumulated. It should normally not be possible to have multiple
972 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100973 * Note that connection errors appearing on the second request of a keep-alive
974 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100975 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100976void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100977{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979
980 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100981 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200982 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100984 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100985 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
986 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100988 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200989 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100991 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200992 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100993 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100995 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
996 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100997 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100998 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +0100999 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001000 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001001 else if (err_type & SI_ET_CONN_RES)
1002 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001003 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1004 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001005 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001006 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001007 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001008}
1009
Willy Tarreau42250582007-04-01 01:30:43 +02001010extern const char sess_term_cond[8];
1011extern const char sess_fin_state[8];
1012extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001013struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001014struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001015struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001016
Willy Tarreau117f59e2007-03-04 18:17:17 +01001017/*
1018 * Capture headers from message starting at <som> according to header list
1019 * <cap_hdr>, and fill the <idx> structure appropriately.
1020 */
1021void capture_headers(char *som, struct hdr_idx *idx,
1022 char **cap, struct cap_hdr *cap_hdr)
1023{
1024 char *eol, *sol, *col, *sov;
1025 int cur_idx;
1026 struct cap_hdr *h;
1027 int len;
1028
1029 sol = som + hdr_idx_first_pos(idx);
1030 cur_idx = hdr_idx_first_idx(idx);
1031
1032 while (cur_idx) {
1033 eol = sol + idx->v[cur_idx].len;
1034
1035 col = sol;
1036 while (col < eol && *col != ':')
1037 col++;
1038
1039 sov = col + 1;
1040 while (sov < eol && http_is_lws[(unsigned char)*sov])
1041 sov++;
1042
1043 for (h = cap_hdr; h; h = h->next) {
1044 if ((h->namelen == col - sol) &&
1045 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1046 if (cap[h->index] == NULL)
1047 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001048 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001049
1050 if (cap[h->index] == NULL) {
1051 Alert("HTTP capture : out of memory.\n");
1052 continue;
1053 }
1054
1055 len = eol - sov;
1056 if (len > h->len)
1057 len = h->len;
1058
1059 memcpy(cap[h->index], sov, len);
1060 cap[h->index][len]=0;
1061 }
1062 }
1063 sol = eol + idx->v[cur_idx].cr + 1;
1064 cur_idx = idx->v[cur_idx].next;
1065 }
1066}
1067
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069/* either we find an LF at <ptr> or we jump to <bad>.
1070 */
1071#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1072
1073/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1074 * otherwise to <http_msg_ood> with <state> set to <st>.
1075 */
1076#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1077 ptr++; \
1078 if (likely(ptr < end)) \
1079 goto good; \
1080 else { \
1081 state = (st); \
1082 goto http_msg_ood; \
1083 } \
1084 } while (0)
1085
1086
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001088 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1090 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1091 * will give undefined results.
1092 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1093 * and that msg->sol points to the beginning of the response.
1094 * If a complete line is found (which implies that at least one CR or LF is
1095 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1096 * returned indicating an incomplete line (which does not mean that parts have
1097 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1098 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1099 * upon next call.
1100 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001101 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1103 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001104 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001106const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001107 enum ht_state state, const char *ptr, const char *end,
1108 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001109{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001110 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001111
Willy Tarreau8973c702007-01-21 23:58:29 +01001112 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001114 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001115 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1117
1118 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001119 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001120 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1121 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001122 state = HTTP_MSG_ERROR;
1123 break;
1124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001126 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001127 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001128 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 goto http_msg_rpcode;
1130 }
1131 if (likely(HTTP_IS_SPHT(*ptr)))
1132 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1133 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001134 state = HTTP_MSG_ERROR;
1135 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001136
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 if (likely(!HTTP_IS_LWS(*ptr)))
1140 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1141
1142 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001143 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001144 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1145 }
1146
1147 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001148 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 http_msg_rsp_reason:
1150 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001151 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 msg->sl.st.r_l = 0;
1153 goto http_msg_rpline_eol;
1154
Willy Tarreau8973c702007-01-21 23:58:29 +01001155 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001156 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001158 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001159 goto http_msg_rpreason;
1160 }
1161 if (likely(HTTP_IS_SPHT(*ptr)))
1162 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1163 /* so it's a CR/LF, so there is no reason phrase */
1164 goto http_msg_rsp_reason;
1165
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001167 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 if (likely(!HTTP_IS_CRLF(*ptr)))
1169 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001170 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001171 http_msg_rpline_eol:
1172 /* We have seen the end of line. Note that we do not
1173 * necessarily have the \n yet, but at least we know that we
1174 * have EITHER \r OR \n, otherwise the response would not be
1175 * complete. We can then record the response length and return
1176 * to the caller which will be able to register it.
1177 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001178 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 return ptr;
1180
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001182#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1184 exit(1);
1185#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001186 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001187 }
1188
1189 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001190 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 if (ret_state)
1192 *ret_state = state;
1193 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001194 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001196}
1197
Willy Tarreau8973c702007-01-21 23:58:29 +01001198/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 * This function parses a request line between <ptr> and <end>, starting with
1200 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1201 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1202 * will give undefined results.
1203 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1204 * and that msg->sol points to the beginning of the request.
1205 * If a complete line is found (which implies that at least one CR or LF is
1206 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1207 * returned indicating an incomplete line (which does not mean that parts have
1208 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1209 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1210 * upon next call.
1211 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001212 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001213 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1214 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001215 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001216 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001217const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001218 enum ht_state state, const char *ptr, const char *end,
1219 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001220{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001221 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001222
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001225 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 if (likely(HTTP_IS_TOKEN(*ptr)))
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001230 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 if (likely(HTTP_IS_CRLF(*ptr))) {
1235 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001236 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001238 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001240 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001242 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 msg->sl.rq.v_l = 0;
1244 goto http_msg_rqline_eol;
1245 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001246 state = HTTP_MSG_ERROR;
1247 break;
1248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001250 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001252 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 goto http_msg_rquri;
1254 }
1255 if (likely(HTTP_IS_SPHT(*ptr)))
1256 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1257 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1258 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001260 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001261 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001262 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001264
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001266 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1268 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001269
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001270 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001271 /* non-ASCII chars are forbidden unless option
1272 * accept-invalid-http-request is enabled in the frontend.
1273 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001274 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001275 if (msg->err_pos < -1)
1276 goto invalid_char;
1277 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001278 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001279 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1280 }
1281
1282 if (likely(HTTP_IS_CRLF(*ptr))) {
1283 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1284 goto http_msg_req09_uri_e;
1285 }
1286
1287 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001289 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001290 state = HTTP_MSG_ERROR;
1291 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001292
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001294 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001296 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 goto http_msg_rqver;
1298 }
1299 if (likely(HTTP_IS_SPHT(*ptr)))
1300 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1301 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1302 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001306 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001308
1309 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001310 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001311 http_msg_rqline_eol:
1312 /* We have seen the end of line. Note that we do not
1313 * necessarily have the \n yet, but at least we know that we
1314 * have EITHER \r OR \n, otherwise the request would not be
1315 * complete. We can then record the request length and return
1316 * to the caller which will be able to register it.
1317 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001318 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 return ptr;
1320 }
1321
1322 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001323 state = HTTP_MSG_ERROR;
1324 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001327#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1329 exit(1);
1330#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001331 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001335 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 if (ret_state)
1337 *ret_state = state;
1338 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001339 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001342
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001343/*
1344 * Returns the data from Authorization header. Function may be called more
1345 * than once so data is stored in txn->auth_data. When no header is found
1346 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001347 * searching again for something we are unable to find anyway. However, if
1348 * the result if valid, the cache is not reused because we would risk to
1349 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001350 */
1351
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001352/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1353 * set according to global.tune.bufsize.
1354 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001355char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356
1357int
1358get_http_auth(struct session *s)
1359{
1360
1361 struct http_txn *txn = &s->txn;
1362 struct chunk auth_method;
1363 struct hdr_ctx ctx;
1364 char *h, *p;
1365 int len;
1366
1367#ifdef DEBUG_AUTH
1368 printf("Auth for session %p: %d\n", s, txn->auth.method);
1369#endif
1370
1371 if (txn->auth.method == HTTP_AUTH_WRONG)
1372 return 0;
1373
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001374 txn->auth.method = HTTP_AUTH_WRONG;
1375
1376 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001377
1378 if (txn->flags & TX_USE_PX_CONN) {
1379 h = "Proxy-Authorization";
1380 len = strlen(h);
1381 } else {
1382 h = "Authorization";
1383 len = strlen(h);
1384 }
1385
Willy Tarreau9b28e032012-10-12 23:49:43 +02001386 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 return 0;
1388
1389 h = ctx.line + ctx.val;
1390
1391 p = memchr(h, ' ', ctx.vlen);
1392 if (!p || p == h)
1393 return 0;
1394
1395 chunk_initlen(&auth_method, h, 0, p-h);
1396 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1397
1398 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1399
1400 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001401 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001402
1403 if (len < 0)
1404 return 0;
1405
1406
1407 get_http_auth_buff[len] = '\0';
1408
1409 p = strchr(get_http_auth_buff, ':');
1410
1411 if (!p)
1412 return 0;
1413
1414 txn->auth.user = get_http_auth_buff;
1415 *p = '\0';
1416 txn->auth.pass = p+1;
1417
1418 txn->auth.method = HTTP_AUTH_BASIC;
1419 return 1;
1420 }
1421
1422 return 0;
1423}
1424
Willy Tarreau58f10d72006-12-04 02:26:12 +01001425
Willy Tarreau8973c702007-01-21 23:58:29 +01001426/*
1427 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001428 * depending on the initial msg->msg_state. The caller is responsible for
1429 * ensuring that the message does not wrap. The function can be preempted
1430 * everywhere when data are missing and recalled at the exact same location
1431 * with no information loss. The message may even be realigned between two
1432 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001433 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001434 * fields. Note that msg->sol will be initialized after completing the first
1435 * state, so that none of the msg pointers has to be initialized prior to the
1436 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001438void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439{
Willy Tarreau3770f232013-12-07 00:01:53 +01001440 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001442 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001443
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001444 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001445 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001446 ptr = buf->p + msg->next;
1447 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 if (unlikely(ptr >= end))
1450 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 /*
1454 * First, states that are specific to the response only.
1455 * We check them first so that request and headers are
1456 * closer to each other (accessed more often).
1457 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001458 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001459 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001460 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001461 /* we have a start of message, but we have to check
1462 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001463 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001464 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001465 if (unlikely(ptr != buf->p)) {
1466 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001467 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001468 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001469 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 }
Willy Tarreau26927362012-05-18 23:22:52 +02001471 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001472 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 hdr_idx_init(idx);
1474 state = HTTP_MSG_RPVER;
1475 goto http_msg_rpver;
1476 }
1477
1478 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1479 goto http_msg_invalid;
1480
1481 if (unlikely(*ptr == '\n'))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1483 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1484 /* stop here */
1485
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1490 /* stop here */
1491
Willy Tarreau8973c702007-01-21 23:58:29 +01001492 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 case HTTP_MSG_RPVER_SP:
1495 case HTTP_MSG_RPCODE:
1496 case HTTP_MSG_RPCODE_SP:
1497 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001498 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001499 state, ptr, end,
1500 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 if (unlikely(!ptr))
1502 return;
1503
1504 /* we have a full response and we know that we have either a CR
1505 * or an LF at <ptr>.
1506 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1508
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001509 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 if (likely(*ptr == '\r'))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1512 goto http_msg_rpline_end;
1513
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001515 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 /* msg->sol must point to the first of CR or LF. */
1517 EXPECT_LF_HERE(ptr, http_msg_invalid);
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1519 /* stop here */
1520
1521 /*
1522 * Second, states that are specific to the request only
1523 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001527 /* we have a start of message, but we have to check
1528 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001529 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001530 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001531 if (likely(ptr != buf->p)) {
1532 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001533 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001534 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001535 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 }
Willy Tarreau26927362012-05-18 23:22:52 +02001537 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001538 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001539 state = HTTP_MSG_RQMETH;
1540 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1544 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(*ptr == '\n'))
1547 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1548 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001549 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001552 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 EXPECT_LF_HERE(ptr, http_msg_invalid);
1554 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001555 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001558 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_RQMETH_SP:
1560 case HTTP_MSG_RQURI:
1561 case HTTP_MSG_RQURI_SP:
1562 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001563 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001564 state, ptr, end,
1565 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 if (unlikely(!ptr))
1567 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 /* we have a full request and we know that we have either a CR
1570 * or an LF at <ptr>.
1571 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001573
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001574 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 if (likely(*ptr == '\r'))
1576 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001580 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 /* check for HTTP/0.9 request : no version information available.
1582 * msg->sol must point to the first of CR or LF.
1583 */
1584 if (unlikely(msg->sl.rq.v_l == 0))
1585 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 EXPECT_LF_HERE(ptr, http_msg_invalid);
1588 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 /*
1592 * Common states below
1593 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001595 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001596 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (likely(!HTTP_IS_CRLF(*ptr))) {
1598 goto http_msg_hdr_name;
1599 }
1600
1601 if (likely(*ptr == '\r'))
1602 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1603 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001606 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 /* assumes msg->sol points to the first char */
1608 if (likely(HTTP_IS_TOKEN(*ptr)))
1609 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001610
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001611 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001614 if (likely(msg->err_pos < -1) || *ptr == '\n')
1615 goto http_msg_invalid;
1616
1617 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001618 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001619
1620 /* and we still accept this non-token character */
1621 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001624 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001625 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 if (likely(HTTP_IS_SPHT(*ptr)))
1627 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 if (likely(!HTTP_IS_CRLF(*ptr))) {
1633 goto http_msg_hdr_val;
1634 }
1635
1636 if (likely(*ptr == '\r'))
1637 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1638 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001641 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 EXPECT_LF_HERE(ptr, http_msg_invalid);
1643 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001646 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 if (likely(HTTP_IS_SPHT(*ptr))) {
1648 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001649 for (; buf->p + msg->sov < ptr; msg->sov++)
1650 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 goto http_msg_hdr_l1_sp;
1652 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001653 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001654 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 goto http_msg_complete_header;
1656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001658 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001659 /* assumes msg->sol points to the first char, and msg->sov
1660 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 */
1662 if (likely(!HTTP_IS_CRLF(*ptr)))
1663 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001664
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001665 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 /* Note: we could also copy eol into ->eoh so that we have the
1667 * real header end in case it ends with lots of LWS, but is this
1668 * really needed ?
1669 */
1670 if (likely(*ptr == '\r'))
1671 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1672 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 EXPECT_LF_HERE(ptr, http_msg_invalid);
1677 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001680 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1682 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001683 for (; buf->p + msg->eol < ptr; msg->eol++)
1684 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 goto http_msg_hdr_val;
1686 }
1687 http_msg_complete_header:
1688 /*
1689 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001690 * Assumes msg->sol points to the first char, msg->sov points
1691 * to the first character of the value and msg->eol to the
1692 * first CR or LF so we know how the line ends. We insert last
1693 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 idx, idx->tail) < 0))
1697 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001698
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001699 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 if (likely(!HTTP_IS_CRLF(*ptr))) {
1701 goto http_msg_hdr_name;
1702 }
1703
1704 if (likely(*ptr == '\r'))
1705 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1706 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001709 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001710 /* Assumes msg->sol points to the first of either CR or LF.
1711 * Sets ->sov and ->next to the total header length, ->eoh to
1712 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1713 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 EXPECT_LF_HERE(ptr, http_msg_invalid);
1715 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001716 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001717 msg->eoh = msg->sol;
1718 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001719 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001720 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001722
1723 case HTTP_MSG_ERROR:
1724 /* this may only happen if we call http_msg_analyser() twice with an error */
1725 break;
1726
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001728#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1730 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001731#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001732 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 }
1734 http_msg_ood:
1735 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001736 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001737 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 http_msg_invalid:
1741 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001742 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001743 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 return;
1745}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001746
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001747/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1748 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1749 * nothing is done and 1 is returned.
1750 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001751static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001752{
1753 int delta;
1754 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001755 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001756
1757 if (msg->sl.rq.v_l != 0)
1758 return 1;
1759
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001760 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1761 if (txn->meth != HTTP_METH_GET)
1762 return 0;
1763
Willy Tarreau9b28e032012-10-12 23:49:43 +02001764 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765 delta = 0;
1766
1767 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001768 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1769 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001770 }
1771 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001772 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001773 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001774 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001775 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 NULL, NULL);
1779 if (unlikely(!cur_end))
1780 return 0;
1781
1782 /* we have a full HTTP/1.0 request now and we know that
1783 * we have either a CR or an LF at <ptr>.
1784 */
1785 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1786 return 1;
1787}
1788
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001789/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001790 * and "keep-alive" values. If we already know that some headers may safely
1791 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001792 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1793 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001794 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001795 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1796 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1797 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001798 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001799 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001801{
Willy Tarreau5b154472009-12-21 20:11:07 +01001802 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001803 const char *hdr_val = "Connection";
1804 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001805
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001806 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001807 return;
1808
Willy Tarreau88d349d2010-01-25 12:15:43 +01001809 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1810 hdr_val = "Proxy-Connection";
1811 hdr_len = 16;
1812 }
1813
Willy Tarreau5b154472009-12-21 20:11:07 +01001814 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001815 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001816 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001817 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1818 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001819 if (to_del & 2)
1820 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821 else
1822 txn->flags |= TX_CON_KAL_SET;
1823 }
1824 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1825 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001826 if (to_del & 1)
1827 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 else
1829 txn->flags |= TX_CON_CLO_SET;
1830 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001831 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1832 txn->flags |= TX_HDR_CONN_UPG;
1833 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001834 }
1835
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001836 txn->flags |= TX_HDR_CONN_PRS;
1837 return;
1838}
Willy Tarreau5b154472009-12-21 20:11:07 +01001839
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001840/* Apply desired changes on the Connection: header. Values may be removed and/or
1841 * added depending on the <wanted> flags, which are exclusively composed of
1842 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1843 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1844 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001845void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001846{
1847 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001848 const char *hdr_val = "Connection";
1849 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001850
1851 ctx.idx = 0;
1852
Willy Tarreau88d349d2010-01-25 12:15:43 +01001853
1854 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1855 hdr_val = "Proxy-Connection";
1856 hdr_len = 16;
1857 }
1858
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001860 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001861 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1862 if (wanted & TX_CON_KAL_SET)
1863 txn->flags |= TX_CON_KAL_SET;
1864 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001865 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001866 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001867 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1868 if (wanted & TX_CON_CLO_SET)
1869 txn->flags |= TX_CON_CLO_SET;
1870 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001871 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001872 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001873 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874
1875 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1876 return;
1877
1878 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1879 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001880 hdr_val = "Connection: close";
1881 hdr_len = 17;
1882 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1883 hdr_val = "Proxy-Connection: close";
1884 hdr_len = 23;
1885 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001886 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887 }
1888
1889 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1890 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001891 hdr_val = "Connection: keep-alive";
1892 hdr_len = 22;
1893 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1894 hdr_val = "Proxy-Connection: keep-alive";
1895 hdr_len = 28;
1896 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001897 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 }
1899 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001900}
1901
Willy Tarreauc24715e2014-04-17 15:21:20 +02001902/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1903 * the first byte of data after the chunk size, so that we know we can forward
1904 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1905 * the chunk size. That way it is always possible to differentiate between the
1906 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001909 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001910static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001911{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001912 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001914 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 const char *end = buf->data + buf->size;
1916 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001917 unsigned int chunk = 0;
1918
1919 /* The chunk size is in the following form, though we are only
1920 * interested in the size and CRLF :
1921 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1922 */
1923 while (1) {
1924 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001925 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001928 if (c < 0) /* not a hex digit anymore */
1929 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001930 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001931 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001932 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001933 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001934 chunk = (chunk << 4) + c;
1935 }
1936
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001938 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001939 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940
1941 while (http_is_spht[(unsigned char)*ptr]) {
1942 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001943 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001944 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001945 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001946 }
1947
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 /* Up to there, we know that at least one byte is present at *ptr. Check
1949 * for the end of chunk size.
1950 */
1951 while (1) {
1952 if (likely(HTTP_IS_CRLF(*ptr))) {
1953 /* we now have a CR or an LF at ptr */
1954 if (likely(*ptr == '\r')) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001957 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958 return 0;
1959 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001962 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001964 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001965 /* done */
1966 break;
1967 }
1968 else if (*ptr == ';') {
1969 /* chunk extension, ends at next CRLF */
1970 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001971 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001972 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001973 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974
1975 while (!HTTP_IS_CRLF(*ptr)) {
1976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001978 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 return 0;
1980 }
1981 /* we have a CRLF now, loop above */
1982 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001983 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001984 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001985 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 }
1987
Willy Tarreaud98cf932009-12-27 22:54:55 +01001988 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02001989 * which may or may not be present. We save that into ->next,
1990 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01001991 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02001992 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02001993 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02001994 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001995 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001996 msg->chunk_len = chunk;
1997 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002000 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002001 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002002 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002003}
2004
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002005/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002006 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002007 * the trailers is found, it is automatically scheduled to be forwarded,
2008 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2009 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002010 * except maybe msg->next if it could parse some lines, and returns zero.
2011 * If a parse error is encountered, the function returns < 0 and does not
2012 * change anything except maybe msg->next. Note that the message must
2013 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002014 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002015 * forwarding, and that msg->next exactly matches the length of trailers
2016 * already parsed and not forwarded. It is also important to note that this
2017 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002018 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002019static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002021 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002022
Willy Tarreaua458b672012-03-05 11:17:50 +01002023 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002024 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002025 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002026 const char *ptr = b_ptr(buf, msg->next);
2027 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002028 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029
2030 /* scan current line and stop at LF or CRLF */
2031 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002032 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 return 0;
2034
2035 if (*ptr == '\n') {
2036 if (!p1)
2037 p1 = ptr;
2038 p2 = ptr;
2039 break;
2040 }
2041
2042 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002043 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002046 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047 p1 = ptr;
2048 }
2049
2050 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002051 if (ptr >= buf->data + buf->size)
2052 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053 }
2054
2055 /* after LF; point to beginning of next line */
2056 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 if (p2 >= buf->data + buf->size)
2058 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002060 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002061 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002063
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002065 /* LF/CRLF at beginning of line => end of trailers at p2.
2066 * Everything was scheduled for forwarding, there's nothing
2067 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002068 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002069 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 msg->msg_state = HTTP_MSG_DONE;
2071 return 1;
2072 }
2073 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 }
2076}
2077
Willy Tarreauc24715e2014-04-17 15:21:20 +02002078/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2079 * or a possible LF alone at the end of a chunk. It automatically adjusts
2080 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002081 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2083 * not enough data are available, the function does not change anything and
2084 * returns zero. If a parse error is encountered, the function returns < 0 and
2085 * does not change anything. Note: this function is designed to parse wrapped
2086 * CRLF at the end of the buffer.
2087 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002088static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002090 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002091 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 int bytes;
2093
2094 /* NB: we'll check data availabilty at the end. It's not a
2095 * problem because whatever we match first will be checked
2096 * against the correct length.
2097 */
2098 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002099 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002100 if (*ptr == '\r') {
2101 bytes++;
2102 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 if (ptr >= buf->data + buf->size)
2104 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 }
2106
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002107 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002108 return 0;
2109
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002110 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002111 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002113 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002114
2115 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002116 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002117 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002118 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002119 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2121 return 1;
2122}
Willy Tarreau5b154472009-12-21 20:11:07 +01002123
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002124/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2125 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2126 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2127 * Unparsable qvalues return 1000 as "q=1.000".
2128 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002129int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002130{
2131 int q = 1000;
2132
2133 if (!isdigit(*qvalue))
2134 goto out;
2135 q = (*qvalue++ - '0') * 1000;
2136
2137 if (*qvalue++ != '.')
2138 goto out;
2139
2140 if (!isdigit(*qvalue))
2141 goto out;
2142 q += (*qvalue++ - '0') * 100;
2143
2144 if (!isdigit(*qvalue))
2145 goto out;
2146 q += (*qvalue++ - '0') * 10;
2147
2148 if (!isdigit(*qvalue))
2149 goto out;
2150 q += (*qvalue++ - '0') * 1;
2151 out:
2152 if (q > 1000)
2153 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002154 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002155 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002156 return q;
2157}
William Lallemand82fe75c2012-10-23 10:25:10 +02002158
2159/*
2160 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002161 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002162int select_compression_request_header(struct session *s, struct buffer *req)
2163{
2164 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002165 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002166 struct hdr_ctx ctx;
2167 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002168 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002169
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002170 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2171 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002172 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2173 */
2174 ctx.idx = 0;
2175 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2176 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002177 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2178 (ctx.vlen < 31 ||
2179 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2180 ctx.line[ctx.val + 30] < '6' ||
2181 (ctx.line[ctx.val + 30] == '6' &&
2182 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2183 s->comp_algo = NULL;
2184 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 }
2186
William Lallemand82fe75c2012-10-23 10:25:10 +02002187 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002188 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002189 int best_q = 0;
2190
William Lallemand82fe75c2012-10-23 10:25:10 +02002191 ctx.idx = 0;
2192 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002193 const char *qval;
2194 int q;
2195 int toklen;
2196
2197 /* try to isolate the token from the optional q-value */
2198 toklen = 0;
2199 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2200 toklen++;
2201
2202 qval = ctx.line + ctx.val + toklen;
2203 while (1) {
2204 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2205 qval++;
2206
2207 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2208 qval = NULL;
2209 break;
2210 }
2211 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002212
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002213 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2214 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002215
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002216 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2217 qval = NULL;
2218 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002219 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002220 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2221 break;
2222
2223 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2224 qval++;
2225 }
2226
2227 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002228 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002229
2230 if (q <= best_q)
2231 continue;
2232
2233 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2234 if (*(ctx.line + ctx.val) == '*' ||
2235 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2236 s->comp_algo = comp_algo;
2237 best_q = q;
2238 break;
2239 }
2240 }
2241 }
2242 }
2243
2244 /* remove all occurrences of the header when "compression offload" is set */
2245 if (s->comp_algo) {
2246 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2247 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2248 ctx.idx = 0;
2249 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2250 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002251 }
2252 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002253 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002254 }
2255
2256 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002257 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2258 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002259 if (comp_algo->add_data == identity_add_data) {
2260 s->comp_algo = comp_algo;
2261 return 1;
2262 }
2263 }
2264 }
2265
2266 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 return 0;
2268}
2269
2270/*
2271 * Selects a comression algorithm depending of the server response.
2272 */
2273int select_compression_response_header(struct session *s, struct buffer *res)
2274{
2275 struct http_txn *txn = &s->txn;
2276 struct http_msg *msg = &txn->rsp;
2277 struct hdr_ctx ctx;
2278 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002279
2280 /* no common compression algorithm was found in request header */
2281 if (s->comp_algo == NULL)
2282 goto fail;
2283
2284 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002285 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002286 goto fail;
2287
William Lallemandd3002612012-11-26 14:34:47 +01002288 /* 200 only */
2289 if (txn->status != 200)
2290 goto fail;
2291
William Lallemand82fe75c2012-10-23 10:25:10 +02002292 /* Content-Length is null */
2293 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2294 goto fail;
2295
2296 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002297 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002298 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2299 goto fail;
2300
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002301 /* no compression when Cache-Control: no-transform is present in the message */
2302 ctx.idx = 0;
2303 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2304 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2305 goto fail;
2306 }
2307
William Lallemand82fe75c2012-10-23 10:25:10 +02002308 comp_type = NULL;
2309
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002310 /* we don't want to compress multipart content-types, nor content-types that are
2311 * not listed in the "compression type" directive if any. If no content-type was
2312 * found but configuration requires one, we don't compress either. Backend has
2313 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002314 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002315 ctx.idx = 0;
2316 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2317 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2318 goto fail;
2319
2320 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2321 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002322 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002323 if (ctx.vlen >= comp_type->name_len &&
2324 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002325 /* this Content-Type should be compressed */
2326 break;
2327 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 /* this Content-Type should not be compressed */
2329 if (comp_type == NULL)
2330 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002332 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002333 else { /* no content-type header */
2334 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2335 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002336 }
2337
William Lallemandd85f9172012-11-09 17:05:39 +01002338 /* limit compression rate */
2339 if (global.comp_rate_lim > 0)
2340 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2341 goto fail;
2342
William Lallemand072a2bf2012-11-20 17:01:01 +01002343 /* limit cpu usage */
2344 if (idle_pct < compress_min_idle)
2345 goto fail;
2346
William Lallemand4c49fae2012-11-07 15:00:23 +01002347 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002348 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002349 goto fail;
2350
William Lallemandec3e3892012-11-12 17:02:18 +01002351 s->flags |= SN_COMP_READY;
2352
William Lallemand82fe75c2012-10-23 10:25:10 +02002353 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002354 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002355 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2356 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2357
2358 /* add Transfer-Encoding header */
2359 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2360 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2361
2362 /*
2363 * Add Content-Encoding header when it's not identity encoding.
2364 * RFC 2616 : Identity encoding: This content-coding is used only in the
2365 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2366 * header.
2367 */
2368 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002369 trash.len = 18;
2370 memcpy(trash.str, "Content-Encoding: ", trash.len);
2371 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2372 trash.len += s->comp_algo->name_len;
2373 trash.str[trash.len] = '\0';
2374 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002375 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002376 return 1;
2377
2378fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002379 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002380 return 0;
2381}
2382
2383
Willy Tarreaud787e662009-07-07 10:14:51 +02002384/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2385 * processing can continue on next analysers, or zero if it either needs more
2386 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2387 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2388 * when it has nothing left to do, and may remove any analyser when it wants to
2389 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002390 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002391int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002392{
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 /*
2394 * We will parse the partial (or complete) lines.
2395 * We will check the request syntax, and also join multi-line
2396 * headers. An index of all the lines will be elaborated while
2397 * parsing.
2398 *
2399 * For the parsing, we use a 28 states FSM.
2400 *
2401 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002402 * req->buf->p = beginning of request
2403 * req->buf->p + msg->eoh = end of processed headers / start of current one
2404 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002405 * msg->eol = end of current header or line (LF or CRLF)
2406 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002407 *
2408 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002409 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002410 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2411 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002412 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002415 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 struct http_txn *txn = &s->txn;
2417 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002418 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002419
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002420 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002421 now_ms, __FUNCTION__,
2422 s,
2423 req,
2424 req->rex, req->wex,
2425 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002426 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002427 req->analysers);
2428
Willy Tarreau52a0c602009-08-16 22:45:38 +02002429 /* we're speaking HTTP here, so let's speak HTTP to the client */
2430 s->srv_error = http_return_srv_error;
2431
Willy Tarreau83e3af02009-12-28 17:39:57 +01002432 /* There's a protected area at the end of the buffer for rewriting
2433 * purposes. We don't want to start to parse the request if the
2434 * protected area is affected, because we may have to move processed
2435 * data later, which is much more complicated.
2436 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002437 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002438 if (txn->flags & TX_NOT_FIRST) {
2439 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002440 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002441 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002442 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002443 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002444 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002445 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002446 return 0;
2447 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002448 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2449 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2450 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002451 }
2452
Willy Tarreau065e8332010-01-08 00:30:20 +01002453 /* Note that we have the same problem with the response ; we
2454 * may want to send a redirect, error or anything which requires
2455 * some spare space. So we'll ensure that we have at least
2456 * maxrewrite bytes available in the response buffer before
2457 * processing that one. This will only affect pipelined
2458 * keep-alive requests.
2459 */
2460 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002461 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002462 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2463 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2464 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002465 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002466 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002467 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002468 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002469 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002470 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002471 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002472 return 0;
2473 }
2474 }
2475
Willy Tarreau9b28e032012-10-12 23:49:43 +02002476 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002477 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002478 }
2479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 /* 1: we might have to print this header in debug mode */
2481 if (unlikely((global.mode & MODE_DEBUG) &&
2482 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002483 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002485
Willy Tarreau9b28e032012-10-12 23:49:43 +02002486 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002487 /* this is a bit complex : in case of error on the request line,
2488 * we know that rq.l is still zero, so we display only the part
2489 * up to the end of the line (truncated by debug_hdr).
2490 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002491 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002492 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 sol += hdr_idx_first_pos(&txn->hdr_idx);
2495 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002496
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 while (cur_idx) {
2498 eol = sol + txn->hdr_idx.v[cur_idx].len;
2499 debug_hdr("clihdr", s, sol, eol);
2500 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2501 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002502 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002503 }
2504
Willy Tarreau58f10d72006-12-04 02:26:12 +01002505
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 /*
2507 * Now we quickly check if we have found a full valid request.
2508 * If not so, we check the FD and buffer states before leaving.
2509 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002510 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002511 * requests are checked first. When waiting for a second request
2512 * on a keep-alive session, if we encounter and error, close, t/o,
2513 * we note the error in the session flags but don't set any state.
2514 * Since the error will be noted there, it will not be counted by
2515 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002516 * Last, we may increase some tracked counters' http request errors on
2517 * the cases that are deliberately the client's fault. For instance,
2518 * a timeout or connection reset is not counted as an error. However
2519 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002521
Willy Tarreau655dce92009-11-08 13:10:58 +01002522 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002523 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002525 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002526 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 session_inc_http_req_ctr(s);
2528 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002529 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002531 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002532
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 /* 1: Since we are in header mode, if there's no space
2534 * left for headers, we won't be able to free more
2535 * later, so the session will never terminate. We
2536 * must terminate it now.
2537 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002538 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 /* FIXME: check if URI is set and return Status
2540 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002541 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002542 session_inc_http_req_ctr(s);
2543 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002544 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002545 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002546 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 goto return_bad_req;
2548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002549
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002551 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002552 if (!(s->flags & SN_ERR_MASK))
2553 s->flags |= SN_ERR_CLICL;
2554
Willy Tarreaufcffa692010-01-10 14:21:19 +01002555 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002556 goto failed_keep_alive;
2557
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002559 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002560 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002561 session_inc_http_err_ctr(s);
2562 }
2563
Willy Tarreaudc979f22012-12-04 10:39:01 +01002564 txn->status = 400;
2565 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 msg->msg_state = HTTP_MSG_ERROR;
2567 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002568
Willy Tarreauda7ff642010-06-23 11:44:09 +02002569 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002570 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002571 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002572 if (s->listener->counters)
2573 s->listener->counters->failed_req++;
2574
Willy Tarreau59234e92008-11-30 23:51:27 +01002575 if (!(s->flags & SN_FINST_MASK))
2576 s->flags |= SN_FINST_R;
2577 return 0;
2578 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002579
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002581 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002582 if (!(s->flags & SN_ERR_MASK))
2583 s->flags |= SN_ERR_CLITO;
2584
Willy Tarreaufcffa692010-01-10 14:21:19 +01002585 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002586 goto failed_keep_alive;
2587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002589 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002590 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002591 session_inc_http_err_ctr(s);
2592 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002594 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 msg->msg_state = HTTP_MSG_ERROR;
2596 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002597
Willy Tarreauda7ff642010-06-23 11:44:09 +02002598 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002599 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002600 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002601 if (s->listener->counters)
2602 s->listener->counters->failed_req++;
2603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 if (!(s->flags & SN_FINST_MASK))
2605 s->flags |= SN_FINST_R;
2606 return 0;
2607 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002610 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002611 if (!(s->flags & SN_ERR_MASK))
2612 s->flags |= SN_ERR_CLICL;
2613
Willy Tarreaufcffa692010-01-10 14:21:19 +01002614 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002615 goto failed_keep_alive;
2616
Willy Tarreau4076a152009-04-02 15:18:36 +02002617 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002618 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002620 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 msg->msg_state = HTTP_MSG_ERROR;
2622 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002623
Willy Tarreauda7ff642010-06-23 11:44:09 +02002624 session_inc_http_err_ctr(s);
2625 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002626 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002627 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002628 if (s->listener->counters)
2629 s->listener->counters->failed_req++;
2630
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 if (!(s->flags & SN_FINST_MASK))
2632 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002633 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002634 }
2635
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002636 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002637 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2638 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002639#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002640 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002641 /* We need more data, we have to re-enable quick-ack in case we
2642 * previously disabled it, otherwise we might cause the client
2643 * to delay next data.
2644 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002645 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002646 }
2647#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002648
Willy Tarreaufcffa692010-01-10 14:21:19 +01002649 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2650 /* If the client starts to talk, let's fall back to
2651 * request timeout processing.
2652 */
2653 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002654 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002655 }
2656
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002658 if (!tick_isset(req->analyse_exp)) {
2659 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2660 (txn->flags & TX_WAIT_NEXT_RQ) &&
2661 tick_isset(s->be->timeout.httpka))
2662 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2663 else
2664 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002666
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 /* we're not ready yet */
2668 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002669
2670 failed_keep_alive:
2671 /* Here we process low-level errors for keep-alive requests. In
2672 * short, if the request is not the first one and it experiences
2673 * a timeout, read error or shutdown, we just silently close so
2674 * that the client can try again.
2675 */
2676 txn->status = 0;
2677 msg->msg_state = HTTP_MSG_RQBEFORE;
2678 req->analysers = 0;
2679 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002680 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002681 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002682 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002683 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002685
Willy Tarreaud787e662009-07-07 10:14:51 +02002686 /* OK now we have a complete HTTP request with indexed headers. Let's
2687 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002688 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002689 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002690 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002691 * byte after the last LF. msg->sov points to the first byte of data.
2692 * msg->eol cannot be trusted because it may have been left uninitialized
2693 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002694 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002695
Willy Tarreauda7ff642010-06-23 11:44:09 +02002696 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002697 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2698
Willy Tarreaub16a5742010-01-10 14:46:16 +01002699 if (txn->flags & TX_WAIT_NEXT_RQ) {
2700 /* kill the pending keep-alive timeout */
2701 txn->flags &= ~TX_WAIT_NEXT_RQ;
2702 req->analyse_exp = TICK_ETERNITY;
2703 }
2704
2705
Willy Tarreaud787e662009-07-07 10:14:51 +02002706 /* Maybe we found in invalid header name while we were configured not
2707 * to block on that, so we have to capture it now.
2708 */
2709 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002710 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002711
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 /*
2713 * 1: identify the method
2714 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002716
2717 /* we can make use of server redirect on GET and HEAD */
2718 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2719 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002720
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 /*
2722 * 2: check if the URI matches the monitor_uri.
2723 * We have to do this for every request which gets in, because
2724 * the monitor-uri is defined by the frontend.
2725 */
2726 if (unlikely((s->fe->monitor_uri_len != 0) &&
2727 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 s->fe->monitor_uri,
2730 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002731 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002733 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002735
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002737 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002740 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002741 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002742
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 ret = acl_pass(ret);
2744 if (cond->pol == ACL_COND_UNLESS)
2745 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002746
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 if (ret) {
2748 /* we fail this request, let's return 503 service unavail */
2749 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002751 if (!(s->flags & SN_ERR_MASK))
2752 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002754 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002755 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002756
Willy Tarreau59234e92008-11-30 23:51:27 +01002757 /* nothing to fail, let's reply normaly */
2758 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002759 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002760 if (!(s->flags & SN_ERR_MASK))
2761 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 goto return_prx_cond;
2763 }
2764
2765 /*
2766 * 3: Maybe we have to copy the original REQURI for the logs ?
2767 * Note: we cannot log anymore if the request has been
2768 * classified as invalid.
2769 */
2770 if (unlikely(s->logs.logwait & LW_REQ)) {
2771 /* we have a complete HTTP request that we must log */
2772 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2773 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002774
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 if (urilen >= REQURI_LEN)
2776 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002777 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002779
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002780 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 s->do_log(s);
2782 } else {
2783 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002784 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002785 }
Willy Tarreau06619262006-12-17 08:37:22 +01002786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002788 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002789 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002790
Willy Tarreau5b154472009-12-21 20:11:07 +01002791 /* ... and check if the request is HTTP/1.1 or above */
2792 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002793 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2794 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2795 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002796 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002797
2798 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002799 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002800
Willy Tarreau88d349d2010-01-25 12:15:43 +01002801 /* if the frontend has "option http-use-proxy-header", we'll check if
2802 * we have what looks like a proxied connection instead of a connection,
2803 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2804 * Note that this is *not* RFC-compliant, however browsers and proxies
2805 * happen to do that despite being non-standard :-(
2806 * We consider that a request not beginning with either '/' or '*' is
2807 * a proxied connection, which covers both "scheme://location" and
2808 * CONNECT ip:port.
2809 */
2810 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002811 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002812 txn->flags |= TX_USE_PX_CONN;
2813
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002814 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002815 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002816
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002818 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002819 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002821
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002822 /* 6: determine the transfer-length.
2823 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2824 * the presence of a message-body in a REQUEST and its transfer length
2825 * must be determined that way (in order of precedence) :
2826 * 1. The presence of a message-body in a request is signaled by the
2827 * inclusion of a Content-Length or Transfer-Encoding header field
2828 * in the request's header fields. When a request message contains
2829 * both a message-body of non-zero length and a method that does
2830 * not define any semantics for that request message-body, then an
2831 * origin server SHOULD either ignore the message-body or respond
2832 * with an appropriate error message (e.g., 413). A proxy or
2833 * gateway, when presented the same request, SHOULD either forward
2834 * the request inbound with the message- body or ignore the
2835 * message-body when determining a response.
2836 *
2837 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2838 * and the "chunked" transfer-coding (Section 6.2) is used, the
2839 * transfer-length is defined by the use of this transfer-coding.
2840 * If a Transfer-Encoding header field is present and the "chunked"
2841 * transfer-coding is not present, the transfer-length is defined
2842 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002843 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002844 * 3. If a Content-Length header field is present, its decimal value in
2845 * OCTETs represents both the entity-length and the transfer-length.
2846 * If a message is received with both a Transfer-Encoding header
2847 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002848 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002849 * 4. By the server closing the connection. (Closing the connection
2850 * cannot be used to indicate the end of a request body, since that
2851 * would leave no possibility for the server to send back a response.)
2852 *
2853 * Whenever a transfer-coding is applied to a message-body, the set of
2854 * transfer-codings MUST include "chunked", unless the message indicates
2855 * it is terminated by closing the connection. When the "chunked"
2856 * transfer-coding is used, it MUST be the last transfer-coding applied
2857 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002858 */
2859
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002860 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002861 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002862 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002863 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002864 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002865 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002866 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2867 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002868 /* bad transfer-encoding (chunked followed by something else) */
2869 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002870 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002871 break;
2872 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002873 }
2874
Willy Tarreau32b47f42009-10-18 20:55:02 +02002875 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002876 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002878 signed long long cl;
2879
Willy Tarreauad14f752011-09-02 20:33:27 +02002880 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002881 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002882 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002883 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002884
Willy Tarreauad14f752011-09-02 20:33:27 +02002885 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002886 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002887 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002888 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002889
Willy Tarreauad14f752011-09-02 20:33:27 +02002890 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002891 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002892 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002893 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002894
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002895 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002896 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002897 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002898 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002899
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002900 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002901 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002902 }
2903
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002904 /* bodyless requests have a known length */
2905 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002906 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002907
Willy Tarreaud787e662009-07-07 10:14:51 +02002908 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002909 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002910 req->analyse_exp = TICK_ETERNITY;
2911 return 1;
2912
2913 return_bad_req:
2914 /* We centralize bad requests processing here */
2915 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2916 /* we detected a parsing error. We want to archive this request
2917 * in the dedicated proxy area for later troubleshooting.
2918 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002919 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002920 }
2921
2922 txn->req.msg_state = HTTP_MSG_ERROR;
2923 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002924 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002925
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002926 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002927 if (s->listener->counters)
2928 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002929
2930 return_prx_cond:
2931 if (!(s->flags & SN_ERR_MASK))
2932 s->flags |= SN_ERR_PRXCOND;
2933 if (!(s->flags & SN_FINST_MASK))
2934 s->flags |= SN_FINST_R;
2935
2936 req->analysers = 0;
2937 req->analyse_exp = TICK_ETERNITY;
2938 return 0;
2939}
2940
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002941
Willy Tarreau347a35d2013-11-22 17:51:09 +01002942/* This function prepares an applet to handle the stats. It can deal with the
2943 * "100-continue" expectation, check that admin rules are met for POST requests,
2944 * and program a response message if something was unexpected. It cannot fail
2945 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002946 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002947 * s->target which is supposed to already point to the stats applet. The caller
2948 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002949 */
2950int http_handle_stats(struct session *s, struct channel *req)
2951{
2952 struct stats_admin_rule *stats_admin_rule;
2953 struct stream_interface *si = s->rep->prod;
2954 struct http_txn *txn = &s->txn;
2955 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002956 struct uri_auth *uri_auth = s->be->uri_auth;
2957 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002958 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002959
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002960 appctx = si_appctx(si);
2961 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2962 appctx->st1 = appctx->st2 = 0;
2963 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2964 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002965 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
2966 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002967
2968 uri = msg->chn->buf->p + msg->sl.rq.u;
2969 lookup = uri + uri_auth->uri_len;
2970
2971 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2972 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002973 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002974 break;
2975 }
2976 }
2977
2978 if (uri_auth->refresh) {
2979 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2980 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002981 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002982 break;
2983 }
2984 }
2985 }
2986
2987 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2988 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002989 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002990 break;
2991 }
2992 }
2993
2994 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2995 if (memcmp(h, ";st=", 4) == 0) {
2996 int i;
2997 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002998 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002999 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3000 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003001 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003002 break;
3003 }
3004 }
3005 break;
3006 }
3007 }
3008
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003009 appctx->ctx.stats.scope_str = 0;
3010 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003011 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3012 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3013 int itx = 0;
3014 const char *h2;
3015 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3016 const char *err;
3017
3018 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3019 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003020 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003021 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3022 itx++;
3023 h++;
3024 }
3025
3026 if (itx > STAT_SCOPE_TXT_MAXLEN)
3027 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003028 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003029
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003030 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003031 memcpy(scope_txt, h2, itx);
3032 scope_txt[itx] = '\0';
3033 err = invalid_char(scope_txt);
3034 if (err) {
3035 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003036 appctx->ctx.stats.scope_str = 0;
3037 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003038 }
3039 break;
3040 }
3041 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003042
3043 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003044 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003045 int ret = 1;
3046
3047 if (stats_admin_rule->cond) {
3048 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3049 ret = acl_pass(ret);
3050 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3051 ret = !ret;
3052 }
3053
3054 if (ret) {
3055 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003056 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003057 break;
3058 }
3059 }
3060
3061 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003062 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003063 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003064 if (msg->msg_state < HTTP_MSG_100_SENT) {
3065 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3066 * send an HTTP/1.1 100 Continue intermediate response.
3067 */
3068 if (msg->flags & HTTP_MSGF_VER_11) {
3069 struct hdr_ctx ctx;
3070 ctx.idx = 0;
3071 /* Expect is allowed in 1.1, look for it */
3072 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3073 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3074 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3075 }
3076 }
3077 msg->msg_state = HTTP_MSG_100_SENT;
3078 s->logs.tv_request = now; /* update the request timer to reflect full request */
3079 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003080 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003081 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003082 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003083 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3084 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003085 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003086 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003087 else {
3088 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003089 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 }
3091
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003092 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003093 return 1;
3094}
3095
Lukas Tribus67db8df2013-06-23 17:37:13 +02003096/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3097 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3098 */
3099static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3100{
3101#ifdef IP_TOS
3102 if (from.ss_family == AF_INET)
3103 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3104#endif
3105#ifdef IPV6_TCLASS
3106 if (from.ss_family == AF_INET6) {
3107 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3108 /* v4-mapped addresses need IP_TOS */
3109 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3110 else
3111 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3112 }
3113#endif
3114}
3115
Willy Tarreau20b0de52012-12-24 15:45:22 +01003116/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003117 * transaction <txn>. Returns the first rule that prevents further processing
3118 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3119 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3120 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003121 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003122static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003123http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003124{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003125 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003126 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003127 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003128
Willy Tarreauff011f22011-01-06 17:51:27 +01003129 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003130 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003131 continue;
3132
Willy Tarreau96257ec2012-12-27 10:46:37 +01003133 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003134 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003135 int ret;
3136
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003137 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003138 ret = acl_pass(ret);
3139
Willy Tarreauff011f22011-01-06 17:51:27 +01003140 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003141 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003142
3143 if (!ret) /* condition not matched */
3144 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003145 }
3146
Willy Tarreau20b0de52012-12-24 15:45:22 +01003147
Willy Tarreau96257ec2012-12-27 10:46:37 +01003148 switch (rule->action) {
3149 case HTTP_REQ_ACT_ALLOW:
3150 return NULL; /* "allow" rules are OK */
3151
3152 case HTTP_REQ_ACT_DENY:
3153 txn->flags |= TX_CLDENY;
3154 return rule;
3155
Willy Tarreauccbcc372012-12-27 12:37:57 +01003156 case HTTP_REQ_ACT_TARPIT:
3157 txn->flags |= TX_CLTARPIT;
3158 return rule;
3159
Willy Tarreau96257ec2012-12-27 10:46:37 +01003160 case HTTP_REQ_ACT_AUTH:
3161 return rule;
3162
Willy Tarreau81499eb2012-12-27 12:19:02 +01003163 case HTTP_REQ_ACT_REDIR:
3164 return rule;
3165
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003166 case HTTP_REQ_ACT_SET_NICE:
3167 s->task->nice = rule->arg.nice;
3168 break;
3169
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003170 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003171 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003172 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003173 break;
3174
Willy Tarreau51347ed2013-06-11 19:34:13 +02003175 case HTTP_REQ_ACT_SET_MARK:
3176#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003177 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003178 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003179#endif
3180 break;
3181
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003182 case HTTP_REQ_ACT_SET_LOGL:
3183 s->logs.level = rule->arg.loglevel;
3184 break;
3185
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003186 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003187 case HTTP_REQ_ACT_SET_HDR:
3188 ctx.idx = 0;
3189 /* remove all occurrences of the header */
3190 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3191 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3192 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003193 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003194 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3195 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003196 /* now fall through to header addition */
3197
3198 case HTTP_REQ_ACT_ADD_HDR:
3199 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3200 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3201 trash.len = rule->arg.hdr_add.name_len;
3202 trash.str[trash.len++] = ':';
3203 trash.str[trash.len++] = ' ';
3204 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3205 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3206 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003207 }
3208 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003209
3210 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003211 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003212}
3213
Willy Tarreau71241ab2012-12-27 11:30:54 +01003214
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003215/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3216 * transaction <txn>. Returns the first rule that prevents further processing
3217 * of the response (deny, ...) or NULL if it executed all rules or stopped
3218 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3219 * rule.
3220 */
3221static struct http_res_rule *
3222http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3223{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003224 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003225 struct http_res_rule *rule;
3226 struct hdr_ctx ctx;
3227
3228 list_for_each_entry(rule, rules, list) {
3229 if (rule->action >= HTTP_RES_ACT_MAX)
3230 continue;
3231
3232 /* check optional condition */
3233 if (rule->cond) {
3234 int ret;
3235
3236 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3237 ret = acl_pass(ret);
3238
3239 if (rule->cond->pol == ACL_COND_UNLESS)
3240 ret = !ret;
3241
3242 if (!ret) /* condition not matched */
3243 continue;
3244 }
3245
3246
3247 switch (rule->action) {
3248 case HTTP_RES_ACT_ALLOW:
3249 return NULL; /* "allow" rules are OK */
3250
3251 case HTTP_RES_ACT_DENY:
3252 txn->flags |= TX_SVDENY;
3253 return rule;
3254
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003255 case HTTP_RES_ACT_SET_NICE:
3256 s->task->nice = rule->arg.nice;
3257 break;
3258
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003259 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003260 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003261 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003262 break;
3263
Willy Tarreau51347ed2013-06-11 19:34:13 +02003264 case HTTP_RES_ACT_SET_MARK:
3265#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003266 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003267 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003268#endif
3269 break;
3270
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003271 case HTTP_RES_ACT_SET_LOGL:
3272 s->logs.level = rule->arg.loglevel;
3273 break;
3274
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003275 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003276 case HTTP_RES_ACT_SET_HDR:
3277 ctx.idx = 0;
3278 /* remove all occurrences of the header */
3279 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3280 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3281 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3282 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003283 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3284 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003285 /* now fall through to header addition */
3286
3287 case HTTP_RES_ACT_ADD_HDR:
3288 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3289 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3290 trash.len = rule->arg.hdr_add.name_len;
3291 trash.str[trash.len++] = ':';
3292 trash.str[trash.len++] = ' ';
3293 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3294 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3295 break;
3296 }
3297 }
3298
3299 /* we reached the end of the rules, nothing to report */
3300 return NULL;
3301}
3302
3303
Willy Tarreau71241ab2012-12-27 11:30:54 +01003304/* Perform an HTTP redirect based on the information in <rule>. The function
3305 * returns non-zero on success, or zero in case of a, irrecoverable error such
3306 * as too large a request to build a valid response.
3307 */
3308static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3309{
3310 struct http_msg *msg = &txn->req;
3311 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003312 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003313
3314 /* build redirect message */
3315 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003316 case 308:
3317 msg_fmt = HTTP_308;
3318 break;
3319 case 307:
3320 msg_fmt = HTTP_307;
3321 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003322 case 303:
3323 msg_fmt = HTTP_303;
3324 break;
3325 case 301:
3326 msg_fmt = HTTP_301;
3327 break;
3328 case 302:
3329 default:
3330 msg_fmt = HTTP_302;
3331 break;
3332 }
3333
3334 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3335 return 0;
3336
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003337 location = trash.str + trash.len;
3338
Willy Tarreau71241ab2012-12-27 11:30:54 +01003339 switch(rule->type) {
3340 case REDIRECT_TYPE_SCHEME: {
3341 const char *path;
3342 const char *host;
3343 struct hdr_ctx ctx;
3344 int pathlen;
3345 int hostlen;
3346
3347 host = "";
3348 hostlen = 0;
3349 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003350 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003351 host = ctx.line + ctx.val;
3352 hostlen = ctx.vlen;
3353 }
3354
3355 path = http_get_path(txn);
3356 /* build message using path */
3357 if (path) {
3358 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3359 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3360 int qs = 0;
3361 while (qs < pathlen) {
3362 if (path[qs] == '?') {
3363 pathlen = qs;
3364 break;
3365 }
3366 qs++;
3367 }
3368 }
3369 } else {
3370 path = "/";
3371 pathlen = 1;
3372 }
3373
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003374 if (rule->rdr_str) { /* this is an old "redirect" rule */
3375 /* check if we can add scheme + "://" + host + path */
3376 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3377 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003378
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003379 /* add scheme */
3380 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3381 trash.len += rule->rdr_len;
3382 }
3383 else {
3384 /* add scheme with executing log format */
3385 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003386
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003387 /* check if we can add scheme + "://" + host + path */
3388 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3389 return 0;
3390 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003391 /* add "://" */
3392 memcpy(trash.str + trash.len, "://", 3);
3393 trash.len += 3;
3394
3395 /* add host */
3396 memcpy(trash.str + trash.len, host, hostlen);
3397 trash.len += hostlen;
3398
3399 /* add path */
3400 memcpy(trash.str + trash.len, path, pathlen);
3401 trash.len += pathlen;
3402
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003403 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003404 if (trash.len && trash.str[trash.len - 1] != '/' &&
3405 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3406 if (trash.len > trash.size - 5)
3407 return 0;
3408 trash.str[trash.len] = '/';
3409 trash.len++;
3410 }
3411
3412 break;
3413 }
3414 case REDIRECT_TYPE_PREFIX: {
3415 const char *path;
3416 int pathlen;
3417
3418 path = http_get_path(txn);
3419 /* build message using path */
3420 if (path) {
3421 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3422 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3423 int qs = 0;
3424 while (qs < pathlen) {
3425 if (path[qs] == '?') {
3426 pathlen = qs;
3427 break;
3428 }
3429 qs++;
3430 }
3431 }
3432 } else {
3433 path = "/";
3434 pathlen = 1;
3435 }
3436
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003437 if (rule->rdr_str) { /* this is an old "redirect" rule */
3438 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3439 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003440
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003441 /* add prefix. Note that if prefix == "/", we don't want to
3442 * add anything, otherwise it makes it hard for the user to
3443 * configure a self-redirection.
3444 */
3445 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3446 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3447 trash.len += rule->rdr_len;
3448 }
3449 }
3450 else {
3451 /* add prefix with executing log format */
3452 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3453
3454 /* Check length */
3455 if (trash.len + pathlen > trash.size - 4)
3456 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003457 }
3458
3459 /* add path */
3460 memcpy(trash.str + trash.len, path, pathlen);
3461 trash.len += pathlen;
3462
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003463 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003464 if (trash.len && trash.str[trash.len - 1] != '/' &&
3465 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3466 if (trash.len > trash.size - 5)
3467 return 0;
3468 trash.str[trash.len] = '/';
3469 trash.len++;
3470 }
3471
3472 break;
3473 }
3474 case REDIRECT_TYPE_LOCATION:
3475 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003476 if (rule->rdr_str) { /* this is an old "redirect" rule */
3477 if (trash.len + rule->rdr_len > trash.size - 4)
3478 return 0;
3479
3480 /* add location */
3481 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3482 trash.len += rule->rdr_len;
3483 }
3484 else {
3485 /* add location with executing log format */
3486 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003487
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003488 /* Check left length */
3489 if (trash.len > trash.size - 4)
3490 return 0;
3491 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003492 break;
3493 }
3494
3495 if (rule->cookie_len) {
3496 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3497 trash.len += 14;
3498 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3499 trash.len += rule->cookie_len;
3500 memcpy(trash.str + trash.len, "\r\n", 2);
3501 trash.len += 2;
3502 }
3503
3504 /* add end of headers and the keep-alive/close status.
3505 * We may choose to set keep-alive if the Location begins
3506 * with a slash, because the client will come back to the
3507 * same server.
3508 */
3509 txn->status = rule->code;
3510 /* let's log the request time */
3511 s->logs.tv_request = now;
3512
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003513 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003514 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3515 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3516 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3517 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3518 /* keep-alive possible */
3519 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3520 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3521 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3522 trash.len += 30;
3523 } else {
3524 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3525 trash.len += 24;
3526 }
3527 }
3528 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3529 trash.len += 4;
3530 bo_inject(txn->rsp.chn, trash.str, trash.len);
3531 /* "eat" the request */
3532 bi_fast_delete(txn->req.chn->buf, msg->sov);
3533 msg->sov = 0;
3534 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3535 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3536 txn->req.msg_state = HTTP_MSG_CLOSED;
3537 txn->rsp.msg_state = HTTP_MSG_DONE;
3538 } else {
3539 /* keep-alive not possible */
3540 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3541 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3542 trash.len += 29;
3543 } else {
3544 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3545 trash.len += 23;
3546 }
3547 stream_int_retnclose(txn->req.chn->prod, &trash);
3548 txn->req.chn->analysers = 0;
3549 }
3550
3551 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003552 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003553 if (!(s->flags & SN_FINST_MASK))
3554 s->flags |= SN_FINST_R;
3555
3556 return 1;
3557}
3558
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003559/* This stream analyser runs all HTTP request processing which is common to
3560 * frontends and backends, which means blocking ACLs, filters, connection-close,
3561 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003562 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003563 * either needs more data or wants to immediately abort the request (eg: deny,
3564 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003565 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003566int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003567{
Willy Tarreaud787e662009-07-07 10:14:51 +02003568 struct http_txn *txn = &s->txn;
3569 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003570 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003571 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003572 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003573 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003574
Willy Tarreau655dce92009-11-08 13:10:58 +01003575 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003576 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003577 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003578 return 0;
3579 }
3580
Willy Tarreau3a816292009-07-07 10:55:49 +02003581 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003582 req->analyse_exp = TICK_ETERNITY;
3583
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003584 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 +02003585 now_ms, __FUNCTION__,
3586 s,
3587 req,
3588 req->rex, req->wex,
3589 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003590 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003591 req->analysers);
3592
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003593 /* first check whether we have some ACLs set to block this request */
3594 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003595 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003596
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003597 ret = acl_pass(ret);
3598 if (cond->pol == ACL_COND_UNLESS)
3599 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003600
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 if (ret) {
3602 txn->status = 403;
3603 /* let's log the request time */
3604 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003605 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003606 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003607 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003608 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003609 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003610
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003611 /* just in case we have some per-backend tracking */
3612 session_inc_be_http_req_ctr(s);
3613
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003614 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003615 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003616
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003618 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003619 if (stats_check_uri(s->rep->prod, txn, px)) {
3620 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003621 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3622 txn->status = 500;
3623 s->logs.tv_request = now;
3624 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003625
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003626 if (!(s->flags & SN_ERR_MASK))
3627 s->flags |= SN_ERR_RESOURCE;
3628 goto return_prx_cond;
3629 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003630 /* parse the whole stats request and extract the relevant information */
3631 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003632 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 +01003633 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003634 }
3635
Willy Tarreau3b44e722013-11-16 10:28:23 +01003636 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3637 * blocked by an http-request rule.
3638 */
3639 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003640 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003641 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003642 }
Willy Tarreau06619262006-12-17 08:37:22 +01003643
Willy Tarreau3b44e722013-11-16 10:28:23 +01003644 /* return a 403 if either rule has blocked */
3645 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003646 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003647 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003648 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003649 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003650 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003651 s->fe->fe_counters.denied_req++;
3652 if (s->fe != s->be)
3653 s->be->be_counters.denied_req++;
3654 if (s->listener->counters)
3655 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003656 goto return_prx_cond;
3657 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003658
3659 /* When a connection is tarpitted, we use the tarpit timeout,
3660 * which may be the same as the connect timeout if unspecified.
3661 * If unset, then set it to zero because we really want it to
3662 * eventually expire. We build the tarpit as an analyser.
3663 */
3664 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003665 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003666 /* wipe the request out so that we can drop the connection early
3667 * if the client closes first.
3668 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003669 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003670 req->analysers = 0; /* remove switching rules etc... */
3671 req->analysers |= AN_REQ_HTTP_TARPIT;
3672 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3673 if (!req->analyse_exp)
3674 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003675 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003676 s->fe->fe_counters.denied_req++;
3677 if (s->fe != s->be)
3678 s->be->be_counters.denied_req++;
3679 if (s->listener->counters)
3680 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003681 return 1;
3682 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003683 }
Willy Tarreau06619262006-12-17 08:37:22 +01003684
Willy Tarreau70dffda2014-01-30 03:07:23 +01003685 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003686 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003687 * Option httpclose by itself sets tunnel mode where headers are mangled.
3688 * However, if another mode is set, it will affect it (eg: server-close/
3689 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3690 * if FE and BE have the same settings (common). The method consists in
3691 * checking if options changed between the two calls (implying that either
3692 * one is non-null, or one of them is non-null and we are there for the first
3693 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003694 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003695
Willy Tarreau416ce612014-01-31 15:45:34 +01003696 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003697 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003698 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003699
Willy Tarreau70dffda2014-01-30 03:07:23 +01003700 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3701 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3702 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3703 tmp = TX_CON_WANT_TUN;
3704
3705 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3706 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3707 tmp = TX_CON_WANT_TUN;
3708 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003709
3710 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003711 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3712 /* option httpclose + server_close => forceclose */
3713 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3714 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3715 tmp = TX_CON_WANT_CLO;
3716 else
3717 tmp = TX_CON_WANT_SCL;
3718 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003719
3720 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3721 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003722 tmp = TX_CON_WANT_CLO;
3723
Willy Tarreau5b154472009-12-21 20:11:07 +01003724 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3725 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003726
Willy Tarreau416ce612014-01-31 15:45:34 +01003727 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3728 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003729 /* parse the Connection header and possibly clean it */
3730 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003731 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003732 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3733 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003734 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003735 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003736 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003737 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003738 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003739
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003740 /* check if client or config asks for explicit close in KAL/SCL */
3741 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3742 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3743 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003744 (!(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 +01003745 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003746 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003747 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3748 }
Willy Tarreau78599912009-10-17 20:12:21 +02003749
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003750 /* we can be blocked here because the request needs to be authenticated,
3751 * either to pass or to access stats.
3752 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003753 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003754 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003755
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003756 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003757 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003758
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003759 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003760 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003761 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003762 /* on 401 we still count one error, because normal browsing
3763 * won't significantly increase the counter but brute force
3764 * attempts will.
3765 */
3766 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003767 goto return_prx_cond;
3768 }
3769
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003770 /* add request headers from the rule sets in the same order */
3771 list_for_each_entry(wl, &px->req_add, list) {
3772 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003773 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003774 ret = acl_pass(ret);
3775 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3776 ret = !ret;
3777 if (!ret)
3778 continue;
3779 }
3780
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003781 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003782 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003783 }
3784
3785 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3786 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3787 goto return_bad_req;
3788 req->analyse_exp = TICK_ETERNITY;
3789 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003790 }
3791
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003792 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003793 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003794 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3795 s->fe->fe_counters.intercepted_req++;
3796
3797 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3798 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3799 if (!(s->flags & SN_FINST_MASK))
3800 s->flags |= SN_FINST_R;
3801
3802 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02003803
3804 /* we may want to compress the stats page */
3805 if (s->fe->comp || s->be->comp)
3806 select_compression_request_header(s, req->buf);
3807
3808 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
3809 req->analysers = AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003810 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003811 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003812
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003813 /* check whether we have some ACLs set to redirect this request */
3814 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003815 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003816 int ret;
3817
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003818 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003819 ret = acl_pass(ret);
3820 if (rule->cond->pol == ACL_COND_UNLESS)
3821 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003822 if (!ret)
3823 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003824 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003825 if (!http_apply_redirect_rule(rule, s, txn))
3826 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003827
Willy Tarreau71241ab2012-12-27 11:30:54 +01003828 req->analyse_exp = TICK_ETERNITY;
3829 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003830 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003831
Willy Tarreau2be39392010-01-03 17:24:51 +01003832 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3833 * If this happens, then the data will not come immediately, so we must
3834 * send all what we have without waiting. Note that due to the small gain
3835 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003836 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003837 * itself once used.
3838 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003839 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003840
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003841 /* that's OK for us now, let's move on to next analysers */
3842 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003843
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003844 return_bad_req:
3845 /* We centralize bad requests processing here */
3846 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3847 /* we detected a parsing error. We want to archive this request
3848 * in the dedicated proxy area for later troubleshooting.
3849 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003850 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003851 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003852
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003853 txn->req.msg_state = HTTP_MSG_ERROR;
3854 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003855 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003856
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003857 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003858 if (s->listener->counters)
3859 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003860
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003861 return_prx_cond:
3862 if (!(s->flags & SN_ERR_MASK))
3863 s->flags |= SN_ERR_PRXCOND;
3864 if (!(s->flags & SN_FINST_MASK))
3865 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003866
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003867 req->analysers = 0;
3868 req->analyse_exp = TICK_ETERNITY;
3869 return 0;
3870}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003871
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003872/* This function performs all the processing enabled for the current request.
3873 * It returns 1 if the processing can continue on next analysers, or zero if it
3874 * needs more data, encounters an error, or wants to immediately abort the
3875 * request. It relies on buffers flags, and updates s->req->analysers.
3876 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003877int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003878{
3879 struct http_txn *txn = &s->txn;
3880 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003881 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003882
Willy Tarreau655dce92009-11-08 13:10:58 +01003883 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003884 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003885 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003886 return 0;
3887 }
3888
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003889 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 +02003890 now_ms, __FUNCTION__,
3891 s,
3892 req,
3893 req->rex, req->wex,
3894 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003895 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003896 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003897
William Lallemand82fe75c2012-10-23 10:25:10 +02003898 if (s->fe->comp || s->be->comp)
3899 select_compression_request_header(s, req->buf);
3900
Willy Tarreau59234e92008-11-30 23:51:27 +01003901 /*
3902 * Right now, we know that we have processed the entire headers
3903 * and that unwanted requests have been filtered out. We can do
3904 * whatever we want with the remaining request. Also, now we
3905 * may have separate values for ->fe, ->be.
3906 */
Willy Tarreau06619262006-12-17 08:37:22 +01003907
Willy Tarreau59234e92008-11-30 23:51:27 +01003908 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003909 * If HTTP PROXY is set we simply get remote server address parsing
3910 * incoming request. Note that this requires that a connection is
3911 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003912 */
3913 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003914 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003915 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003916
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003917 /* Note that for now we don't reuse existing proxy connections */
3918 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003919 txn->req.msg_state = HTTP_MSG_ERROR;
3920 txn->status = 500;
3921 req->analysers = 0;
3922 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3923
3924 if (!(s->flags & SN_ERR_MASK))
3925 s->flags |= SN_ERR_RESOURCE;
3926 if (!(s->flags & SN_FINST_MASK))
3927 s->flags |= SN_FINST_R;
3928
3929 return 0;
3930 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003931
3932 path = http_get_path(txn);
3933 url2sa(req->buf->p + msg->sl.rq.u,
3934 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003935 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003936 /* if the path was found, we have to remove everything between
3937 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3938 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3939 * u_l characters by a single "/".
3940 */
3941 if (path) {
3942 char *cur_ptr = req->buf->p;
3943 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3944 int delta;
3945
3946 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3947 http_msg_move_end(&txn->req, delta);
3948 cur_end += delta;
3949 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3950 goto return_bad_req;
3951 }
3952 else {
3953 char *cur_ptr = req->buf->p;
3954 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3955 int delta;
3956
3957 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3958 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3959 http_msg_move_end(&txn->req, delta);
3960 cur_end += delta;
3961 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3962 goto return_bad_req;
3963 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003964 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003965
Willy Tarreau59234e92008-11-30 23:51:27 +01003966 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003967 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003968 * Note that doing so might move headers in the request, but
3969 * the fields will stay coherent and the URI will not move.
3970 * This should only be performed in the backend.
3971 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003972 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003973 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3974 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003975
Willy Tarreau59234e92008-11-30 23:51:27 +01003976 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003977 * 8: the appsession cookie was looked up very early in 1.2,
3978 * so let's do the same now.
3979 */
3980
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003981 /* It needs to look into the URI unless persistence must be ignored */
3982 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003983 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 +01003984 }
3985
William Lallemanda73203e2012-03-12 12:48:57 +01003986 /* add unique-id if "header-unique-id" is specified */
3987
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003988 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3989 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3990 goto return_bad_req;
3991 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003992 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003993 }
William Lallemanda73203e2012-03-12 12:48:57 +01003994
3995 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003996 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3997 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003998 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003999 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004000 goto return_bad_req;
4001 }
4002
Cyril Bontéb21570a2009-11-29 20:04:48 +01004003 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004004 * 9: add X-Forwarded-For if either the frontend or the backend
4005 * asks for it.
4006 */
4007 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004008 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004009 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004010 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4011 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004012 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004013 /* The header is set to be added only if none is present
4014 * and we found it, so don't do anything.
4015 */
4016 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004017 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004018 /* Add an X-Forwarded-For header unless the source IP is
4019 * in the 'except' network range.
4020 */
4021 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004022 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004023 != s->fe->except_net.s_addr) &&
4024 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004025 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004026 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004027 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004028 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004029 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004030
4031 /* Note: we rely on the backend to get the header name to be used for
4032 * x-forwarded-for, because the header is really meant for the backends.
4033 * However, if the backend did not specify any option, we have to rely
4034 * on the frontend's header name.
4035 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004036 if (s->be->fwdfor_hdr_len) {
4037 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004038 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004039 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004040 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004041 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004042 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004043 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 +01004044
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004045 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004046 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004047 }
4048 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004049 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004050 /* FIXME: for the sake of completeness, we should also support
4051 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004052 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004053 int len;
4054 char pn[INET6_ADDRSTRLEN];
4055 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004056 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004057 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004058
Willy Tarreau59234e92008-11-30 23:51:27 +01004059 /* Note: we rely on the backend to get the header name to be used for
4060 * x-forwarded-for, because the header is really meant for the backends.
4061 * However, if the backend did not specify any option, we have to rely
4062 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004063 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004064 if (s->be->fwdfor_hdr_len) {
4065 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004066 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004067 } else {
4068 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004069 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004070 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004071 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004072
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004073 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004074 goto return_bad_req;
4075 }
4076 }
4077
4078 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004079 * 10: add X-Original-To if either the frontend or the backend
4080 * asks for it.
4081 */
4082 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4083
4084 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004085 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004086 /* Add an X-Original-To header unless the destination IP is
4087 * in the 'except' network range.
4088 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004089 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004090
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004091 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004092 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004093 (((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 +02004094 != s->fe->except_to.s_addr) &&
4095 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004096 (((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 +02004097 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004098 int len;
4099 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004100 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004101
4102 /* Note: we rely on the backend to get the header name to be used for
4103 * x-original-to, because the header is really meant for the backends.
4104 * However, if the backend did not specify any option, we have to rely
4105 * on the frontend's header name.
4106 */
4107 if (s->be->orgto_hdr_len) {
4108 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004109 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004110 } else {
4111 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004112 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004113 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004114 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 +02004115
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004116 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004117 goto return_bad_req;
4118 }
4119 }
4120 }
4121
Willy Tarreau50fc7772012-11-11 22:19:57 +01004122 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4123 * If an "Upgrade" token is found, the header is left untouched in order not to have
4124 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4125 * "Upgrade" is present in the Connection header.
4126 */
4127 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4128 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004129 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4130 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004131 unsigned int want_flags = 0;
4132
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004133 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004134 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004135 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4136 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004137 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004138 want_flags |= TX_CON_CLO_SET;
4139 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004140 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004141 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4142 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004143 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004144 want_flags |= TX_CON_KAL_SET;
4145 }
4146
4147 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004148 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004149 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004150
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004151
Willy Tarreau522d6c02009-12-06 18:49:18 +01004152 /* If we have no server assigned yet and we're balancing on url_param
4153 * with a POST request, we may be interested in checking the body for
4154 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004155 */
4156 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4157 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004158 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004159 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004160 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004161 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004162
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004163 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004164 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004165#ifdef TCP_QUICKACK
4166 /* We expect some data from the client. Unless we know for sure
4167 * we already have a full request, we have to re-enable quick-ack
4168 * in case we previously disabled it, otherwise we might cause
4169 * the client to delay further data.
4170 */
4171 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004172 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004173 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004174 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004175 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004176#endif
4177 }
Willy Tarreau03945942009-12-22 16:50:27 +01004178
Willy Tarreau59234e92008-11-30 23:51:27 +01004179 /*************************************************************
4180 * OK, that's finished for the headers. We have done what we *
4181 * could. Let's switch to the DATA state. *
4182 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004183 req->analyse_exp = TICK_ETERNITY;
4184 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004185
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004186 /* if the server closes the connection, we want to immediately react
4187 * and close the socket to save packets and syscalls.
4188 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004189 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4190 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004191
Willy Tarreau59234e92008-11-30 23:51:27 +01004192 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004193 /* OK let's go on with the BODY now */
4194 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004195
Willy Tarreau59234e92008-11-30 23:51:27 +01004196 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004197 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004198 /* we detected a parsing error. We want to archive this request
4199 * in the dedicated proxy area for later troubleshooting.
4200 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004201 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004202 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004203
Willy Tarreau59234e92008-11-30 23:51:27 +01004204 txn->req.msg_state = HTTP_MSG_ERROR;
4205 txn->status = 400;
4206 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004207 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004208
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004209 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004210 if (s->listener->counters)
4211 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004212
Willy Tarreau59234e92008-11-30 23:51:27 +01004213 if (!(s->flags & SN_ERR_MASK))
4214 s->flags |= SN_ERR_PRXCOND;
4215 if (!(s->flags & SN_FINST_MASK))
4216 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004217 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004218}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004219
Willy Tarreau60b85b02008-11-30 23:28:40 +01004220/* This function is an analyser which processes the HTTP tarpit. It always
4221 * returns zero, at the beginning because it prevents any other processing
4222 * from occurring, and at the end because it terminates the request.
4223 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004224int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004225{
4226 struct http_txn *txn = &s->txn;
4227
4228 /* This connection is being tarpitted. The CLIENT side has
4229 * already set the connect expiration date to the right
4230 * timeout. We just have to check that the client is still
4231 * there and that the timeout has not expired.
4232 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004233 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004234 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004235 !tick_is_expired(req->analyse_exp, now_ms))
4236 return 0;
4237
4238 /* We will set the queue timer to the time spent, just for
4239 * logging purposes. We fake a 500 server error, so that the
4240 * attacker will not suspect his connection has been tarpitted.
4241 * It will not cause trouble to the logs because we can exclude
4242 * the tarpitted connections by filtering on the 'PT' status flags.
4243 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004244 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4245
4246 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004247 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004248 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004249
4250 req->analysers = 0;
4251 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004252
Willy Tarreau60b85b02008-11-30 23:28:40 +01004253 if (!(s->flags & SN_ERR_MASK))
4254 s->flags |= SN_ERR_PRXCOND;
4255 if (!(s->flags & SN_FINST_MASK))
4256 s->flags |= SN_FINST_T;
4257 return 0;
4258}
4259
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004260/* This function is an analyser which waits for the HTTP request body. It waits
4261 * for either the buffer to be full, or the full advertised contents to have
4262 * reached the buffer. It must only be called after the standard HTTP request
4263 * processing has occurred, because it expects the request to be parsed and will
4264 * look for the Expect header. It may send a 100-Continue interim response. It
4265 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4266 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4267 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004268 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004269int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004270{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004271 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004272 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004273
4274 /* We have to parse the HTTP request body to find any required data.
4275 * "balance url_param check_post" should have been the only way to get
4276 * into this. We were brought here after HTTP header analysis, so all
4277 * related structures are ready.
4278 */
4279
Willy Tarreau890988f2014-04-10 11:59:33 +02004280 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4281 /* This is the first call */
4282 if (msg->msg_state < HTTP_MSG_BODY)
4283 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004284
Willy Tarreau890988f2014-04-10 11:59:33 +02004285 if (msg->msg_state < HTTP_MSG_100_SENT) {
4286 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4287 * send an HTTP/1.1 100 Continue intermediate response.
4288 */
4289 if (msg->flags & HTTP_MSGF_VER_11) {
4290 struct hdr_ctx ctx;
4291 ctx.idx = 0;
4292 /* Expect is allowed in 1.1, look for it */
4293 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4294 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4295 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4296 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004297 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004298 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004299 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004300
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004301 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004302 * req->buf->p still points to the beginning of the message. We
4303 * must save the body in msg->next because it survives buffer
4304 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004305 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004306 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004307
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004308 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004309 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4310 else
4311 msg->msg_state = HTTP_MSG_DATA;
4312 }
4313
Willy Tarreau890988f2014-04-10 11:59:33 +02004314 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4315 /* We're in content-length mode, we just have to wait for enough data. */
4316 if (req->buf->i - msg->sov < msg->body_len)
4317 goto missing_data;
4318
4319 /* OK we have everything we need now */
4320 goto http_end;
4321 }
4322
4323 /* OK here we're parsing a chunked-encoded message */
4324
Willy Tarreau522d6c02009-12-06 18:49:18 +01004325 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004326 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004327 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004328 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004329 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004330 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004331
Willy Tarreau115acb92009-12-26 13:56:06 +01004332 if (!ret)
4333 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004334 else if (ret < 0) {
4335 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004336 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004337 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004338 }
4339
Willy Tarreaud98cf932009-12-27 22:54:55 +01004340 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004341 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004342 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004343 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004344 if (msg->msg_state == HTTP_MSG_TRAILERS)
4345 goto http_end;
4346
Willy Tarreau226071e2014-04-10 11:55:45 +02004347 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004348 goto http_end;
4349
4350 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004351 /* we get here if we need to wait for more data. If the buffer is full,
4352 * we have the maximum we can expect.
4353 */
4354 if (buffer_full(req->buf, global.tune.maxrewrite))
4355 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004356
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004357 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004358 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004359 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004360
4361 if (!(s->flags & SN_ERR_MASK))
4362 s->flags |= SN_ERR_CLITO;
4363 if (!(s->flags & SN_FINST_MASK))
4364 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004365 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004366 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004367
4368 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004369 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004370 /* Not enough data. We'll re-use the http-request
4371 * timeout here. Ideally, we should set the timeout
4372 * relative to the accept() date. We just set the
4373 * request timeout once at the beginning of the
4374 * request.
4375 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004376 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004377 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004378 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004379 return 0;
4380 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004381
4382 http_end:
4383 /* The situation will not evolve, so let's give up on the analysis. */
4384 s->logs.tv_request = now; /* update the request timer to reflect full request */
4385 req->analysers &= ~an_bit;
4386 req->analyse_exp = TICK_ETERNITY;
4387 return 1;
4388
4389 return_bad_req: /* let's centralize all bad requests */
4390 txn->req.msg_state = HTTP_MSG_ERROR;
4391 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004392 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004393
Willy Tarreau79ebac62010-06-07 13:47:49 +02004394 if (!(s->flags & SN_ERR_MASK))
4395 s->flags |= SN_ERR_PRXCOND;
4396 if (!(s->flags & SN_FINST_MASK))
4397 s->flags |= SN_FINST_R;
4398
Willy Tarreau522d6c02009-12-06 18:49:18 +01004399 return_err_msg:
4400 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004401 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004402 if (s->listener->counters)
4403 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004404 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004405}
4406
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004407/* send a server's name with an outgoing request over an established connection.
4408 * Note: this function is designed to be called once the request has been scheduled
4409 * for being forwarded. This is the reason why it rewinds the buffer before
4410 * proceeding.
4411 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004412int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004413
4414 struct hdr_ctx ctx;
4415
Mark Lamourinec2247f02012-01-04 13:02:01 -05004416 char *hdr_name = be->server_id_hdr_name;
4417 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004418 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004419 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004420 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004421
William Lallemandd9e90662012-01-30 17:27:17 +01004422 ctx.idx = 0;
4423
Willy Tarreau211cdec2014-04-17 20:18:08 +02004424 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004425 if (old_o) {
4426 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004427 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004428 txn->req.next += old_o;
4429 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004430 }
4431
Willy Tarreau9b28e032012-10-12 23:49:43 +02004432 old_i = chn->buf->i;
4433 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 -05004434 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004435 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004436 }
4437
4438 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004439 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004440 memcpy(hdr_val, hdr_name, hdr_name_len);
4441 hdr_val += hdr_name_len;
4442 *hdr_val++ = ':';
4443 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004444 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4445 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004446
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004447 if (old_o) {
4448 /* If this was a forwarded request, we must readjust the amount of
4449 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004450 * variations. Note that the current state is >= HTTP_MSG_BODY,
4451 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004452 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004453 old_o += chn->buf->i - old_i;
4454 b_adv(chn->buf, old_o);
4455 txn->req.next -= old_o;
4456 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004457 }
4458
Mark Lamourinec2247f02012-01-04 13:02:01 -05004459 return 0;
4460}
4461
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462/* Terminate current transaction and prepare a new one. This is very tricky
4463 * right now but it works.
4464 */
4465void http_end_txn_clean_session(struct session *s)
4466{
Willy Tarreau068621e2013-12-23 15:11:25 +01004467 int prev_status = s->txn.status;
4468
Willy Tarreau610ecce2010-01-04 21:15:02 +01004469 /* FIXME: We need a more portable way of releasing a backend's and a
4470 * server's connections. We need a safer way to reinitialize buffer
4471 * flags. We also need a more accurate method for computing per-request
4472 * data.
4473 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474
Willy Tarreau4213a112013-12-15 10:25:42 +01004475 /* unless we're doing keep-alive, we want to quickly close the connection
4476 * to the server.
4477 */
4478 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4479 !si_conn_ready(s->req->cons)) {
4480 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4481 si_shutr(s->req->cons);
4482 si_shutw(s->req->cons);
4483 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004484
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004485 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004487 if (unlikely(s->srv_conn))
4488 sess_change_server(s, NULL);
4489 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490
4491 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4492 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004493 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494
4495 if (s->txn.status) {
4496 int n;
4497
4498 n = s->txn.status / 100;
4499 if (n < 1 || n > 5)
4500 n = 0;
4501
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004502 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004503 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004504 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004505 s->fe->fe_counters.p.http.comp_rsp++;
4506 }
Willy Tarreau24657792010-02-26 10:30:28 +01004507 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004508 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004509 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004510 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004511 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004512 s->be->be_counters.p.http.comp_rsp++;
4513 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 }
4515
4516 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004517 s->logs.bytes_in -= s->req->buf->i;
4518 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004519
4520 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004521 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 !(s->flags & SN_MONITOR) &&
4523 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4524 s->do_log(s);
4525 }
4526
4527 s->logs.accept_date = date; /* user-visible date for logging */
4528 s->logs.tv_accept = now; /* corrected date for internal use */
4529 tv_zero(&s->logs.tv_request);
4530 s->logs.t_queue = -1;
4531 s->logs.t_connect = -1;
4532 s->logs.t_data = -1;
4533 s->logs.t_close = 0;
4534 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4535 s->logs.srv_queue_size = 0; /* we will get this number soon */
4536
Willy Tarreau9b28e032012-10-12 23:49:43 +02004537 s->logs.bytes_in = s->req->total = s->req->buf->i;
4538 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004539
4540 if (s->pend_pos)
4541 pendconn_free(s->pend_pos);
4542
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004543 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544 if (s->flags & SN_CURR_SESS) {
4545 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004546 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004548 if (may_dequeue_tasks(objt_server(s->target), s->be))
4549 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 }
4551
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004552 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553
Willy Tarreau4213a112013-12-15 10:25:42 +01004554 /* only release our endpoint if we don't intend to reuse the
4555 * connection.
4556 */
4557 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4558 !si_conn_ready(s->req->cons)) {
4559 si_release_endpoint(s->req->cons);
4560 }
4561
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004564 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004566 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004567 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4568 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 +02004569 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 +01004570 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004571
Willy Tarreau610ecce2010-01-04 21:15:02 +01004572 s->txn.meth = 0;
4573 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004574 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004575
4576 if (prev_status == 401 || prev_status == 407) {
4577 /* In HTTP keep-alive mode, if we receive a 401, we still have
4578 * a chance of being able to send the visitor again to the same
4579 * server over the same connection. This is required by some
4580 * broken protocols such as NTLM, and anyway whenever there is
4581 * an opportunity for sending the challenge to the proper place,
4582 * it's better to do it (at least it helps with debugging).
4583 */
4584 s->txn.flags |= TX_PREFER_LAST;
4585 }
4586
Willy Tarreauee55dc02010-06-01 10:56:34 +02004587 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 s->req->cons->flags |= SI_FL_INDEP_STR;
4589
Willy Tarreau96e31212011-05-30 18:10:30 +02004590 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004591 s->req->flags |= CF_NEVER_WAIT;
4592 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004593 }
4594
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 /* if the request buffer is not empty, it means we're
4596 * about to process another request, so send pending
4597 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004598 * Just don't do this if the buffer is close to be full,
4599 * because the request will wait for it to flush a little
4600 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004601 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004602 if (s->req->buf->i) {
4603 if (s->rep->buf->o &&
4604 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4605 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004606 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004607 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004608
4609 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004610 channel_auto_read(s->req);
4611 channel_auto_close(s->req);
4612 channel_auto_read(s->rep);
4613 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004614
Willy Tarreau27375622013-12-17 00:00:28 +01004615 /* we're in keep-alive with an idle connection, monitor it */
4616 si_idle_conn(s->req->cons);
4617
Willy Tarreau342b11c2010-11-24 16:22:09 +01004618 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620}
4621
4622
4623/* This function updates the request state machine according to the response
4624 * state machine and buffer flags. It returns 1 if it changes anything (flag
4625 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4626 * it is only used to find when a request/response couple is complete. Both
4627 * this function and its equivalent should loop until both return zero. It
4628 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4629 */
4630int http_sync_req_state(struct session *s)
4631{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004632 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004634 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 unsigned int old_state = txn->req.msg_state;
4636
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4638 return 0;
4639
4640 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004641 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004642 * We can shut the read side unless we want to abort_on_close,
4643 * or we have a POST request. The issue with POST requests is
4644 * that some browsers still send a CRLF after the request, and
4645 * this CRLF must be read so that it does not remain in the kernel
4646 * buffers, otherwise a close could cause an RST on some systems
4647 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004648 * Note that if we're using keep-alive on the client side, we'd
4649 * rather poll now and keep the polling enabled for the whole
4650 * session's life than enabling/disabling it between each
4651 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004652 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004653 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4654 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4655 !(s->be->options & PR_O_ABRT_CLOSE) &&
4656 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004657 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658
Willy Tarreau40f151a2012-12-20 12:10:09 +01004659 /* if the server closes the connection, we want to immediately react
4660 * and close the socket to save packets and syscalls.
4661 */
4662 chn->cons->flags |= SI_FL_NOHALF;
4663
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4665 goto wait_other_side;
4666
4667 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4668 /* The server has not finished to respond, so we
4669 * don't want to move in order not to upset it.
4670 */
4671 goto wait_other_side;
4672 }
4673
4674 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4675 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004676 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004677 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004678 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004679 goto wait_other_side;
4680 }
4681
4682 /* When we get here, it means that both the request and the
4683 * response have finished receiving. Depending on the connection
4684 * mode, we'll have to wait for the last bytes to leave in either
4685 * direction, and sometimes for a close to be effective.
4686 */
4687
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004688 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4689 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004690 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4691 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004692 }
4693 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4694 /* Option forceclose is set, or either side wants to close,
4695 * let's enforce it now that we're not expecting any new
4696 * data to come. The caller knows the session is complete
4697 * once both states are CLOSED.
4698 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004699 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4700 channel_shutr_now(chn);
4701 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004702 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004703 }
4704 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004705 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4706 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004707 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004708 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4709 channel_auto_read(chn);
4710 txn->req.msg_state = HTTP_MSG_TUNNEL;
4711 chn->flags |= CF_NEVER_WAIT;
4712 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004713 }
4714
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004715 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004717 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004718
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004719 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720 txn->req.msg_state = HTTP_MSG_CLOSING;
4721 goto http_msg_closing;
4722 }
4723 else {
4724 txn->req.msg_state = HTTP_MSG_CLOSED;
4725 goto http_msg_closed;
4726 }
4727 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004728 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 }
4730
4731 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4732 http_msg_closing:
4733 /* nothing else to forward, just waiting for the output buffer
4734 * to be empty and for the shutw_now to take effect.
4735 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004736 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004737 txn->req.msg_state = HTTP_MSG_CLOSED;
4738 goto http_msg_closed;
4739 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004740 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 txn->req.msg_state = HTTP_MSG_ERROR;
4742 goto wait_other_side;
4743 }
4744 }
4745
4746 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4747 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004748 /* see above in MSG_DONE why we only do this in these states */
4749 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4750 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4751 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004752 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004753 goto wait_other_side;
4754 }
4755
4756 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004757 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004758}
4759
4760
4761/* This function updates the response state machine according to the request
4762 * state machine and buffer flags. It returns 1 if it changes anything (flag
4763 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4764 * it is only used to find when a request/response couple is complete. Both
4765 * this function and its equivalent should loop until both return zero. It
4766 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4767 */
4768int http_sync_res_state(struct session *s)
4769{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004770 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004771 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004772 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773 unsigned int old_state = txn->rsp.msg_state;
4774
Willy Tarreau610ecce2010-01-04 21:15:02 +01004775 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4776 return 0;
4777
4778 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4779 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004780 * still monitor the server connection for a possible close
4781 * while the request is being uploaded, so we don't disable
4782 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004783 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004784 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004785
4786 if (txn->req.msg_state == HTTP_MSG_ERROR)
4787 goto wait_other_side;
4788
4789 if (txn->req.msg_state < HTTP_MSG_DONE) {
4790 /* The client seems to still be sending data, probably
4791 * because we got an error response during an upload.
4792 * We have the choice of either breaking the connection
4793 * or letting it pass through. Let's do the later.
4794 */
4795 goto wait_other_side;
4796 }
4797
4798 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4799 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004800 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004802 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004803 goto wait_other_side;
4804 }
4805
4806 /* When we get here, it means that both the request and the
4807 * response have finished receiving. Depending on the connection
4808 * mode, we'll have to wait for the last bytes to leave in either
4809 * direction, and sometimes for a close to be effective.
4810 */
4811
4812 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4813 /* Server-close mode : shut read and wait for the request
4814 * side to close its output buffer. The caller will detect
4815 * when we're in DONE and the other is in CLOSED and will
4816 * catch that for the final cleanup.
4817 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004818 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4819 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004820 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004821 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4822 /* Option forceclose is set, or either side wants to close,
4823 * let's enforce it now that we're not expecting any new
4824 * data to come. The caller knows the session is complete
4825 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004826 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004827 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4828 channel_shutr_now(chn);
4829 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004830 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831 }
4832 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004833 /* The last possible modes are keep-alive and tunnel. Tunnel will
4834 * need to forward remaining data. Keep-alive will need to monitor
4835 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004836 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004837 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004838 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004839 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4840 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004841 }
4842
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004843 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004844 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004845 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004846 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4847 goto http_msg_closing;
4848 }
4849 else {
4850 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4851 goto http_msg_closed;
4852 }
4853 }
4854 goto wait_other_side;
4855 }
4856
4857 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4858 http_msg_closing:
4859 /* nothing else to forward, just waiting for the output buffer
4860 * to be empty and for the shutw_now to take effect.
4861 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004862 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004863 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4864 goto http_msg_closed;
4865 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004866 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004868 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004869 if (objt_server(s->target))
4870 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004871 goto wait_other_side;
4872 }
4873 }
4874
4875 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4876 http_msg_closed:
4877 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004878 bi_erase(chn);
4879 channel_auto_close(chn);
4880 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004881 goto wait_other_side;
4882 }
4883
4884 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004885 /* We force the response to leave immediately if we're waiting for the
4886 * other side, since there is no pending shutdown to push it out.
4887 */
4888 if (!channel_is_empty(chn))
4889 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004890 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004891}
4892
4893
4894/* Resync the request and response state machines. Return 1 if either state
4895 * changes.
4896 */
4897int http_resync_states(struct session *s)
4898{
4899 struct http_txn *txn = &s->txn;
4900 int old_req_state = txn->req.msg_state;
4901 int old_res_state = txn->rsp.msg_state;
4902
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 http_sync_req_state(s);
4904 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004905 if (!http_sync_res_state(s))
4906 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004907 if (!http_sync_req_state(s))
4908 break;
4909 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004910
Willy Tarreau610ecce2010-01-04 21:15:02 +01004911 /* OK, both state machines agree on a compatible state.
4912 * There are a few cases we're interested in :
4913 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4914 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4915 * directions, so let's simply disable both analysers.
4916 * - HTTP_MSG_CLOSED on the response only means we must abort the
4917 * request.
4918 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4919 * with server-close mode means we've completed one request and we
4920 * must re-initialize the server connection.
4921 */
4922
4923 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4924 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4925 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4926 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4927 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004928 channel_auto_close(s->req);
4929 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +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 Tarreau610ecce2010-01-04 21:15:02 +01004933 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004934 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4935 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004936 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004937 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004938 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004939 channel_auto_close(s->rep);
4940 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004941 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004942 channel_abort(s->req);
4943 channel_auto_close(s->req);
4944 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004945 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004947 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4948 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004949 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004950 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4951 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4952 /* server-close/keep-alive: terminate this transaction,
4953 * possibly killing the server connection and reinitialize
4954 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004955 */
4956 http_end_txn_clean_session(s);
4957 }
4958
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 return txn->req.msg_state != old_req_state ||
4960 txn->rsp.msg_state != old_res_state;
4961}
4962
Willy Tarreaud98cf932009-12-27 22:54:55 +01004963/* This function is an analyser which forwards request body (including chunk
4964 * sizes if any). It is called as soon as we must forward, even if we forward
4965 * zero byte. The only situation where it must not be called is when we're in
4966 * tunnel mode and we want to forward till the close. It's used both to forward
4967 * remaining data and to resync after end of body. It expects the msg_state to
4968 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4969 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004970 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004971 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004972 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004973int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004974{
4975 struct http_txn *txn = &s->txn;
4976 struct http_msg *msg = &s->txn.req;
4977
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004978 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4979 return 0;
4980
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004981 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004982 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004983 /* Output closed while we were sending data. We must abort and
4984 * wake the other side up.
4985 */
4986 msg->msg_state = HTTP_MSG_ERROR;
4987 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004988 return 1;
4989 }
4990
Willy Tarreaud98cf932009-12-27 22:54:55 +01004991 /* Note that we don't have to send 100-continue back because we don't
4992 * need the data to complete our job, and it's up to the server to
4993 * decide whether to return 100, 417 or anything else in return of
4994 * an "Expect: 100-continue" header.
4995 */
4996
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02004997 if (msg->sov) {
4998 /* we have msg->sov which points to the first byte of message
4999 * body, and req->buf.p still points to the beginning of the
5000 * message. We forward the headers now, as we don't need them
5001 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005002 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005003 b_adv(req->buf, msg->sov);
5004 msg->next -= msg->sov;
5005 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005006
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005007 /* The previous analysers guarantee that the state is somewhere
5008 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5009 * msg->next are always correct.
5010 */
5011 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5012 if (msg->flags & HTTP_MSGF_TE_CHNK)
5013 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5014 else
5015 msg->msg_state = HTTP_MSG_DATA;
5016 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005017 }
5018
Willy Tarreau7ba23542014-04-17 21:50:00 +02005019 /* Some post-connect processing might want us to refrain from starting to
5020 * forward data. Currently, the only reason for this is "balance url_param"
5021 * whichs need to parse/process the request after we've enabled forwarding.
5022 */
5023 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5024 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5025 channel_auto_connect(req);
5026 goto missing_data;
5027 }
5028 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5029 }
5030
Willy Tarreau80a92c02014-03-12 10:41:13 +01005031 /* in most states, we should abort in case of early close */
5032 channel_auto_close(req);
5033
Willy Tarreauefdf0942014-04-24 20:08:57 +02005034 if (req->to_forward) {
5035 /* We can't process the buffer's contents yet */
5036 req->flags |= CF_WAKE_WRITE;
5037 goto missing_data;
5038 }
5039
Willy Tarreaud98cf932009-12-27 22:54:55 +01005040 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005041 if (msg->msg_state == HTTP_MSG_DATA) {
5042 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005043 /* we may have some pending data starting at req->buf->p */
5044 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005045 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005046 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005047 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005048 msg->next += msg->chunk_len;
5049 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005050
5051 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005052 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005053 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005054 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005055 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005056 }
5057 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005058 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005059 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005060 * TRAILERS state.
5061 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005062 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005063
Willy Tarreau54d23df2012-10-25 19:04:45 +02005064 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005065 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 Tarreau8a0cef22012-03-09 13:39:23 +01005069 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, 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 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005073 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005074 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005075 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005076 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005077
5078 if (ret == 0)
5079 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005080 else if (ret < 0) {
5081 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005082 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005083 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005084 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005085 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005086 /* we're in MSG_CHUNK_SIZE now */
5087 }
5088 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005089 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005090
5091 if (ret == 0)
5092 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005093 else if (ret < 0) {
5094 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005095 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005096 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005097 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005098 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005099 /* we're in HTTP_MSG_DONE now */
5100 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005102 int old_state = msg->msg_state;
5103
Willy Tarreau610ecce2010-01-04 21:15:02 +01005104 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005105
5106 /* we may have some pending data starting at req->buf->p
5107 * such as last chunk of data or trailers.
5108 */
5109 b_adv(req->buf, msg->next);
5110 msg->next = 0;
5111
Willy Tarreau4fe41902010-06-07 22:27:41 +02005112 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005113 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5114 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005115 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116 if (http_resync_states(s)) {
5117 /* some state changes occurred, maybe the analyser
5118 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005119 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005120 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005121 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005122 /* request errors are most likely due to
5123 * the server aborting the transfer.
5124 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005125 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005126 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005127 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005128 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005130 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005132 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005133
5134 /* If "option abortonclose" is set on the backend, we
5135 * want to monitor the client's connection and forward
5136 * any shutdown notification to the server, which will
5137 * decide whether to close or to go on processing the
5138 * request.
5139 */
5140 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005141 channel_auto_read(req);
5142 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005143 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005144 else if (s->txn.meth == HTTP_METH_POST) {
5145 /* POST requests may require to read extra CRLF
5146 * sent by broken browsers and which could cause
5147 * an RST to be sent upon close on some systems
5148 * (eg: Linux).
5149 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005150 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005151 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005152
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005154 }
5155 }
5156
Willy Tarreaud98cf932009-12-27 22:54:55 +01005157 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005158 /* we may have some pending data starting at req->buf->p */
5159 b_adv(req->buf, msg->next);
5160 msg->next = 0;
5161 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5162
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005163 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005164 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005165 if (!(s->flags & SN_ERR_MASK))
5166 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005167 if (!(s->flags & SN_FINST_MASK)) {
5168 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5169 s->flags |= SN_FINST_H;
5170 else
5171 s->flags |= SN_FINST_D;
5172 }
5173
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005174 s->fe->fe_counters.cli_aborts++;
5175 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005176 if (objt_server(s->target))
5177 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005178
5179 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005180 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005181
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005182 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005183 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005184 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005185
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005186 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005187 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005188 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005189 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005190 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005191
Willy Tarreau5c620922011-05-11 19:56:11 +02005192 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005193 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005194 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005195 * modes are already handled by the stream sock layer. We must not do
5196 * this in content-length mode because it could present the MSG_MORE
5197 * flag with the last block of forwarded data, which would cause an
5198 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005199 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005200 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005201 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005202
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005203 return 0;
5204
Willy Tarreaud98cf932009-12-27 22:54:55 +01005205 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005206 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005207 if (s->listener->counters)
5208 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005209
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005210 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005211 /* we may have some pending data starting at req->buf->p */
5212 b_adv(req->buf, msg->next);
5213 msg->next = 0;
5214
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005215 txn->req.msg_state = HTTP_MSG_ERROR;
5216 if (txn->status) {
5217 /* Note: we don't send any error if some data were already sent */
5218 stream_int_retnclose(req->prod, NULL);
5219 } else {
5220 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005221 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005222 }
5223 req->analysers = 0;
5224 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005225
5226 if (!(s->flags & SN_ERR_MASK))
5227 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005228 if (!(s->flags & SN_FINST_MASK)) {
5229 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5230 s->flags |= SN_FINST_H;
5231 else
5232 s->flags |= SN_FINST_D;
5233 }
5234 return 0;
5235
5236 aborted_xfer:
5237 txn->req.msg_state = HTTP_MSG_ERROR;
5238 if (txn->status) {
5239 /* Note: we don't send any error if some data were already sent */
5240 stream_int_retnclose(req->prod, NULL);
5241 } else {
5242 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005243 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005244 }
5245 req->analysers = 0;
5246 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5247
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005248 s->fe->fe_counters.srv_aborts++;
5249 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005250 if (objt_server(s->target))
5251 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005252
5253 if (!(s->flags & SN_ERR_MASK))
5254 s->flags |= SN_ERR_SRVCL;
5255 if (!(s->flags & SN_FINST_MASK)) {
5256 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5257 s->flags |= SN_FINST_H;
5258 else
5259 s->flags |= SN_FINST_D;
5260 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005261 return 0;
5262}
5263
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5265 * processing can continue on next analysers, or zero if it either needs more
5266 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5267 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5268 * when it has nothing left to do, and may remove any analyser when it wants to
5269 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005270 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005271int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005272{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005273 struct http_txn *txn = &s->txn;
5274 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005275 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005276 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005277 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005278 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005279
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005280 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 +02005281 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005282 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005283 rep,
5284 rep->rex, rep->wex,
5285 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005286 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005287 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005288
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005289 /*
5290 * Now parse the partial (or complete) lines.
5291 * We will check the response syntax, and also join multi-line
5292 * headers. An index of all the lines will be elaborated while
5293 * parsing.
5294 *
5295 * For the parsing, we use a 28 states FSM.
5296 *
5297 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005298 * rep->buf->p = beginning of response
5299 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5300 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005301 * msg->eol = end of current header or line (LF or CRLF)
5302 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005303 */
5304
Willy Tarreau628c40c2014-04-24 19:11:26 +02005305 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005306 /* There's a protected area at the end of the buffer for rewriting
5307 * purposes. We don't want to start to parse the request if the
5308 * protected area is affected, because we may have to move processed
5309 * data later, which is much more complicated.
5310 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005311 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005312 if (unlikely(!channel_reserved(rep))) {
5313 /* some data has still not left the buffer, wake us once that's done */
5314 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5315 goto abort_response;
5316 channel_dont_close(rep);
5317 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005318 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005319 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005320 }
5321
Willy Tarreau379357a2013-06-08 12:55:46 +02005322 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5323 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5324 buffer_slow_realign(rep->buf);
5325
Willy Tarreau9b28e032012-10-12 23:49:43 +02005326 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005327 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005328 }
5329
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 /* 1: we might have to print this header in debug mode */
5331 if (unlikely((global.mode & MODE_DEBUG) &&
5332 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005333 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005334 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005335
Willy Tarreau9b28e032012-10-12 23:49:43 +02005336 sol = rep->buf->p;
5337 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005338 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005339
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005340 sol += hdr_idx_first_pos(&txn->hdr_idx);
5341 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005342
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005343 while (cur_idx) {
5344 eol = sol + txn->hdr_idx.v[cur_idx].len;
5345 debug_hdr("srvhdr", s, sol, eol);
5346 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5347 cur_idx = txn->hdr_idx.v[cur_idx].next;
5348 }
5349 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005350
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005351 /*
5352 * Now we quickly check if we have found a full valid response.
5353 * If not so, we check the FD and buffer states before leaving.
5354 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005355 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005356 * responses are checked first.
5357 *
5358 * Depending on whether the client is still there or not, we
5359 * may send an error response back or not. Note that normally
5360 * we should only check for HTTP status there, and check I/O
5361 * errors somewhere else.
5362 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005363
Willy Tarreau655dce92009-11-08 13:10:58 +01005364 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005365 /* Invalid response */
5366 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5367 /* we detected a parsing error. We want to archive this response
5368 * in the dedicated proxy area for later troubleshooting.
5369 */
5370 hdr_response_bad:
5371 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005372 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005373
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005374 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005375 if (objt_server(s->target)) {
5376 objt_server(s->target)->counters.failed_resp++;
5377 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005378 }
Willy Tarreau64648412010-03-05 10:41:54 +01005379 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005380 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005381 rep->analysers = 0;
5382 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005383 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005384 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005385 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005386
5387 if (!(s->flags & SN_ERR_MASK))
5388 s->flags |= SN_ERR_PRXCOND;
5389 if (!(s->flags & SN_FINST_MASK))
5390 s->flags |= SN_FINST_H;
5391
5392 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005393 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005394
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005395 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005396 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005397 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005398 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005399 goto hdr_response_bad;
5400 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005401
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005402 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005403 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005404 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005405 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005406 else if (txn->flags & TX_NOT_FIRST)
5407 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005408
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005409 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005410 if (objt_server(s->target)) {
5411 objt_server(s->target)->counters.failed_resp++;
5412 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005413 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005414
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005415 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005416 rep->analysers = 0;
5417 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005418 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005419 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005420 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005421
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005422 if (!(s->flags & SN_ERR_MASK))
5423 s->flags |= SN_ERR_SRVCL;
5424 if (!(s->flags & SN_FINST_MASK))
5425 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005426 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005427 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005428
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005430 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005431 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005432 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005433 else if (txn->flags & TX_NOT_FIRST)
5434 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005435
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005436 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005437 if (objt_server(s->target)) {
5438 objt_server(s->target)->counters.failed_resp++;
5439 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005440 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005441
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005442 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005443 rep->analysers = 0;
5444 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005445 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005446 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005447 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005448
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005449 if (!(s->flags & SN_ERR_MASK))
5450 s->flags |= SN_ERR_SRVTO;
5451 if (!(s->flags & SN_FINST_MASK))
5452 s->flags |= SN_FINST_H;
5453 return 0;
5454 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005455
Willy Tarreauf003d372012-11-26 13:35:37 +01005456 /* client abort with an abortonclose */
5457 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5458 s->fe->fe_counters.cli_aborts++;
5459 s->be->be_counters.cli_aborts++;
5460 if (objt_server(s->target))
5461 objt_server(s->target)->counters.cli_aborts++;
5462
5463 rep->analysers = 0;
5464 channel_auto_close(rep);
5465
5466 txn->status = 400;
5467 bi_erase(rep);
5468 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5469
5470 if (!(s->flags & SN_ERR_MASK))
5471 s->flags |= SN_ERR_CLICL;
5472 if (!(s->flags & SN_FINST_MASK))
5473 s->flags |= SN_FINST_H;
5474
5475 /* process_session() will take care of the error */
5476 return 0;
5477 }
5478
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005479 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005480 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005481 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005482 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005483 else if (txn->flags & TX_NOT_FIRST)
5484 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005485
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005486 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005487 if (objt_server(s->target)) {
5488 objt_server(s->target)->counters.failed_resp++;
5489 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005490 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005491
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005492 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005493 rep->analysers = 0;
5494 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005495 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005496 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005497 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005498
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005499 if (!(s->flags & SN_ERR_MASK))
5500 s->flags |= SN_ERR_SRVCL;
5501 if (!(s->flags & SN_FINST_MASK))
5502 s->flags |= SN_FINST_H;
5503 return 0;
5504 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005505
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005506 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005507 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005508 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005509 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005510 else if (txn->flags & TX_NOT_FIRST)
5511 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005512
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005513 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005514 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005515 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005516
5517 if (!(s->flags & SN_ERR_MASK))
5518 s->flags |= SN_ERR_CLICL;
5519 if (!(s->flags & SN_FINST_MASK))
5520 s->flags |= SN_FINST_H;
5521
5522 /* process_session() will take care of the error */
5523 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005524 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005525
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005526 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005527 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 return 0;
5529 }
5530
5531 /* More interesting part now : we know that we have a complete
5532 * response which at least looks like HTTP. We have an indicator
5533 * of each header's length, so we can parse them quickly.
5534 */
5535
5536 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005537 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005538
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005539 /*
5540 * 1: get the status code
5541 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005542 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005543 if (n < 1 || n > 5)
5544 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005545 /* when the client triggers a 4xx from the server, it's most often due
5546 * to a missing object or permission. These events should be tracked
5547 * because if they happen often, it may indicate a brute force or a
5548 * vulnerability scan.
5549 */
5550 if (n == 4)
5551 session_inc_http_err_ctr(s);
5552
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005553 if (objt_server(s->target))
5554 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005555
Willy Tarreau5b154472009-12-21 20:11:07 +01005556 /* check if the response is HTTP/1.1 or above */
5557 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005558 ((rep->buf->p[5] > '1') ||
5559 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005560 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005561
5562 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005563 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 +01005564
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005565 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005566 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005567
Willy Tarreau9b28e032012-10-12 23:49:43 +02005568 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005569
Willy Tarreau39650402010-03-15 19:44:39 +01005570 /* Adjust server's health based on status code. Note: status codes 501
5571 * and 505 are triggered on demand by client request, so we must not
5572 * count them as server failures.
5573 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005574 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005575 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005576 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005577 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005578 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005579 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005581 /*
5582 * 2: check for cacheability.
5583 */
5584
5585 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005586 case 100:
5587 /*
5588 * We may be facing a 100-continue response, in which case this
5589 * is not the right response, and we're waiting for the next one.
5590 * Let's allow this response to go to the client and wait for the
5591 * next one.
5592 */
5593 hdr_idx_init(&txn->hdr_idx);
5594 msg->next -= channel_forward(rep, msg->next);
5595 msg->msg_state = HTTP_MSG_RPBEFORE;
5596 txn->status = 0;
5597 s->logs.t_data = -1; /* was not a response yet */
5598 goto next_one;
5599
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005600 case 200:
5601 case 203:
5602 case 206:
5603 case 300:
5604 case 301:
5605 case 410:
5606 /* RFC2616 @13.4:
5607 * "A response received with a status code of
5608 * 200, 203, 206, 300, 301 or 410 MAY be stored
5609 * by a cache (...) unless a cache-control
5610 * directive prohibits caching."
5611 *
5612 * RFC2616 @9.5: POST method :
5613 * "Responses to this method are not cacheable,
5614 * unless the response includes appropriate
5615 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005616 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005617 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005618 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005619 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5620 break;
5621 default:
5622 break;
5623 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005624
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005625 /*
5626 * 3: we may need to capture headers
5627 */
5628 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005629 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005630 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005631 txn->rsp.cap, s->fe->rsp_cap);
5632
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005633 /* 4: determine the transfer-length.
5634 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5635 * the presence of a message-body in a RESPONSE and its transfer length
5636 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005637 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005638 * All responses to the HEAD request method MUST NOT include a
5639 * message-body, even though the presence of entity-header fields
5640 * might lead one to believe they do. All 1xx (informational), 204
5641 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5642 * message-body. All other responses do include a message-body,
5643 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005644 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005645 * 1. Any response which "MUST NOT" include a message-body (such as the
5646 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5647 * always terminated by the first empty line after the header fields,
5648 * regardless of the entity-header fields present in the message.
5649 *
5650 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5651 * the "chunked" transfer-coding (Section 6.2) is used, the
5652 * transfer-length is defined by the use of this transfer-coding.
5653 * If a Transfer-Encoding header field is present and the "chunked"
5654 * transfer-coding is not present, the transfer-length is defined by
5655 * the sender closing the connection.
5656 *
5657 * 3. If a Content-Length header field is present, its decimal value in
5658 * OCTETs represents both the entity-length and the transfer-length.
5659 * If a message is received with both a Transfer-Encoding header
5660 * field and a Content-Length header field, the latter MUST be ignored.
5661 *
5662 * 4. If the message uses the media type "multipart/byteranges", and
5663 * the transfer-length is not otherwise specified, then this self-
5664 * delimiting media type defines the transfer-length. This media
5665 * type MUST NOT be used unless the sender knows that the recipient
5666 * can parse it; the presence in a request of a Range header with
5667 * multiple byte-range specifiers from a 1.1 client implies that the
5668 * client can parse multipart/byteranges responses.
5669 *
5670 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005671 */
5672
5673 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005674 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005675 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005676 * FIXME: should we parse anyway and return an error on chunked encoding ?
5677 */
5678 if (txn->meth == HTTP_METH_HEAD ||
5679 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005680 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005681 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005682 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005683 goto skip_content_length;
5684 }
5685
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005686 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005687 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005688 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005689 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005690 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005691 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5692 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005693 /* bad transfer-encoding (chunked followed by something else) */
5694 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005695 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005696 break;
5697 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005698 }
5699
5700 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5701 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005702 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005703 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005704 signed long long cl;
5705
Willy Tarreauad14f752011-09-02 20:33:27 +02005706 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005707 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005708 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005709 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005710
Willy Tarreauad14f752011-09-02 20:33:27 +02005711 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005712 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005713 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005714 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005715
Willy Tarreauad14f752011-09-02 20:33:27 +02005716 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005717 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005718 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005719 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005720
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005721 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005722 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005723 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005724 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005725
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005726 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005727 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005728 }
5729
William Lallemand82fe75c2012-10-23 10:25:10 +02005730 if (s->fe->comp || s->be->comp)
5731 select_compression_response_header(s, rep->buf);
5732
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005733 /* FIXME: we should also implement the multipart/byterange method.
5734 * For now on, we resort to close mode in this case (unknown length).
5735 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005736skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005737
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005738 /* end of job, return OK */
5739 rep->analysers &= ~an_bit;
5740 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005741 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005742 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005743
5744 abort_keep_alive:
5745 /* A keep-alive request to the server failed on a network error.
5746 * The client is required to retry. We need to close without returning
5747 * any other information so that the client retries.
5748 */
5749 txn->status = 0;
5750 rep->analysers = 0;
5751 s->req->analysers = 0;
5752 channel_auto_close(rep);
5753 s->logs.logwait = 0;
5754 s->logs.level = 0;
5755 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5756 bi_erase(rep);
5757 stream_int_retnclose(rep->cons, NULL);
5758 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005759}
5760
5761/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005762 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005763 * and updates s->rep->analysers. It might make sense to explode it into several
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005764 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005765 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005766int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005767{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005768 struct http_txn *txn = &s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005769 struct http_msg *msg = &txn->rsp;
5770 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005771 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005772 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005773
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005774 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 +02005775 now_ms, __FUNCTION__,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005776 s,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005777 rep,
5778 rep->rex, rep->wex,
5779 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005780 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005781 rep->analysers);
5782
Willy Tarreau655dce92009-11-08 13:10:58 +01005783 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005784 return 0;
5785
5786 rep->analysers &= ~an_bit;
5787 rep->analyse_exp = TICK_ETERNITY;
5788
Willy Tarreau5b154472009-12-21 20:11:07 +01005789 /* Now we have to check if we need to modify the Connection header.
5790 * This is more difficult on the response than it is on the request,
5791 * because we can have two different HTTP versions and we don't know
5792 * how the client will interprete a response. For instance, let's say
5793 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5794 * HTTP/1.1 response without any header. Maybe it will bound itself to
5795 * HTTP/1.0 because it only knows about it, and will consider the lack
5796 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5797 * the lack of header as a keep-alive. Thus we will use two flags
5798 * indicating how a request MAY be understood by the client. In case
5799 * of multiple possibilities, we'll fix the header to be explicit. If
5800 * ambiguous cases such as both close and keepalive are seen, then we
5801 * will fall back to explicit close. Note that we won't take risks with
5802 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005803 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005804 */
5805
Willy Tarreaudc008c52010-02-01 16:20:08 +01005806 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5807 txn->status == 101)) {
5808 /* Either we've established an explicit tunnel, or we're
5809 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005810 * to understand the next protocols. We have to switch to tunnel
5811 * mode, so that we transfer the request and responses then let
5812 * this protocol pass unmodified. When we later implement specific
5813 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005814 * header which contains information about that protocol for
5815 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005816 */
5817 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5818 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005819 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5820 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005821 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5822 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005823 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005824
Willy Tarreau70dffda2014-01-30 03:07:23 +01005825 /* this situation happens when combining pretend-keepalive with httpclose. */
5826 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005827 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5828 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005829 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5830
Willy Tarreau60466522010-01-18 19:08:45 +01005831 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005832 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005833 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5834 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005835
Willy Tarreau60466522010-01-18 19:08:45 +01005836 /* now adjust header transformations depending on current state */
5837 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5838 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5839 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005840 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005841 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005842 }
Willy Tarreau60466522010-01-18 19:08:45 +01005843 else { /* SCL / KAL */
5844 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005845 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005846 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005847 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005848
Willy Tarreau60466522010-01-18 19:08:45 +01005849 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005850 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005851
Willy Tarreau60466522010-01-18 19:08:45 +01005852 /* Some keep-alive responses are converted to Server-close if
5853 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005854 */
Willy Tarreau60466522010-01-18 19:08:45 +01005855 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5856 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005857 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005858 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005859 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005860 }
5861
Willy Tarreau7959a552013-09-23 16:44:27 +02005862 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005863 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005864
Willy Tarreau70730dd2014-04-24 18:06:27 +02005865 /* The stats applet needs to adjust the Connection header but we don't
5866 * apply any filter there.
5867 */
5868 if (unlikely(objt_applet(s->target) == &http_stats_applet))
5869 goto skip_filters;
5870
Willy Tarreau58975672014-04-24 21:13:57 +02005871 /*
5872 * We will have to evaluate the filters.
5873 * As opposed to version 1.2, now they will be evaluated in the
5874 * filters order and not in the header order. This means that
5875 * each filter has to be validated among all headers.
5876 *
5877 * Filters are tried with ->be first, then with ->fe if it is
5878 * different from ->be.
5879 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005880
Willy Tarreau58975672014-04-24 21:13:57 +02005881 cur_proxy = s->be;
5882 while (1) {
5883 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005884
Willy Tarreau58975672014-04-24 21:13:57 +02005885 /* evaluate http-response rules */
5886 if (!http_res_last_rule)
5887 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005888
Willy Tarreau58975672014-04-24 21:13:57 +02005889 /* try headers filters */
5890 if (rule_set->rsp_exp != NULL) {
5891 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5892 return_bad_resp:
5893 if (objt_server(s->target)) {
5894 objt_server(s->target)->counters.failed_resp++;
5895 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005896 }
Willy Tarreau58975672014-04-24 21:13:57 +02005897 s->be->be_counters.failed_resp++;
5898 return_srv_prx_502:
5899 rep->analysers = 0;
5900 txn->status = 502;
5901 s->logs.t_data = -1; /* was not a valid response */
5902 rep->prod->flags |= SI_FL_NOLINGER;
5903 bi_erase(rep);
5904 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
5905 if (!(s->flags & SN_ERR_MASK))
5906 s->flags |= SN_ERR_PRXCOND;
5907 if (!(s->flags & SN_FINST_MASK))
5908 s->flags |= SN_FINST_H;
5909 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005910 }
Willy Tarreau58975672014-04-24 21:13:57 +02005911 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005912
Willy Tarreau58975672014-04-24 21:13:57 +02005913 /* has the response been denied ? */
5914 if (txn->flags & TX_SVDENY) {
5915 if (objt_server(s->target))
5916 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005917
Willy Tarreau58975672014-04-24 21:13:57 +02005918 s->be->be_counters.denied_resp++;
5919 s->fe->fe_counters.denied_resp++;
5920 if (s->listener->counters)
5921 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005922
Willy Tarreau58975672014-04-24 21:13:57 +02005923 goto return_srv_prx_502;
5924 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005925
Willy Tarreau58975672014-04-24 21:13:57 +02005926 /* add response headers from the rule sets in the same order */
5927 list_for_each_entry(wl, &rule_set->rsp_add, list) {
5928 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005929 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005930 if (wl->cond) {
5931 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
5932 ret = acl_pass(ret);
5933 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5934 ret = !ret;
5935 if (!ret)
5936 continue;
5937 }
5938 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5939 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005940 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005941
Willy Tarreau58975672014-04-24 21:13:57 +02005942 /* check whether we're already working on the frontend */
5943 if (cur_proxy == s->fe)
5944 break;
5945 cur_proxy = s->fe;
5946 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005947
Willy Tarreau58975672014-04-24 21:13:57 +02005948 /* OK that's all we can do for 1xx responses */
5949 if (unlikely(txn->status < 200))
5950 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005951
Willy Tarreau58975672014-04-24 21:13:57 +02005952 /*
5953 * Now check for a server cookie.
5954 */
5955 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
5956 (s->be->options & PR_O_CHK_CACHE))
5957 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005958
Willy Tarreau58975672014-04-24 21:13:57 +02005959 /*
5960 * Check for cache-control or pragma headers if required.
5961 */
5962 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
5963 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005964
Willy Tarreau58975672014-04-24 21:13:57 +02005965 /*
5966 * Add server cookie in the response if needed
5967 */
5968 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5969 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
5970 (!(s->flags & SN_DIRECT) ||
5971 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5972 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5973 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5974 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5975 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
5976 !(s->flags & SN_IGNORE_PRST)) {
5977 /* the server is known, it's not the one the client requested, or the
5978 * cookie's last seen date needs to be refreshed. We have to
5979 * insert a set-cookie here, except if we want to insert only on POST
5980 * requests and this one isn't. Note that servers which don't have cookies
5981 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005982 */
Willy Tarreau58975672014-04-24 21:13:57 +02005983 if (!objt_server(s->target)->cookie) {
5984 chunk_printf(&trash,
5985 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5986 s->be->cookie_name);
5987 }
5988 else {
5989 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005990
Willy Tarreau58975672014-04-24 21:13:57 +02005991 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5992 /* emit last_date, which is mandatory */
5993 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5994 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5995 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005996
Willy Tarreau58975672014-04-24 21:13:57 +02005997 if (s->be->cookie_maxlife) {
5998 /* emit first_date, which is either the original one or
5999 * the current date.
6000 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006001 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006002 s30tob64(txn->cookie_first_date ?
6003 txn->cookie_first_date >> 2 :
6004 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006005 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006006 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006007 }
Willy Tarreau58975672014-04-24 21:13:57 +02006008 chunk_appendf(&trash, "; path=/");
6009 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006010
Willy Tarreau58975672014-04-24 21:13:57 +02006011 if (s->be->cookie_domain)
6012 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006013
Willy Tarreau58975672014-04-24 21:13:57 +02006014 if (s->be->ck_opts & PR_CK_HTTPONLY)
6015 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006016
Willy Tarreau58975672014-04-24 21:13:57 +02006017 if (s->be->ck_opts & PR_CK_SECURE)
6018 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006019
Willy Tarreau58975672014-04-24 21:13:57 +02006020 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6021 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006022
Willy Tarreau58975672014-04-24 21:13:57 +02006023 txn->flags &= ~TX_SCK_MASK;
6024 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6025 /* the server did not change, only the date was updated */
6026 txn->flags |= TX_SCK_UPDATED;
6027 else
6028 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006029
Willy Tarreau58975672014-04-24 21:13:57 +02006030 /* Here, we will tell an eventual cache on the client side that we don't
6031 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6032 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6033 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006034 */
Willy Tarreau58975672014-04-24 21:13:57 +02006035 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036
Willy Tarreau58975672014-04-24 21:13:57 +02006037 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006038
Willy Tarreau58975672014-04-24 21:13:57 +02006039 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6040 "Cache-control: private", 22) < 0))
6041 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006042 }
Willy Tarreau58975672014-04-24 21:13:57 +02006043 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006044
Willy Tarreau58975672014-04-24 21:13:57 +02006045 /*
6046 * Check if result will be cacheable with a cookie.
6047 * We'll block the response if security checks have caught
6048 * nasty things such as a cacheable cookie.
6049 */
6050 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6051 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6052 (s->be->options & PR_O_CHK_CACHE)) {
6053 /* we're in presence of a cacheable response containing
6054 * a set-cookie header. We'll block it as requested by
6055 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056 */
Willy Tarreau58975672014-04-24 21:13:57 +02006057 if (objt_server(s->target))
6058 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006059
Willy Tarreau58975672014-04-24 21:13:57 +02006060 s->be->be_counters.denied_resp++;
6061 s->fe->fe_counters.denied_resp++;
6062 if (s->listener->counters)
6063 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006064
Willy Tarreau58975672014-04-24 21:13:57 +02006065 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6066 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6067 send_log(s->be, LOG_ALERT,
6068 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6069 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6070 goto return_srv_prx_502;
6071 }
Willy Tarreau03945942009-12-22 16:50:27 +01006072
Willy Tarreau70730dd2014-04-24 18:06:27 +02006073 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006074 /*
6075 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6076 * If an "Upgrade" token is found, the header is left untouched in order
6077 * not to have to deal with some client bugs : some of them fail an upgrade
6078 * if anything but "Upgrade" is present in the Connection header.
6079 */
6080 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6081 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6082 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6083 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6084 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006085
Willy Tarreau58975672014-04-24 21:13:57 +02006086 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6087 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6088 /* we want a keep-alive response here. Keep-alive header
6089 * required if either side is not 1.1.
6090 */
6091 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6092 want_flags |= TX_CON_KAL_SET;
6093 }
6094 else {
6095 /* we want a close response here. Close header required if
6096 * the server is 1.1, regardless of the client.
6097 */
6098 if (msg->flags & HTTP_MSGF_VER_11)
6099 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006100 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006101
Willy Tarreau58975672014-04-24 21:13:57 +02006102 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6103 http_change_connection_header(txn, msg, want_flags);
6104 }
6105
6106 skip_header_mangling:
6107 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6108 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6109 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006110
Willy Tarreau58975672014-04-24 21:13:57 +02006111 /* if the user wants to log as soon as possible, without counting
6112 * bytes from the server, then this is the right moment. We have
6113 * to temporarily assign bytes_out to log what we currently have.
6114 */
6115 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6116 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6117 s->logs.bytes_out = txn->rsp.eoh;
6118 s->do_log(s);
6119 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006120 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006121 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006122}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006123
Willy Tarreaud98cf932009-12-27 22:54:55 +01006124/* This function is an analyser which forwards response body (including chunk
6125 * sizes if any). It is called as soon as we must forward, even if we forward
6126 * zero byte. The only situation where it must not be called is when we're in
6127 * tunnel mode and we want to forward till the close. It's used both to forward
6128 * remaining data and to resync after end of body. It expects the msg_state to
6129 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6130 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006131 *
6132 * It is capable of compressing response data both in content-length mode and
6133 * in chunked mode. The state machines follows different flows depending on
6134 * whether content-length and chunked modes are used, since there are no
6135 * trailers in content-length :
6136 *
6137 * chk-mode cl-mode
6138 * ,----- BODY -----.
6139 * / \
6140 * V size > 0 V chk-mode
6141 * .--> SIZE -------------> DATA -------------> CRLF
6142 * | | size == 0 | last byte |
6143 * | v final crlf v inspected |
6144 * | TRAILERS -----------> DONE |
6145 * | |
6146 * `----------------------------------------------'
6147 *
6148 * Compression only happens in the DATA state, and must be flushed in final
6149 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6150 * is performed at once on final states for all bytes parsed, or when leaving
6151 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006152 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006153int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006154{
6155 struct http_txn *txn = &s->txn;
6156 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006157 static struct buffer *tmpbuf = NULL;
6158 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006159 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006160
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006161 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6162 return 0;
6163
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006164 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006165 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006166 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006167 /* Output closed while we were sending data. We must abort and
6168 * wake the other side up.
6169 */
6170 msg->msg_state = HTTP_MSG_ERROR;
6171 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006172 return 1;
6173 }
6174
Willy Tarreau4fe41902010-06-07 22:27:41 +02006175 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006176 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006177
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006178 if (msg->sov) {
6179 /* we have msg->sov which points to the first byte of message
6180 * body, and res->buf.p still points to the beginning of the
6181 * message. We forward the headers now, as we don't need them
6182 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006183 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006184 b_adv(res->buf, msg->sov);
6185 msg->next -= msg->sov;
6186 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006187
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006188 /* The previous analysers guarantee that the state is somewhere
6189 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6190 * msg->next are always correct.
6191 */
6192 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6193 if (msg->flags & HTTP_MSGF_TE_CHNK)
6194 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6195 else
6196 msg->msg_state = HTTP_MSG_DATA;
6197 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006198 }
6199
Willy Tarreauefdf0942014-04-24 20:08:57 +02006200 if (res->to_forward) {
6201 /* We can't process the buffer's contents yet */
6202 res->flags |= CF_WAKE_WRITE;
6203 goto missing_data;
6204 }
6205
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006206 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6207 /* We need a compression buffer in the DATA state to put the
6208 * output of compressed data, and in CRLF state to let the
6209 * TRAILERS state finish the job of removing the trailing CRLF.
6210 */
6211 if (unlikely(tmpbuf == NULL)) {
6212 /* this is the first time we need the compression buffer */
6213 tmpbuf = pool_alloc2(pool2_buffer);
6214 if (tmpbuf == NULL)
6215 goto aborted_xfer; /* no memory */
6216 }
6217
6218 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006219 if (ret < 0) {
6220 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006221 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006222 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006223 compressing = 1;
6224 }
6225
Willy Tarreaud98cf932009-12-27 22:54:55 +01006226 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006227 switch (msg->msg_state - HTTP_MSG_DATA) {
6228 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006229 /* we may have some pending data starting at res->buf->p */
6230 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006231 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006232 if (ret < 0)
6233 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006234
Willy Tarreaud5a67832014-04-21 10:54:27 +02006235 if (msg->chunk_len) {
6236 /* input empty or output full */
6237 if (res->buf->i > msg->next)
6238 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006239 goto missing_data;
6240 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006241 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006242 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006243 if (msg->chunk_len > res->buf->i - msg->next) {
6244 /* output full */
6245 res->flags |= CF_WAKE_WRITE;
6246 goto missing_data;
6247 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006248 msg->next += msg->chunk_len;
6249 msg->chunk_len = 0;
6250 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006251
6252 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006253 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006254 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006255 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006256 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006257 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006258 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006259 /* fall through for HTTP_MSG_CHUNK_CRLF */
6260
6261 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6262 /* we want the CRLF after the data */
6263
6264 ret = http_skip_chunk_crlf(msg);
6265 if (ret == 0)
6266 goto missing_data;
6267 else if (ret < 0) {
6268 if (msg->err_pos >= 0)
6269 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6270 goto return_bad_res;
6271 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006272 /* we're in MSG_CHUNK_SIZE now, fall through */
6273
6274 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006275 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006276 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006277 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006278 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006279
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006280 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006281 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006282 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006283 else if (ret < 0) {
6284 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006285 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006286 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006287 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006288 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006289 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006290
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006291 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006292 if (unlikely(compressing)) {
6293 /* we need to flush output contents before syncing FSMs */
6294 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6295 compressing = 0;
6296 }
6297
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006298 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006299 if (ret == 0)
6300 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006301 else if (ret < 0) {
6302 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006303 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006304 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006305 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006306 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006307
6308 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006309 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006310 if (unlikely(compressing)) {
6311 /* we need to flush output contents before syncing FSMs */
6312 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6313 compressing = 0;
6314 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006315
Willy Tarreauc623c172014-04-18 09:53:50 +02006316 /* we may have some pending data starting at res->buf->p
6317 * such as a last chunk of data or trailers.
6318 */
6319 b_adv(res->buf, msg->next);
6320 msg->next = 0;
6321
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006322 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006323 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006324 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6325 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006326 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006327
Willy Tarreau610ecce2010-01-04 21:15:02 +01006328 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006329 /* some state changes occurred, maybe the analyser
6330 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006331 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006332 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006333 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006334 /* response errors are most likely due to
6335 * the client aborting the transfer.
6336 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006337 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006338 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006339 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006340 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006341 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006342 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006343 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006344 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006345 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006346 }
6347 }
6348
Willy Tarreaud98cf932009-12-27 22:54:55 +01006349 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006350 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006351 if (unlikely(compressing)) {
6352 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006353 compressing = 0;
6354 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006355
Willy Tarreauc623c172014-04-18 09:53:50 +02006356 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6357 b_adv(res->buf, msg->next);
6358 msg->next = 0;
6359 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6360 }
6361
Willy Tarreauf003d372012-11-26 13:35:37 +01006362 if (res->flags & CF_SHUTW)
6363 goto aborted_xfer;
6364
6365 /* stop waiting for data if the input is closed before the end. If the
6366 * client side was already closed, it means that the client has aborted,
6367 * so we don't want to count this as a server abort. Otherwise it's a
6368 * server abort.
6369 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006370 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006371 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006372 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006373 if (!(s->flags & SN_ERR_MASK))
6374 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006375 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006376 if (objt_server(s->target))
6377 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006378 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006379 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006380
Willy Tarreau40dba092010-03-04 18:14:51 +01006381 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006382 if (!s->req->analysers)
6383 goto return_bad_res;
6384
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006385 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006386 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006387 * Similarly, with keep-alive on the client side, we don't want to forward a
6388 * close.
6389 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006390 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006391 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6392 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006393 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006394
Willy Tarreau5c620922011-05-11 19:56:11 +02006395 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006396 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006397 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006398 * modes are already handled by the stream sock layer. We must not do
6399 * this in content-length mode because it could present the MSG_MORE
6400 * flag with the last block of forwarded data, which would cause an
6401 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006402 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006403 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006404 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006405
Willy Tarreaud98cf932009-12-27 22:54:55 +01006406 /* the session handler will take care of timeouts and errors */
6407 return 0;
6408
Willy Tarreau40dba092010-03-04 18:14:51 +01006409 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006410 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006411 if (objt_server(s->target))
6412 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006413
6414 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006415 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006416 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006417 compressing = 0;
6418 }
6419
Willy Tarreauc623c172014-04-18 09:53:50 +02006420 /* we may have some pending data starting at res->buf->p */
6421 if (s->comp_algo == NULL) {
6422 b_adv(res->buf, msg->next);
6423 msg->next = 0;
6424 }
6425
Willy Tarreaud98cf932009-12-27 22:54:55 +01006426 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006427 /* don't send any error message as we're in the body */
6428 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006429 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006430 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006431 if (objt_server(s->target))
6432 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006433
6434 if (!(s->flags & SN_ERR_MASK))
6435 s->flags |= SN_ERR_PRXCOND;
6436 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006437 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006438 return 0;
6439
6440 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006441 if (unlikely(compressing)) {
6442 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6443 compressing = 0;
6444 }
6445
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006446 txn->rsp.msg_state = HTTP_MSG_ERROR;
6447 /* don't send any error message as we're in the body */
6448 stream_int_retnclose(res->cons, NULL);
6449 res->analysers = 0;
6450 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6451
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006452 s->fe->fe_counters.cli_aborts++;
6453 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006454 if (objt_server(s->target))
6455 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006456
6457 if (!(s->flags & SN_ERR_MASK))
6458 s->flags |= SN_ERR_CLICL;
6459 if (!(s->flags & SN_FINST_MASK))
6460 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006461 return 0;
6462}
6463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464/* Iterate the same filter through all request headers.
6465 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006466 * Since it can manage the switch to another backend, it updates the per-proxy
6467 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006468 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006469int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006470{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006471 char term;
6472 char *cur_ptr, *cur_end, *cur_next;
6473 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006474 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006476 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 last_hdr = 0;
6479
Willy Tarreau9b28e032012-10-12 23:49:43 +02006480 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006481 old_idx = 0;
6482
6483 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006484 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006486 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006487 (exp->action == ACT_ALLOW ||
6488 exp->action == ACT_DENY ||
6489 exp->action == ACT_TARPIT))
6490 return 0;
6491
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006492 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493 if (!cur_idx)
6494 break;
6495
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006496 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 cur_ptr = cur_next;
6498 cur_end = cur_ptr + cur_hdr->len;
6499 cur_next = cur_end + cur_hdr->cr + 1;
6500
6501 /* Now we have one header between cur_ptr and cur_end,
6502 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503 */
6504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 /* The annoying part is that pattern matching needs
6506 * that we modify the contents to null-terminate all
6507 * strings before testing them.
6508 */
6509
6510 term = *cur_end;
6511 *cur_end = '\0';
6512
6513 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6514 switch (exp->action) {
6515 case ACT_SETBE:
6516 /* It is not possible to jump a second time.
6517 * FIXME: should we return an HTTP/500 here so that
6518 * the admin knows there's a problem ?
6519 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006520 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006521 break;
6522
6523 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006524 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 last_hdr = 1;
6526 break;
6527
6528 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006529 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530 last_hdr = 1;
6531 break;
6532
6533 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006534 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006535 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 break;
6537
6538 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006539 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 break;
6542
6543 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006544 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6545 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546 /* FIXME: if the user adds a newline in the replacement, the
6547 * index will not be recalculated for now, and the new line
6548 * will not be counted as a new header.
6549 */
6550
6551 cur_end += delta;
6552 cur_next += delta;
6553 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006554 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 break;
6556
6557 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006558 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006559 cur_next += delta;
6560
Willy Tarreaufa355d42009-11-29 18:12:29 +01006561 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006562 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6563 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 cur_hdr->len = 0;
6565 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006566 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 break;
6568
6569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006570 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 if (cur_end)
6572 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006574 /* keep the link from this header to next one in case of later
6575 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006576 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006577 old_idx = cur_idx;
6578 }
6579 return 0;
6580}
6581
6582
6583/* Apply the filter to the request line.
6584 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6585 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586 * Since it can manage the switch to another backend, it updates the per-proxy
6587 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006588 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006589int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006590{
6591 char term;
6592 char *cur_ptr, *cur_end;
6593 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006594 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006595 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006596
Willy Tarreau3d300592007-03-18 18:34:41 +01006597 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006598 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006599 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006600 (exp->action == ACT_ALLOW ||
6601 exp->action == ACT_DENY ||
6602 exp->action == ACT_TARPIT))
6603 return 0;
6604 else if (exp->action == ACT_REMOVE)
6605 return 0;
6606
6607 done = 0;
6608
Willy Tarreau9b28e032012-10-12 23:49:43 +02006609 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006610 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006611
6612 /* Now we have the request line between cur_ptr and cur_end */
6613
6614 /* The annoying part is that pattern matching needs
6615 * that we modify the contents to null-terminate all
6616 * strings before testing them.
6617 */
6618
6619 term = *cur_end;
6620 *cur_end = '\0';
6621
6622 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6623 switch (exp->action) {
6624 case ACT_SETBE:
6625 /* It is not possible to jump a second time.
6626 * FIXME: should we return an HTTP/500 here so that
6627 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006628 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006629 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006630 break;
6631
6632 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006633 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006634 done = 1;
6635 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006637 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006638 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006639 done = 1;
6640 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006642 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006643 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006644 done = 1;
6645 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006647 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006648 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006649 done = 1;
6650 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006652 case ACT_REPLACE:
6653 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006654 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6655 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006656 /* FIXME: if the user adds a newline in the replacement, the
6657 * index will not be recalculated for now, and the new line
6658 * will not be counted as a new header.
6659 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006660
Willy Tarreaufa355d42009-11-29 18:12:29 +01006661 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006662 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006663 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006664 HTTP_MSG_RQMETH,
6665 cur_ptr, cur_end + 1,
6666 NULL, NULL);
6667 if (unlikely(!cur_end))
6668 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006670 /* we have a full request and we know that we have either a CR
6671 * or an LF at <ptr>.
6672 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006673 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6674 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006675 /* there is no point trying this regex on headers */
6676 return 1;
6677 }
6678 }
6679 *cur_end = term; /* restore the string terminator */
6680 return done;
6681}
Willy Tarreau97de6242006-12-27 17:18:38 +01006682
Willy Tarreau58f10d72006-12-04 02:26:12 +01006683
Willy Tarreau58f10d72006-12-04 02:26:12 +01006684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006685/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006686 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006687 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688 * unparsable request. Since it can manage the switch to another backend, it
6689 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006690 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006691int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006692{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006693 struct http_txn *txn = &s->txn;
6694 struct hdr_exp *exp;
6695
6696 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006697 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006699 /*
6700 * The interleaving of transformations and verdicts
6701 * makes it difficult to decide to continue or stop
6702 * the evaluation.
6703 */
6704
Willy Tarreau6c123b12010-01-28 20:22:06 +01006705 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6706 break;
6707
Willy Tarreau3d300592007-03-18 18:34:41 +01006708 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006709 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006710 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006711 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006712
6713 /* if this filter had a condition, evaluate it now and skip to
6714 * next filter if the condition does not match.
6715 */
6716 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006717 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006718 ret = acl_pass(ret);
6719 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6720 ret = !ret;
6721
6722 if (!ret)
6723 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006724 }
6725
6726 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006727 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006728 if (unlikely(ret < 0))
6729 return -1;
6730
6731 if (likely(ret == 0)) {
6732 /* The filter did not match the request, it can be
6733 * iterated through all headers.
6734 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006735 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006736 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006737 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006738 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006739}
6740
6741
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742
Willy Tarreau58f10d72006-12-04 02:26:12 +01006743/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006744 * Try to retrieve the server associated to the appsession.
6745 * If the server is found, it's assigned to the session.
6746 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006747void manage_client_side_appsession(struct session *s, const char *buf, int len) {
6748 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006749 appsess *asession = NULL;
6750 char *sessid_temp = NULL;
6751
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006752 if (len > s->be->appsession_len) {
6753 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006754 }
6755
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006756 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006757 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006758 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006759 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006760 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006761 }
6762
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006763 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006764 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006765 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006766 return;
6767 }
6768
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006769 memcpy(txn->sessid, buf, len);
6770 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006771 }
6772
6773 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6774 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006775 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006776 return;
6777 }
6778
Cyril Bontéb21570a2009-11-29 20:04:48 +01006779 memcpy(sessid_temp, buf, len);
6780 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006781
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006782 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006783 /* free previously allocated memory */
6784 pool_free2(apools.sessid, sessid_temp);
6785
6786 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006787 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
6788 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006789 asession->request_count++;
6790
6791 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006792 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006793
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006794 while (srv) {
6795 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006796 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006797 (s->be->options & PR_O_PERSIST) ||
6798 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006799 /* we found the server and it's usable */
6800 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006801 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006802 s->flags |= SN_DIRECT | SN_ASSIGNED;
6803 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006804
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006805 break;
6806 } else {
6807 txn->flags &= ~TX_CK_MASK;
6808 txn->flags |= TX_CK_DOWN;
6809 }
6810 }
6811 srv = srv->next;
6812 }
6813 }
6814 }
6815}
6816
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006817/* Find the end of a cookie value contained between <s> and <e>. It works the
6818 * same way as with headers above except that the semi-colon also ends a token.
6819 * See RFC2965 for more information. Note that it requires a valid header to
6820 * return a valid result.
6821 */
6822char *find_cookie_value_end(char *s, const char *e)
6823{
6824 int quoted, qdpair;
6825
6826 quoted = qdpair = 0;
6827 for (; s < e; s++) {
6828 if (qdpair) qdpair = 0;
6829 else if (quoted) {
6830 if (*s == '\\') qdpair = 1;
6831 else if (*s == '"') quoted = 0;
6832 }
6833 else if (*s == '"') quoted = 1;
6834 else if (*s == ',' || *s == ';') return s;
6835 }
6836 return s;
6837}
6838
6839/* Delete a value in a header between delimiters <from> and <next> in buffer
6840 * <buf>. The number of characters displaced is returned, and the pointer to
6841 * the first delimiter is updated if required. The function tries as much as
6842 * possible to respect the following principles :
6843 * - replace <from> delimiter by the <next> one unless <from> points to a
6844 * colon, in which case <next> is simply removed
6845 * - set exactly one space character after the new first delimiter, unless
6846 * there are not enough characters in the block being moved to do so.
6847 * - remove unneeded spaces before the previous delimiter and after the new
6848 * one.
6849 *
6850 * It is the caller's responsibility to ensure that :
6851 * - <from> points to a valid delimiter or the colon ;
6852 * - <next> points to a valid delimiter or the final CR/LF ;
6853 * - there are non-space chars before <from> ;
6854 * - there is a CR/LF at or after <next>.
6855 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006856int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006857{
6858 char *prev = *from;
6859
6860 if (*prev == ':') {
6861 /* We're removing the first value, preserve the colon and add a
6862 * space if possible.
6863 */
6864 if (!http_is_crlf[(unsigned char)*next])
6865 next++;
6866 prev++;
6867 if (prev < next)
6868 *prev++ = ' ';
6869
6870 while (http_is_spht[(unsigned char)*next])
6871 next++;
6872 } else {
6873 /* Remove useless spaces before the old delimiter. */
6874 while (http_is_spht[(unsigned char)*(prev-1)])
6875 prev--;
6876 *from = prev;
6877
6878 /* copy the delimiter and if possible a space if we're
6879 * not at the end of the line.
6880 */
6881 if (!http_is_crlf[(unsigned char)*next]) {
6882 *prev++ = *next++;
6883 if (prev + 1 < next)
6884 *prev++ = ' ';
6885 while (http_is_spht[(unsigned char)*next])
6886 next++;
6887 }
6888 }
6889 return buffer_replace2(buf, prev, next, NULL, 0);
6890}
6891
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006892/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006893 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006894 * desirable to call it only when needed. This code is quite complex because
6895 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6896 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006898void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006899{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006900 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006902 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6904 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006905
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006906 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006907 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006908 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006909
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006910 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006911 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006912 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006913
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006914 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 hdr_beg = hdr_next;
6916 hdr_end = hdr_beg + cur_hdr->len;
6917 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 /* We have one full header between hdr_beg and hdr_end, and the
6920 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921 * "Cookie:" headers.
6922 */
6923
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006925 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006926 old_idx = cur_idx;
6927 continue;
6928 }
6929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006930 del_from = NULL; /* nothing to be deleted */
6931 preserve_hdr = 0; /* assume we may kill the whole header */
6932
Willy Tarreau58f10d72006-12-04 02:26:12 +01006933 /* Now look for cookies. Conforming to RFC2109, we have to support
6934 * attributes whose name begin with a '$', and associate them with
6935 * the right cookie, if we want to delete this cookie.
6936 * So there are 3 cases for each cookie read :
6937 * 1) it's a special attribute, beginning with a '$' : ignore it.
6938 * 2) it's a server id cookie that we *MAY* want to delete : save
6939 * some pointers on it (last semi-colon, beginning of cookie...)
6940 * 3) it's an application cookie : we *MAY* have to delete a previous
6941 * "special" cookie.
6942 * At the end of loop, if a "special" cookie remains, we may have to
6943 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 * *MUST* delete it.
6945 *
6946 * Note: RFC2965 is unclear about the processing of spaces around
6947 * the equal sign in the ATTR=VALUE form. A careful inspection of
6948 * the RFC explicitly allows spaces before it, and not within the
6949 * tokens (attrs or values). An inspection of RFC2109 allows that
6950 * too but section 10.1.3 lets one think that spaces may be allowed
6951 * after the equal sign too, resulting in some (rare) buggy
6952 * implementations trying to do that. So let's do what servers do.
6953 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6954 * allowed quoted strings in values, with any possible character
6955 * after a backslash, including control chars and delimitors, which
6956 * causes parsing to become ambiguous. Browsers also allow spaces
6957 * within values even without quotes.
6958 *
6959 * We have to keep multiple pointers in order to support cookie
6960 * removal at the beginning, middle or end of header without
6961 * corrupting the header. All of these headers are valid :
6962 *
6963 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6964 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6965 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6966 * | | | | | | | | |
6967 * | | | | | | | | hdr_end <--+
6968 * | | | | | | | +--> next
6969 * | | | | | | +----> val_end
6970 * | | | | | +-----------> val_beg
6971 * | | | | +--------------> equal
6972 * | | | +----------------> att_end
6973 * | | +---------------------> att_beg
6974 * | +--------------------------> prev
6975 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006976 */
6977
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006978 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6979 /* Iterate through all cookies on this line */
6980
6981 /* find att_beg */
6982 att_beg = prev + 1;
6983 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6984 att_beg++;
6985
6986 /* find att_end : this is the first character after the last non
6987 * space before the equal. It may be equal to hdr_end.
6988 */
6989 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006990
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006991 while (equal < hdr_end) {
6992 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006993 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 if (http_is_spht[(unsigned char)*equal++])
6995 continue;
6996 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006997 }
6998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7000 * is between <att_beg> and <equal>, both may be identical.
7001 */
7002
7003 /* look for end of cookie if there is an equal sign */
7004 if (equal < hdr_end && *equal == '=') {
7005 /* look for the beginning of the value */
7006 val_beg = equal + 1;
7007 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7008 val_beg++;
7009
7010 /* find the end of the value, respecting quotes */
7011 next = find_cookie_value_end(val_beg, hdr_end);
7012
7013 /* make val_end point to the first white space or delimitor after the value */
7014 val_end = next;
7015 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7016 val_end--;
7017 } else {
7018 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007019 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007020
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007021 /* We have nothing to do with attributes beginning with '$'. However,
7022 * they will automatically be removed if a header before them is removed,
7023 * since they're supposed to be linked together.
7024 */
7025 if (*att_beg == '$')
7026 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007027
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007028 /* Ignore cookies with no equal sign */
7029 if (equal == next) {
7030 /* This is not our cookie, so we must preserve it. But if we already
7031 * scheduled another cookie for removal, we cannot remove the
7032 * complete header, but we can remove the previous block itself.
7033 */
7034 preserve_hdr = 1;
7035 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007036 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007037 val_end += delta;
7038 next += delta;
7039 hdr_end += delta;
7040 hdr_next += delta;
7041 cur_hdr->len += delta;
7042 http_msg_move_end(&txn->req, delta);
7043 prev = del_from;
7044 del_from = NULL;
7045 }
7046 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007047 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007049 /* if there are spaces around the equal sign, we need to
7050 * strip them otherwise we'll get trouble for cookie captures,
7051 * or even for rewrites. Since this happens extremely rarely,
7052 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007054 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7055 int stripped_before = 0;
7056 int stripped_after = 0;
7057
7058 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007059 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007060 equal += stripped_before;
7061 val_beg += stripped_before;
7062 }
7063
7064 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007065 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007066 val_beg += stripped_after;
7067 stripped_before += stripped_after;
7068 }
7069
7070 val_end += stripped_before;
7071 next += stripped_before;
7072 hdr_end += stripped_before;
7073 hdr_next += stripped_before;
7074 cur_hdr->len += stripped_before;
7075 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007077 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007078
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007079 /* First, let's see if we want to capture this cookie. We check
7080 * that we don't already have a client side cookie, because we
7081 * can only capture one. Also as an optimisation, we ignore
7082 * cookies shorter than the declared name.
7083 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007084 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7085 (val_end - att_beg >= s->fe->capture_namelen) &&
7086 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 int log_len = val_end - att_beg;
7088
7089 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7090 Alert("HTTP logging : out of memory.\n");
7091 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007092 if (log_len > s->fe->capture_len)
7093 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007094 memcpy(txn->cli_cookie, att_beg, log_len);
7095 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098
Willy Tarreaubca99692010-10-06 19:25:55 +02007099 /* Persistence cookies in passive, rewrite or insert mode have the
7100 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007102 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007104 * For cookies in prefix mode, the form is :
7105 *
7106 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007107 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007108 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7109 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7110 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007111 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007113 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7114 * have the server ID between val_beg and delim, and the original cookie between
7115 * delim+1 and val_end. Otherwise, delim==val_end :
7116 *
7117 * Cookie: NAME=SRV; # in all but prefix modes
7118 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7119 * | || || | |+-> next
7120 * | || || | +--> val_end
7121 * | || || +---------> delim
7122 * | || |+------------> val_beg
7123 * | || +-------------> att_end = equal
7124 * | |+-----------------> att_beg
7125 * | +------------------> prev
7126 * +-------------------------> hdr_beg
7127 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007128
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007129 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007130 for (delim = val_beg; delim < val_end; delim++)
7131 if (*delim == COOKIE_DELIM)
7132 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007133 } else {
7134 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007135 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007136 /* Now check if the cookie contains a date field, which would
7137 * appear after a vertical bar ('|') just after the server name
7138 * and before the delimiter.
7139 */
7140 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7141 if (vbar1) {
7142 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007143 * right is the last seen date. It is a base64 encoded
7144 * 30-bit value representing the UNIX date since the
7145 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007146 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007147 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007148 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007149 if (val_end - vbar1 >= 5) {
7150 val = b64tos30(vbar1);
7151 if (val > 0)
7152 txn->cookie_last_date = val << 2;
7153 }
7154 /* look for a second vertical bar */
7155 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7156 if (vbar1 && (val_end - vbar1 > 5)) {
7157 val = b64tos30(vbar1 + 1);
7158 if (val > 0)
7159 txn->cookie_first_date = val << 2;
7160 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007161 }
7162 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163
Willy Tarreauf64d1412010-10-07 20:06:11 +02007164 /* if the cookie has an expiration date and the proxy wants to check
7165 * it, then we do that now. We first check if the cookie is too old,
7166 * then only if it has expired. We detect strict overflow because the
7167 * time resolution here is not great (4 seconds). Cookies with dates
7168 * in the future are ignored if their offset is beyond one day. This
7169 * allows an admin to fix timezone issues without expiring everyone
7170 * and at the same time avoids keeping unwanted side effects for too
7171 * long.
7172 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007173 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7174 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007175 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007176 txn->flags &= ~TX_CK_MASK;
7177 txn->flags |= TX_CK_OLD;
7178 delim = val_beg; // let's pretend we have not found the cookie
7179 txn->cookie_first_date = 0;
7180 txn->cookie_last_date = 0;
7181 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007182 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7183 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007184 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007185 txn->flags &= ~TX_CK_MASK;
7186 txn->flags |= TX_CK_EXPIRED;
7187 delim = val_beg; // let's pretend we have not found the cookie
7188 txn->cookie_first_date = 0;
7189 txn->cookie_last_date = 0;
7190 }
7191
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007192 /* Here, we'll look for the first running server which supports the cookie.
7193 * This allows to share a same cookie between several servers, for example
7194 * to dedicate backup servers to specific servers only.
7195 * However, to prevent clients from sticking to cookie-less backup server
7196 * when they have incidentely learned an empty cookie, we simply ignore
7197 * empty cookies and mark them as invalid.
7198 * The same behaviour is applied when persistence must be ignored.
7199 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007200 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007201 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007202
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007203 while (srv) {
7204 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7205 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7206 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007207 (s->be->options & PR_O_PERSIST) ||
7208 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007209 /* we found the server and we can use it */
7210 txn->flags &= ~TX_CK_MASK;
7211 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007212 s->flags |= SN_DIRECT | SN_ASSIGNED;
7213 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007214 break;
7215 } else {
7216 /* we found a server, but it's down,
7217 * mark it as such and go on in case
7218 * another one is available.
7219 */
7220 txn->flags &= ~TX_CK_MASK;
7221 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007222 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007224 srv = srv->next;
7225 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007226
Willy Tarreauf64d1412010-10-07 20:06:11 +02007227 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007228 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007229 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007230 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007231 txn->flags |= TX_CK_UNUSED;
7232 else
7233 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007235
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007236 /* depending on the cookie mode, we may have to either :
7237 * - delete the complete cookie if we're in insert+indirect mode, so that
7238 * the server never sees it ;
7239 * - remove the server id from the cookie value, and tag the cookie as an
7240 * application cookie so that it does not get accidentely removed later,
7241 * if we're in cookie prefix mode
7242 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007243 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007244 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007245
Willy Tarreau9b28e032012-10-12 23:49:43 +02007246 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007247 val_end += delta;
7248 next += delta;
7249 hdr_end += delta;
7250 hdr_next += delta;
7251 cur_hdr->len += delta;
7252 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007253
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007254 del_from = NULL;
7255 preserve_hdr = 1; /* we want to keep this cookie */
7256 }
7257 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007258 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007259 del_from = prev;
7260 }
7261 } else {
7262 /* This is not our cookie, so we must preserve it. But if we already
7263 * scheduled another cookie for removal, we cannot remove the
7264 * complete header, but we can remove the previous block itself.
7265 */
7266 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007268 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007269 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007270 if (att_beg >= del_from)
7271 att_beg += delta;
7272 if (att_end >= del_from)
7273 att_end += delta;
7274 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007275 val_end += delta;
7276 next += delta;
7277 hdr_end += delta;
7278 hdr_next += delta;
7279 cur_hdr->len += delta;
7280 http_msg_move_end(&txn->req, delta);
7281 prev = del_from;
7282 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007283 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007284 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007285
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007286 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007287 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007288 int cmp_len, value_len;
7289 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007290
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007291 if (s->be->options2 & PR_O2_AS_PFX) {
7292 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7293 value_begin = att_beg + s->be->appsession_name_len;
7294 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295 } else {
7296 cmp_len = att_end - att_beg;
7297 value_begin = val_beg;
7298 value_len = val_end - val_beg;
7299 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007301 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007302 if (cmp_len == s->be->appsession_name_len &&
7303 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7304 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007305 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306 }
7307
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007308 /* continue with next cookie on this header line */
7309 att_beg = next;
7310 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007311
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 /* There are no more cookies on this line.
7313 * We may still have one (or several) marked for deletion at the
7314 * end of the line. We must do this now in two ways :
7315 * - if some cookies must be preserved, we only delete from the
7316 * mark to the end of line ;
7317 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007319 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007320 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007321 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007322 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007323 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324 cur_hdr->len += delta;
7325 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007326 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327
7328 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007329 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7330 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007331 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007332 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007334 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007335 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336 }
7337
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007338 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007340 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007341}
7342
7343
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344/* Iterate the same filter through all response headers contained in <rtr>.
7345 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7346 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007347int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348{
7349 char term;
7350 char *cur_ptr, *cur_end, *cur_next;
7351 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007352 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007354 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355
7356 last_hdr = 0;
7357
Willy Tarreau9b28e032012-10-12 23:49:43 +02007358 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359 old_idx = 0;
7360
7361 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007362 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007363 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007364 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365 (exp->action == ACT_ALLOW ||
7366 exp->action == ACT_DENY))
7367 return 0;
7368
7369 cur_idx = txn->hdr_idx.v[old_idx].next;
7370 if (!cur_idx)
7371 break;
7372
7373 cur_hdr = &txn->hdr_idx.v[cur_idx];
7374 cur_ptr = cur_next;
7375 cur_end = cur_ptr + cur_hdr->len;
7376 cur_next = cur_end + cur_hdr->cr + 1;
7377
7378 /* Now we have one header between cur_ptr and cur_end,
7379 * and the next header starts at cur_next.
7380 */
7381
7382 /* The annoying part is that pattern matching needs
7383 * that we modify the contents to null-terminate all
7384 * strings before testing them.
7385 */
7386
7387 term = *cur_end;
7388 *cur_end = '\0';
7389
7390 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7391 switch (exp->action) {
7392 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007393 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394 last_hdr = 1;
7395 break;
7396
7397 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007398 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007399 last_hdr = 1;
7400 break;
7401
7402 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007403 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7404 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405 /* FIXME: if the user adds a newline in the replacement, the
7406 * index will not be recalculated for now, and the new line
7407 * will not be counted as a new header.
7408 */
7409
7410 cur_end += delta;
7411 cur_next += delta;
7412 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007413 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414 break;
7415
7416 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007417 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 cur_next += delta;
7419
Willy Tarreaufa355d42009-11-29 18:12:29 +01007420 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7422 txn->hdr_idx.used--;
7423 cur_hdr->len = 0;
7424 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007425 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 break;
7427
7428 }
7429 }
7430 if (cur_end)
7431 *cur_end = term; /* restore the string terminator */
7432
7433 /* keep the link from this header to next one in case of later
7434 * removal of next header.
7435 */
7436 old_idx = cur_idx;
7437 }
7438 return 0;
7439}
7440
7441
7442/* Apply the filter to the status line in the response buffer <rtr>.
7443 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7444 * or -1 if a replacement resulted in an invalid status line.
7445 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007446int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447{
7448 char term;
7449 char *cur_ptr, *cur_end;
7450 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007451 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007452 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453
7454
Willy Tarreau3d300592007-03-18 18:34:41 +01007455 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007457 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007458 (exp->action == ACT_ALLOW ||
7459 exp->action == ACT_DENY))
7460 return 0;
7461 else if (exp->action == ACT_REMOVE)
7462 return 0;
7463
7464 done = 0;
7465
Willy Tarreau9b28e032012-10-12 23:49:43 +02007466 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007467 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007468
7469 /* Now we have the status line between cur_ptr and cur_end */
7470
7471 /* The annoying part is that pattern matching needs
7472 * that we modify the contents to null-terminate all
7473 * strings before testing them.
7474 */
7475
7476 term = *cur_end;
7477 *cur_end = '\0';
7478
7479 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7480 switch (exp->action) {
7481 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007482 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007483 done = 1;
7484 break;
7485
7486 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007487 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488 done = 1;
7489 break;
7490
7491 case ACT_REPLACE:
7492 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007493 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7494 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007495 /* FIXME: if the user adds a newline in the replacement, the
7496 * index will not be recalculated for now, and the new line
7497 * will not be counted as a new header.
7498 */
7499
Willy Tarreaufa355d42009-11-29 18:12:29 +01007500 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007502 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007503 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504 cur_ptr, cur_end + 1,
7505 NULL, NULL);
7506 if (unlikely(!cur_end))
7507 return -1;
7508
7509 /* we have a full respnse and we know that we have either a CR
7510 * or an LF at <ptr>.
7511 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007512 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007513 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007514 /* there is no point trying this regex on headers */
7515 return 1;
7516 }
7517 }
7518 *cur_end = term; /* restore the string terminator */
7519 return done;
7520}
7521
7522
7523
7524/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007525 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007526 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7527 * unparsable response.
7528 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007529int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007531 struct http_txn *txn = &s->txn;
7532 struct hdr_exp *exp;
7533
7534 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007535 int ret;
7536
7537 /*
7538 * The interleaving of transformations and verdicts
7539 * makes it difficult to decide to continue or stop
7540 * the evaluation.
7541 */
7542
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007543 if (txn->flags & TX_SVDENY)
7544 break;
7545
Willy Tarreau3d300592007-03-18 18:34:41 +01007546 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7548 exp->action == ACT_PASS)) {
7549 exp = exp->next;
7550 continue;
7551 }
7552
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007553 /* if this filter had a condition, evaluate it now and skip to
7554 * next filter if the condition does not match.
7555 */
7556 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007557 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007558 ret = acl_pass(ret);
7559 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7560 ret = !ret;
7561 if (!ret)
7562 continue;
7563 }
7564
Willy Tarreaua15645d2007-03-18 16:22:39 +01007565 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007566 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007567 if (unlikely(ret < 0))
7568 return -1;
7569
7570 if (likely(ret == 0)) {
7571 /* The filter did not match the response, it can be
7572 * iterated through all headers.
7573 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007574 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007575 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007576 }
7577 return 0;
7578}
7579
7580
Willy Tarreaua15645d2007-03-18 16:22:39 +01007581/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007582 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007583 * desirable to call it only when needed. This function is also used when we
7584 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007585 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007586void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007587{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007588 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007589 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007590 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007591 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007592 char *hdr_beg, *hdr_end, *hdr_next;
7593 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007594
Willy Tarreaua15645d2007-03-18 16:22:39 +01007595 /* Iterate through the headers.
7596 * we start with the start line.
7597 */
7598 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007599 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600
7601 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7602 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007603 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604
7605 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007606 hdr_beg = hdr_next;
7607 hdr_end = hdr_beg + cur_hdr->len;
7608 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609
Willy Tarreau24581ba2010-08-31 22:39:35 +02007610 /* We have one full header between hdr_beg and hdr_end, and the
7611 * next header starts at hdr_next. We're only interested in
7612 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007613 */
7614
Willy Tarreau24581ba2010-08-31 22:39:35 +02007615 is_cookie2 = 0;
7616 prev = hdr_beg + 10;
7617 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007618 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007619 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7620 if (!val) {
7621 old_idx = cur_idx;
7622 continue;
7623 }
7624 is_cookie2 = 1;
7625 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007626 }
7627
Willy Tarreau24581ba2010-08-31 22:39:35 +02007628 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7629 * <prev> points to the colon.
7630 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007631 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632
Willy Tarreau24581ba2010-08-31 22:39:35 +02007633 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7634 * check-cache is enabled) and we are not interested in checking
7635 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007636 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007637 if (s->be->cookie_name == NULL &&
7638 s->be->appsession_name == NULL &&
7639 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 return;
7641
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 /* OK so now we know we have to process this response cookie.
7643 * The format of the Set-Cookie header is slightly different
7644 * from the format of the Cookie header in that it does not
7645 * support the comma as a cookie delimiter (thus the header
7646 * cannot be folded) because the Expires attribute described in
7647 * the original Netscape's spec may contain an unquoted date
7648 * with a comma inside. We have to live with this because
7649 * many browsers don't support Max-Age and some browsers don't
7650 * support quoted strings. However the Set-Cookie2 header is
7651 * clean.
7652 *
7653 * We have to keep multiple pointers in order to support cookie
7654 * removal at the beginning, middle or end of header without
7655 * corrupting the header (in case of set-cookie2). A special
7656 * pointer, <scav> points to the beginning of the set-cookie-av
7657 * fields after the first semi-colon. The <next> pointer points
7658 * either to the end of line (set-cookie) or next unquoted comma
7659 * (set-cookie2). All of these headers are valid :
7660 *
7661 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7662 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7663 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7664 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7665 * | | | | | | | | | |
7666 * | | | | | | | | +-> next hdr_end <--+
7667 * | | | | | | | +------------> scav
7668 * | | | | | | +--------------> val_end
7669 * | | | | | +--------------------> val_beg
7670 * | | | | +----------------------> equal
7671 * | | | +------------------------> att_end
7672 * | | +----------------------------> att_beg
7673 * | +------------------------------> prev
7674 * +-----------------------------------------> hdr_beg
7675 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676
Willy Tarreau24581ba2010-08-31 22:39:35 +02007677 for (; prev < hdr_end; prev = next) {
7678 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679
Willy Tarreau24581ba2010-08-31 22:39:35 +02007680 /* find att_beg */
7681 att_beg = prev + 1;
7682 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7683 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007684
Willy Tarreau24581ba2010-08-31 22:39:35 +02007685 /* find att_end : this is the first character after the last non
7686 * space before the equal. It may be equal to hdr_end.
7687 */
7688 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007689
Willy Tarreau24581ba2010-08-31 22:39:35 +02007690 while (equal < hdr_end) {
7691 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7692 break;
7693 if (http_is_spht[(unsigned char)*equal++])
7694 continue;
7695 att_end = equal;
7696 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007697
Willy Tarreau24581ba2010-08-31 22:39:35 +02007698 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7699 * is between <att_beg> and <equal>, both may be identical.
7700 */
7701
7702 /* look for end of cookie if there is an equal sign */
7703 if (equal < hdr_end && *equal == '=') {
7704 /* look for the beginning of the value */
7705 val_beg = equal + 1;
7706 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7707 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007708
Willy Tarreau24581ba2010-08-31 22:39:35 +02007709 /* find the end of the value, respecting quotes */
7710 next = find_cookie_value_end(val_beg, hdr_end);
7711
7712 /* make val_end point to the first white space or delimitor after the value */
7713 val_end = next;
7714 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7715 val_end--;
7716 } else {
7717 /* <equal> points to next comma, semi-colon or EOL */
7718 val_beg = val_end = next = equal;
7719 }
7720
7721 if (next < hdr_end) {
7722 /* Set-Cookie2 supports multiple cookies, and <next> points to
7723 * a colon or semi-colon before the end. So skip all attr-value
7724 * pairs and look for the next comma. For Set-Cookie, since
7725 * commas are permitted in values, skip to the end.
7726 */
7727 if (is_cookie2)
7728 next = find_hdr_value_end(next, hdr_end);
7729 else
7730 next = hdr_end;
7731 }
7732
7733 /* Now everything is as on the diagram above */
7734
7735 /* Ignore cookies with no equal sign */
7736 if (equal == val_end)
7737 continue;
7738
7739 /* If there are spaces around the equal sign, we need to
7740 * strip them otherwise we'll get trouble for cookie captures,
7741 * or even for rewrites. Since this happens extremely rarely,
7742 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007744 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7745 int stripped_before = 0;
7746 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007747
Willy Tarreau24581ba2010-08-31 22:39:35 +02007748 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007749 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007750 equal += stripped_before;
7751 val_beg += stripped_before;
7752 }
7753
7754 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007755 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007756 val_beg += stripped_after;
7757 stripped_before += stripped_after;
7758 }
7759
7760 val_end += stripped_before;
7761 next += stripped_before;
7762 hdr_end += stripped_before;
7763 hdr_next += stripped_before;
7764 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007765 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007766 }
7767
7768 /* First, let's see if we want to capture this cookie. We check
7769 * that we don't already have a server side cookie, because we
7770 * can only capture one. Also as an optimisation, we ignore
7771 * cookies shorter than the declared name.
7772 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007773 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007774 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007775 (val_end - att_beg >= s->fe->capture_namelen) &&
7776 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007777 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007778 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 Alert("HTTP logging : out of memory.\n");
7780 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007781 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007782 if (log_len > s->fe->capture_len)
7783 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007784 memcpy(txn->srv_cookie, att_beg, log_len);
7785 txn->srv_cookie[log_len] = 0;
7786 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 }
7788
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007789 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007791 if (!(s->flags & SN_IGNORE_PRST) &&
7792 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7793 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007794 /* assume passive cookie by default */
7795 txn->flags &= ~TX_SCK_MASK;
7796 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797
7798 /* If the cookie is in insert mode on a known server, we'll delete
7799 * this occurrence because we'll insert another one later.
7800 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007801 * a direct access.
7802 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007803 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007804 /* The "preserve" flag was set, we don't want to touch the
7805 * server's cookie.
7806 */
7807 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007808 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
7809 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007810 /* this cookie must be deleted */
7811 if (*prev == ':' && next == hdr_end) {
7812 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007814 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7815 txn->hdr_idx.used--;
7816 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007817 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007818 hdr_next += delta;
7819 http_msg_move_end(&txn->rsp, delta);
7820 /* note: while both invalid now, <next> and <hdr_end>
7821 * are still equal, so the for() will stop as expected.
7822 */
7823 } else {
7824 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007825 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007826 next = prev;
7827 hdr_end += delta;
7828 hdr_next += delta;
7829 cur_hdr->len += delta;
7830 http_msg_move_end(&txn->rsp, delta);
7831 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007832 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007833 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007834 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007836 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007837 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838 * with this server since we know it.
7839 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007840 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007841 next += delta;
7842 hdr_end += delta;
7843 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007845 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846
Willy Tarreauf1348312010-10-07 15:54:11 +02007847 txn->flags &= ~TX_SCK_MASK;
7848 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007850 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007851 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007852 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007854 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007855 next += delta;
7856 hdr_end += delta;
7857 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007859 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860
Willy Tarreau827aee92011-03-10 16:55:02 +01007861 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007862 txn->flags &= ~TX_SCK_MASK;
7863 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864 }
7865 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007866 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007867 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007868 int cmp_len, value_len;
7869 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007870
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007871 if (s->be->options2 & PR_O2_AS_PFX) {
7872 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7873 value_begin = att_beg + s->be->appsession_name_len;
7874 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007875 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007876 cmp_len = att_end - att_beg;
7877 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007878 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007879 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007880
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007881 if ((cmp_len == s->be->appsession_name_len) &&
7882 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007883 /* free a possibly previously allocated memory */
7884 pool_free2(apools.sessid, txn->sessid);
7885
Cyril Bontéb21570a2009-11-29 20:04:48 +01007886 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007887 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007888 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007889 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01007890 return;
7891 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007892 memcpy(txn->sessid, value_begin, value_len);
7893 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007894 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007895 }
7896 /* that's done for this cookie, check the next one on the same
7897 * line when next != hdr_end (only if is_cookie2).
7898 */
7899 }
7900 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007901 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007902 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007903
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007904 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007905 appsess *asession = NULL;
7906 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007907 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007908 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007909 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007910 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7911 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007912 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007913 return;
7914 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007915 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7916
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007917 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7918 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007919 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7920 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007921 return;
7922 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007923 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
7924 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007925
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007926 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007927 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007928 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007929 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7930 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007931 return;
7932 }
7933 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007934 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007935
7936 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007937 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007938 }
7939
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007940 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007941 asession->request_count++;
7942 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007943}
7944
7945
Willy Tarreaua15645d2007-03-18 16:22:39 +01007946/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007947 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007948 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007949void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007951 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952 char *p1, *p2;
7953
7954 char *cur_ptr, *cur_end, *cur_next;
7955 int cur_idx;
7956
Willy Tarreau5df51872007-11-25 16:20:08 +01007957 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958 return;
7959
7960 /* Iterate through the headers.
7961 * we start with the start line.
7962 */
7963 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007964 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965
7966 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7967 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007968 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007969
7970 cur_hdr = &txn->hdr_idx.v[cur_idx];
7971 cur_ptr = cur_next;
7972 cur_end = cur_ptr + cur_hdr->len;
7973 cur_next = cur_end + cur_hdr->cr + 1;
7974
7975 /* We have one full header between cur_ptr and cur_end, and the
7976 * next header starts at cur_next. We're only interested in
7977 * "Cookie:" headers.
7978 */
7979
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007980 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7981 if (val) {
7982 if ((cur_end - (cur_ptr + val) >= 8) &&
7983 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7984 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7985 return;
7986 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987 }
7988
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007989 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7990 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991 continue;
7992
7993 /* OK, right now we know we have a cache-control header at cur_ptr */
7994
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007995 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996
7997 if (p1 >= cur_end) /* no more info */
7998 continue;
7999
8000 /* p1 is at the beginning of the value */
8001 p2 = p1;
8002
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008003 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 p2++;
8005
8006 /* we have a complete value between p1 and p2 */
8007 if (p2 < cur_end && *p2 == '=') {
8008 /* we have something of the form no-cache="set-cookie" */
8009 if ((cur_end - p1 >= 21) &&
8010 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8011 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008012 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 continue;
8014 }
8015
8016 /* OK, so we know that either p2 points to the end of string or to a comma */
8017 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008018 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8020 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8021 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 return;
8024 }
8025
8026 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008027 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 continue;
8029 }
8030 }
8031}
8032
8033
Willy Tarreau58f10d72006-12-04 02:26:12 +01008034/*
8035 * Try to retrieve a known appsession in the URI, then the associated server.
8036 * If the server is found, it's assigned to the session.
8037 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008038void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008039{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008040 char *end_params, *first_param, *cur_param, *next_param;
8041 char separator;
8042 int value_len;
8043
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008044 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008045
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008046 if (s->be->appsession_name == NULL ||
8047 (s->txn.meth != HTTP_METH_GET && s->txn.meth != HTTP_METH_POST && s->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008048 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008050
Cyril Bontéb21570a2009-11-29 20:04:48 +01008051 first_param = NULL;
8052 switch (mode) {
8053 case PR_O2_AS_M_PP:
8054 first_param = memchr(begin, ';', len);
8055 break;
8056 case PR_O2_AS_M_QS:
8057 first_param = memchr(begin, '?', len);
8058 break;
8059 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008060
Cyril Bontéb21570a2009-11-29 20:04:48 +01008061 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008062 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008063 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008064
Cyril Bontéb21570a2009-11-29 20:04:48 +01008065 switch (mode) {
8066 case PR_O2_AS_M_PP:
8067 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8068 end_params = (char *) begin + len;
8069 }
8070 separator = ';';
8071 break;
8072 case PR_O2_AS_M_QS:
8073 end_params = (char *) begin + len;
8074 separator = '&';
8075 break;
8076 default:
8077 /* unknown mode, shouldn't happen */
8078 return;
8079 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008080
Cyril Bontéb21570a2009-11-29 20:04:48 +01008081 cur_param = next_param = end_params;
8082 while (cur_param > first_param) {
8083 cur_param--;
8084 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8085 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008086 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8087 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8088 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008089 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008090 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8091 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008092 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008093 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008094 }
8095 break;
8096 }
8097 next_param = cur_param;
8098 }
8099 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008100#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008101 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008102 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008103#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008104}
8105
Willy Tarreaub2513902006-12-17 14:52:38 +01008106/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008107 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008108 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008109 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008110 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008111 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008112 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008113 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008114 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008115int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008116{
8117 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008118 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008119 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008120
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008121 if (!uri_auth)
8122 return 0;
8123
Cyril Bonté70be45d2010-10-12 00:14:35 +02008124 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008125 return 0;
8126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008127 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008128 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008129 return 0;
8130
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008131 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008132 return 0;
8133
Willy Tarreaub2513902006-12-17 14:52:38 +01008134 return 1;
8135}
8136
Willy Tarreau4076a152009-04-02 15:18:36 +02008137/*
8138 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008139 * By default it tries to report the error position as msg->err_pos. However if
8140 * this one is not set, it will then report msg->next, which is the last known
8141 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008142 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008143 */
8144void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008145 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008146 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008147{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008148 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008149 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008150
Willy Tarreau9b28e032012-10-12 23:49:43 +02008151 es->len = MIN(chn->buf->i, sizeof(es->buf));
8152 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008153 len1 = MIN(len1, es->len);
8154 len2 = es->len - len1; /* remaining data if buffer wraps */
8155
Willy Tarreau9b28e032012-10-12 23:49:43 +02008156 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008157 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008158 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008159
Willy Tarreau4076a152009-04-02 15:18:36 +02008160 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008161 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008162 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008163 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008164
Willy Tarreau4076a152009-04-02 15:18:36 +02008165 es->when = date; // user-visible date
8166 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008167 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008168 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008169 if (objt_conn(s->req->prod->end))
8170 es->src = __objt_conn(s->req->prod->end)->addr.from;
8171 else
8172 memset(&es->src, 0, sizeof(es->src));
8173
Willy Tarreau078272e2010-12-12 12:46:33 +01008174 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008175 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008176 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008177 es->s_flags = s->flags;
8178 es->t_flags = s->txn.flags;
8179 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008180 es->b_out = chn->buf->o;
8181 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008182 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008183 es->m_clen = msg->chunk_len;
8184 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008185}
Willy Tarreaub2513902006-12-17 14:52:38 +01008186
Willy Tarreau294c4732011-12-16 21:35:50 +01008187/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8188 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8189 * performed over the whole headers. Otherwise it must contain a valid header
8190 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8191 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8192 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8193 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008194 * -1. The value fetch stops at commas, so this function is suited for use with
8195 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008196 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008197 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008198unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008199 struct hdr_idx *idx, int occ,
8200 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008201{
Willy Tarreau294c4732011-12-16 21:35:50 +01008202 struct hdr_ctx local_ctx;
8203 char *ptr_hist[MAX_HDR_HISTORY];
8204 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008205 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008206 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008207
Willy Tarreau294c4732011-12-16 21:35:50 +01008208 if (!ctx) {
8209 local_ctx.idx = 0;
8210 ctx = &local_ctx;
8211 }
8212
Willy Tarreaubce70882009-09-07 11:51:47 +02008213 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008214 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008215 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008216 occ--;
8217 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008218 *vptr = ctx->line + ctx->val;
8219 *vlen = ctx->vlen;
8220 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008221 }
8222 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008223 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008224 }
8225
8226 /* negative occurrence, we scan all the list then walk back */
8227 if (-occ > MAX_HDR_HISTORY)
8228 return 0;
8229
Willy Tarreau294c4732011-12-16 21:35:50 +01008230 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008231 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008232 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8233 len_hist[hist_ptr] = ctx->vlen;
8234 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008235 hist_ptr = 0;
8236 found++;
8237 }
8238 if (-occ > found)
8239 return 0;
8240 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008241 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8242 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8243 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008244 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008245 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008246 if (hist_ptr >= MAX_HDR_HISTORY)
8247 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008248 *vptr = ptr_hist[hist_ptr];
8249 *vlen = len_hist[hist_ptr];
8250 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008251}
8252
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008253/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8254 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8255 * performed over the whole headers. Otherwise it must contain a valid header
8256 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8257 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8258 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8259 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8260 * -1. This function differs from http_get_hdr() in that it only returns full
8261 * line header values and does not stop at commas.
8262 * The return value is 0 if nothing was found, or non-zero otherwise.
8263 */
8264unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8265 struct hdr_idx *idx, int occ,
8266 struct hdr_ctx *ctx, char **vptr, int *vlen)
8267{
8268 struct hdr_ctx local_ctx;
8269 char *ptr_hist[MAX_HDR_HISTORY];
8270 int len_hist[MAX_HDR_HISTORY];
8271 unsigned int hist_ptr;
8272 int found;
8273
8274 if (!ctx) {
8275 local_ctx.idx = 0;
8276 ctx = &local_ctx;
8277 }
8278
8279 if (occ >= 0) {
8280 /* search from the beginning */
8281 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8282 occ--;
8283 if (occ <= 0) {
8284 *vptr = ctx->line + ctx->val;
8285 *vlen = ctx->vlen;
8286 return 1;
8287 }
8288 }
8289 return 0;
8290 }
8291
8292 /* negative occurrence, we scan all the list then walk back */
8293 if (-occ > MAX_HDR_HISTORY)
8294 return 0;
8295
8296 found = hist_ptr = 0;
8297 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8298 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8299 len_hist[hist_ptr] = ctx->vlen;
8300 if (++hist_ptr >= MAX_HDR_HISTORY)
8301 hist_ptr = 0;
8302 found++;
8303 }
8304 if (-occ > found)
8305 return 0;
8306 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8307 * find occurrence -occ, so we have to check [hist_ptr+occ].
8308 */
8309 hist_ptr += occ;
8310 if (hist_ptr >= MAX_HDR_HISTORY)
8311 hist_ptr -= MAX_HDR_HISTORY;
8312 *vptr = ptr_hist[hist_ptr];
8313 *vlen = len_hist[hist_ptr];
8314 return 1;
8315}
8316
Willy Tarreaubaaee002006-06-26 02:48:02 +02008317/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008318 * Print a debug line with a header. Always stop at the first CR or LF char,
8319 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8320 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008321 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008322void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008323{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008324 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008325 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008326 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008327 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8328 objt_conn(s->req->cons->end) ? (unsigned short)objt_conn(s->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008329
8330 for (max = 0; start + max < end; max++)
8331 if (start[max] == '\r' || start[max] == '\n')
8332 break;
8333
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008334 UBOUND(max, trash.size - trash.len - 3);
8335 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8336 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008337 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008338}
8339
Willy Tarreau0937bc42009-12-22 15:03:09 +01008340/*
8341 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8342 * the required fields are properly allocated and that we only need to (re)init
8343 * them. This should be used before processing any new request.
8344 */
8345void http_init_txn(struct session *s)
8346{
8347 struct http_txn *txn = &s->txn;
8348 struct proxy *fe = s->fe;
8349
8350 txn->flags = 0;
8351 txn->status = -1;
8352
Willy Tarreauf64d1412010-10-07 20:06:11 +02008353 txn->cookie_first_date = 0;
8354 txn->cookie_last_date = 0;
8355
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008356 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008357 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008358 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008359 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008360 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008361 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008362 txn->req.chunk_len = 0LL;
8363 txn->req.body_len = 0LL;
8364 txn->rsp.chunk_len = 0LL;
8365 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008366 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8367 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008368 txn->req.chn = s->req;
8369 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008370
8371 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008372
8373 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8374 if (fe->options2 & PR_O2_REQBUG_OK)
8375 txn->req.err_pos = -1; /* let buggy requests pass */
8376
Willy Tarreau46023632010-01-07 22:51:47 +01008377 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008378 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8379
Willy Tarreau46023632010-01-07 22:51:47 +01008380 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008381 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8382
8383 if (txn->hdr_idx.v)
8384 hdr_idx_init(&txn->hdr_idx);
8385}
8386
8387/* to be used at the end of a transaction */
8388void http_end_txn(struct session *s)
8389{
8390 struct http_txn *txn = &s->txn;
8391
Willy Tarreau75195602014-03-11 15:48:55 +01008392 /* release any possible compression context */
8393 if (s->flags & SN_COMP_READY)
8394 s->comp_algo->end(&s->comp_ctx);
8395 s->comp_algo = NULL;
8396 s->flags &= ~SN_COMP_READY;
8397
Willy Tarreau0937bc42009-12-22 15:03:09 +01008398 /* these ones will have been dynamically allocated */
8399 pool_free2(pool2_requri, txn->uri);
8400 pool_free2(pool2_capture, txn->cli_cookie);
8401 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008402 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008403 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008404
William Lallemanda73203e2012-03-12 12:48:57 +01008405 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008406 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008407 txn->uri = NULL;
8408 txn->srv_cookie = NULL;
8409 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008410
8411 if (txn->req.cap) {
8412 struct cap_hdr *h;
8413 for (h = s->fe->req_cap; h; h = h->next)
8414 pool_free2(h->pool, txn->req.cap[h->index]);
8415 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8416 }
8417
8418 if (txn->rsp.cap) {
8419 struct cap_hdr *h;
8420 for (h = s->fe->rsp_cap; h; h = h->next)
8421 pool_free2(h->pool, txn->rsp.cap[h->index]);
8422 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8423 }
8424
Willy Tarreau0937bc42009-12-22 15:03:09 +01008425}
8426
8427/* to be used at the end of a transaction to prepare a new one */
8428void http_reset_txn(struct session *s)
8429{
8430 http_end_txn(s);
8431 http_init_txn(s);
8432
8433 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008434 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008435 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008436 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008437 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008438 /* re-init store persistence */
8439 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008440 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008441
Willy Tarreau0937bc42009-12-22 15:03:09 +01008442 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008443
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008444 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008445
Willy Tarreau739cfba2010-01-25 23:11:14 +01008446 /* We must trim any excess data from the response buffer, because we
8447 * may have blocked an invalid response from a server that we don't
8448 * want to accidentely forward once we disable the analysers, nor do
8449 * we want those data to come along with next response. A typical
8450 * example of such data would be from a buggy server responding to
8451 * a HEAD with some data, or sending more than the advertised
8452 * content-length.
8453 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008454 if (unlikely(s->rep->buf->i))
8455 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008456
Willy Tarreau0937bc42009-12-22 15:03:09 +01008457 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008458 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008459
Willy Tarreaud04e8582010-05-31 12:31:35 +02008460 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008461 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008462
8463 s->req->rex = TICK_ETERNITY;
8464 s->req->wex = TICK_ETERNITY;
8465 s->req->analyse_exp = TICK_ETERNITY;
8466 s->rep->rex = TICK_ETERNITY;
8467 s->rep->wex = TICK_ETERNITY;
8468 s->rep->analyse_exp = TICK_ETERNITY;
8469}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008470
Willy Tarreauff011f22011-01-06 17:51:27 +01008471void free_http_req_rules(struct list *r) {
8472 struct http_req_rule *tr, *pr;
8473
8474 list_for_each_entry_safe(pr, tr, r, list) {
8475 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008476 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008477 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008478
8479 free(pr);
8480 }
8481}
8482
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008483/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008484struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8485{
8486 struct http_req_rule *rule;
8487 int cur_arg;
8488
8489 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8490 if (!rule) {
8491 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008492 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008493 }
8494
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008495 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008496 rule->action = HTTP_REQ_ACT_ALLOW;
8497 cur_arg = 1;
8498 } else if (!strcmp(args[0], "deny")) {
8499 rule->action = HTTP_REQ_ACT_DENY;
8500 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008501 } else if (!strcmp(args[0], "tarpit")) {
8502 rule->action = HTTP_REQ_ACT_TARPIT;
8503 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008504 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008505 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008506 cur_arg = 1;
8507
8508 while(*args[cur_arg]) {
8509 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008510 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008511 cur_arg+=2;
8512 continue;
8513 } else
8514 break;
8515 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008516 } else if (!strcmp(args[0], "set-nice")) {
8517 rule->action = HTTP_REQ_ACT_SET_NICE;
8518 cur_arg = 1;
8519
8520 if (!*args[cur_arg] ||
8521 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8522 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8523 file, linenum, args[0]);
8524 goto out_err;
8525 }
8526 rule->arg.nice = atoi(args[cur_arg]);
8527 if (rule->arg.nice < -1024)
8528 rule->arg.nice = -1024;
8529 else if (rule->arg.nice > 1024)
8530 rule->arg.nice = 1024;
8531 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008532 } else if (!strcmp(args[0], "set-tos")) {
8533#ifdef IP_TOS
8534 char *err;
8535 rule->action = HTTP_REQ_ACT_SET_TOS;
8536 cur_arg = 1;
8537
8538 if (!*args[cur_arg] ||
8539 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8540 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8541 file, linenum, args[0]);
8542 goto out_err;
8543 }
8544
8545 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8546 if (err && *err != '\0') {
8547 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8548 file, linenum, err, args[0]);
8549 goto out_err;
8550 }
8551 cur_arg++;
8552#else
8553 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8554 goto out_err;
8555#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008556 } else if (!strcmp(args[0], "set-mark")) {
8557#ifdef SO_MARK
8558 char *err;
8559 rule->action = HTTP_REQ_ACT_SET_MARK;
8560 cur_arg = 1;
8561
8562 if (!*args[cur_arg] ||
8563 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8564 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8565 file, linenum, args[0]);
8566 goto out_err;
8567 }
8568
8569 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8570 if (err && *err != '\0') {
8571 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8572 file, linenum, err, args[0]);
8573 goto out_err;
8574 }
8575 cur_arg++;
8576 global.last_checks |= LSTCHK_NETADM;
8577#else
8578 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8579 goto out_err;
8580#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008581 } else if (!strcmp(args[0], "set-log-level")) {
8582 rule->action = HTTP_REQ_ACT_SET_LOGL;
8583 cur_arg = 1;
8584
8585 if (!*args[cur_arg] ||
8586 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8587 bad_log_level:
8588 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8589 file, linenum, args[0]);
8590 goto out_err;
8591 }
8592 if (strcmp(args[cur_arg], "silent") == 0)
8593 rule->arg.loglevel = -1;
8594 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8595 goto bad_log_level;
8596 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008597 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8598 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8599 cur_arg = 1;
8600
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008601 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8602 (*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 +01008603 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8604 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008605 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008606 }
8607
8608 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8609 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8610 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008611
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008612 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008613 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008614 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8615 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008616 free(proxy->conf.lfs_file);
8617 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8618 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008619 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008620 } else if (strcmp(args[0], "del-header") == 0) {
8621 rule->action = HTTP_REQ_ACT_DEL_HDR;
8622 cur_arg = 1;
8623
8624 if (!*args[cur_arg] ||
8625 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8626 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8627 file, linenum, args[0]);
8628 goto out_err;
8629 }
8630
8631 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8632 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8633
8634 proxy->conf.args.ctx = ARGC_HRQ;
8635 free(proxy->conf.lfs_file);
8636 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8637 proxy->conf.lfs_line = proxy->conf.args.line;
8638 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008639 } else if (strcmp(args[0], "redirect") == 0) {
8640 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008641 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008642
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008643 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008644 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8645 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8646 goto out_err;
8647 }
8648
8649 /* this redirect rule might already contain a parsed condition which
8650 * we'll pass to the http-request rule.
8651 */
8652 rule->action = HTTP_REQ_ACT_REDIR;
8653 rule->arg.redir = redir;
8654 rule->cond = redir->cond;
8655 redir->cond = NULL;
8656 cur_arg = 2;
8657 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008658 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008659 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 +01008660 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008661 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008662 }
8663
8664 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8665 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008666 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008667
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008668 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8669 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8670 file, linenum, args[0], errmsg);
8671 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008672 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008673 }
8674 rule->cond = cond;
8675 }
8676 else if (*args[cur_arg]) {
8677 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8678 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8679 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008680 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008681 }
8682
8683 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008684 out_err:
8685 free(rule);
8686 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008687}
8688
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008689/* parse an "http-respose" rule */
8690struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8691{
8692 struct http_res_rule *rule;
8693 int cur_arg;
8694
8695 rule = calloc(1, sizeof(*rule));
8696 if (!rule) {
8697 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8698 goto out_err;
8699 }
8700
8701 if (!strcmp(args[0], "allow")) {
8702 rule->action = HTTP_RES_ACT_ALLOW;
8703 cur_arg = 1;
8704 } else if (!strcmp(args[0], "deny")) {
8705 rule->action = HTTP_RES_ACT_DENY;
8706 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008707 } else if (!strcmp(args[0], "set-nice")) {
8708 rule->action = HTTP_RES_ACT_SET_NICE;
8709 cur_arg = 1;
8710
8711 if (!*args[cur_arg] ||
8712 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8713 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8714 file, linenum, args[0]);
8715 goto out_err;
8716 }
8717 rule->arg.nice = atoi(args[cur_arg]);
8718 if (rule->arg.nice < -1024)
8719 rule->arg.nice = -1024;
8720 else if (rule->arg.nice > 1024)
8721 rule->arg.nice = 1024;
8722 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008723 } else if (!strcmp(args[0], "set-tos")) {
8724#ifdef IP_TOS
8725 char *err;
8726 rule->action = HTTP_RES_ACT_SET_TOS;
8727 cur_arg = 1;
8728
8729 if (!*args[cur_arg] ||
8730 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8731 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8732 file, linenum, args[0]);
8733 goto out_err;
8734 }
8735
8736 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8737 if (err && *err != '\0') {
8738 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8739 file, linenum, err, args[0]);
8740 goto out_err;
8741 }
8742 cur_arg++;
8743#else
8744 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8745 goto out_err;
8746#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008747 } else if (!strcmp(args[0], "set-mark")) {
8748#ifdef SO_MARK
8749 char *err;
8750 rule->action = HTTP_RES_ACT_SET_MARK;
8751 cur_arg = 1;
8752
8753 if (!*args[cur_arg] ||
8754 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8755 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8756 file, linenum, args[0]);
8757 goto out_err;
8758 }
8759
8760 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8761 if (err && *err != '\0') {
8762 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8763 file, linenum, err, args[0]);
8764 goto out_err;
8765 }
8766 cur_arg++;
8767 global.last_checks |= LSTCHK_NETADM;
8768#else
8769 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8770 goto out_err;
8771#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008772 } else if (!strcmp(args[0], "set-log-level")) {
8773 rule->action = HTTP_RES_ACT_SET_LOGL;
8774 cur_arg = 1;
8775
8776 if (!*args[cur_arg] ||
8777 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8778 bad_log_level:
8779 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8780 file, linenum, args[0]);
8781 goto out_err;
8782 }
8783 if (strcmp(args[cur_arg], "silent") == 0)
8784 rule->arg.loglevel = -1;
8785 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8786 goto bad_log_level;
8787 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008788 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8789 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8790 cur_arg = 1;
8791
8792 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8793 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8794 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8795 file, linenum, args[0]);
8796 goto out_err;
8797 }
8798
8799 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8800 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8801 LIST_INIT(&rule->arg.hdr_add.fmt);
8802
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008803 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008804 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008805 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8806 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008807 free(proxy->conf.lfs_file);
8808 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8809 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008810 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008811 } else if (strcmp(args[0], "del-header") == 0) {
8812 rule->action = HTTP_RES_ACT_DEL_HDR;
8813 cur_arg = 1;
8814
8815 if (!*args[cur_arg] ||
8816 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8817 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8818 file, linenum, args[0]);
8819 goto out_err;
8820 }
8821
8822 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8823 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8824
8825 proxy->conf.args.ctx = ARGC_HRS;
8826 free(proxy->conf.lfs_file);
8827 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8828 proxy->conf.lfs_line = proxy->conf.args.line;
8829 cur_arg += 1;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008830 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008831 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 +02008832 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8833 goto out_err;
8834 }
8835
8836 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8837 struct acl_cond *cond;
8838 char *errmsg = NULL;
8839
8840 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8841 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8842 file, linenum, args[0], errmsg);
8843 free(errmsg);
8844 goto out_err;
8845 }
8846 rule->cond = cond;
8847 }
8848 else if (*args[cur_arg]) {
8849 Alert("parsing [%s:%d]: 'http-response %s' expects"
8850 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8851 file, linenum, args[0], args[cur_arg]);
8852 goto out_err;
8853 }
8854
8855 return rule;
8856 out_err:
8857 free(rule);
8858 return NULL;
8859}
8860
Willy Tarreau4baae242012-12-27 12:00:31 +01008861/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008862 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8863 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008864 */
8865struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008866 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008867{
8868 struct redirect_rule *rule;
8869 int cur_arg;
8870 int type = REDIRECT_TYPE_NONE;
8871 int code = 302;
8872 const char *destination = NULL;
8873 const char *cookie = NULL;
8874 int cookie_set = 0;
8875 unsigned int flags = REDIRECT_FLAG_NONE;
8876 struct acl_cond *cond = NULL;
8877
8878 cur_arg = 0;
8879 while (*(args[cur_arg])) {
8880 if (strcmp(args[cur_arg], "location") == 0) {
8881 if (!*args[cur_arg + 1])
8882 goto missing_arg;
8883
8884 type = REDIRECT_TYPE_LOCATION;
8885 cur_arg++;
8886 destination = args[cur_arg];
8887 }
8888 else if (strcmp(args[cur_arg], "prefix") == 0) {
8889 if (!*args[cur_arg + 1])
8890 goto missing_arg;
8891
8892 type = REDIRECT_TYPE_PREFIX;
8893 cur_arg++;
8894 destination = args[cur_arg];
8895 }
8896 else if (strcmp(args[cur_arg], "scheme") == 0) {
8897 if (!*args[cur_arg + 1])
8898 goto missing_arg;
8899
8900 type = REDIRECT_TYPE_SCHEME;
8901 cur_arg++;
8902 destination = args[cur_arg];
8903 }
8904 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8905 if (!*args[cur_arg + 1])
8906 goto missing_arg;
8907
8908 cur_arg++;
8909 cookie = args[cur_arg];
8910 cookie_set = 1;
8911 }
8912 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8913 if (!*args[cur_arg + 1])
8914 goto missing_arg;
8915
8916 cur_arg++;
8917 cookie = args[cur_arg];
8918 cookie_set = 0;
8919 }
8920 else if (strcmp(args[cur_arg], "code") == 0) {
8921 if (!*args[cur_arg + 1])
8922 goto missing_arg;
8923
8924 cur_arg++;
8925 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008926 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008927 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008928 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008929 args[cur_arg - 1], args[cur_arg]);
8930 return NULL;
8931 }
8932 }
8933 else if (!strcmp(args[cur_arg],"drop-query")) {
8934 flags |= REDIRECT_FLAG_DROP_QS;
8935 }
8936 else if (!strcmp(args[cur_arg],"append-slash")) {
8937 flags |= REDIRECT_FLAG_APPEND_SLASH;
8938 }
8939 else if (strcmp(args[cur_arg], "if") == 0 ||
8940 strcmp(args[cur_arg], "unless") == 0) {
8941 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8942 if (!cond) {
8943 memprintf(errmsg, "error in condition: %s", *errmsg);
8944 return NULL;
8945 }
8946 break;
8947 }
8948 else {
8949 memprintf(errmsg,
8950 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8951 args[cur_arg]);
8952 return NULL;
8953 }
8954 cur_arg++;
8955 }
8956
8957 if (type == REDIRECT_TYPE_NONE) {
8958 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8959 return NULL;
8960 }
8961
8962 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8963 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008964 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008965
8966 if (!use_fmt) {
8967 /* old-style static redirect rule */
8968 rule->rdr_str = strdup(destination);
8969 rule->rdr_len = strlen(destination);
8970 }
8971 else {
8972 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008973
8974 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8975 * if prefix == "/", we don't want to add anything, otherwise it
8976 * makes it hard for the user to configure a self-redirection.
8977 */
8978 proxy->conf.args.ctx = ARGC_RDR;
8979 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008980 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008981 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8982 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008983 free(curproxy->conf.lfs_file);
8984 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8985 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008986 }
8987 }
8988
Willy Tarreau4baae242012-12-27 12:00:31 +01008989 if (cookie) {
8990 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8991 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8992 */
8993 rule->cookie_len = strlen(cookie);
8994 if (cookie_set) {
8995 rule->cookie_str = malloc(rule->cookie_len + 10);
8996 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8997 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8998 rule->cookie_len += 9;
8999 } else {
9000 rule->cookie_str = malloc(rule->cookie_len + 21);
9001 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9002 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9003 rule->cookie_len += 20;
9004 }
9005 }
9006 rule->type = type;
9007 rule->code = code;
9008 rule->flags = flags;
9009 LIST_INIT(&rule->list);
9010 return rule;
9011
9012 missing_arg:
9013 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9014 return NULL;
9015}
9016
Willy Tarreau8797c062007-05-07 00:55:35 +02009017/************************************************************************/
9018/* The code below is dedicated to ACL parsing and matching */
9019/************************************************************************/
9020
9021
Willy Tarreau14174bc2012-04-16 14:34:04 +02009022/* This function ensures that the prerequisites for an L7 fetch are ready,
9023 * which means that a request or response is ready. If some data is missing,
9024 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009025 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9026 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009027 *
9028 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009029 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9030 * decide whether or not an HTTP message is present ;
9031 * 0 if the requested data cannot be fetched or if it is certain that
9032 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009033 * 1 if an HTTP message is ready
9034 */
9035static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009036smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009037 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009038{
9039 struct http_txn *txn = l7;
9040 struct http_msg *msg = &txn->req;
9041
9042 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9043 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9044 */
9045
9046 if (unlikely(!s || !txn))
9047 return 0;
9048
9049 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009050 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009051
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009052 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009053 if (unlikely(!s->req))
9054 return 0;
9055
Willy Tarreauaae75e32013-03-29 12:31:49 +01009056 /* If the buffer does not leave enough free space at the end,
9057 * we must first realign it.
9058 */
9059 if (s->req->buf->p > s->req->buf->data &&
9060 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9061 buffer_slow_realign(s->req->buf);
9062
Willy Tarreau14174bc2012-04-16 14:34:04 +02009063 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009064 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009065 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009066
9067 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009068 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009069 http_msg_analyzer(msg, &txn->hdr_idx);
9070
9071 /* Still no valid request ? */
9072 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009073 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009074 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009075 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009076 }
9077 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009078 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009079 return 0;
9080 }
9081
9082 /* OK we just got a valid HTTP request. We have some minor
9083 * preparation to perform so that further checks can rely
9084 * on HTTP tests.
9085 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009086
9087 /* If the request was parsed but was too large, we must absolutely
9088 * return an error so that it is not processed. At the moment this
9089 * cannot happen, but if the parsers are to change in the future,
9090 * we want this check to be maintained.
9091 */
9092 if (unlikely(s->req->buf->i + s->req->buf->p >
9093 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9094 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009095 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009096 return 1;
9097 }
9098
Willy Tarreau9b28e032012-10-12 23:49:43 +02009099 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009100 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9101 s->flags |= SN_REDIRECTABLE;
9102
Willy Tarreau506d0502013-07-06 13:29:24 +02009103 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9104 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009105 }
9106
Willy Tarreau506d0502013-07-06 13:29:24 +02009107 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009108 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009109 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009110
9111 /* otherwise everything's ready for the request */
9112 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009113 else {
9114 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009115 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9116 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009117 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009118 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009119 }
9120
9121 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009122 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009123 return 1;
9124}
Willy Tarreau8797c062007-05-07 00:55:35 +02009125
Willy Tarreauc0239e02012-04-16 14:42:55 +02009126#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009127 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 +02009128
Willy Tarreau24e32d82012-04-23 23:55:44 +02009129#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009130 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 +02009131
Willy Tarreau8797c062007-05-07 00:55:35 +02009132
9133/* 1. Check on METHOD
9134 * We use the pre-parsed method if it is known, and store its number as an
9135 * integer. If it is unknown, we use the pointer and the length.
9136 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009137static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009138{
9139 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009140 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009141
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009142 len = strlen(text);
9143 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009144
9145 pattern->val.i = meth;
9146 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009147 trash = get_trash_chunk();
9148 if (trash->size < len) {
9149 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9150 len, trash->size);
9151 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009152 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009153 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009154 pattern->len = len;
9155 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009156 else {
9157 pattern->ptr.str = NULL;
9158 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009159 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009160 return 1;
9161}
9162
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009163/* This function fetches the method of current HTTP request and stores
9164 * it in the global pattern struct as a chunk. There are two possibilities :
9165 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9166 * in <len> and <ptr> is NULL ;
9167 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9168 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009169 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009170 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009171static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009172smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009173 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009174{
9175 int meth;
9176 struct http_txn *txn = l7;
9177
Willy Tarreau24e32d82012-04-23 23:55:44 +02009178 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009179
Willy Tarreau8797c062007-05-07 00:55:35 +02009180 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009181 smp->type = SMP_T_METH;
9182 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009183 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009184 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9185 /* ensure the indexes are not affected */
9186 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009187 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009188 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9189 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009190 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009191 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009192 return 1;
9193}
9194
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009195/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009196static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009197{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009198 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009199 struct pattern_list *lst;
9200 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009201
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009202 list_for_each_entry(lst, &expr->patterns, list) {
9203 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009204
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009205 /* well-known method */
9206 if (pattern->val.i != HTTP_METH_OTHER) {
9207 if (smp->data.meth.meth == pattern->val.i)
9208 return pattern;
9209 else
9210 continue;
9211 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009212
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009213 /* Other method, we must compare the strings */
9214 if (pattern->len != smp->data.meth.str.len)
9215 continue;
9216
9217 icase = pattern->flags & PAT_F_IGNORE_CASE;
9218 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9219 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9220 return pattern;
9221 }
9222 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009223}
9224
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009225static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009226smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009227 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009228{
9229 struct http_txn *txn = l7;
9230 char *ptr;
9231 int len;
9232
Willy Tarreauc0239e02012-04-16 14:42:55 +02009233 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009234
Willy Tarreau8797c062007-05-07 00:55:35 +02009235 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009236 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009237
9238 while ((len-- > 0) && (*ptr++ != '/'));
9239 if (len <= 0)
9240 return 0;
9241
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009242 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009243 smp->data.str.str = ptr;
9244 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009245
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009246 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009247 return 1;
9248}
9249
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009250static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009251smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009252 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009253{
9254 struct http_txn *txn = l7;
9255 char *ptr;
9256 int len;
9257
Willy Tarreauc0239e02012-04-16 14:42:55 +02009258 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009259
Willy Tarreauf26b2522012-12-14 08:33:14 +01009260 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9261 return 0;
9262
Willy Tarreau8797c062007-05-07 00:55:35 +02009263 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009264 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009265
9266 while ((len-- > 0) && (*ptr++ != '/'));
9267 if (len <= 0)
9268 return 0;
9269
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009270 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009271 smp->data.str.str = ptr;
9272 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009273
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009274 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009275 return 1;
9276}
9277
9278/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009279static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009280smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009281 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009282{
9283 struct http_txn *txn = l7;
9284 char *ptr;
9285 int len;
9286
Willy Tarreauc0239e02012-04-16 14:42:55 +02009287 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009288
Willy Tarreauf26b2522012-12-14 08:33:14 +01009289 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9290 return 0;
9291
Willy Tarreau8797c062007-05-07 00:55:35 +02009292 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009293 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009294
Willy Tarreauf853c462012-04-23 18:53:56 +02009295 smp->type = SMP_T_UINT;
9296 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009297 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009298 return 1;
9299}
9300
9301/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009302static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009303smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009304 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009305{
9306 struct http_txn *txn = l7;
9307
Willy Tarreauc0239e02012-04-16 14:42:55 +02009308 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009309
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009310 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009311 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009312 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009313 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009314 return 1;
9315}
9316
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009317static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009318smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009319 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009320{
9321 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009322 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009323
Willy Tarreauc0239e02012-04-16 14:42:55 +02009324 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009325
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009326 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 +02009327 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009328 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009329
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009330 smp->type = SMP_T_IPV4;
9331 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009332 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009333 return 1;
9334}
9335
9336static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009337smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009338 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009339{
9340 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009341 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009342
Willy Tarreauc0239e02012-04-16 14:42:55 +02009343 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009344
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009345 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 +02009346 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9347 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009348
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009349 smp->type = SMP_T_UINT;
9350 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009351 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009352 return 1;
9353}
9354
Willy Tarreau185b5c42012-04-26 15:11:51 +02009355/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9356 * Accepts an optional argument of type string containing the header field name,
9357 * and an optional argument of type signed or unsigned integer to request an
9358 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009359 * headers are considered from the first one. It does not stop on commas and
9360 * returns full lines instead (useful for User-Agent or Date for example).
9361 */
9362static int
9363smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009364 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009365{
9366 struct http_txn *txn = l7;
9367 struct hdr_idx *idx = &txn->hdr_idx;
9368 struct hdr_ctx *ctx = smp->ctx.a[0];
9369 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9370 int occ = 0;
9371 const char *name_str = NULL;
9372 int name_len = 0;
9373
9374 if (!ctx) {
9375 /* first call */
9376 ctx = &static_hdr_ctx;
9377 ctx->idx = 0;
9378 smp->ctx.a[0] = ctx;
9379 }
9380
9381 if (args) {
9382 if (args[0].type != ARGT_STR)
9383 return 0;
9384 name_str = args[0].data.str.str;
9385 name_len = args[0].data.str.len;
9386
9387 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9388 occ = args[1].data.uint;
9389 }
9390
9391 CHECK_HTTP_MESSAGE_FIRST();
9392
9393 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9394 /* search for header from the beginning */
9395 ctx->idx = 0;
9396
9397 if (!occ && !(opt & SMP_OPT_ITERATE))
9398 /* no explicit occurrence and single fetch => last header by default */
9399 occ = -1;
9400
9401 if (!occ)
9402 /* prepare to report multiple occurrences for ACL fetches */
9403 smp->flags |= SMP_F_NOT_LAST;
9404
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009405 smp->type = SMP_T_STR;
9406 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009407 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9408 return 1;
9409
9410 smp->flags &= ~SMP_F_NOT_LAST;
9411 return 0;
9412}
9413
9414/* 6. Check on HTTP header count. The number of occurrences is returned.
9415 * Accepts exactly 1 argument of type string. It does not stop on commas and
9416 * returns full lines instead (useful for User-Agent or Date for example).
9417 */
9418static int
9419smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009420 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009421{
9422 struct http_txn *txn = l7;
9423 struct hdr_idx *idx = &txn->hdr_idx;
9424 struct hdr_ctx ctx;
9425 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9426 int cnt;
9427
9428 if (!args || args->type != ARGT_STR)
9429 return 0;
9430
9431 CHECK_HTTP_MESSAGE_FIRST();
9432
9433 ctx.idx = 0;
9434 cnt = 0;
9435 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9436 cnt++;
9437
9438 smp->type = SMP_T_UINT;
9439 smp->data.uint = cnt;
9440 smp->flags = SMP_F_VOL_HDR;
9441 return 1;
9442}
9443
9444/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9445 * Accepts an optional argument of type string containing the header field name,
9446 * and an optional argument of type signed or unsigned integer to request an
9447 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009448 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009449 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009450static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009451smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009452 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009453{
9454 struct http_txn *txn = l7;
9455 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009456 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009457 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009458 int occ = 0;
9459 const char *name_str = NULL;
9460 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009461
Willy Tarreaua890d072013-04-02 12:01:06 +02009462 if (!ctx) {
9463 /* first call */
9464 ctx = &static_hdr_ctx;
9465 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009466 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009467 }
9468
Willy Tarreau185b5c42012-04-26 15:11:51 +02009469 if (args) {
9470 if (args[0].type != ARGT_STR)
9471 return 0;
9472 name_str = args[0].data.str.str;
9473 name_len = args[0].data.str.len;
9474
9475 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9476 occ = args[1].data.uint;
9477 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009478
Willy Tarreaue333ec92012-04-16 16:26:40 +02009479 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009480
Willy Tarreau185b5c42012-04-26 15:11:51 +02009481 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009482 /* search for header from the beginning */
9483 ctx->idx = 0;
9484
Willy Tarreau185b5c42012-04-26 15:11:51 +02009485 if (!occ && !(opt & SMP_OPT_ITERATE))
9486 /* no explicit occurrence and single fetch => last header by default */
9487 occ = -1;
9488
9489 if (!occ)
9490 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009491 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009492
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009493 smp->type = SMP_T_STR;
9494 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009495 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 +02009496 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009497
Willy Tarreau37406352012-04-23 16:16:37 +02009498 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009499 return 0;
9500}
9501
Willy Tarreauc11416f2007-06-17 16:58:38 +02009502/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009503 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009504 */
9505static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009506smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009507 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009508{
9509 struct http_txn *txn = l7;
9510 struct hdr_idx *idx = &txn->hdr_idx;
9511 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009512 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009513 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009514
Willy Tarreau24e32d82012-04-23 23:55:44 +02009515 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009516 return 0;
9517
Willy Tarreaue333ec92012-04-16 16:26:40 +02009518 CHECK_HTTP_MESSAGE_FIRST();
9519
Willy Tarreau33a7e692007-06-10 19:45:56 +02009520 ctx.idx = 0;
9521 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009522 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 +02009523 cnt++;
9524
Willy Tarreauf853c462012-04-23 18:53:56 +02009525 smp->type = SMP_T_UINT;
9526 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009527 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009528 return 1;
9529}
9530
Willy Tarreau185b5c42012-04-26 15:11:51 +02009531/* Fetch an HTTP header's integer value. The integer value is returned. It
9532 * takes a mandatory argument of type string and an optional one of type int
9533 * to designate a specific occurrence. It returns an unsigned integer, which
9534 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009535 */
9536static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009537smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009538 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009539{
Willy Tarreauef38c392013-07-22 16:29:32 +02009540 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009541
Willy Tarreauf853c462012-04-23 18:53:56 +02009542 if (ret > 0) {
9543 smp->type = SMP_T_UINT;
9544 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9545 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009546
Willy Tarreaud53e2422012-04-16 17:21:11 +02009547 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009548}
9549
Cyril Bonté69fa9922012-10-25 00:01:06 +02009550/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9551 * and an optional one of type int to designate a specific occurrence.
9552 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009553 */
9554static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009555smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009556 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009557{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009558 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009559
Willy Tarreauef38c392013-07-22 16:29:32 +02009560 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009561 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9562 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009563 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009564 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009565 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009566 if (smp->data.str.len < temp->size - 1) {
9567 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9568 temp->str[smp->data.str.len] = '\0';
9569 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9570 smp->type = SMP_T_IPV6;
9571 break;
9572 }
9573 }
9574 }
9575
Willy Tarreaud53e2422012-04-16 17:21:11 +02009576 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009577 if (!(smp->flags & SMP_F_NOT_LAST))
9578 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009579 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009580 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009581}
9582
Willy Tarreau737b0c12007-06-10 21:28:46 +02009583/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9584 * the first '/' after the possible hostname, and ends before the possible '?'.
9585 */
9586static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009587smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009588 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009589{
9590 struct http_txn *txn = l7;
9591 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009592
Willy Tarreauc0239e02012-04-16 14:42:55 +02009593 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009594
Willy Tarreau9b28e032012-10-12 23:49:43 +02009595 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009596 ptr = http_get_path(txn);
9597 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009598 return 0;
9599
9600 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009601 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009602 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009603
9604 while (ptr < end && *ptr != '?')
9605 ptr++;
9606
Willy Tarreauf853c462012-04-23 18:53:56 +02009607 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009608 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009609 return 1;
9610}
9611
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009612/* This produces a concatenation of the first occurrence of the Host header
9613 * followed by the path component if it begins with a slash ('/'). This means
9614 * that '*' will not be added, resulting in exactly the first Host entry.
9615 * If no Host header is found, then the path is returned as-is. The returned
9616 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009617 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009618 */
9619static int
9620smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009621 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009622{
9623 struct http_txn *txn = l7;
9624 char *ptr, *end, *beg;
9625 struct hdr_ctx ctx;
9626
9627 CHECK_HTTP_MESSAGE_FIRST();
9628
9629 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009630 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009631 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009632
9633 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009634 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009635 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009636 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009637 smp->data.str.len = ctx.vlen;
9638
9639 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009640 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009641 beg = http_get_path(txn);
9642 if (!beg)
9643 beg = end;
9644
9645 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9646
9647 if (beg < ptr && *beg == '/') {
9648 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9649 smp->data.str.len += ptr - beg;
9650 }
9651
9652 smp->flags = SMP_F_VOL_1ST;
9653 return 1;
9654}
9655
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009656/* This produces a 32-bit hash of the concatenation of the first occurrence of
9657 * the Host header followed by the path component if it begins with a slash ('/').
9658 * This means that '*' will not be added, resulting in exactly the first Host
9659 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009660 * is hashed using the path hash followed by a full avalanche hash and provides a
9661 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009662 * high-traffic sites without having to store whole paths.
9663 */
9664static int
9665smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009666 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009667{
9668 struct http_txn *txn = l7;
9669 struct hdr_ctx ctx;
9670 unsigned int hash = 0;
9671 char *ptr, *beg, *end;
9672 int len;
9673
9674 CHECK_HTTP_MESSAGE_FIRST();
9675
9676 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009677 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009678 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9679 ptr = ctx.line + ctx.val;
9680 len = ctx.vlen;
9681 while (len--)
9682 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9683 }
9684
9685 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009686 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009687 beg = http_get_path(txn);
9688 if (!beg)
9689 beg = end;
9690
9691 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9692
9693 if (beg < ptr && *beg == '/') {
9694 while (beg < ptr)
9695 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9696 }
9697 hash = full_hash(hash);
9698
9699 smp->type = SMP_T_UINT;
9700 smp->data.uint = hash;
9701 smp->flags = SMP_F_VOL_1ST;
9702 return 1;
9703}
9704
Willy Tarreau4a550602012-12-09 14:53:32 +01009705/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009706 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9707 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9708 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009709 * that in environments where IPv6 is insignificant, truncating the output to
9710 * 8 bytes would still work.
9711 */
9712static int
9713smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009714 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009715{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009716 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009717 struct connection *cli_conn = objt_conn(l4->si[0].end);
9718
9719 if (!cli_conn)
9720 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009721
Willy Tarreauef38c392013-07-22 16:29:32 +02009722 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009723 return 0;
9724
Willy Tarreau47ca5452012-12-23 20:22:19 +01009725 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009726 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9727 temp->len += sizeof(smp->data.uint);
9728
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009729 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009730 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009731 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009732 temp->len += 4;
9733 break;
9734 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009735 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009736 temp->len += 16;
9737 break;
9738 default:
9739 return 0;
9740 }
9741
9742 smp->data.str = *temp;
9743 smp->type = SMP_T_BIN;
9744 return 1;
9745}
9746
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009747static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009748smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009749 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009750{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009751 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9752 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9753 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009754
Willy Tarreau24e32d82012-04-23 23:55:44 +02009755 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009756
Willy Tarreauf853c462012-04-23 18:53:56 +02009757 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009758 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009759 return 1;
9760}
9761
Willy Tarreau7f18e522010-10-22 20:04:13 +02009762/* return a valid test if the current request is the first one on the connection */
9763static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009764smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009765 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009766{
9767 if (!s)
9768 return 0;
9769
Willy Tarreauf853c462012-04-23 18:53:56 +02009770 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009771 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009772 return 1;
9773}
9774
Willy Tarreau34db1082012-04-19 17:16:54 +02009775/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009776static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009777smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009778 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009779{
9780
Willy Tarreau24e32d82012-04-23 23:55:44 +02009781 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009782 return 0;
9783
Willy Tarreauc0239e02012-04-16 14:42:55 +02009784 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009785
Willy Tarreauc0239e02012-04-16 14:42:55 +02009786 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009787 return 0;
9788
Willy Tarreauf853c462012-04-23 18:53:56 +02009789 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009790 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009791 return 1;
9792}
Willy Tarreau8797c062007-05-07 00:55:35 +02009793
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009794/* Accepts exactly 1 argument of type userlist */
9795static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009796smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009797 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009798{
9799
9800 if (!args || args->type != ARGT_USR)
9801 return 0;
9802
9803 CHECK_HTTP_MESSAGE_FIRST();
9804
9805 if (!get_http_auth(l4))
9806 return 0;
9807
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009808 /* if the user does not belong to the userlist or has a wrong password,
9809 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009810 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009811 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009812 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9813 return 0;
9814
9815 /* pat_match_auth() will need the user list */
9816 smp->ctx.a[0] = args->data.usr;
9817
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009818 smp->type = SMP_T_STR;
9819 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009820 smp->data.str.str = l4->txn.auth.user;
9821 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009822
9823 return 1;
9824}
9825
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009826/* Try to find the next occurrence of a cookie name in a cookie header value.
9827 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9828 * the cookie value is returned into *value and *value_l, and the function
9829 * returns a pointer to the next pointer to search from if the value was found.
9830 * Otherwise if the cookie was not found, NULL is returned and neither value
9831 * nor value_l are touched. The input <hdr> string should first point to the
9832 * header's value, and the <hdr_end> pointer must point to the first character
9833 * not part of the value. <list> must be non-zero if value may represent a list
9834 * of values (cookie headers). This makes it faster to abort parsing when no
9835 * list is expected.
9836 */
9837static char *
9838extract_cookie_value(char *hdr, const char *hdr_end,
9839 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009840 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009841{
9842 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9843 char *next;
9844
9845 /* we search at least a cookie name followed by an equal, and more
9846 * generally something like this :
9847 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9848 */
9849 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9850 /* Iterate through all cookies on this line */
9851
9852 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9853 att_beg++;
9854
9855 /* find att_end : this is the first character after the last non
9856 * space before the equal. It may be equal to hdr_end.
9857 */
9858 equal = att_end = att_beg;
9859
9860 while (equal < hdr_end) {
9861 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9862 break;
9863 if (http_is_spht[(unsigned char)*equal++])
9864 continue;
9865 att_end = equal;
9866 }
9867
9868 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9869 * is between <att_beg> and <equal>, both may be identical.
9870 */
9871
9872 /* look for end of cookie if there is an equal sign */
9873 if (equal < hdr_end && *equal == '=') {
9874 /* look for the beginning of the value */
9875 val_beg = equal + 1;
9876 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9877 val_beg++;
9878
9879 /* find the end of the value, respecting quotes */
9880 next = find_cookie_value_end(val_beg, hdr_end);
9881
9882 /* make val_end point to the first white space or delimitor after the value */
9883 val_end = next;
9884 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9885 val_end--;
9886 } else {
9887 val_beg = val_end = next = equal;
9888 }
9889
9890 /* We have nothing to do with attributes beginning with '$'. However,
9891 * they will automatically be removed if a header before them is removed,
9892 * since they're supposed to be linked together.
9893 */
9894 if (*att_beg == '$')
9895 continue;
9896
9897 /* Ignore cookies with no equal sign */
9898 if (equal == next)
9899 continue;
9900
9901 /* Now we have the cookie name between att_beg and att_end, and
9902 * its value between val_beg and val_end.
9903 */
9904
9905 if (att_end - att_beg == cookie_name_l &&
9906 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9907 /* let's return this value and indicate where to go on from */
9908 *value = val_beg;
9909 *value_l = val_end - val_beg;
9910 return next + 1;
9911 }
9912
9913 /* Set-Cookie headers only have the name in the first attr=value part */
9914 if (!list)
9915 break;
9916 }
9917
9918 return NULL;
9919}
9920
William Lallemanda43ba4e2014-01-28 18:14:25 +01009921/* Fetch a captured HTTP request header. The index is the position of
9922 * the "capture" option in the configuration file
9923 */
9924static int
9925smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9926 const struct arg *args, struct sample *smp, const char *kw)
9927{
9928 struct proxy *fe = l4->fe;
9929 struct http_txn *txn = l7;
9930 int idx;
9931
9932 if (!args || args->type != ARGT_UINT)
9933 return 0;
9934
9935 idx = args->data.uint;
9936
9937 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9938 return 0;
9939
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009940 smp->type = SMP_T_STR;
9941 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009942 smp->data.str.str = txn->req.cap[idx];
9943 smp->data.str.len = strlen(txn->req.cap[idx]);
9944
9945 return 1;
9946}
9947
9948/* Fetch a captured HTTP response header. The index is the position of
9949 * the "capture" option in the configuration file
9950 */
9951static int
9952smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9953 const struct arg *args, struct sample *smp, const char *kw)
9954{
9955 struct proxy *fe = l4->fe;
9956 struct http_txn *txn = l7;
9957 int idx;
9958
9959 if (!args || args->type != ARGT_UINT)
9960 return 0;
9961
9962 idx = args->data.uint;
9963
9964 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9965 return 0;
9966
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009967 smp->type = SMP_T_STR;
9968 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009969 smp->data.str.str = txn->rsp.cap[idx];
9970 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9971
9972 return 1;
9973}
9974
William Lallemand65ad6e12014-01-31 15:08:02 +01009975/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9976static int
9977smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9978 const struct arg *args, struct sample *smp, const char *kw)
9979{
9980 struct chunk *temp;
9981 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009982 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009983
9984 if (!txn->uri)
9985 return 0;
9986
William Lallemand96a77852014-02-05 00:30:02 +01009987 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009988
William Lallemand96a77852014-02-05 00:30:02 +01009989 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9990 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009991
William Lallemand96a77852014-02-05 00:30:02 +01009992 temp = get_trash_chunk();
9993 temp->str = txn->uri;
9994 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009995 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009996 smp->type = SMP_T_STR;
9997 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009998
9999 return 1;
10000
10001}
10002
10003/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10004static int
10005smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10006 const struct arg *args, struct sample *smp, const char *kw)
10007{
10008 struct chunk *temp;
10009 struct http_txn *txn = l7;
10010 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010011
10012 if (!txn->uri)
10013 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010014
William Lallemand65ad6e12014-01-31 15:08:02 +010010015 ptr = txn->uri;
10016
10017 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10018 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010019
William Lallemand65ad6e12014-01-31 15:08:02 +010010020 if (!*ptr)
10021 return 0;
10022
10023 ptr++; /* skip the space */
10024
10025 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010026 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010027 if (!ptr)
10028 return 0;
10029 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10030 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010031
10032 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010033 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010034 smp->type = SMP_T_STR;
10035 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010036
10037 return 1;
10038}
10039
10040
Willy Tarreaue333ec92012-04-16 16:26:40 +020010041/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010042 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010043 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010044 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010045 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010046 * Accepts exactly 1 argument of type string. If the input options indicate
10047 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010048 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010049 */
10050static int
Willy Tarreau51539362012-05-08 12:46:28 +020010051smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010052 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010053{
10054 struct http_txn *txn = l7;
10055 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010056 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010057 const struct http_msg *msg;
10058 const char *hdr_name;
10059 int hdr_name_len;
10060 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010061 int occ = 0;
10062 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010063
Willy Tarreau24e32d82012-04-23 23:55:44 +020010064 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010065 return 0;
10066
Willy Tarreaua890d072013-04-02 12:01:06 +020010067 if (!ctx) {
10068 /* first call */
10069 ctx = &static_hdr_ctx;
10070 ctx->idx = 0;
10071 smp->ctx.a[2] = ctx;
10072 }
10073
Willy Tarreaue333ec92012-04-16 16:26:40 +020010074 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010075
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010076 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010077 msg = &txn->req;
10078 hdr_name = "Cookie";
10079 hdr_name_len = 6;
10080 } else {
10081 msg = &txn->rsp;
10082 hdr_name = "Set-Cookie";
10083 hdr_name_len = 10;
10084 }
10085
Willy Tarreau28376d62012-04-26 21:26:10 +020010086 if (!occ && !(opt & SMP_OPT_ITERATE))
10087 /* no explicit occurrence and single fetch => last cookie by default */
10088 occ = -1;
10089
10090 /* OK so basically here, either we want only one value and it's the
10091 * last one, or we want to iterate over all of them and we fetch the
10092 * next one.
10093 */
10094
Willy Tarreau9b28e032012-10-12 23:49:43 +020010095 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010096 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010097 /* search for the header from the beginning, we must first initialize
10098 * the search parameters.
10099 */
Willy Tarreau37406352012-04-23 16:16:37 +020010100 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010101 ctx->idx = 0;
10102 }
10103
Willy Tarreau28376d62012-04-26 21:26:10 +020010104 smp->flags |= SMP_F_VOL_HDR;
10105
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010106 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010107 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10108 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010109 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10110 goto out;
10111
Willy Tarreau24e32d82012-04-23 23:55:44 +020010112 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010113 continue;
10114
Willy Tarreau37406352012-04-23 16:16:37 +020010115 smp->ctx.a[0] = ctx->line + ctx->val;
10116 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010117 }
10118
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010119 smp->type = SMP_T_STR;
10120 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010121 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010122 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010123 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010124 &smp->data.str.str,
10125 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010126 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010127 found = 1;
10128 if (occ >= 0) {
10129 /* one value was returned into smp->data.str.{str,len} */
10130 smp->flags |= SMP_F_NOT_LAST;
10131 return 1;
10132 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010133 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010134 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010135 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010136 /* all cookie headers and values were scanned. If we're looking for the
10137 * last occurrence, we may return it now.
10138 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010139 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010140 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010141 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010142}
10143
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010144/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010145 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010146 * multiple cookies may be parsed on the same line. The returned sample is of
10147 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010148 */
10149static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010150smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010151 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010152{
10153 struct http_txn *txn = l7;
10154 struct hdr_idx *idx = &txn->hdr_idx;
10155 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010156 const struct http_msg *msg;
10157 const char *hdr_name;
10158 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010159 int cnt;
10160 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010161 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010162
Willy Tarreau24e32d82012-04-23 23:55:44 +020010163 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010164 return 0;
10165
Willy Tarreaue333ec92012-04-16 16:26:40 +020010166 CHECK_HTTP_MESSAGE_FIRST();
10167
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010168 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010169 msg = &txn->req;
10170 hdr_name = "Cookie";
10171 hdr_name_len = 6;
10172 } else {
10173 msg = &txn->rsp;
10174 hdr_name = "Set-Cookie";
10175 hdr_name_len = 10;
10176 }
10177
Willy Tarreau9b28e032012-10-12 23:49:43 +020010178 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010179 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010180 ctx.idx = 0;
10181 cnt = 0;
10182
10183 while (1) {
10184 /* Note: val_beg == NULL every time we need to fetch a new header */
10185 if (!val_beg) {
10186 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10187 break;
10188
Willy Tarreau24e32d82012-04-23 23:55:44 +020010189 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010190 continue;
10191
10192 val_beg = ctx.line + ctx.val;
10193 val_end = val_beg + ctx.vlen;
10194 }
10195
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010196 smp->type = SMP_T_STR;
10197 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010198 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010199 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010200 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010201 &smp->data.str.str,
10202 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010203 cnt++;
10204 }
10205 }
10206
Willy Tarreaub169eba2013-12-16 15:14:43 +010010207 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010208 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010209 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010210 return 1;
10211}
10212
Willy Tarreau51539362012-05-08 12:46:28 +020010213/* Fetch an cookie's integer value. The integer value is returned. It
10214 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10215 */
10216static int
10217smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010218 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010219{
Willy Tarreauef38c392013-07-22 16:29:32 +020010220 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010221
10222 if (ret > 0) {
10223 smp->type = SMP_T_UINT;
10224 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10225 }
10226
10227 return ret;
10228}
10229
Willy Tarreau8797c062007-05-07 00:55:35 +020010230/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010231/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010232/************************************************************************/
10233
David Cournapeau16023ee2010-12-23 20:55:41 +090010234/*
10235 * Given a path string and its length, find the position of beginning of the
10236 * query string. Returns NULL if no query string is found in the path.
10237 *
10238 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10239 *
10240 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10241 */
bedis4c75cca2012-10-05 08:38:24 +020010242static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010243{
10244 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010245
bedis4c75cca2012-10-05 08:38:24 +020010246 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010247 return p ? p + 1 : NULL;
10248}
10249
bedis4c75cca2012-10-05 08:38:24 +020010250static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010251{
bedis4c75cca2012-10-05 08:38:24 +020010252 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010253}
10254
10255/*
10256 * Given a url parameter, find the starting position of the first occurence,
10257 * or NULL if the parameter is not found.
10258 *
10259 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10260 * the function will return query_string+8.
10261 */
10262static char*
10263find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010264 char* url_param_name, size_t url_param_name_l,
10265 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010266{
10267 char *pos, *last;
10268
10269 pos = query_string;
10270 last = query_string + query_string_l - url_param_name_l - 1;
10271
10272 while (pos <= last) {
10273 if (pos[url_param_name_l] == '=') {
10274 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10275 return pos;
10276 pos += url_param_name_l + 1;
10277 }
bedis4c75cca2012-10-05 08:38:24 +020010278 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010279 pos++;
10280 pos++;
10281 }
10282 return NULL;
10283}
10284
10285/*
10286 * Given a url parameter name, returns its value and size into *value and
10287 * *value_l respectively, and returns non-zero. If the parameter is not found,
10288 * zero is returned and value/value_l are not touched.
10289 */
10290static int
10291find_url_param_value(char* path, size_t path_l,
10292 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010293 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010294{
10295 char *query_string, *qs_end;
10296 char *arg_start;
10297 char *value_start, *value_end;
10298
bedis4c75cca2012-10-05 08:38:24 +020010299 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010300 if (!query_string)
10301 return 0;
10302
10303 qs_end = path + path_l;
10304 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010305 url_param_name, url_param_name_l,
10306 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010307 if (!arg_start)
10308 return 0;
10309
10310 value_start = arg_start + url_param_name_l + 1;
10311 value_end = value_start;
10312
bedis4c75cca2012-10-05 08:38:24 +020010313 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010314 value_end++;
10315
10316 *value = value_start;
10317 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010318 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010319}
10320
10321static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010322smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010323 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010324{
bedis4c75cca2012-10-05 08:38:24 +020010325 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010326 struct http_txn *txn = l7;
10327 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010328
bedis4c75cca2012-10-05 08:38:24 +020010329 if (!args || args[0].type != ARGT_STR ||
10330 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010331 return 0;
10332
10333 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010334
bedis4c75cca2012-10-05 08:38:24 +020010335 if (args[1].type)
10336 delim = *args[1].data.str.str;
10337
Willy Tarreau9b28e032012-10-12 23:49:43 +020010338 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010339 args->data.str.str, args->data.str.len,
10340 &smp->data.str.str, &smp->data.str.len,
10341 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010342 return 0;
10343
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010344 smp->type = SMP_T_STR;
10345 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010346 return 1;
10347}
10348
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010349/* Return the signed integer value for the specified url parameter (see url_param
10350 * above).
10351 */
10352static int
10353smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010354 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010355{
Willy Tarreauef38c392013-07-22 16:29:32 +020010356 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010357
10358 if (ret > 0) {
10359 smp->type = SMP_T_UINT;
10360 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10361 }
10362
10363 return ret;
10364}
10365
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010366/* This produces a 32-bit hash of the concatenation of the first occurrence of
10367 * the Host header followed by the path component if it begins with a slash ('/').
10368 * This means that '*' will not be added, resulting in exactly the first Host
10369 * entry. If no Host header is found, then the path is used. The resulting value
10370 * is hashed using the url hash followed by a full avalanche hash and provides a
10371 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10372 * high-traffic sites without having to store whole paths.
10373 * this differs from the base32 functions in that it includes the url parameters
10374 * as well as the path
10375 */
10376static int
10377smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010378 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010379{
10380 struct http_txn *txn = l7;
10381 struct hdr_ctx ctx;
10382 unsigned int hash = 0;
10383 char *ptr, *beg, *end;
10384 int len;
10385
10386 CHECK_HTTP_MESSAGE_FIRST();
10387
10388 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010389 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010390 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10391 ptr = ctx.line + ctx.val;
10392 len = ctx.vlen;
10393 while (len--)
10394 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10395 }
10396
10397 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010398 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 +000010399 beg = http_get_path(txn);
10400 if (!beg)
10401 beg = end;
10402
10403 for (ptr = beg; ptr < end ; ptr++);
10404
10405 if (beg < ptr && *beg == '/') {
10406 while (beg < ptr)
10407 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10408 }
10409 hash = full_hash(hash);
10410
10411 smp->type = SMP_T_UINT;
10412 smp->data.uint = hash;
10413 smp->flags = SMP_F_VOL_1ST;
10414 return 1;
10415}
10416
10417/* This concatenates the source address with the 32-bit hash of the Host and
10418 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10419 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10420 * on the source address length. The URL hash is stored before the address so
10421 * that in environments where IPv6 is insignificant, truncating the output to
10422 * 8 bytes would still work.
10423 */
10424static int
10425smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010426 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010427{
10428 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010429 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010430
Willy Tarreaue155ec22013-11-18 18:33:22 +010010431 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010432 return 0;
10433
10434 temp = get_trash_chunk();
10435 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10436 temp->len += sizeof(smp->data.uint);
10437
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010438 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010439 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010440 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010441 temp->len += 4;
10442 break;
10443 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010444 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010445 temp->len += 16;
10446 break;
10447 default:
10448 return 0;
10449 }
10450
10451 smp->data.str = *temp;
10452 smp->type = SMP_T_BIN;
10453 return 1;
10454}
10455
Willy Tarreau185b5c42012-04-26 15:11:51 +020010456/* This function is used to validate the arguments passed to any "hdr" fetch
10457 * keyword. These keywords support an optional positive or negative occurrence
10458 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10459 * is assumed that the types are already the correct ones. Returns 0 on error,
10460 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10461 * error message in case of error, that the caller is responsible for freeing.
10462 * The initial location must either be freeable or NULL.
10463 */
10464static int val_hdr(struct arg *arg, char **err_msg)
10465{
10466 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010467 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010468 return 0;
10469 }
10470 return 1;
10471}
10472
Willy Tarreau276fae92013-07-25 14:36:01 +020010473/* takes an UINT value on input supposed to represent the time since EPOCH,
10474 * adds an optional offset found in args[0] and emits a string representing
10475 * the date in RFC-1123/5322 format.
10476 */
10477static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10478{
10479 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10480 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10481 struct chunk *temp;
10482 struct tm *tm;
10483 time_t curr_date = smp->data.uint;
10484
10485 /* add offset */
10486 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10487 curr_date += args[0].data.sint;
10488
10489 tm = gmtime(&curr_date);
10490
10491 temp = get_trash_chunk();
10492 temp->len = snprintf(temp->str, temp->size - temp->len,
10493 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10494 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10495 tm->tm_hour, tm->tm_min, tm->tm_sec);
10496
10497 smp->data.str = *temp;
10498 smp->type = SMP_T_STR;
10499 return 1;
10500}
10501
Thierry FOURNIERad903512014-04-11 17:51:01 +020010502/* Match language range with language tag. RFC2616 14.4:
10503 *
10504 * A language-range matches a language-tag if it exactly equals
10505 * the tag, or if it exactly equals a prefix of the tag such
10506 * that the first tag character following the prefix is "-".
10507 *
10508 * Return 1 if the strings match, else return 0.
10509 */
10510static inline int language_range_match(const char *range, int range_len,
10511 const char *tag, int tag_len)
10512{
10513 const char *end = range + range_len;
10514 const char *tend = tag + tag_len;
10515 while (range < end) {
10516 if (*range == '-' && tag == tend)
10517 return 1;
10518 if (*range != *tag || tag == tend)
10519 return 0;
10520 range++;
10521 tag++;
10522 }
10523 /* Return true only if the last char of the tag is matched. */
10524 return tag == tend;
10525}
10526
10527/* Arguments: The list of expected value, the number of parts returned and the separator */
10528static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
10529{
10530 const char *al = smp->data.str.str;
10531 const char *end = al + smp->data.str.len;
10532 const char *token;
10533 int toklen;
10534 int qvalue;
10535 const char *str;
10536 const char *w;
10537 int best_q = 0;
10538
10539 /* Set the constant to the sample, because the output of the
10540 * function will be peek in the constant configuration string.
10541 */
10542 smp->flags |= SMP_F_CONST;
10543 smp->data.str.size = 0;
10544 smp->data.str.str = "";
10545 smp->data.str.len = 0;
10546
10547 /* Parse the accept language */
10548 while (1) {
10549
10550 /* Jump spaces, quit if the end is detected. */
10551 while (al < end && isspace(*al))
10552 al++;
10553 if (al >= end)
10554 break;
10555
10556 /* Start of the fisrt word. */
10557 token = al;
10558
10559 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
10560 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
10561 al++;
10562 if (al == token)
10563 goto expect_comma;
10564
10565 /* Length of the token. */
10566 toklen = al - token;
10567 qvalue = 1000;
10568
10569 /* Check if the token exists in the list. If the token not exists,
10570 * jump to the next token.
10571 */
10572 str = args[0].data.str.str;
10573 w = str;
10574 while (1) {
10575 if (*str == ';' || *str == '\0') {
10576 if (language_range_match(token, toklen, w, str-w))
10577 goto look_for_q;
10578 if (*str == '\0')
10579 goto expect_comma;
10580 w = str + 1;
10581 }
10582 str++;
10583 }
10584 goto expect_comma;
10585
10586look_for_q:
10587
10588 /* Jump spaces, quit if the end is detected. */
10589 while (al < end && isspace(*al))
10590 al++;
10591 if (al >= end)
10592 goto process_value;
10593
10594 /* If ',' is found, process the result */
10595 if (*al == ',')
10596 goto process_value;
10597
10598 /* If the character is different from ';', look
10599 * for the end of the header part in best effort.
10600 */
10601 if (*al != ';')
10602 goto expect_comma;
10603
10604 /* Assumes that the char is ';', now expect "q=". */
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 'q'. If no 'q', continue in best effort */
10614 if (*al != 'q')
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 /* Expect '='. If no '=', continue in best effort */
10625 if (*al != '=')
10626 goto process_value;
10627 al++;
10628
10629 /* Jump spaces, process value if the end is detected. */
10630 while (al < end && isspace(*al))
10631 al++;
10632 if (al >= end)
10633 goto process_value;
10634
10635 /* Parse the q value. */
10636 qvalue = parse_qvalue(al, &al);
10637
10638process_value:
10639
10640 /* If the new q value is the best q value, then store the associated
10641 * language in the response. If qvalue is the biggest value (1000),
10642 * break the process.
10643 */
10644 if (qvalue > best_q) {
10645 smp->data.str.str = (char *)w;
10646 smp->data.str.len = str - w;
10647 if (qvalue >= 1000)
10648 break;
10649 best_q = qvalue;
10650 }
10651
10652expect_comma:
10653
10654 /* Expect comma or end. If the end is detected, quit the loop. */
10655 while (al < end && *al != ',')
10656 al++;
10657 if (al >= end)
10658 break;
10659
10660 /* Comma is found, jump it and restart the analyzer. */
10661 al++;
10662 }
10663
10664 /* Set default value if required. */
10665 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
10666 smp->data.str.str = args[1].data.str.str;
10667 smp->data.str.len = args[1].data.str.len;
10668 }
10669
10670 /* Return true only if a matching language was found. */
10671 return smp->data.str.len != 0;
10672}
10673
Willy Tarreau4a568972010-05-12 08:08:50 +020010674/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010675/* All supported ACL keywords must be declared here. */
10676/************************************************************************/
10677
10678/* Note: must not be declared <const> as its list will be overwritten.
10679 * Please take care of keeping this list alphabetically sorted.
10680 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010681static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010682 { "base", "base", PAT_MATCH_STR },
10683 { "base_beg", "base", PAT_MATCH_BEG },
10684 { "base_dir", "base", PAT_MATCH_DIR },
10685 { "base_dom", "base", PAT_MATCH_DOM },
10686 { "base_end", "base", PAT_MATCH_END },
10687 { "base_len", "base", PAT_MATCH_LEN },
10688 { "base_reg", "base", PAT_MATCH_REG },
10689 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010690
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010691 { "cook", "req.cook", PAT_MATCH_STR },
10692 { "cook_beg", "req.cook", PAT_MATCH_BEG },
10693 { "cook_dir", "req.cook", PAT_MATCH_DIR },
10694 { "cook_dom", "req.cook", PAT_MATCH_DOM },
10695 { "cook_end", "req.cook", PAT_MATCH_END },
10696 { "cook_len", "req.cook", PAT_MATCH_LEN },
10697 { "cook_reg", "req.cook", PAT_MATCH_REG },
10698 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010699
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010700 { "hdr", "req.hdr", PAT_MATCH_STR },
10701 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
10702 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
10703 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
10704 { "hdr_end", "req.hdr", PAT_MATCH_END },
10705 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
10706 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
10707 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010708
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010709 /* these two declarations uses strings with list storage (in place
10710 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
10711 * and delete functions are relative to the list management. The parse
10712 * and match method are related to the corresponding fetch methods. This
10713 * is very particular ACL declaration mode.
10714 */
10715 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
10716 { "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 +020010717
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010718 { "path", "path", PAT_MATCH_STR },
10719 { "path_beg", "path", PAT_MATCH_BEG },
10720 { "path_dir", "path", PAT_MATCH_DIR },
10721 { "path_dom", "path", PAT_MATCH_DOM },
10722 { "path_end", "path", PAT_MATCH_END },
10723 { "path_len", "path", PAT_MATCH_LEN },
10724 { "path_reg", "path", PAT_MATCH_REG },
10725 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010726
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010727 { "req_ver", "req.ver", PAT_MATCH_STR },
10728 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010729
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010730 { "scook", "res.cook", PAT_MATCH_STR },
10731 { "scook_beg", "res.cook", PAT_MATCH_BEG },
10732 { "scook_dir", "res.cook", PAT_MATCH_DIR },
10733 { "scook_dom", "res.cook", PAT_MATCH_DOM },
10734 { "scook_end", "res.cook", PAT_MATCH_END },
10735 { "scook_len", "res.cook", PAT_MATCH_LEN },
10736 { "scook_reg", "res.cook", PAT_MATCH_REG },
10737 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010738
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010739 { "shdr", "res.hdr", PAT_MATCH_STR },
10740 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
10741 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
10742 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
10743 { "shdr_end", "res.hdr", PAT_MATCH_END },
10744 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
10745 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
10746 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010747
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010748 { "url", "url", PAT_MATCH_STR },
10749 { "url_beg", "url", PAT_MATCH_BEG },
10750 { "url_dir", "url", PAT_MATCH_DIR },
10751 { "url_dom", "url", PAT_MATCH_DOM },
10752 { "url_end", "url", PAT_MATCH_END },
10753 { "url_len", "url", PAT_MATCH_LEN },
10754 { "url_reg", "url", PAT_MATCH_REG },
10755 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010756
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010757 { "urlp", "urlp", PAT_MATCH_STR },
10758 { "urlp_beg", "urlp", PAT_MATCH_BEG },
10759 { "urlp_dir", "urlp", PAT_MATCH_DIR },
10760 { "urlp_dom", "urlp", PAT_MATCH_DOM },
10761 { "urlp_end", "urlp", PAT_MATCH_END },
10762 { "urlp_len", "urlp", PAT_MATCH_LEN },
10763 { "urlp_reg", "urlp", PAT_MATCH_REG },
10764 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010765
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010766 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010767}};
10768
10769/************************************************************************/
10770/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010771/************************************************************************/
10772/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010773static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010774 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010775 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10776 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10777
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010778 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10779 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemand65ad6e12014-01-31 15:08:02 +010010780
William Lallemanda43ba4e2014-01-28 18:14:25 +010010781 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010782 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
10783 { "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 +010010784
Willy Tarreau409bcde2013-01-08 00:31:00 +010010785 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10786 * are only here to match the ACL's name, are request-only and are used
10787 * for ACL compatibility only.
10788 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010789 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10790 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010791 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10792 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10793
10794 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10795 * only here to match the ACL's name, are request-only and are used for
10796 * ACL compatibility only.
10797 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010798 { "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 +010010799 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10800 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10801 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10802
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010803 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010804 { "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 +010010805 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010806 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010807 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010808
10809 /* HTTP protocol on the request path */
10810 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010811 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010812
10813 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010814 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10815 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010816
10817 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010818 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10819 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010820
Willy Tarreau18ed2562013-01-14 15:56:36 +010010821 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010822 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010823 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10824 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10825
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010826 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010827 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010828 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010829 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10830 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10831 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10832
10833 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010834 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010835 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10836 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10837
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010838 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010839 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010840 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010841 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10842 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10843 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10844
Willy Tarreau409bcde2013-01-08 00:31:00 +010010845 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010846 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010847 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10848 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010849 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010850
10851 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010852 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010853 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10854 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10855 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10856
10857 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010858 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010859 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10860 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010861 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10862 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010863 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10864 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010865 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10866 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010867}};
10868
Willy Tarreau8797c062007-05-07 00:55:35 +020010869
Willy Tarreau276fae92013-07-25 14:36:01 +020010870/* Note: must not be declared <const> as its list will be overwritten */
10871static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020010872 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
10873 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020010874 { NULL, NULL, 0, 0, 0 },
10875}};
10876
Willy Tarreau8797c062007-05-07 00:55:35 +020010877__attribute__((constructor))
10878static void __http_protocol_init(void)
10879{
10880 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010881 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010882 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010883}
10884
10885
Willy Tarreau58f10d72006-12-04 02:26:12 +010010886/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010887 * Local variables:
10888 * c-indent-level: 8
10889 * c-basic-offset: 8
10890 * End:
10891 */