blob: 105b9a8d8eba08f3f23dad9caf4aaa23bce98e1c [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 Tarreaue988a792010-01-04 21:13:14 +0100444 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
445 */
446#if defined(DEBUG_FSM)
447static void http_silent_debug(int line, struct session *s)
448{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100449 chunk_printf(&trash,
450 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
451 line,
452 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
453 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
454 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100455
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100456 chunk_printf(&trash,
457 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
458 line,
459 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
460 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
461 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100462}
463#else
464#define http_silent_debug(l,s) do { } while (0)
465#endif
466
467/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100468 * Adds a header and its CRLF at the tail of the message's buffer, just before
469 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100470 * The header is also automatically added to the index <hdr_idx>, and the end
471 * of headers is automatically adjusted. The number of bytes added is returned
472 * on success, otherwise <0 is returned indicating an error.
473 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100474int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475{
476 int bytes, len;
477
478 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200479 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100480 if (!bytes)
481 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100482 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
484}
485
486/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100487 * Adds a header and its CRLF at the tail of the message's buffer, just before
488 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 * the buffer is only opened and the space reserved, but nothing is copied.
490 * The header is also automatically added to the index <hdr_idx>, and the end
491 * of headers is automatically adjusted. The number of bytes added is returned
492 * on success, otherwise <0 is returned indicating an error.
493 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100494int http_header_add_tail2(struct http_msg *msg,
495 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100496{
497 int bytes;
498
Willy Tarreau9b28e032012-10-12 23:49:43 +0200499 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100500 if (!bytes)
501 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100502 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100503 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
504}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505
506/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100507 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
508 * If so, returns the position of the first non-space character relative to
509 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
510 * to return a pointer to the place after the first space. Returns 0 if the
511 * header name does not match. Checks are case-insensitive.
512 */
513int http_header_match2(const char *hdr, const char *end,
514 const char *name, int len)
515{
516 const char *val;
517
518 if (hdr + len >= end)
519 return 0;
520 if (hdr[len] != ':')
521 return 0;
522 if (strncasecmp(hdr, name, len) != 0)
523 return 0;
524 val = hdr + len + 1;
525 while (val < end && HTTP_IS_SPHT(*val))
526 val++;
527 if ((val >= end) && (len + 2 <= end - hdr))
528 return len + 2; /* we may replace starting from second space */
529 return val - hdr;
530}
531
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200532/* Find the first or next occurrence of header <name> in message buffer <sol>
533 * using headers index <idx>, and return it in the <ctx> structure. This
534 * structure holds everything necessary to use the header and find next
535 * occurrence. If its <idx> member is 0, the header is searched from the
536 * beginning. Otherwise, the next occurrence is returned. The function returns
537 * 1 when it finds a value, and 0 when there is no more. It is very similar to
538 * http_find_header2() except that it is designed to work with full-line headers
539 * whose comma is not a delimiter but is part of the syntax. As a special case,
540 * if ctx->val is NULL when searching for a new values of a header, the current
541 * header is rescanned. This allows rescanning after a header deletion.
542 */
543int http_find_full_header2(const char *name, int len,
544 char *sol, struct hdr_idx *idx,
545 struct hdr_ctx *ctx)
546{
547 char *eol, *sov;
548 int cur_idx, old_idx;
549
550 cur_idx = ctx->idx;
551 if (cur_idx) {
552 /* We have previously returned a header, let's search another one */
553 sol = ctx->line;
554 eol = sol + idx->v[cur_idx].len;
555 goto next_hdr;
556 }
557
558 /* first request for this header */
559 sol += hdr_idx_first_pos(idx);
560 old_idx = 0;
561 cur_idx = hdr_idx_first_idx(idx);
562 while (cur_idx) {
563 eol = sol + idx->v[cur_idx].len;
564
565 if (len == 0) {
566 /* No argument was passed, we want any header.
567 * To achieve this, we simply build a fake request. */
568 while (sol + len < eol && sol[len] != ':')
569 len++;
570 name = sol;
571 }
572
573 if ((len < eol - sol) &&
574 (sol[len] == ':') &&
575 (strncasecmp(sol, name, len) == 0)) {
576 ctx->del = len;
577 sov = sol + len + 1;
578 while (sov < eol && http_is_lws[(unsigned char)*sov])
579 sov++;
580
581 ctx->line = sol;
582 ctx->prev = old_idx;
583 ctx->idx = cur_idx;
584 ctx->val = sov - sol;
585 ctx->tws = 0;
586 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
587 eol--;
588 ctx->tws++;
589 }
590 ctx->vlen = eol - sov;
591 return 1;
592 }
593 next_hdr:
594 sol = eol + idx->v[cur_idx].cr + 1;
595 old_idx = cur_idx;
596 cur_idx = idx->v[cur_idx].next;
597 }
598 return 0;
599}
600
Willy Tarreau68085d82010-01-18 14:54:04 +0100601/* Find the end of the header value contained between <s> and <e>. See RFC2616,
602 * par 2.2 for more information. Note that it requires a valid header to return
603 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100605char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200606{
607 int quoted, qdpair;
608
609 quoted = qdpair = 0;
610 for (; s < e; s++) {
611 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200612 else if (quoted) {
613 if (*s == '\\') qdpair = 1;
614 else if (*s == '"') quoted = 0;
615 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 else if (*s == '"') quoted = 1;
617 else if (*s == ',') return s;
618 }
619 return s;
620}
621
622/* Find the first or next occurrence of header <name> in message buffer <sol>
623 * using headers index <idx>, and return it in the <ctx> structure. This
624 * structure holds everything necessary to use the header and find next
625 * occurrence. If its <idx> member is 0, the header is searched from the
626 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 * 1 when it finds a value, and 0 when there is no more. It is designed to work
628 * with headers defined as comma-separated lists. As a special case, if ctx->val
629 * is NULL when searching for a new values of a header, the current header is
630 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200631 */
632int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200634 struct hdr_ctx *ctx)
635{
Willy Tarreau68085d82010-01-18 14:54:04 +0100636 char *eol, *sov;
637 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 cur_idx = ctx->idx;
640 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200641 /* We have previously returned a value, let's search
642 * another one on the same line.
643 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200644 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200645 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100646 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200647 eol = sol + idx->v[cur_idx].len;
648
649 if (sov >= eol)
650 /* no more values in this header */
651 goto next_hdr;
652
Willy Tarreau68085d82010-01-18 14:54:04 +0100653 /* values remaining for this header, skip the comma but save it
654 * for later use (eg: for header deletion).
655 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 sov++;
657 while (sov < eol && http_is_lws[(unsigned char)*sov])
658 sov++;
659
660 goto return_hdr;
661 }
662
663 /* first request for this header */
664 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100665 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200666 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 while (cur_idx) {
668 eol = sol + idx->v[cur_idx].len;
669
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200670 if (len == 0) {
671 /* No argument was passed, we want any header.
672 * To achieve this, we simply build a fake request. */
673 while (sol + len < eol && sol[len] != ':')
674 len++;
675 name = sol;
676 }
677
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 if ((len < eol - sol) &&
679 (sol[len] == ':') &&
680 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100681 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200682 sov = sol + len + 1;
683 while (sov < eol && http_is_lws[(unsigned char)*sov])
684 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100685
Willy Tarreau33a7e692007-06-10 19:45:56 +0200686 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100687 ctx->prev = old_idx;
688 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200689 ctx->idx = cur_idx;
690 ctx->val = sov - sol;
691
692 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200693 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200694 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 eol--;
696 ctx->tws++;
697 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200698 ctx->vlen = eol - sov;
699 return 1;
700 }
701 next_hdr:
702 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100703 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200704 cur_idx = idx->v[cur_idx].next;
705 }
706 return 0;
707}
708
709int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100710 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200711 struct hdr_ctx *ctx)
712{
713 return http_find_header2(name, strlen(name), sol, idx, ctx);
714}
715
Willy Tarreau68085d82010-01-18 14:54:04 +0100716/* Remove one value of a header. This only works on a <ctx> returned by one of
717 * the http_find_header functions. The value is removed, as well as surrounding
718 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100719 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100720 * message <msg>. The new index is returned. If it is zero, it means there is
721 * no more header, so any processing may stop. The ctx is always left in a form
722 * that can be handled by http_find_header2() to find next occurrence.
723 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100724int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100725{
726 int cur_idx = ctx->idx;
727 char *sol = ctx->line;
728 struct hdr_idx_elem *hdr;
729 int delta, skip_comma;
730
731 if (!cur_idx)
732 return 0;
733
734 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200735 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100736 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200737 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100738 http_msg_move_end(msg, delta);
739 idx->used--;
740 hdr->len = 0; /* unused entry */
741 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100742 if (idx->tail == ctx->idx)
743 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100744 ctx->idx = ctx->prev; /* walk back to the end of previous header */
745 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
746 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 return ctx->idx;
749 }
750
751 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
753 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100754 */
755
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200756 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200757 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200758 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 NULL, 0);
760 hdr->len += delta;
761 http_msg_move_end(msg, delta);
762 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200763 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100764 return ctx->idx;
765}
766
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100767/* This function handles a server error at the stream interface level. The
768 * stream interface is assumed to be already in a closed state. An optional
769 * message is copied into the input buffer, and an HTTP status code stored.
770 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100771 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100773static void http_server_error(struct session *t, struct stream_interface *si,
774 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200776 channel_auto_read(si->ob);
777 channel_abort(si->ob);
778 channel_auto_close(si->ob);
779 channel_erase(si->ob);
780 channel_auto_close(si->ib);
781 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100782 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100783 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200784 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 }
786 if (!(t->flags & SN_ERR_MASK))
787 t->flags |= err;
788 if (!(t->flags & SN_FINST_MASK))
789 t->flags |= finst;
790}
791
Willy Tarreau80587432006-12-24 17:47:20 +0100792/* This function returns the appropriate error location for the given session
793 * and message.
794 */
795
Willy Tarreau783f2582012-09-04 12:19:04 +0200796struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100797{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 if (s->be->errmsg[msgnum].str)
799 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100800 else if (s->fe->errmsg[msgnum].str)
801 return &s->fe->errmsg[msgnum];
802 else
803 return &http_err_chunks[msgnum];
804}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805
Willy Tarreau53b6c742006-12-17 13:37:46 +0100806/*
807 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
808 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
809 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100810enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811{
812 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100813 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100814
815 m = ((unsigned)*str - 'A');
816
817 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100818 for (h = http_methods[m]; h->len > 0; h++) {
819 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100820 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100821 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100822 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100823 };
824 return HTTP_METH_OTHER;
825 }
826 return HTTP_METH_NONE;
827
828}
829
Willy Tarreau21d2af32008-02-14 20:25:24 +0100830/* Parse the URI from the given transaction (which is assumed to be in request
831 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
832 * It is returned otherwise.
833 */
834static char *
835http_get_path(struct http_txn *txn)
836{
837 char *ptr, *end;
838
Willy Tarreau9b28e032012-10-12 23:49:43 +0200839 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100840 end = ptr + txn->req.sl.rq.u_l;
841
842 if (ptr >= end)
843 return NULL;
844
845 /* RFC2616, par. 5.1.2 :
846 * Request-URI = "*" | absuri | abspath | authority
847 */
848
849 if (*ptr == '*')
850 return NULL;
851
852 if (isalpha((unsigned char)*ptr)) {
853 /* this is a scheme as described by RFC3986, par. 3.1 */
854 ptr++;
855 while (ptr < end &&
856 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
857 ptr++;
858 /* skip '://' */
859 if (ptr == end || *ptr++ != ':')
860 return NULL;
861 if (ptr == end || *ptr++ != '/')
862 return NULL;
863 if (ptr == end || *ptr++ != '/')
864 return NULL;
865 }
866 /* skip [user[:passwd]@]host[:[port]] */
867
868 while (ptr < end && *ptr != '/')
869 ptr++;
870
871 if (ptr == end)
872 return NULL;
873
874 /* OK, we got the '/' ! */
875 return ptr;
876}
877
William Lallemand65ad6e12014-01-31 15:08:02 +0100878/* Parse the URI from the given string and look for the "/" beginning the PATH.
879 * If not found, return NULL. It is returned otherwise.
880 */
881static char *
882http_get_path_from_string(char *str)
883{
884 char *ptr = str;
885
886 /* RFC2616, par. 5.1.2 :
887 * Request-URI = "*" | absuri | abspath | authority
888 */
889
890 if (*ptr == '*')
891 return NULL;
892
893 if (isalpha((unsigned char)*ptr)) {
894 /* this is a scheme as described by RFC3986, par. 3.1 */
895 ptr++;
896 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
897 ptr++;
898 /* skip '://' */
899 if (*ptr == '\0' || *ptr++ != ':')
900 return NULL;
901 if (*ptr == '\0' || *ptr++ != '/')
902 return NULL;
903 if (*ptr == '\0' || *ptr++ != '/')
904 return NULL;
905 }
906 /* skip [user[:passwd]@]host[:[port]] */
907
908 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
909 ptr++;
910
911 if (*ptr == '\0' || *ptr == ' ')
912 return NULL;
913
914 /* OK, we got the '/' ! */
915 return ptr;
916}
917
Willy Tarreau71241ab2012-12-27 11:30:54 +0100918/* Returns a 302 for a redirectable request that reaches a server working in
919 * in redirect mode. This may only be called just after the stream interface
920 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
921 * follow normal proxy processing. NOTE: this function is designed to support
922 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100924void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925{
926 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100927 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200929 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930
931 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 trash.len = strlen(HTTP_302);
933 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100935 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100936
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100938 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 return;
940
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100941 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100942 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100943 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
944 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100945 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100946
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200947 /* 3: add the request URI. Since it was already forwarded, we need
948 * to temporarily rewind the buffer.
949 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200951 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200952
Willy Tarreauefb453c2008-10-26 20:49:47 +0100953 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200954 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 +0200955
Willy Tarreau9b28e032012-10-12 23:49:43 +0200956 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200957
Willy Tarreauefb453c2008-10-26 20:49:47 +0100958 if (!path)
959 return;
960
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100961 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100962 return;
963
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100964 memcpy(trash.str + trash.len, path, len);
965 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100966
967 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100968 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
969 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100970 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100971 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
972 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100973 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100974
975 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200976 si_shutr(si);
977 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 si->state = SI_ST_CLO;
980
981 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200982 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100985 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500986 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987}
988
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 * that the server side is closed. Note that err_type is actually a
991 * bitmask, where almost only aborts may be cumulated with other
992 * values. We consider that aborted operations are more important
993 * than timeouts or errors due to the fact that nobody else in the
994 * logs might explain incomplete retries. All others should avoid
995 * being cumulated. It should normally not be possible to have multiple
996 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100997 * Note that connection errors appearing on the second request of a keep-alive
998 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001000void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001001{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001002 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003
1004 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001005 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001006 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001008 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001009 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1010 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001012 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001013 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001014 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001015 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001016 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001017 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001019 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1020 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001022 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001023 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001024 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001025 else if (err_type & SI_ET_CONN_RES)
1026 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001027 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1028 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001029 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001030 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001031 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032}
1033
Willy Tarreau42250582007-04-01 01:30:43 +02001034extern const char sess_term_cond[8];
1035extern const char sess_fin_state[8];
1036extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001037struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001038struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001039struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040
Willy Tarreau117f59e2007-03-04 18:17:17 +01001041/*
1042 * Capture headers from message starting at <som> according to header list
1043 * <cap_hdr>, and fill the <idx> structure appropriately.
1044 */
1045void capture_headers(char *som, struct hdr_idx *idx,
1046 char **cap, struct cap_hdr *cap_hdr)
1047{
1048 char *eol, *sol, *col, *sov;
1049 int cur_idx;
1050 struct cap_hdr *h;
1051 int len;
1052
1053 sol = som + hdr_idx_first_pos(idx);
1054 cur_idx = hdr_idx_first_idx(idx);
1055
1056 while (cur_idx) {
1057 eol = sol + idx->v[cur_idx].len;
1058
1059 col = sol;
1060 while (col < eol && *col != ':')
1061 col++;
1062
1063 sov = col + 1;
1064 while (sov < eol && http_is_lws[(unsigned char)*sov])
1065 sov++;
1066
1067 for (h = cap_hdr; h; h = h->next) {
1068 if ((h->namelen == col - sol) &&
1069 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1070 if (cap[h->index] == NULL)
1071 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001072 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001073
1074 if (cap[h->index] == NULL) {
1075 Alert("HTTP capture : out of memory.\n");
1076 continue;
1077 }
1078
1079 len = eol - sov;
1080 if (len > h->len)
1081 len = h->len;
1082
1083 memcpy(cap[h->index], sov, len);
1084 cap[h->index][len]=0;
1085 }
1086 }
1087 sol = eol + idx->v[cur_idx].cr + 1;
1088 cur_idx = idx->v[cur_idx].next;
1089 }
1090}
1091
1092
Willy Tarreau42250582007-04-01 01:30:43 +02001093/* either we find an LF at <ptr> or we jump to <bad>.
1094 */
1095#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1096
1097/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1098 * otherwise to <http_msg_ood> with <state> set to <st>.
1099 */
1100#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1101 ptr++; \
1102 if (likely(ptr < end)) \
1103 goto good; \
1104 else { \
1105 state = (st); \
1106 goto http_msg_ood; \
1107 } \
1108 } while (0)
1109
1110
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001112 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1114 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1115 * will give undefined results.
1116 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1117 * and that msg->sol points to the beginning of the response.
1118 * If a complete line is found (which implies that at least one CR or LF is
1119 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1120 * returned indicating an incomplete line (which does not mean that parts have
1121 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1122 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1123 * upon next call.
1124 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001125 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1127 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001128 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001130const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001131 enum ht_state state, const char *ptr, const char *end,
1132 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001133{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001134 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001135
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001139 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1141
1142 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001143 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001144 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1145 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001146 state = HTTP_MSG_ERROR;
1147 break;
1148
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001152 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001153 goto http_msg_rpcode;
1154 }
1155 if (likely(HTTP_IS_SPHT(*ptr)))
1156 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1157 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001158 state = HTTP_MSG_ERROR;
1159 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001160
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001162 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001163 if (likely(!HTTP_IS_LWS(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1165
1166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001167 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1169 }
1170
1171 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001172 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 http_msg_rsp_reason:
1174 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001176 msg->sl.st.r_l = 0;
1177 goto http_msg_rpline_eol;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001182 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 goto http_msg_rpreason;
1184 }
1185 if (likely(HTTP_IS_SPHT(*ptr)))
1186 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1187 /* so it's a CR/LF, so there is no reason phrase */
1188 goto http_msg_rsp_reason;
1189
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 if (likely(!HTTP_IS_CRLF(*ptr)))
1193 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001194 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 http_msg_rpline_eol:
1196 /* We have seen the end of line. Note that we do not
1197 * necessarily have the \n yet, but at least we know that we
1198 * have EITHER \r OR \n, otherwise the response would not be
1199 * complete. We can then record the response length and return
1200 * to the caller which will be able to register it.
1201 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001202 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 return ptr;
1204
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001206#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001207 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1208 exit(1);
1209#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001210 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001211 }
1212
1213 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001214 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001215 if (ret_state)
1216 *ret_state = state;
1217 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001218 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001219 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001220}
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 * This function parses a request line between <ptr> and <end>, starting with
1224 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1225 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1226 * will give undefined results.
1227 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1228 * and that msg->sol points to the beginning of the request.
1229 * If a complete line is found (which implies that at least one CR or LF is
1230 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1231 * returned indicating an incomplete line (which does not mean that parts have
1232 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1233 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1234 * upon next call.
1235 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001236 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1238 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001239 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001241const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001242 enum ht_state state, const char *ptr, const char *end,
1243 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001245 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001249 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 if (likely(HTTP_IS_TOKEN(*ptr)))
1251 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001254 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 if (likely(HTTP_IS_CRLF(*ptr))) {
1259 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001260 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001262 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001264 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001266 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 msg->sl.rq.v_l = 0;
1268 goto http_msg_rqline_eol;
1269 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001270 state = HTTP_MSG_ERROR;
1271 break;
1272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001276 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 goto http_msg_rquri;
1278 }
1279 if (likely(HTTP_IS_SPHT(*ptr)))
1280 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1281 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1282 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001285 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001286 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001290 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1292 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001293
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001294 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001295 /* non-ASCII chars are forbidden unless option
1296 * accept-invalid-http-request is enabled in the frontend.
1297 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001298 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001299 if (msg->err_pos < -1)
1300 goto invalid_char;
1301 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1304 }
1305
1306 if (likely(HTTP_IS_CRLF(*ptr))) {
1307 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1308 goto http_msg_req09_uri_e;
1309 }
1310
1311 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001312 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001313 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001314 state = HTTP_MSG_ERROR;
1315 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001320 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 goto http_msg_rqver;
1322 }
1323 if (likely(HTTP_IS_SPHT(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1325 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1326 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001330 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332
1333 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001334 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001335 http_msg_rqline_eol:
1336 /* We have seen the end of line. Note that we do not
1337 * necessarily have the \n yet, but at least we know that we
1338 * have EITHER \r OR \n, otherwise the request would not be
1339 * complete. We can then record the request length and return
1340 * to the caller which will be able to register it.
1341 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001342 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001343 return ptr;
1344 }
1345
1346 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001347 state = HTTP_MSG_ERROR;
1348 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001351#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1353 exit(1);
1354#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001355 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001359 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 if (ret_state)
1361 *ret_state = state;
1362 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001363 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001367/*
1368 * Returns the data from Authorization header. Function may be called more
1369 * than once so data is stored in txn->auth_data. When no header is found
1370 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001371 * searching again for something we are unable to find anyway. However, if
1372 * the result if valid, the cache is not reused because we would risk to
1373 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001374 */
1375
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001376/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1377 * set according to global.tune.bufsize.
1378 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001379char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001380
1381int
1382get_http_auth(struct session *s)
1383{
1384
1385 struct http_txn *txn = &s->txn;
1386 struct chunk auth_method;
1387 struct hdr_ctx ctx;
1388 char *h, *p;
1389 int len;
1390
1391#ifdef DEBUG_AUTH
1392 printf("Auth for session %p: %d\n", s, txn->auth.method);
1393#endif
1394
1395 if (txn->auth.method == HTTP_AUTH_WRONG)
1396 return 0;
1397
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001398 txn->auth.method = HTTP_AUTH_WRONG;
1399
1400 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001401
1402 if (txn->flags & TX_USE_PX_CONN) {
1403 h = "Proxy-Authorization";
1404 len = strlen(h);
1405 } else {
1406 h = "Authorization";
1407 len = strlen(h);
1408 }
1409
Willy Tarreau9b28e032012-10-12 23:49:43 +02001410 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411 return 0;
1412
1413 h = ctx.line + ctx.val;
1414
1415 p = memchr(h, ' ', ctx.vlen);
1416 if (!p || p == h)
1417 return 0;
1418
1419 chunk_initlen(&auth_method, h, 0, p-h);
1420 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1421
1422 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1423
1424 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001425 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001426
1427 if (len < 0)
1428 return 0;
1429
1430
1431 get_http_auth_buff[len] = '\0';
1432
1433 p = strchr(get_http_auth_buff, ':');
1434
1435 if (!p)
1436 return 0;
1437
1438 txn->auth.user = get_http_auth_buff;
1439 *p = '\0';
1440 txn->auth.pass = p+1;
1441
1442 txn->auth.method = HTTP_AUTH_BASIC;
1443 return 1;
1444 }
1445
1446 return 0;
1447}
1448
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau8973c702007-01-21 23:58:29 +01001450/*
1451 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001452 * depending on the initial msg->msg_state. The caller is responsible for
1453 * ensuring that the message does not wrap. The function can be preempted
1454 * everywhere when data are missing and recalled at the exact same location
1455 * with no information loss. The message may even be realigned between two
1456 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001457 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001458 * fields. Note that msg->sol will be initialized after completing the first
1459 * state, so that none of the msg pointers has to be initialized prior to the
1460 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001462void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463{
Willy Tarreau3770f232013-12-07 00:01:53 +01001464 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001467
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001469 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 ptr = buf->p + msg->next;
1471 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (unlikely(ptr >= end))
1474 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 /*
1478 * First, states that are specific to the response only.
1479 * We check them first so that request and headers are
1480 * closer to each other (accessed more often).
1481 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001485 /* we have a start of message, but we have to check
1486 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001487 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001488 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001489 if (unlikely(ptr != buf->p)) {
1490 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001491 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001492 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001493 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 }
Willy Tarreau26927362012-05-18 23:22:52 +02001495 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001496 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 hdr_idx_init(idx);
1498 state = HTTP_MSG_RPVER;
1499 goto http_msg_rpver;
1500 }
1501
1502 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1503 goto http_msg_invalid;
1504
1505 if (unlikely(*ptr == '\n'))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1507 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1508 /* stop here */
1509
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001511 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001512 EXPECT_LF_HERE(ptr, http_msg_invalid);
1513 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1514 /* stop here */
1515
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001517 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 case HTTP_MSG_RPVER_SP:
1519 case HTTP_MSG_RPCODE:
1520 case HTTP_MSG_RPCODE_SP:
1521 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001522 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001523 state, ptr, end,
1524 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 if (unlikely(!ptr))
1526 return;
1527
1528 /* we have a full response and we know that we have either a CR
1529 * or an LF at <ptr>.
1530 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001531 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1532
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001533 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 if (likely(*ptr == '\r'))
1535 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1536 goto http_msg_rpline_end;
1537
Willy Tarreau8973c702007-01-21 23:58:29 +01001538 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001539 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001540 /* msg->sol must point to the first of CR or LF. */
1541 EXPECT_LF_HERE(ptr, http_msg_invalid);
1542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1543 /* stop here */
1544
1545 /*
1546 * Second, states that are specific to the request only
1547 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001549 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001551 /* we have a start of message, but we have to check
1552 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001553 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001554 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001555 if (likely(ptr != buf->p)) {
1556 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001557 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001558 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 }
Willy Tarreau26927362012-05-18 23:22:52 +02001561 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001562 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 state = HTTP_MSG_RQMETH;
1564 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1568 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 if (unlikely(*ptr == '\n'))
1571 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1572 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001573 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001579 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001582 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 case HTTP_MSG_RQMETH_SP:
1584 case HTTP_MSG_RQURI:
1585 case HTTP_MSG_RQURI_SP:
1586 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001587 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001588 state, ptr, end,
1589 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 if (unlikely(!ptr))
1591 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 /* we have a full request and we know that we have either a CR
1594 * or an LF at <ptr>.
1595 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001598 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 if (likely(*ptr == '\r'))
1600 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 /* check for HTTP/0.9 request : no version information available.
1606 * msg->sol must point to the first of CR or LF.
1607 */
1608 if (unlikely(msg->sl.rq.v_l == 0))
1609 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8973c702007-01-21 23:58:29 +01001615 /*
1616 * Common states below
1617 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001620 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 if (likely(!HTTP_IS_CRLF(*ptr))) {
1622 goto http_msg_hdr_name;
1623 }
1624
1625 if (likely(*ptr == '\r'))
1626 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1627 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001630 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 /* assumes msg->sol points to the first char */
1632 if (likely(HTTP_IS_TOKEN(*ptr)))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001634
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001635 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001638 if (likely(msg->err_pos < -1) || *ptr == '\n')
1639 goto http_msg_invalid;
1640
1641 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001642 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001643
1644 /* and we still accept this non-token character */
1645 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001648 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001649 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 if (likely(HTTP_IS_SPHT(*ptr)))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001654 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 if (likely(!HTTP_IS_CRLF(*ptr))) {
1657 goto http_msg_hdr_val;
1658 }
1659
1660 if (likely(*ptr == '\r'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1662 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001665 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 EXPECT_LF_HERE(ptr, http_msg_invalid);
1667 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 if (likely(HTTP_IS_SPHT(*ptr))) {
1672 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 for (; buf->p + msg->sov < ptr; msg->sov++)
1674 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 goto http_msg_hdr_l1_sp;
1676 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001677 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001678 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 goto http_msg_complete_header;
1680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001682 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001683 /* assumes msg->sol points to the first char, and msg->sov
1684 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 */
1686 if (likely(!HTTP_IS_CRLF(*ptr)))
1687 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001689 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 /* Note: we could also copy eol into ->eoh so that we have the
1691 * real header end in case it ends with lots of LWS, but is this
1692 * really needed ?
1693 */
1694 if (likely(*ptr == '\r'))
1695 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1696 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001699 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 EXPECT_LF_HERE(ptr, http_msg_invalid);
1701 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001704 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1706 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 for (; buf->p + msg->eol < ptr; msg->eol++)
1708 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 goto http_msg_hdr_val;
1710 }
1711 http_msg_complete_header:
1712 /*
1713 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001714 * Assumes msg->sol points to the first char, msg->sov points
1715 * to the first character of the value and msg->eol to the
1716 * first CR or LF so we know how the line ends. We insert last
1717 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001719 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 idx, idx->tail) < 0))
1721 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001722
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001723 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(!HTTP_IS_CRLF(*ptr))) {
1725 goto http_msg_hdr_name;
1726 }
1727
1728 if (likely(*ptr == '\r'))
1729 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1730 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001731
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001733 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 /* Assumes msg->sol points to the first of either CR or LF */
1735 EXPECT_LF_HERE(ptr, http_msg_invalid);
1736 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001737 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001738 msg->eoh = msg->sol;
1739 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001740 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001742
1743 case HTTP_MSG_ERROR:
1744 /* this may only happen if we call http_msg_analyser() twice with an error */
1745 break;
1746
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001748#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1750 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001751#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001752 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 }
1754 http_msg_ood:
1755 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001756 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001757 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 http_msg_invalid:
1761 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001762 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001763 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 return;
1765}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001766
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001767/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1768 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1769 * nothing is done and 1 is returned.
1770 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001771static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001772{
1773 int delta;
1774 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001775 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776
1777 if (msg->sl.rq.v_l != 0)
1778 return 1;
1779
Willy Tarreau9b28e032012-10-12 23:49:43 +02001780 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001781 delta = 0;
1782
1783 if (msg->sl.rq.u_l == 0) {
1784 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001785 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001786 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001787 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001788 }
1789 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001791 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001792 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001793 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001794 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001795 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001796 NULL, NULL);
1797 if (unlikely(!cur_end))
1798 return 0;
1799
1800 /* we have a full HTTP/1.0 request now and we know that
1801 * we have either a CR or an LF at <ptr>.
1802 */
1803 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1804 return 1;
1805}
1806
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001807/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001808 * and "keep-alive" values. If we already know that some headers may safely
1809 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001810 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1811 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001812 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001813 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1814 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1815 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001816 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001817 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001818void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001819{
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001821 const char *hdr_val = "Connection";
1822 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001823
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001824 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001825 return;
1826
Willy Tarreau88d349d2010-01-25 12:15:43 +01001827 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1828 hdr_val = "Proxy-Connection";
1829 hdr_len = 16;
1830 }
1831
Willy Tarreau5b154472009-12-21 20:11:07 +01001832 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001833 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001834 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001835 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1836 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001837 if (to_del & 2)
1838 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001839 else
1840 txn->flags |= TX_CON_KAL_SET;
1841 }
1842 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1843 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001844 if (to_del & 1)
1845 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001846 else
1847 txn->flags |= TX_CON_CLO_SET;
1848 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001849 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1850 txn->flags |= TX_HDR_CONN_UPG;
1851 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001852 }
1853
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001854 txn->flags |= TX_HDR_CONN_PRS;
1855 return;
1856}
Willy Tarreau5b154472009-12-21 20:11:07 +01001857
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001858/* Apply desired changes on the Connection: header. Values may be removed and/or
1859 * added depending on the <wanted> flags, which are exclusively composed of
1860 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1861 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1862 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001863void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001864{
1865 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866 const char *hdr_val = "Connection";
1867 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001868
1869 ctx.idx = 0;
1870
Willy Tarreau88d349d2010-01-25 12:15:43 +01001871
1872 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1873 hdr_val = "Proxy-Connection";
1874 hdr_len = 16;
1875 }
1876
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001877 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001878 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001879 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1880 if (wanted & TX_CON_KAL_SET)
1881 txn->flags |= TX_CON_KAL_SET;
1882 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001883 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001885 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1886 if (wanted & TX_CON_CLO_SET)
1887 txn->flags |= TX_CON_CLO_SET;
1888 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001889 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001890 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001891 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892
1893 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1894 return;
1895
1896 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1897 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001898 hdr_val = "Connection: close";
1899 hdr_len = 17;
1900 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1901 hdr_val = "Proxy-Connection: close";
1902 hdr_len = 23;
1903 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001904 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905 }
1906
1907 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1908 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001909 hdr_val = "Connection: keep-alive";
1910 hdr_len = 22;
1911 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1912 hdr_val = "Proxy-Connection: keep-alive";
1913 hdr_len = 28;
1914 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001915 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001916 }
1917 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001918}
1919
Willy Tarreaua458b672012-03-05 11:17:50 +01001920/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001922 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001923 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001924 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001925 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001926static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001927{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001928 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001929 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001930 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001931 const char *end = buf->data + buf->size;
1932 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001933 unsigned int chunk = 0;
1934
1935 /* The chunk size is in the following form, though we are only
1936 * interested in the size and CRLF :
1937 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1938 */
1939 while (1) {
1940 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001941 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001942 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001944 if (c < 0) /* not a hex digit anymore */
1945 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001946 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001947 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001948 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001949 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001950 chunk = (chunk << 4) + c;
1951 }
1952
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001954 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001955 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956
1957 while (http_is_spht[(unsigned char)*ptr]) {
1958 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001959 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001960 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001961 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001962 }
1963
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 /* Up to there, we know that at least one byte is present at *ptr. Check
1965 * for the end of chunk size.
1966 */
1967 while (1) {
1968 if (likely(HTTP_IS_CRLF(*ptr))) {
1969 /* we now have a CR or an LF at ptr */
1970 if (likely(*ptr == '\r')) {
1971 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001972 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001973 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 return 0;
1975 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001976
Willy Tarreaud98cf932009-12-27 22:54:55 +01001977 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001978 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001980 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981 /* done */
1982 break;
1983 }
1984 else if (*ptr == ';') {
1985 /* chunk extension, ends at next CRLF */
1986 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001987 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001988 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001989 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990
1991 while (!HTTP_IS_CRLF(*ptr)) {
1992 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001993 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001994 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001995 return 0;
1996 }
1997 /* we have a CRLF now, loop above */
1998 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002000 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002001 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002002 }
2003
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01002005 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002007 */
Willy Tarreau0161d622013-04-02 01:26:55 +02002008 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002009 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01002010 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002011 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002012 msg->chunk_len = chunk;
2013 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002015 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002017 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002018 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002019}
2020
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002021/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002022 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002023 * the trailers is found, it is automatically scheduled to be forwarded,
2024 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2025 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01002026 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01002027 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01002028 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01002029 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2030 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02002031 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01002032 * matches the length of trailers already parsed and not forwarded. It is also
2033 * important to note that this function is designed to be able to parse wrapped
2034 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002036static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002038 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002039
Willy Tarreaua458b672012-03-05 11:17:50 +01002040 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002042 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002043 const char *ptr = b_ptr(buf, msg->next);
2044 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002045 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046
2047 /* scan current line and stop at LF or CRLF */
2048 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002049 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002050 return 0;
2051
2052 if (*ptr == '\n') {
2053 if (!p1)
2054 p1 = ptr;
2055 p2 = ptr;
2056 break;
2057 }
2058
2059 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002060 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002061 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002063 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002064 p1 = ptr;
2065 }
2066
2067 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 if (ptr >= buf->data + buf->size)
2069 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 }
2071
2072 /* after LF; point to beginning of next line */
2073 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 if (p2 >= buf->data + buf->size)
2075 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002078 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002079 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002080
2081 /* schedule this line for forwarding */
2082 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002083 if (msg->sov >= buf->size)
2084 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002086 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002087 /* LF/CRLF at beginning of line => end of trailers at p2.
2088 * Everything was scheduled for forwarding, there's nothing
2089 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002090 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002091 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 msg->msg_state = HTTP_MSG_DONE;
2093 return 1;
2094 }
2095 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002096 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097 }
2098}
2099
Willy Tarreau54d23df2012-10-25 19:04:45 +02002100/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002102 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002103 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002104 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2105 * not enough data are available, the function does not change anything and
2106 * returns zero. If a parse error is encountered, the function returns < 0 and
2107 * does not change anything. Note: this function is designed to parse wrapped
2108 * CRLF at the end of the buffer.
2109 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002110static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002111{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002112 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002113 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002114 int bytes;
2115
2116 /* NB: we'll check data availabilty at the end. It's not a
2117 * problem because whatever we match first will be checked
2118 * against the correct length.
2119 */
2120 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002121 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 if (*ptr == '\r') {
2123 bytes++;
2124 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002125 if (ptr >= buf->data + buf->size)
2126 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127 }
2128
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002129 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 return 0;
2131
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002132 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002133 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002134 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002135 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
2137 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002138 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002140 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2141 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002142 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2144 return 1;
2145}
Willy Tarreau5b154472009-12-21 20:11:07 +01002146
William Lallemand82fe75c2012-10-23 10:25:10 +02002147
2148/*
2149 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002150 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002151int select_compression_request_header(struct session *s, struct buffer *req)
2152{
2153 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002154 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002155 struct hdr_ctx ctx;
2156 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002157 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002158
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002159 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2160 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002161 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2162 */
2163 ctx.idx = 0;
2164 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2165 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002166 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2167 (ctx.vlen < 31 ||
2168 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2169 ctx.line[ctx.val + 30] < '6' ||
2170 (ctx.line[ctx.val + 30] == '6' &&
2171 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2172 s->comp_algo = NULL;
2173 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002174 }
2175
William Lallemand82fe75c2012-10-23 10:25:10 +02002176 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002177 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002178 ctx.idx = 0;
2179 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002180 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002181 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2182 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002183
2184 /* remove all occurrences of the header when "compression offload" is set */
2185
2186 if ((s->be->comp && s->be->comp->offload) ||
2187 (s->fe->comp && s->fe->comp->offload)) {
2188 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2189 ctx.idx = 0;
2190 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2191 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2192 }
2193 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002194 return 1;
2195 }
2196 }
2197 }
2198 }
2199
2200 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002201 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2202 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002203 if (comp_algo->add_data == identity_add_data) {
2204 s->comp_algo = comp_algo;
2205 return 1;
2206 }
2207 }
2208 }
2209
2210 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002211 return 0;
2212}
2213
2214/*
2215 * Selects a comression algorithm depending of the server response.
2216 */
2217int select_compression_response_header(struct session *s, struct buffer *res)
2218{
2219 struct http_txn *txn = &s->txn;
2220 struct http_msg *msg = &txn->rsp;
2221 struct hdr_ctx ctx;
2222 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002223
2224 /* no common compression algorithm was found in request header */
2225 if (s->comp_algo == NULL)
2226 goto fail;
2227
2228 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002229 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002230 goto fail;
2231
William Lallemandd3002612012-11-26 14:34:47 +01002232 /* 200 only */
2233 if (txn->status != 200)
2234 goto fail;
2235
William Lallemand82fe75c2012-10-23 10:25:10 +02002236 /* Content-Length is null */
2237 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2238 goto fail;
2239
Willy Tarreau667c2a32013-04-09 08:13:58 +02002240 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2241 if (msg->flags & HTTP_MSGF_TE_CHNK)
2242 goto fail;
2243
William Lallemand82fe75c2012-10-23 10:25:10 +02002244 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002245 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002246 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2247 goto fail;
2248
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002249 /* no compression when Cache-Control: no-transform is present in the message */
2250 ctx.idx = 0;
2251 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2252 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2253 goto fail;
2254 }
2255
William Lallemand82fe75c2012-10-23 10:25:10 +02002256 comp_type = NULL;
2257
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002258 /* we don't want to compress multipart content-types, nor content-types that are
2259 * not listed in the "compression type" directive if any. If no content-type was
2260 * found but configuration requires one, we don't compress either. Backend has
2261 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002262 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002263 ctx.idx = 0;
2264 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2265 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2266 goto fail;
2267
2268 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2269 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002270 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002271 if (ctx.vlen >= comp_type->name_len &&
2272 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002273 /* this Content-Type should be compressed */
2274 break;
2275 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002276 /* this Content-Type should not be compressed */
2277 if (comp_type == NULL)
2278 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002279 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002281 else { /* no content-type header */
2282 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2283 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002284 }
2285
William Lallemandd85f9172012-11-09 17:05:39 +01002286 /* limit compression rate */
2287 if (global.comp_rate_lim > 0)
2288 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2289 goto fail;
2290
William Lallemand072a2bf2012-11-20 17:01:01 +01002291 /* limit cpu usage */
2292 if (idle_pct < compress_min_idle)
2293 goto fail;
2294
William Lallemand4c49fae2012-11-07 15:00:23 +01002295 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002296 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002297 goto fail;
2298
William Lallemandec3e3892012-11-12 17:02:18 +01002299 s->flags |= SN_COMP_READY;
2300
William Lallemand82fe75c2012-10-23 10:25:10 +02002301 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002302 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002303 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2304 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2305
2306 /* add Transfer-Encoding header */
2307 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2308 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2309
2310 /*
2311 * Add Content-Encoding header when it's not identity encoding.
2312 * RFC 2616 : Identity encoding: This content-coding is used only in the
2313 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2314 * header.
2315 */
2316 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002317 trash.len = 18;
2318 memcpy(trash.str, "Content-Encoding: ", trash.len);
2319 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2320 trash.len += s->comp_algo->name_len;
2321 trash.str[trash.len] = '\0';
2322 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002323 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002324 return 1;
2325
2326fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002327 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 return 0;
2329}
2330
2331
Willy Tarreaud787e662009-07-07 10:14:51 +02002332/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2333 * processing can continue on next analysers, or zero if it either needs more
2334 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2335 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2336 * when it has nothing left to do, and may remove any analyser when it wants to
2337 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002338 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002339int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002340{
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 /*
2342 * We will parse the partial (or complete) lines.
2343 * We will check the request syntax, and also join multi-line
2344 * headers. An index of all the lines will be elaborated while
2345 * parsing.
2346 *
2347 * For the parsing, we use a 28 states FSM.
2348 *
2349 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002350 * req->buf->p = beginning of request
2351 * req->buf->p + msg->eoh = end of processed headers / start of current one
2352 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002353 * msg->eol = end of current header or line (LF or CRLF)
2354 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002355 *
2356 * At end of parsing, we may perform a capture of the error (if any), and
2357 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2358 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2359 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002361
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002363 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 struct http_txn *txn = &s->txn;
2365 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002366 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002367
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002368 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 +01002369 now_ms, __FUNCTION__,
2370 s,
2371 req,
2372 req->rex, req->wex,
2373 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002374 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002375 req->analysers);
2376
Willy Tarreau52a0c602009-08-16 22:45:38 +02002377 /* we're speaking HTTP here, so let's speak HTTP to the client */
2378 s->srv_error = http_return_srv_error;
2379
Willy Tarreau83e3af02009-12-28 17:39:57 +01002380 /* There's a protected area at the end of the buffer for rewriting
2381 * purposes. We don't want to start to parse the request if the
2382 * protected area is affected, because we may have to move processed
2383 * data later, which is much more complicated.
2384 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002385 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002386 if (txn->flags & TX_NOT_FIRST) {
2387 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002388 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002389 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002390 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002391 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002392 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002393 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002394 return 0;
2395 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002396 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2397 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2398 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002399 }
2400
Willy Tarreau065e8332010-01-08 00:30:20 +01002401 /* Note that we have the same problem with the response ; we
2402 * may want to send a redirect, error or anything which requires
2403 * some spare space. So we'll ensure that we have at least
2404 * maxrewrite bytes available in the response buffer before
2405 * processing that one. This will only affect pipelined
2406 * keep-alive requests.
2407 */
2408 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002409 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002410 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2411 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2412 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002413 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002414 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002415 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002416 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002417 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002418 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002419 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002420 return 0;
2421 }
2422 }
2423
Willy Tarreau9b28e032012-10-12 23:49:43 +02002424 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002425 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002426 }
2427
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 /* 1: we might have to print this header in debug mode */
2429 if (unlikely((global.mode & MODE_DEBUG) &&
2430 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002431 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002433
Willy Tarreau9b28e032012-10-12 23:49:43 +02002434 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002435 /* this is a bit complex : in case of error on the request line,
2436 * we know that rq.l is still zero, so we display only the part
2437 * up to the end of the line (truncated by debug_hdr).
2438 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002439 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 sol += hdr_idx_first_pos(&txn->hdr_idx);
2443 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 while (cur_idx) {
2446 eol = sol + txn->hdr_idx.v[cur_idx].len;
2447 debug_hdr("clihdr", s, sol, eol);
2448 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2449 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002450 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002451 }
2452
Willy Tarreau58f10d72006-12-04 02:26:12 +01002453
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 /*
2455 * Now we quickly check if we have found a full valid request.
2456 * If not so, we check the FD and buffer states before leaving.
2457 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002458 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002459 * requests are checked first. When waiting for a second request
2460 * on a keep-alive session, if we encounter and error, close, t/o,
2461 * we note the error in the session flags but don't set any state.
2462 * Since the error will be noted there, it will not be counted by
2463 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002464 * Last, we may increase some tracked counters' http request errors on
2465 * the cases that are deliberately the client's fault. For instance,
2466 * a timeout or connection reset is not counted as an error. However
2467 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002468 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002469
Willy Tarreau655dce92009-11-08 13:10:58 +01002470 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002471 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002473 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002474 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002475 session_inc_http_req_ctr(s);
2476 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002477 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002479 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002480
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 /* 1: Since we are in header mode, if there's no space
2482 * left for headers, we won't be able to free more
2483 * later, so the session will never terminate. We
2484 * must terminate it now.
2485 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002486 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002487 /* FIXME: check if URI is set and return Status
2488 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002489 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002490 session_inc_http_req_ctr(s);
2491 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002492 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002493 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002494 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 goto return_bad_req;
2496 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002497
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002499 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002500 if (!(s->flags & SN_ERR_MASK))
2501 s->flags |= SN_ERR_CLICL;
2502
Willy Tarreaufcffa692010-01-10 14:21:19 +01002503 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002504 goto failed_keep_alive;
2505
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002507 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002508 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002509 session_inc_http_err_ctr(s);
2510 }
2511
Willy Tarreaudc979f22012-12-04 10:39:01 +01002512 txn->status = 400;
2513 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 msg->msg_state = HTTP_MSG_ERROR;
2515 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002516
Willy Tarreauda7ff642010-06-23 11:44:09 +02002517 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002518 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002519 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002520 if (s->listener->counters)
2521 s->listener->counters->failed_req++;
2522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 if (!(s->flags & SN_FINST_MASK))
2524 s->flags |= SN_FINST_R;
2525 return 0;
2526 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002529 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002530 if (!(s->flags & SN_ERR_MASK))
2531 s->flags |= SN_ERR_CLITO;
2532
Willy Tarreaufcffa692010-01-10 14:21:19 +01002533 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002534 goto failed_keep_alive;
2535
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002537 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002538 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002539 session_inc_http_err_ctr(s);
2540 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002542 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 msg->msg_state = HTTP_MSG_ERROR;
2544 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002545
Willy Tarreauda7ff642010-06-23 11:44:09 +02002546 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002547 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002548 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002549 if (s->listener->counters)
2550 s->listener->counters->failed_req++;
2551
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 if (!(s->flags & SN_FINST_MASK))
2553 s->flags |= SN_FINST_R;
2554 return 0;
2555 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002556
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002558 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002559 if (!(s->flags & SN_ERR_MASK))
2560 s->flags |= SN_ERR_CLICL;
2561
Willy Tarreaufcffa692010-01-10 14:21:19 +01002562 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002563 goto failed_keep_alive;
2564
Willy Tarreau4076a152009-04-02 15:18:36 +02002565 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002566 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002568 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 msg->msg_state = HTTP_MSG_ERROR;
2570 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002571
Willy Tarreauda7ff642010-06-23 11:44:09 +02002572 session_inc_http_err_ctr(s);
2573 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002574 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002575 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002576 if (s->listener->counters)
2577 s->listener->counters->failed_req++;
2578
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 if (!(s->flags & SN_FINST_MASK))
2580 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002581 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002582 }
2583
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002584 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002585 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2586 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002587#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002588 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 +01002589 /* We need more data, we have to re-enable quick-ack in case we
2590 * previously disabled it, otherwise we might cause the client
2591 * to delay next data.
2592 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002593 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002594 }
2595#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002596
Willy Tarreaufcffa692010-01-10 14:21:19 +01002597 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2598 /* If the client starts to talk, let's fall back to
2599 * request timeout processing.
2600 */
2601 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002602 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002603 }
2604
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002606 if (!tick_isset(req->analyse_exp)) {
2607 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2608 (txn->flags & TX_WAIT_NEXT_RQ) &&
2609 tick_isset(s->be->timeout.httpka))
2610 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2611 else
2612 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 /* we're not ready yet */
2616 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002617
2618 failed_keep_alive:
2619 /* Here we process low-level errors for keep-alive requests. In
2620 * short, if the request is not the first one and it experiences
2621 * a timeout, read error or shutdown, we just silently close so
2622 * that the client can try again.
2623 */
2624 txn->status = 0;
2625 msg->msg_state = HTTP_MSG_RQBEFORE;
2626 req->analysers = 0;
2627 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002628 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002629 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002630 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002631 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002633
Willy Tarreaud787e662009-07-07 10:14:51 +02002634 /* OK now we have a complete HTTP request with indexed headers. Let's
2635 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002636 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002637 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002638 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002639 * byte after the last LF. msg->sov points to the first byte of data.
2640 * msg->eol cannot be trusted because it may have been left uninitialized
2641 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002642 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002643
Willy Tarreauda7ff642010-06-23 11:44:09 +02002644 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002645 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2646
Willy Tarreaub16a5742010-01-10 14:46:16 +01002647 if (txn->flags & TX_WAIT_NEXT_RQ) {
2648 /* kill the pending keep-alive timeout */
2649 txn->flags &= ~TX_WAIT_NEXT_RQ;
2650 req->analyse_exp = TICK_ETERNITY;
2651 }
2652
2653
Willy Tarreaud787e662009-07-07 10:14:51 +02002654 /* Maybe we found in invalid header name while we were configured not
2655 * to block on that, so we have to capture it now.
2656 */
2657 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002658 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 /*
2661 * 1: identify the method
2662 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002663 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002664
2665 /* we can make use of server redirect on GET and HEAD */
2666 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2667 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002668
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 /*
2670 * 2: check if the URI matches the monitor_uri.
2671 * We have to do this for every request which gets in, because
2672 * the monitor-uri is defined by the frontend.
2673 */
2674 if (unlikely((s->fe->monitor_uri_len != 0) &&
2675 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 s->fe->monitor_uri,
2678 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002681 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002683
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002685 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002686
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002688 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002689 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002690
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 ret = acl_pass(ret);
2692 if (cond->pol == ACL_COND_UNLESS)
2693 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002694
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 if (ret) {
2696 /* we fail this request, let's return 503 service unavail */
2697 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002698 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002699 if (!(s->flags & SN_ERR_MASK))
2700 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002702 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002703 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 /* nothing to fail, let's reply normaly */
2706 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002707 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002708 if (!(s->flags & SN_ERR_MASK))
2709 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002710 goto return_prx_cond;
2711 }
2712
2713 /*
2714 * 3: Maybe we have to copy the original REQURI for the logs ?
2715 * Note: we cannot log anymore if the request has been
2716 * classified as invalid.
2717 */
2718 if (unlikely(s->logs.logwait & LW_REQ)) {
2719 /* we have a complete HTTP request that we must log */
2720 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2721 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 if (urilen >= REQURI_LEN)
2724 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002725 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002726 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002727
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002728 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 s->do_log(s);
2730 } else {
2731 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002732 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 }
Willy Tarreau06619262006-12-17 08:37:22 +01002734
Willy Tarreau59234e92008-11-30 23:51:27 +01002735 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002736 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002737 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002738
Willy Tarreau5b154472009-12-21 20:11:07 +01002739 /* ... and check if the request is HTTP/1.1 or above */
2740 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002741 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2742 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2743 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002744 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002745
2746 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002747 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 +01002748
Willy Tarreau88d349d2010-01-25 12:15:43 +01002749 /* if the frontend has "option http-use-proxy-header", we'll check if
2750 * we have what looks like a proxied connection instead of a connection,
2751 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2752 * Note that this is *not* RFC-compliant, however browsers and proxies
2753 * happen to do that despite being non-standard :-(
2754 * We consider that a request not beginning with either '/' or '*' is
2755 * a proxied connection, which covers both "scheme://location" and
2756 * CONNECT ip:port.
2757 */
2758 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002759 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002760 txn->flags |= TX_USE_PX_CONN;
2761
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002762 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002763 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002766 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002767 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002768 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002769
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002770 /* 6: determine the transfer-length.
2771 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2772 * the presence of a message-body in a REQUEST and its transfer length
2773 * must be determined that way (in order of precedence) :
2774 * 1. The presence of a message-body in a request is signaled by the
2775 * inclusion of a Content-Length or Transfer-Encoding header field
2776 * in the request's header fields. When a request message contains
2777 * both a message-body of non-zero length and a method that does
2778 * not define any semantics for that request message-body, then an
2779 * origin server SHOULD either ignore the message-body or respond
2780 * with an appropriate error message (e.g., 413). A proxy or
2781 * gateway, when presented the same request, SHOULD either forward
2782 * the request inbound with the message- body or ignore the
2783 * message-body when determining a response.
2784 *
2785 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2786 * and the "chunked" transfer-coding (Section 6.2) is used, the
2787 * transfer-length is defined by the use of this transfer-coding.
2788 * If a Transfer-Encoding header field is present and the "chunked"
2789 * transfer-coding is not present, the transfer-length is defined
2790 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002791 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002792 * 3. If a Content-Length header field is present, its decimal value in
2793 * OCTETs represents both the entity-length and the transfer-length.
2794 * If a message is received with both a Transfer-Encoding header
2795 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002796 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002797 * 4. By the server closing the connection. (Closing the connection
2798 * cannot be used to indicate the end of a request body, since that
2799 * would leave no possibility for the server to send back a response.)
2800 *
2801 * Whenever a transfer-coding is applied to a message-body, the set of
2802 * transfer-codings MUST include "chunked", unless the message indicates
2803 * it is terminated by closing the connection. When the "chunked"
2804 * transfer-coding is used, it MUST be the last transfer-coding applied
2805 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002806 */
2807
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002808 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002809 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002810 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002811 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002812 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002813 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002814 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2815 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002816 /* bad transfer-encoding (chunked followed by something else) */
2817 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002818 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002819 break;
2820 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002821 }
2822
Willy Tarreau32b47f42009-10-18 20:55:02 +02002823 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002824 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002825 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002826 signed long long cl;
2827
Willy Tarreauad14f752011-09-02 20:33:27 +02002828 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002829 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002830 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002831 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002832
Willy Tarreauad14f752011-09-02 20:33:27 +02002833 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002834 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002835 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002836 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002837
Willy Tarreauad14f752011-09-02 20:33:27 +02002838 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002839 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002840 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002841 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002842
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002843 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002844 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002845 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002846 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002847
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002848 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002849 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002850 }
2851
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002852 /* bodyless requests have a known length */
2853 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002854 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002855
Willy Tarreaud787e662009-07-07 10:14:51 +02002856 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002857 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002858 req->analyse_exp = TICK_ETERNITY;
2859 return 1;
2860
2861 return_bad_req:
2862 /* We centralize bad requests processing here */
2863 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2864 /* we detected a parsing error. We want to archive this request
2865 * in the dedicated proxy area for later troubleshooting.
2866 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002867 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002868 }
2869
2870 txn->req.msg_state = HTTP_MSG_ERROR;
2871 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002872 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002873
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002874 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002875 if (s->listener->counters)
2876 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002877
2878 return_prx_cond:
2879 if (!(s->flags & SN_ERR_MASK))
2880 s->flags |= SN_ERR_PRXCOND;
2881 if (!(s->flags & SN_FINST_MASK))
2882 s->flags |= SN_FINST_R;
2883
2884 req->analysers = 0;
2885 req->analyse_exp = TICK_ETERNITY;
2886 return 0;
2887}
2888
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002889
Willy Tarreau347a35d2013-11-22 17:51:09 +01002890/* This function prepares an applet to handle the stats. It can deal with the
2891 * "100-continue" expectation, check that admin rules are met for POST requests,
2892 * and program a response message if something was unexpected. It cannot fail
2893 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002894 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002895 * s->target which is supposed to already point to the stats applet. The caller
2896 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002897 */
2898int http_handle_stats(struct session *s, struct channel *req)
2899{
2900 struct stats_admin_rule *stats_admin_rule;
2901 struct stream_interface *si = s->rep->prod;
2902 struct http_txn *txn = &s->txn;
2903 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002904 struct uri_auth *uri_auth = s->be->uri_auth;
2905 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002906 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002907
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002908 appctx = si_appctx(si);
2909 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2910 appctx->st1 = appctx->st2 = 0;
2911 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2912 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002913
2914 uri = msg->chn->buf->p + msg->sl.rq.u;
2915 lookup = uri + uri_auth->uri_len;
2916
2917 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2918 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002919 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002920 break;
2921 }
2922 }
2923
2924 if (uri_auth->refresh) {
2925 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2926 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002927 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002928 break;
2929 }
2930 }
2931 }
2932
2933 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2934 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002935 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002936 break;
2937 }
2938 }
2939
2940 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2941 if (memcmp(h, ";st=", 4) == 0) {
2942 int i;
2943 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002945 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2946 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002947 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002948 break;
2949 }
2950 }
2951 break;
2952 }
2953 }
2954
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002955 appctx->ctx.stats.scope_str = 0;
2956 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002957 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2958 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2959 int itx = 0;
2960 const char *h2;
2961 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2962 const char *err;
2963
2964 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2965 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002966 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002967 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2968 itx++;
2969 h++;
2970 }
2971
2972 if (itx > STAT_SCOPE_TXT_MAXLEN)
2973 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002974 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002975
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002976 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002977 memcpy(scope_txt, h2, itx);
2978 scope_txt[itx] = '\0';
2979 err = invalid_char(scope_txt);
2980 if (err) {
2981 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002982 appctx->ctx.stats.scope_str = 0;
2983 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002984 }
2985 break;
2986 }
2987 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002988
2989 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002990 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002991 int ret = 1;
2992
2993 if (stats_admin_rule->cond) {
2994 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2995 ret = acl_pass(ret);
2996 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2997 ret = !ret;
2998 }
2999
3000 if (ret) {
3001 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003002 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003003 break;
3004 }
3005 }
3006
3007 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003008 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003009 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003010 if (msg->msg_state < HTTP_MSG_100_SENT) {
3011 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3012 * send an HTTP/1.1 100 Continue intermediate response.
3013 */
3014 if (msg->flags & HTTP_MSGF_VER_11) {
3015 struct hdr_ctx ctx;
3016 ctx.idx = 0;
3017 /* Expect is allowed in 1.1, look for it */
3018 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3019 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3020 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3021 }
3022 }
3023 msg->msg_state = HTTP_MSG_100_SENT;
3024 s->logs.tv_request = now; /* update the request timer to reflect full request */
3025 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003026 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003027 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003028 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003029 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3030 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003031 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003032 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003033 else {
3034 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003035 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003036 }
3037
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003038 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003039 return 1;
3040}
3041
Lukas Tribus67db8df2013-06-23 17:37:13 +02003042/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3043 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3044 */
3045static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3046{
3047#ifdef IP_TOS
3048 if (from.ss_family == AF_INET)
3049 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3050#endif
3051#ifdef IPV6_TCLASS
3052 if (from.ss_family == AF_INET6) {
3053 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3054 /* v4-mapped addresses need IP_TOS */
3055 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3056 else
3057 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3058 }
3059#endif
3060}
3061
Willy Tarreau20b0de52012-12-24 15:45:22 +01003062/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003063 * transaction <txn>. Returns the first rule that prevents further processing
3064 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3065 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3066 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003067 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003068static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003069http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003070{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003071 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003072 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003073 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003074
Willy Tarreauff011f22011-01-06 17:51:27 +01003075 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003076 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003077 continue;
3078
Willy Tarreau96257ec2012-12-27 10:46:37 +01003079 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003080 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003081 int ret;
3082
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003083 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003084 ret = acl_pass(ret);
3085
Willy Tarreauff011f22011-01-06 17:51:27 +01003086 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003087 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003088
3089 if (!ret) /* condition not matched */
3090 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003091 }
3092
Willy Tarreau20b0de52012-12-24 15:45:22 +01003093
Willy Tarreau96257ec2012-12-27 10:46:37 +01003094 switch (rule->action) {
3095 case HTTP_REQ_ACT_ALLOW:
3096 return NULL; /* "allow" rules are OK */
3097
3098 case HTTP_REQ_ACT_DENY:
3099 txn->flags |= TX_CLDENY;
3100 return rule;
3101
Willy Tarreauccbcc372012-12-27 12:37:57 +01003102 case HTTP_REQ_ACT_TARPIT:
3103 txn->flags |= TX_CLTARPIT;
3104 return rule;
3105
Willy Tarreau96257ec2012-12-27 10:46:37 +01003106 case HTTP_REQ_ACT_AUTH:
3107 return rule;
3108
Willy Tarreau81499eb2012-12-27 12:19:02 +01003109 case HTTP_REQ_ACT_REDIR:
3110 return rule;
3111
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003112 case HTTP_REQ_ACT_SET_NICE:
3113 s->task->nice = rule->arg.nice;
3114 break;
3115
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003116 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003117 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003118 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003119 break;
3120
Willy Tarreau51347ed2013-06-11 19:34:13 +02003121 case HTTP_REQ_ACT_SET_MARK:
3122#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003123 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003124 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003125#endif
3126 break;
3127
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003128 case HTTP_REQ_ACT_SET_LOGL:
3129 s->logs.level = rule->arg.loglevel;
3130 break;
3131
Willy Tarreau96257ec2012-12-27 10:46:37 +01003132 case HTTP_REQ_ACT_SET_HDR:
3133 ctx.idx = 0;
3134 /* remove all occurrences of the header */
3135 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3136 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3137 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003138 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003139 /* now fall through to header addition */
3140
3141 case HTTP_REQ_ACT_ADD_HDR:
3142 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3143 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3144 trash.len = rule->arg.hdr_add.name_len;
3145 trash.str[trash.len++] = ':';
3146 trash.str[trash.len++] = ' ';
3147 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3148 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3149 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003150 }
3151 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003152
3153 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003154 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003155}
3156
Willy Tarreau71241ab2012-12-27 11:30:54 +01003157
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003158/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3159 * transaction <txn>. Returns the first rule that prevents further processing
3160 * of the response (deny, ...) or NULL if it executed all rules or stopped
3161 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3162 * rule.
3163 */
3164static struct http_res_rule *
3165http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3166{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003167 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003168 struct http_res_rule *rule;
3169 struct hdr_ctx ctx;
3170
3171 list_for_each_entry(rule, rules, list) {
3172 if (rule->action >= HTTP_RES_ACT_MAX)
3173 continue;
3174
3175 /* check optional condition */
3176 if (rule->cond) {
3177 int ret;
3178
3179 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3180 ret = acl_pass(ret);
3181
3182 if (rule->cond->pol == ACL_COND_UNLESS)
3183 ret = !ret;
3184
3185 if (!ret) /* condition not matched */
3186 continue;
3187 }
3188
3189
3190 switch (rule->action) {
3191 case HTTP_RES_ACT_ALLOW:
3192 return NULL; /* "allow" rules are OK */
3193
3194 case HTTP_RES_ACT_DENY:
3195 txn->flags |= TX_SVDENY;
3196 return rule;
3197
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003198 case HTTP_RES_ACT_SET_NICE:
3199 s->task->nice = rule->arg.nice;
3200 break;
3201
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003202 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003203 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003204 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003205 break;
3206
Willy Tarreau51347ed2013-06-11 19:34:13 +02003207 case HTTP_RES_ACT_SET_MARK:
3208#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003209 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003210 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003211#endif
3212 break;
3213
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003214 case HTTP_RES_ACT_SET_LOGL:
3215 s->logs.level = rule->arg.loglevel;
3216 break;
3217
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003218 case HTTP_RES_ACT_SET_HDR:
3219 ctx.idx = 0;
3220 /* remove all occurrences of the header */
3221 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3222 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3223 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3224 }
3225 /* now fall through to header addition */
3226
3227 case HTTP_RES_ACT_ADD_HDR:
3228 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3229 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3230 trash.len = rule->arg.hdr_add.name_len;
3231 trash.str[trash.len++] = ':';
3232 trash.str[trash.len++] = ' ';
3233 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3234 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3235 break;
3236 }
3237 }
3238
3239 /* we reached the end of the rules, nothing to report */
3240 return NULL;
3241}
3242
3243
Willy Tarreau71241ab2012-12-27 11:30:54 +01003244/* Perform an HTTP redirect based on the information in <rule>. The function
3245 * returns non-zero on success, or zero in case of a, irrecoverable error such
3246 * as too large a request to build a valid response.
3247 */
3248static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3249{
3250 struct http_msg *msg = &txn->req;
3251 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003252 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003253
3254 /* build redirect message */
3255 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003256 case 308:
3257 msg_fmt = HTTP_308;
3258 break;
3259 case 307:
3260 msg_fmt = HTTP_307;
3261 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003262 case 303:
3263 msg_fmt = HTTP_303;
3264 break;
3265 case 301:
3266 msg_fmt = HTTP_301;
3267 break;
3268 case 302:
3269 default:
3270 msg_fmt = HTTP_302;
3271 break;
3272 }
3273
3274 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3275 return 0;
3276
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003277 location = trash.str + trash.len;
3278
Willy Tarreau71241ab2012-12-27 11:30:54 +01003279 switch(rule->type) {
3280 case REDIRECT_TYPE_SCHEME: {
3281 const char *path;
3282 const char *host;
3283 struct hdr_ctx ctx;
3284 int pathlen;
3285 int hostlen;
3286
3287 host = "";
3288 hostlen = 0;
3289 ctx.idx = 0;
3290 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3291 host = ctx.line + ctx.val;
3292 hostlen = ctx.vlen;
3293 }
3294
3295 path = http_get_path(txn);
3296 /* build message using path */
3297 if (path) {
3298 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3299 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3300 int qs = 0;
3301 while (qs < pathlen) {
3302 if (path[qs] == '?') {
3303 pathlen = qs;
3304 break;
3305 }
3306 qs++;
3307 }
3308 }
3309 } else {
3310 path = "/";
3311 pathlen = 1;
3312 }
3313
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003314 if (rule->rdr_str) { /* this is an old "redirect" rule */
3315 /* check if we can add scheme + "://" + host + path */
3316 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3317 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003318
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003319 /* add scheme */
3320 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3321 trash.len += rule->rdr_len;
3322 }
3323 else {
3324 /* add scheme with executing log format */
3325 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003326
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003327 /* check if we can add scheme + "://" + host + path */
3328 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3329 return 0;
3330 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003331 /* add "://" */
3332 memcpy(trash.str + trash.len, "://", 3);
3333 trash.len += 3;
3334
3335 /* add host */
3336 memcpy(trash.str + trash.len, host, hostlen);
3337 trash.len += hostlen;
3338
3339 /* add path */
3340 memcpy(trash.str + trash.len, path, pathlen);
3341 trash.len += pathlen;
3342
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003343 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003344 if (trash.len && trash.str[trash.len - 1] != '/' &&
3345 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3346 if (trash.len > trash.size - 5)
3347 return 0;
3348 trash.str[trash.len] = '/';
3349 trash.len++;
3350 }
3351
3352 break;
3353 }
3354 case REDIRECT_TYPE_PREFIX: {
3355 const char *path;
3356 int pathlen;
3357
3358 path = http_get_path(txn);
3359 /* build message using path */
3360 if (path) {
3361 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3362 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3363 int qs = 0;
3364 while (qs < pathlen) {
3365 if (path[qs] == '?') {
3366 pathlen = qs;
3367 break;
3368 }
3369 qs++;
3370 }
3371 }
3372 } else {
3373 path = "/";
3374 pathlen = 1;
3375 }
3376
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003377 if (rule->rdr_str) { /* this is an old "redirect" rule */
3378 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3379 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003380
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003381 /* add prefix. Note that if prefix == "/", we don't want to
3382 * add anything, otherwise it makes it hard for the user to
3383 * configure a self-redirection.
3384 */
3385 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3386 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3387 trash.len += rule->rdr_len;
3388 }
3389 }
3390 else {
3391 /* add prefix with executing log format */
3392 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3393
3394 /* Check length */
3395 if (trash.len + pathlen > trash.size - 4)
3396 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003397 }
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_LOCATION:
3415 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003416 if (rule->rdr_str) { /* this is an old "redirect" rule */
3417 if (trash.len + rule->rdr_len > trash.size - 4)
3418 return 0;
3419
3420 /* add location */
3421 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3422 trash.len += rule->rdr_len;
3423 }
3424 else {
3425 /* add location with executing log format */
3426 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003427
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003428 /* Check left length */
3429 if (trash.len > trash.size - 4)
3430 return 0;
3431 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003432 break;
3433 }
3434
3435 if (rule->cookie_len) {
3436 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3437 trash.len += 14;
3438 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3439 trash.len += rule->cookie_len;
3440 memcpy(trash.str + trash.len, "\r\n", 2);
3441 trash.len += 2;
3442 }
3443
3444 /* add end of headers and the keep-alive/close status.
3445 * We may choose to set keep-alive if the Location begins
3446 * with a slash, because the client will come back to the
3447 * same server.
3448 */
3449 txn->status = rule->code;
3450 /* let's log the request time */
3451 s->logs.tv_request = now;
3452
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003453 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003454 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3455 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3456 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3457 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3458 /* keep-alive possible */
3459 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3460 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3461 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3462 trash.len += 30;
3463 } else {
3464 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3465 trash.len += 24;
3466 }
3467 }
3468 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3469 trash.len += 4;
3470 bo_inject(txn->rsp.chn, trash.str, trash.len);
3471 /* "eat" the request */
3472 bi_fast_delete(txn->req.chn->buf, msg->sov);
3473 msg->sov = 0;
3474 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3475 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3476 txn->req.msg_state = HTTP_MSG_CLOSED;
3477 txn->rsp.msg_state = HTTP_MSG_DONE;
3478 } else {
3479 /* keep-alive not possible */
3480 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3481 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3482 trash.len += 29;
3483 } else {
3484 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3485 trash.len += 23;
3486 }
3487 stream_int_retnclose(txn->req.chn->prod, &trash);
3488 txn->req.chn->analysers = 0;
3489 }
3490
3491 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003492 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003493 if (!(s->flags & SN_FINST_MASK))
3494 s->flags |= SN_FINST_R;
3495
3496 return 1;
3497}
3498
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003499/* This stream analyser runs all HTTP request processing which is common to
3500 * frontends and backends, which means blocking ACLs, filters, connection-close,
3501 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003502 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003503 * either needs more data or wants to immediately abort the request (eg: deny,
3504 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003505 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003506int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003507{
Willy Tarreaud787e662009-07-07 10:14:51 +02003508 struct http_txn *txn = &s->txn;
3509 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003510 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003511 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003512 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003513 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003514
Willy Tarreau655dce92009-11-08 13:10:58 +01003515 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003516 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003517 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003518 return 0;
3519 }
3520
Willy Tarreau3a816292009-07-07 10:55:49 +02003521 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003522 req->analyse_exp = TICK_ETERNITY;
3523
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003524 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 +02003525 now_ms, __FUNCTION__,
3526 s,
3527 req,
3528 req->rex, req->wex,
3529 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003530 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003531 req->analysers);
3532
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003533 /* first check whether we have some ACLs set to block this request */
3534 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003535 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003536
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003537 ret = acl_pass(ret);
3538 if (cond->pol == ACL_COND_UNLESS)
3539 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003540
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003541 if (ret) {
3542 txn->status = 403;
3543 /* let's log the request time */
3544 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003545 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003546 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003547 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003548 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003549 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003550
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003551 /* just in case we have some per-backend tracking */
3552 session_inc_be_http_req_ctr(s);
3553
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003554 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003555 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003556
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003557 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003558 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003559 if (stats_check_uri(s->rep->prod, txn, px)) {
3560 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003561 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3562 txn->status = 500;
3563 s->logs.tv_request = now;
3564 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003565
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003566 if (!(s->flags & SN_ERR_MASK))
3567 s->flags |= SN_ERR_RESOURCE;
3568 goto return_prx_cond;
3569 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003570 /* parse the whole stats request and extract the relevant information */
3571 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003572 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 +01003573 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003574 }
3575
Willy Tarreau3b44e722013-11-16 10:28:23 +01003576 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3577 * blocked by an http-request rule.
3578 */
3579 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003580 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003581 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003582 }
Willy Tarreau06619262006-12-17 08:37:22 +01003583
Willy Tarreau3b44e722013-11-16 10:28:23 +01003584 /* return a 403 if either rule has blocked */
3585 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003586 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003587 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003588 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003589 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003590 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003591 s->fe->fe_counters.denied_req++;
3592 if (s->fe != s->be)
3593 s->be->be_counters.denied_req++;
3594 if (s->listener->counters)
3595 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003596 goto return_prx_cond;
3597 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003598
3599 /* When a connection is tarpitted, we use the tarpit timeout,
3600 * which may be the same as the connect timeout if unspecified.
3601 * If unset, then set it to zero because we really want it to
3602 * eventually expire. We build the tarpit as an analyser.
3603 */
3604 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003605 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003606 /* wipe the request out so that we can drop the connection early
3607 * if the client closes first.
3608 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003609 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003610 req->analysers = 0; /* remove switching rules etc... */
3611 req->analysers |= AN_REQ_HTTP_TARPIT;
3612 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3613 if (!req->analyse_exp)
3614 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003615 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003616 s->fe->fe_counters.denied_req++;
3617 if (s->fe != s->be)
3618 s->be->be_counters.denied_req++;
3619 if (s->listener->counters)
3620 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003621 return 1;
3622 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003623 }
Willy Tarreau06619262006-12-17 08:37:22 +01003624
Willy Tarreau70dffda2014-01-30 03:07:23 +01003625 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003626 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003627 * Option httpclose by itself sets tunnel mode where headers are mangled.
3628 * However, if another mode is set, it will affect it (eg: server-close/
3629 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3630 * if FE and BE have the same settings (common). The method consists in
3631 * checking if options changed between the two calls (implying that either
3632 * one is non-null, or one of them is non-null and we are there for the first
3633 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003634 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003635
Willy Tarreau416ce612014-01-31 15:45:34 +01003636 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003637 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003638 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003639
Willy Tarreau70dffda2014-01-30 03:07:23 +01003640 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3641 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3642 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3643 tmp = TX_CON_WANT_TUN;
3644
3645 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3646 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3647 tmp = TX_CON_WANT_TUN;
3648 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003649
3650 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003651 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3652 /* option httpclose + server_close => forceclose */
3653 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3654 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3655 tmp = TX_CON_WANT_CLO;
3656 else
3657 tmp = TX_CON_WANT_SCL;
3658 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003659
3660 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3661 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003662 tmp = TX_CON_WANT_CLO;
3663
Willy Tarreau5b154472009-12-21 20:11:07 +01003664 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3665 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003666
Willy Tarreau416ce612014-01-31 15:45:34 +01003667 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3668 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003669 /* parse the Connection header and possibly clean it */
3670 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003671 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003672 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3673 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003674 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003675 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003676 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003677 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003678 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003679
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003680 /* check if client or config asks for explicit close in KAL/SCL */
3681 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3682 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3683 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003684 (!(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 +01003685 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003686 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003687 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3688 }
Willy Tarreau78599912009-10-17 20:12:21 +02003689
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003690 /* we can be blocked here because the request needs to be authenticated,
3691 * either to pass or to access stats.
3692 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003693 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003694 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003695
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003696 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003697 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003699 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003700 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003701 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003702 /* on 401 we still count one error, because normal browsing
3703 * won't significantly increase the counter but brute force
3704 * attempts will.
3705 */
3706 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003707 goto return_prx_cond;
3708 }
3709
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003710 /* add request headers from the rule sets in the same order */
3711 list_for_each_entry(wl, &px->req_add, list) {
3712 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003713 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003714 ret = acl_pass(ret);
3715 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3716 ret = !ret;
3717 if (!ret)
3718 continue;
3719 }
3720
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003721 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003722 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003723 }
3724
3725 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3726 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3727 goto return_bad_req;
3728 req->analyse_exp = TICK_ETERNITY;
3729 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003730 }
3731
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003732 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003733 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003734 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3735 s->fe->fe_counters.intercepted_req++;
3736
3737 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3738 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3739 if (!(s->flags & SN_FINST_MASK))
3740 s->flags |= SN_FINST_R;
3741
3742 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003743 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003744 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003745 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003746
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003747 /* check whether we have some ACLs set to redirect this request */
3748 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003749 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003750 int ret;
3751
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003752 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003753 ret = acl_pass(ret);
3754 if (rule->cond->pol == ACL_COND_UNLESS)
3755 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003756 if (!ret)
3757 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003758 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003759 if (!http_apply_redirect_rule(rule, s, txn))
3760 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003761
Willy Tarreau71241ab2012-12-27 11:30:54 +01003762 req->analyse_exp = TICK_ETERNITY;
3763 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003764 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003765
Willy Tarreau2be39392010-01-03 17:24:51 +01003766 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3767 * If this happens, then the data will not come immediately, so we must
3768 * send all what we have without waiting. Note that due to the small gain
3769 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003770 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003771 * itself once used.
3772 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003773 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003774
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003775 /* that's OK for us now, let's move on to next analysers */
3776 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003777
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003778 return_bad_req:
3779 /* We centralize bad requests processing here */
3780 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3781 /* we detected a parsing error. We want to archive this request
3782 * in the dedicated proxy area for later troubleshooting.
3783 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003784 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003785 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003786
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003787 txn->req.msg_state = HTTP_MSG_ERROR;
3788 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003789 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003790
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003791 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003792 if (s->listener->counters)
3793 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003794
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003795 return_prx_cond:
3796 if (!(s->flags & SN_ERR_MASK))
3797 s->flags |= SN_ERR_PRXCOND;
3798 if (!(s->flags & SN_FINST_MASK))
3799 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003800
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003801 req->analysers = 0;
3802 req->analyse_exp = TICK_ETERNITY;
3803 return 0;
3804}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003805
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003806/* This function performs all the processing enabled for the current request.
3807 * It returns 1 if the processing can continue on next analysers, or zero if it
3808 * needs more data, encounters an error, or wants to immediately abort the
3809 * request. It relies on buffers flags, and updates s->req->analysers.
3810 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003811int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003812{
3813 struct http_txn *txn = &s->txn;
3814 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003815 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003816
Willy Tarreau655dce92009-11-08 13:10:58 +01003817 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003818 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003819 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003820 return 0;
3821 }
3822
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003823 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 +02003824 now_ms, __FUNCTION__,
3825 s,
3826 req,
3827 req->rex, req->wex,
3828 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003829 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003830 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003831
William Lallemand82fe75c2012-10-23 10:25:10 +02003832 if (s->fe->comp || s->be->comp)
3833 select_compression_request_header(s, req->buf);
3834
Willy Tarreau59234e92008-11-30 23:51:27 +01003835 /*
3836 * Right now, we know that we have processed the entire headers
3837 * and that unwanted requests have been filtered out. We can do
3838 * whatever we want with the remaining request. Also, now we
3839 * may have separate values for ->fe, ->be.
3840 */
Willy Tarreau06619262006-12-17 08:37:22 +01003841
Willy Tarreau59234e92008-11-30 23:51:27 +01003842 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003843 * If HTTP PROXY is set we simply get remote server address parsing
3844 * incoming request. Note that this requires that a connection is
3845 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003846 */
3847 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003848 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003849 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003850
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003851 /* Note that for now we don't reuse existing proxy connections */
3852 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003853 txn->req.msg_state = HTTP_MSG_ERROR;
3854 txn->status = 500;
3855 req->analysers = 0;
3856 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3857
3858 if (!(s->flags & SN_ERR_MASK))
3859 s->flags |= SN_ERR_RESOURCE;
3860 if (!(s->flags & SN_FINST_MASK))
3861 s->flags |= SN_FINST_R;
3862
3863 return 0;
3864 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003865
3866 path = http_get_path(txn);
3867 url2sa(req->buf->p + msg->sl.rq.u,
3868 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3869 &conn->addr.to);
3870 /* if the path was found, we have to remove everything between
3871 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3872 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3873 * u_l characters by a single "/".
3874 */
3875 if (path) {
3876 char *cur_ptr = req->buf->p;
3877 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3878 int delta;
3879
3880 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3881 http_msg_move_end(&txn->req, delta);
3882 cur_end += delta;
3883 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3884 goto return_bad_req;
3885 }
3886 else {
3887 char *cur_ptr = req->buf->p;
3888 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3889 int delta;
3890
3891 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3892 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3893 http_msg_move_end(&txn->req, delta);
3894 cur_end += delta;
3895 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3896 goto return_bad_req;
3897 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003898 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003899
Willy Tarreau59234e92008-11-30 23:51:27 +01003900 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003901 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003902 * Note that doing so might move headers in the request, but
3903 * the fields will stay coherent and the URI will not move.
3904 * This should only be performed in the backend.
3905 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003906 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003907 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3908 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003909
Willy Tarreau59234e92008-11-30 23:51:27 +01003910 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003911 * 8: the appsession cookie was looked up very early in 1.2,
3912 * so let's do the same now.
3913 */
3914
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003915 /* It needs to look into the URI unless persistence must be ignored */
3916 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003917 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 +01003918 }
3919
William Lallemanda73203e2012-03-12 12:48:57 +01003920 /* add unique-id if "header-unique-id" is specified */
3921
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003922 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3923 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3924 goto return_bad_req;
3925 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003926 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003927 }
William Lallemanda73203e2012-03-12 12:48:57 +01003928
3929 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003930 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3931 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003932 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003933 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003934 goto return_bad_req;
3935 }
3936
Cyril Bontéb21570a2009-11-29 20:04:48 +01003937 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003938 * 9: add X-Forwarded-For if either the frontend or the backend
3939 * asks for it.
3940 */
3941 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003942 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003943 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003944 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3945 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003946 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003947 /* The header is set to be added only if none is present
3948 * and we found it, so don't do anything.
3949 */
3950 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003951 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003952 /* Add an X-Forwarded-For header unless the source IP is
3953 * in the 'except' network range.
3954 */
3955 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003956 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003957 != s->fe->except_net.s_addr) &&
3958 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003959 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003960 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003961 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003962 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003963 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003964
3965 /* Note: we rely on the backend to get the header name to be used for
3966 * x-forwarded-for, because the header is really meant for the backends.
3967 * However, if the backend did not specify any option, we have to rely
3968 * on the frontend's header name.
3969 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003970 if (s->be->fwdfor_hdr_len) {
3971 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003972 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003973 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003974 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003975 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003976 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003977 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003978
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003979 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003980 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003981 }
3982 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003983 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003984 /* FIXME: for the sake of completeness, we should also support
3985 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003986 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003987 int len;
3988 char pn[INET6_ADDRSTRLEN];
3989 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003990 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003991 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003992
Willy Tarreau59234e92008-11-30 23:51:27 +01003993 /* Note: we rely on the backend to get the header name to be used for
3994 * x-forwarded-for, because the header is really meant for the backends.
3995 * However, if the backend did not specify any option, we have to rely
3996 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003997 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003998 if (s->be->fwdfor_hdr_len) {
3999 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004000 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004001 } else {
4002 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004003 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004004 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004005 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004006
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004007 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004008 goto return_bad_req;
4009 }
4010 }
4011
4012 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004013 * 10: add X-Original-To if either the frontend or the backend
4014 * asks for it.
4015 */
4016 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4017
4018 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004019 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004020 /* Add an X-Original-To header unless the destination IP is
4021 * in the 'except' network range.
4022 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004023 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004024
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004025 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004026 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004027 (((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 +02004028 != s->fe->except_to.s_addr) &&
4029 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004030 (((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 +02004031 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004032 int len;
4033 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004034 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004035
4036 /* Note: we rely on the backend to get the header name to be used for
4037 * x-original-to, because the header is really meant for the backends.
4038 * However, if the backend did not specify any option, we have to rely
4039 * on the frontend's header name.
4040 */
4041 if (s->be->orgto_hdr_len) {
4042 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004043 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004044 } else {
4045 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004046 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004047 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004048 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004049
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004050 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004051 goto return_bad_req;
4052 }
4053 }
4054 }
4055
Willy Tarreau50fc7772012-11-11 22:19:57 +01004056 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4057 * If an "Upgrade" token is found, the header is left untouched in order not to have
4058 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4059 * "Upgrade" is present in the Connection header.
4060 */
4061 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4062 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004063 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4064 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004065 unsigned int want_flags = 0;
4066
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004067 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004068 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004069 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4070 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004071 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004072 want_flags |= TX_CON_CLO_SET;
4073 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004074 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004075 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4076 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004077 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004078 want_flags |= TX_CON_KAL_SET;
4079 }
4080
4081 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004082 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004083 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004084
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004085
Willy Tarreau522d6c02009-12-06 18:49:18 +01004086 /* If we have no server assigned yet and we're balancing on url_param
4087 * with a POST request, we may be interested in checking the body for
4088 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004089 */
4090 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4091 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004092 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004093 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004094 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004095 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004096 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004097
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004098 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004099 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004100#ifdef TCP_QUICKACK
4101 /* We expect some data from the client. Unless we know for sure
4102 * we already have a full request, we have to re-enable quick-ack
4103 * in case we previously disabled it, otherwise we might cause
4104 * the client to delay further data.
4105 */
4106 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004107 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004108 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004109 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004110 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004111#endif
4112 }
Willy Tarreau03945942009-12-22 16:50:27 +01004113
Willy Tarreau59234e92008-11-30 23:51:27 +01004114 /*************************************************************
4115 * OK, that's finished for the headers. We have done what we *
4116 * could. Let's switch to the DATA state. *
4117 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004118 req->analyse_exp = TICK_ETERNITY;
4119 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004120
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004121 /* if the server closes the connection, we want to immediately react
4122 * and close the socket to save packets and syscalls.
4123 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004124 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4125 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004126
Willy Tarreau59234e92008-11-30 23:51:27 +01004127 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004128 /* OK let's go on with the BODY now */
4129 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004130
Willy Tarreau59234e92008-11-30 23:51:27 +01004131 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004132 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004133 /* we detected a parsing error. We want to archive this request
4134 * in the dedicated proxy area for later troubleshooting.
4135 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004136 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004137 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004138
Willy Tarreau59234e92008-11-30 23:51:27 +01004139 txn->req.msg_state = HTTP_MSG_ERROR;
4140 txn->status = 400;
4141 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004142 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004143
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004144 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004145 if (s->listener->counters)
4146 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004147
Willy Tarreau59234e92008-11-30 23:51:27 +01004148 if (!(s->flags & SN_ERR_MASK))
4149 s->flags |= SN_ERR_PRXCOND;
4150 if (!(s->flags & SN_FINST_MASK))
4151 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004152 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004153}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004154
Willy Tarreau60b85b02008-11-30 23:28:40 +01004155/* This function is an analyser which processes the HTTP tarpit. It always
4156 * returns zero, at the beginning because it prevents any other processing
4157 * from occurring, and at the end because it terminates the request.
4158 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004159int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004160{
4161 struct http_txn *txn = &s->txn;
4162
4163 /* This connection is being tarpitted. The CLIENT side has
4164 * already set the connect expiration date to the right
4165 * timeout. We just have to check that the client is still
4166 * there and that the timeout has not expired.
4167 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004168 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004169 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004170 !tick_is_expired(req->analyse_exp, now_ms))
4171 return 0;
4172
4173 /* We will set the queue timer to the time spent, just for
4174 * logging purposes. We fake a 500 server error, so that the
4175 * attacker will not suspect his connection has been tarpitted.
4176 * It will not cause trouble to the logs because we can exclude
4177 * the tarpitted connections by filtering on the 'PT' status flags.
4178 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004179 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4180
4181 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004182 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004183 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004184
4185 req->analysers = 0;
4186 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004187
Willy Tarreau60b85b02008-11-30 23:28:40 +01004188 if (!(s->flags & SN_ERR_MASK))
4189 s->flags |= SN_ERR_PRXCOND;
4190 if (!(s->flags & SN_FINST_MASK))
4191 s->flags |= SN_FINST_T;
4192 return 0;
4193}
4194
Willy Tarreaud34af782008-11-30 23:36:37 +01004195/* This function is an analyser which processes the HTTP request body. It looks
4196 * for parameters to be used for the load balancing algorithm (url_param). It
4197 * must only be called after the standard HTTP request processing has occurred,
4198 * because it expects the request to be parsed. It returns zero if it needs to
4199 * read more data, or 1 once it has completed its analysis.
4200 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004201int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004202{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004203 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004204 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004205 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004206
4207 /* We have to parse the HTTP request body to find any required data.
4208 * "balance url_param check_post" should have been the only way to get
4209 * into this. We were brought here after HTTP header analysis, so all
4210 * related structures are ready.
4211 */
4212
Willy Tarreau522d6c02009-12-06 18:49:18 +01004213 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4214 goto missing_data;
4215
4216 if (msg->msg_state < HTTP_MSG_100_SENT) {
4217 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4218 * send an HTTP/1.1 100 Continue intermediate response.
4219 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004220 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004221 struct hdr_ctx ctx;
4222 ctx.idx = 0;
4223 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004224 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004225 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004226 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004227 }
4228 }
4229 msg->msg_state = HTTP_MSG_100_SENT;
4230 }
4231
4232 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004233 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004234 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004235 * is still null. We must save the body in msg->next because it
4236 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004237 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004238 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004239
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004240 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004241 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4242 else
4243 msg->msg_state = HTTP_MSG_DATA;
4244 }
4245
4246 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004247 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004248 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004249 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004250 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004251 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004252
Willy Tarreau115acb92009-12-26 13:56:06 +01004253 if (!ret)
4254 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004255 else if (ret < 0) {
4256 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004257 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004258 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004259 }
4260
Willy Tarreaud98cf932009-12-27 22:54:55 +01004261 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004262 * We have the first data byte is in msg->sov. We're waiting for at
4263 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004264 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004265
Willy Tarreau124d9912011-03-01 20:30:48 +01004266 if (msg->body_len < limit)
4267 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004268
Willy Tarreau9b28e032012-10-12 23:49:43 +02004269 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004270 goto http_end;
4271
4272 missing_data:
4273 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004274 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004275 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004276 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004277 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004278
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004279 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004280 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004281 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004282
4283 if (!(s->flags & SN_ERR_MASK))
4284 s->flags |= SN_ERR_CLITO;
4285 if (!(s->flags & SN_FINST_MASK))
4286 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004287 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004288 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004289
4290 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004291 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004292 /* Not enough data. We'll re-use the http-request
4293 * timeout here. Ideally, we should set the timeout
4294 * relative to the accept() date. We just set the
4295 * request timeout once at the beginning of the
4296 * request.
4297 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004298 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004299 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004300 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004301 return 0;
4302 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004303
4304 http_end:
4305 /* The situation will not evolve, so let's give up on the analysis. */
4306 s->logs.tv_request = now; /* update the request timer to reflect full request */
4307 req->analysers &= ~an_bit;
4308 req->analyse_exp = TICK_ETERNITY;
4309 return 1;
4310
4311 return_bad_req: /* let's centralize all bad requests */
4312 txn->req.msg_state = HTTP_MSG_ERROR;
4313 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004314 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004315
Willy Tarreau79ebac62010-06-07 13:47:49 +02004316 if (!(s->flags & SN_ERR_MASK))
4317 s->flags |= SN_ERR_PRXCOND;
4318 if (!(s->flags & SN_FINST_MASK))
4319 s->flags |= SN_FINST_R;
4320
Willy Tarreau522d6c02009-12-06 18:49:18 +01004321 return_err_msg:
4322 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004323 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004324 if (s->listener->counters)
4325 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004326 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004327}
4328
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004329/* send a server's name with an outgoing request over an established connection.
4330 * Note: this function is designed to be called once the request has been scheduled
4331 * for being forwarded. This is the reason why it rewinds the buffer before
4332 * proceeding.
4333 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004334int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004335
4336 struct hdr_ctx ctx;
4337
Mark Lamourinec2247f02012-01-04 13:02:01 -05004338 char *hdr_name = be->server_id_hdr_name;
4339 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004340 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004341 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004342 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004343
William Lallemandd9e90662012-01-30 17:27:17 +01004344 ctx.idx = 0;
4345
Willy Tarreau9b28e032012-10-12 23:49:43 +02004346 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004347 if (old_o) {
4348 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004349 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004350 }
4351
Willy Tarreau9b28e032012-10-12 23:49:43 +02004352 old_i = chn->buf->i;
4353 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 -05004354 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004355 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004356 }
4357
4358 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004359 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004360 memcpy(hdr_val, hdr_name, hdr_name_len);
4361 hdr_val += hdr_name_len;
4362 *hdr_val++ = ':';
4363 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004364 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4365 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004366
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004367 if (old_o) {
4368 /* If this was a forwarded request, we must readjust the amount of
4369 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004370 * variations. Note that if the request was already scheduled for
4371 * forwarding, it had its req->sol pointing to the body, which
4372 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004373 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004374 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004375 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004376 }
4377
Mark Lamourinec2247f02012-01-04 13:02:01 -05004378 return 0;
4379}
4380
Willy Tarreau610ecce2010-01-04 21:15:02 +01004381/* Terminate current transaction and prepare a new one. This is very tricky
4382 * right now but it works.
4383 */
4384void http_end_txn_clean_session(struct session *s)
4385{
Willy Tarreau068621e2013-12-23 15:11:25 +01004386 int prev_status = s->txn.status;
4387
Willy Tarreau610ecce2010-01-04 21:15:02 +01004388 /* FIXME: We need a more portable way of releasing a backend's and a
4389 * server's connections. We need a safer way to reinitialize buffer
4390 * flags. We also need a more accurate method for computing per-request
4391 * data.
4392 */
4393 http_silent_debug(__LINE__, s);
4394
Willy Tarreau4213a112013-12-15 10:25:42 +01004395 /* unless we're doing keep-alive, we want to quickly close the connection
4396 * to the server.
4397 */
4398 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4399 !si_conn_ready(s->req->cons)) {
4400 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4401 si_shutr(s->req->cons);
4402 si_shutw(s->req->cons);
4403 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404
4405 http_silent_debug(__LINE__, s);
4406
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004407 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004408 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004409 if (unlikely(s->srv_conn))
4410 sess_change_server(s, NULL);
4411 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412
4413 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4414 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004415 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004416
4417 if (s->txn.status) {
4418 int n;
4419
4420 n = s->txn.status / 100;
4421 if (n < 1 || n > 5)
4422 n = 0;
4423
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004424 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004425 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004426 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004427 s->fe->fe_counters.p.http.comp_rsp++;
4428 }
Willy Tarreau24657792010-02-26 10:30:28 +01004429 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004430 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004431 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004432 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004433 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004434 s->be->be_counters.p.http.comp_rsp++;
4435 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004436 }
4437
4438 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004439 s->logs.bytes_in -= s->req->buf->i;
4440 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441
4442 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004443 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 !(s->flags & SN_MONITOR) &&
4445 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4446 s->do_log(s);
4447 }
4448
4449 s->logs.accept_date = date; /* user-visible date for logging */
4450 s->logs.tv_accept = now; /* corrected date for internal use */
4451 tv_zero(&s->logs.tv_request);
4452 s->logs.t_queue = -1;
4453 s->logs.t_connect = -1;
4454 s->logs.t_data = -1;
4455 s->logs.t_close = 0;
4456 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4457 s->logs.srv_queue_size = 0; /* we will get this number soon */
4458
Willy Tarreau9b28e032012-10-12 23:49:43 +02004459 s->logs.bytes_in = s->req->total = s->req->buf->i;
4460 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004461
4462 if (s->pend_pos)
4463 pendconn_free(s->pend_pos);
4464
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004465 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004466 if (s->flags & SN_CURR_SESS) {
4467 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004468 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004469 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004470 if (may_dequeue_tasks(objt_server(s->target), s->be))
4471 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004472 }
4473
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004474 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004475
Willy Tarreau4213a112013-12-15 10:25:42 +01004476 /* only release our endpoint if we don't intend to reuse the
4477 * connection.
4478 */
4479 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4480 !si_conn_ready(s->req->cons)) {
4481 si_release_endpoint(s->req->cons);
4482 }
4483
Willy Tarreau610ecce2010-01-04 21:15:02 +01004484 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004486 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004487 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004488 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004489 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4490 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 +02004491 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 +01004492 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004493
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 s->txn.meth = 0;
4495 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004496 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004497
4498 if (prev_status == 401 || prev_status == 407) {
4499 /* In HTTP keep-alive mode, if we receive a 401, we still have
4500 * a chance of being able to send the visitor again to the same
4501 * server over the same connection. This is required by some
4502 * broken protocols such as NTLM, and anyway whenever there is
4503 * an opportunity for sending the challenge to the proper place,
4504 * it's better to do it (at least it helps with debugging).
4505 */
4506 s->txn.flags |= TX_PREFER_LAST;
4507 }
4508
Willy Tarreauee55dc02010-06-01 10:56:34 +02004509 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004510 s->req->cons->flags |= SI_FL_INDEP_STR;
4511
Willy Tarreau96e31212011-05-30 18:10:30 +02004512 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004513 s->req->flags |= CF_NEVER_WAIT;
4514 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004515 }
4516
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517 /* if the request buffer is not empty, it means we're
4518 * about to process another request, so send pending
4519 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004520 * Just don't do this if the buffer is close to be full,
4521 * because the request will wait for it to flush a little
4522 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004524 if (s->req->buf->i) {
4525 if (s->rep->buf->o &&
4526 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4527 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004528 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004529 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004530
4531 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004532 channel_auto_read(s->req);
4533 channel_auto_close(s->req);
4534 channel_auto_read(s->rep);
4535 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536
Willy Tarreau27375622013-12-17 00:00:28 +01004537 /* we're in keep-alive with an idle connection, monitor it */
4538 si_idle_conn(s->req->cons);
4539
Willy Tarreau342b11c2010-11-24 16:22:09 +01004540 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004541 s->rep->analysers = 0;
4542
4543 http_silent_debug(__LINE__, s);
4544}
4545
4546
4547/* This function updates the request state machine according to the response
4548 * state machine and buffer flags. It returns 1 if it changes anything (flag
4549 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4550 * it is only used to find when a request/response couple is complete. Both
4551 * this function and its equivalent should loop until both return zero. It
4552 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4553 */
4554int http_sync_req_state(struct session *s)
4555{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004556 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004557 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004558 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559 unsigned int old_state = txn->req.msg_state;
4560
4561 http_silent_debug(__LINE__, s);
4562 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4563 return 0;
4564
4565 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004566 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004567 * We can shut the read side unless we want to abort_on_close,
4568 * or we have a POST request. The issue with POST requests is
4569 * that some browsers still send a CRLF after the request, and
4570 * this CRLF must be read so that it does not remain in the kernel
4571 * buffers, otherwise a close could cause an RST on some systems
4572 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004573 * Note that if we're using keep-alive on the client side, we'd
4574 * rather poll now and keep the polling enabled for the whole
4575 * session's life than enabling/disabling it between each
4576 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004577 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004578 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4579 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4580 !(s->be->options & PR_O_ABRT_CLOSE) &&
4581 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004582 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583
Willy Tarreau40f151a2012-12-20 12:10:09 +01004584 /* if the server closes the connection, we want to immediately react
4585 * and close the socket to save packets and syscalls.
4586 */
4587 chn->cons->flags |= SI_FL_NOHALF;
4588
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4590 goto wait_other_side;
4591
4592 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4593 /* The server has not finished to respond, so we
4594 * don't want to move in order not to upset it.
4595 */
4596 goto wait_other_side;
4597 }
4598
4599 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4600 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004603 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004604 goto wait_other_side;
4605 }
4606
4607 /* When we get here, it means that both the request and the
4608 * response have finished receiving. Depending on the connection
4609 * mode, we'll have to wait for the last bytes to leave in either
4610 * direction, and sometimes for a close to be effective.
4611 */
4612
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004613 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4614 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004615 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4616 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004617 }
4618 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4619 /* Option forceclose is set, or either side wants to close,
4620 * let's enforce it now that we're not expecting any new
4621 * data to come. The caller knows the session is complete
4622 * once both states are CLOSED.
4623 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004624 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4625 channel_shutr_now(chn);
4626 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004628 }
4629 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004630 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4631 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004632 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004633 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4634 channel_auto_read(chn);
4635 txn->req.msg_state = HTTP_MSG_TUNNEL;
4636 chn->flags |= CF_NEVER_WAIT;
4637 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004638 }
4639
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004640 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004642 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004643
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004644 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645 txn->req.msg_state = HTTP_MSG_CLOSING;
4646 goto http_msg_closing;
4647 }
4648 else {
4649 txn->req.msg_state = HTTP_MSG_CLOSED;
4650 goto http_msg_closed;
4651 }
4652 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004653 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004654 }
4655
4656 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4657 http_msg_closing:
4658 /* nothing else to forward, just waiting for the output buffer
4659 * to be empty and for the shutw_now to take effect.
4660 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004661 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 txn->req.msg_state = HTTP_MSG_CLOSED;
4663 goto http_msg_closed;
4664 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004665 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004666 txn->req.msg_state = HTTP_MSG_ERROR;
4667 goto wait_other_side;
4668 }
4669 }
4670
4671 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4672 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004673 /* see above in MSG_DONE why we only do this in these states */
4674 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4675 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4676 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004677 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 goto wait_other_side;
4679 }
4680
4681 wait_other_side:
4682 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004683 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684}
4685
4686
4687/* This function updates the response state machine according to the request
4688 * state machine and buffer flags. It returns 1 if it changes anything (flag
4689 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4690 * it is only used to find when a request/response couple is complete. Both
4691 * this function and its equivalent should loop until both return zero. It
4692 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4693 */
4694int http_sync_res_state(struct session *s)
4695{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004696 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004698 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004699 unsigned int old_state = txn->rsp.msg_state;
4700
4701 http_silent_debug(__LINE__, s);
4702 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4703 return 0;
4704
4705 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4706 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004707 * still monitor the server connection for a possible close
4708 * while the request is being uploaded, so we don't disable
4709 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004711 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004712
4713 if (txn->req.msg_state == HTTP_MSG_ERROR)
4714 goto wait_other_side;
4715
4716 if (txn->req.msg_state < HTTP_MSG_DONE) {
4717 /* The client seems to still be sending data, probably
4718 * because we got an error response during an upload.
4719 * We have the choice of either breaking the connection
4720 * or letting it pass through. Let's do the later.
4721 */
4722 goto wait_other_side;
4723 }
4724
4725 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4726 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004727 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004728 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004729 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 goto wait_other_side;
4731 }
4732
4733 /* When we get here, it means that both the request and the
4734 * response have finished receiving. Depending on the connection
4735 * mode, we'll have to wait for the last bytes to leave in either
4736 * direction, and sometimes for a close to be effective.
4737 */
4738
4739 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4740 /* Server-close mode : shut read and wait for the request
4741 * side to close its output buffer. The caller will detect
4742 * when we're in DONE and the other is in CLOSED and will
4743 * catch that for the final cleanup.
4744 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004745 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4746 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004747 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004748 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4749 /* Option forceclose is set, or either side wants to close,
4750 * let's enforce it now that we're not expecting any new
4751 * data to come. The caller knows the session is complete
4752 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004753 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004754 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4755 channel_shutr_now(chn);
4756 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004757 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004758 }
4759 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004760 /* The last possible modes are keep-alive and tunnel. Tunnel will
4761 * need to forward remaining data. Keep-alive will need to monitor
4762 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004763 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004764 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004765 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004766 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4767 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004768 }
4769
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004770 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004771 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004772 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4774 goto http_msg_closing;
4775 }
4776 else {
4777 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4778 goto http_msg_closed;
4779 }
4780 }
4781 goto wait_other_side;
4782 }
4783
4784 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4785 http_msg_closing:
4786 /* nothing else to forward, just waiting for the output buffer
4787 * to be empty and for the shutw_now to take effect.
4788 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004789 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004790 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4791 goto http_msg_closed;
4792 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004793 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004794 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004795 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004796 if (objt_server(s->target))
4797 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798 goto wait_other_side;
4799 }
4800 }
4801
4802 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4803 http_msg_closed:
4804 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004805 bi_erase(chn);
4806 channel_auto_close(chn);
4807 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004808 goto wait_other_side;
4809 }
4810
4811 wait_other_side:
4812 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004813 /* We force the response to leave immediately if we're waiting for the
4814 * other side, since there is no pending shutdown to push it out.
4815 */
4816 if (!channel_is_empty(chn))
4817 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004818 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004819}
4820
4821
4822/* Resync the request and response state machines. Return 1 if either state
4823 * changes.
4824 */
4825int http_resync_states(struct session *s)
4826{
4827 struct http_txn *txn = &s->txn;
4828 int old_req_state = txn->req.msg_state;
4829 int old_res_state = txn->rsp.msg_state;
4830
4831 http_silent_debug(__LINE__, s);
4832 http_sync_req_state(s);
4833 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004834 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004835 if (!http_sync_res_state(s))
4836 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004837 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838 if (!http_sync_req_state(s))
4839 break;
4840 }
4841 http_silent_debug(__LINE__, s);
4842 /* OK, both state machines agree on a compatible state.
4843 * There are a few cases we're interested in :
4844 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4845 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4846 * directions, so let's simply disable both analysers.
4847 * - HTTP_MSG_CLOSED on the response only means we must abort the
4848 * request.
4849 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4850 * with server-close mode means we've completed one request and we
4851 * must re-initialize the server connection.
4852 */
4853
4854 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4855 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4856 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4857 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4858 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004859 channel_auto_close(s->req);
4860 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004861 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004862 channel_auto_close(s->rep);
4863 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004864 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004865 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4866 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004867 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004868 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004869 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004870 channel_auto_close(s->rep);
4871 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004872 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004873 channel_abort(s->req);
4874 channel_auto_close(s->req);
4875 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004876 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004877 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004878 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4879 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004880 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004881 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4882 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4883 /* server-close/keep-alive: terminate this transaction,
4884 * possibly killing the server connection and reinitialize
4885 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004886 */
4887 http_end_txn_clean_session(s);
4888 }
4889
4890 http_silent_debug(__LINE__, s);
4891 return txn->req.msg_state != old_req_state ||
4892 txn->rsp.msg_state != old_res_state;
4893}
4894
Willy Tarreaud98cf932009-12-27 22:54:55 +01004895/* This function is an analyser which forwards request body (including chunk
4896 * sizes if any). It is called as soon as we must forward, even if we forward
4897 * zero byte. The only situation where it must not be called is when we're in
4898 * tunnel mode and we want to forward till the close. It's used both to forward
4899 * remaining data and to resync after end of body. It expects the msg_state to
4900 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4901 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004902 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004903 * bytes of pending data + the headers if not already done (between sol and sov).
4904 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004905 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004906int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004907{
4908 struct http_txn *txn = &s->txn;
4909 struct http_msg *msg = &s->txn.req;
4910
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004911 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4912 return 0;
4913
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004914 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004915 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004916 /* Output closed while we were sending data. We must abort and
4917 * wake the other side up.
4918 */
4919 msg->msg_state = HTTP_MSG_ERROR;
4920 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004921 return 1;
4922 }
4923
Willy Tarreau80a92c02014-03-12 10:41:13 +01004924 /* Some post-connect processing might want us to refrain from starting to
4925 * forward data. Currently, the only reason for this is "balance url_param"
4926 * whichs need to parse/process the request after we've enabled forwarding.
4927 */
4928 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
4929 if (!(s->rep->flags & CF_READ_ATTACHED)) {
4930 channel_auto_connect(req);
4931 goto missing_data;
4932 }
4933 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4934 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004935
4936 /* Note that we don't have to send 100-continue back because we don't
4937 * need the data to complete our job, and it's up to the server to
4938 * decide whether to return 100, 417 or anything else in return of
4939 * an "Expect: 100-continue" header.
4940 */
4941
4942 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004943 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004944 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004945 * is still null. We must save the body in msg->next because it
4946 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004947 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004948 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004949
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004950 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004952 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004953 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004954 }
4955
Willy Tarreau80a92c02014-03-12 10:41:13 +01004956 /* in most states, we should abort in case of early close */
4957 channel_auto_close(req);
4958
Willy Tarreaud98cf932009-12-27 22:54:55 +01004959 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004960 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004961
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004963 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004964 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004965 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004966 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004967 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004968 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004969 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004970 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004971
Willy Tarreaucaabe412010-01-03 23:08:28 +01004972 if (msg->msg_state == HTTP_MSG_DATA) {
4973 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004974 if (req->to_forward) {
4975 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004976 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004977 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01004978
4979 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004980 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004981 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004982 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004983 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004984 }
4985 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004986 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004987 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004988 * TRAILERS state.
4989 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004990 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004991
Willy Tarreau54d23df2012-10-25 19:04:45 +02004992 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004993 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004994 else if (ret < 0) {
4995 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004996 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004997 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004998 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004999 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005000 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005001 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005002 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005003 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005004 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005005
5006 if (ret == 0)
5007 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005008 else if (ret < 0) {
5009 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005010 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005011 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005012 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005013 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005014 /* we're in MSG_CHUNK_SIZE now */
5015 }
5016 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005017 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005018
5019 if (ret == 0)
5020 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005021 else if (ret < 0) {
5022 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005023 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005024 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005025 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005026 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005027 /* we're in HTTP_MSG_DONE now */
5028 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005029 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005030 int old_state = msg->msg_state;
5031
Willy Tarreau610ecce2010-01-04 21:15:02 +01005032 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005033 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005034 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5035 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005036 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037 if (http_resync_states(s)) {
5038 /* some state changes occurred, maybe the analyser
5039 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005040 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005041 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005042 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005043 /* request errors are most likely due to
5044 * the server aborting the transfer.
5045 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005046 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005047 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005048 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005049 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005050 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005051 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005052 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005053 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005054
5055 /* If "option abortonclose" is set on the backend, we
5056 * want to monitor the client's connection and forward
5057 * any shutdown notification to the server, which will
5058 * decide whether to close or to go on processing the
5059 * request.
5060 */
5061 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005062 channel_auto_read(req);
5063 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005064 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005065 else if (s->txn.meth == HTTP_METH_POST) {
5066 /* POST requests may require to read extra CRLF
5067 * sent by broken browsers and which could cause
5068 * an RST to be sent upon close on some systems
5069 * (eg: Linux).
5070 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005071 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005072 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005073
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005075 }
5076 }
5077
Willy Tarreaud98cf932009-12-27 22:54:55 +01005078 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005079 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005080 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005081 if (!(s->flags & SN_ERR_MASK))
5082 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005083 if (!(s->flags & SN_FINST_MASK)) {
5084 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5085 s->flags |= SN_FINST_H;
5086 else
5087 s->flags |= SN_FINST_D;
5088 }
5089
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005090 s->fe->fe_counters.cli_aborts++;
5091 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005092 if (objt_server(s->target))
5093 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005094
5095 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005096 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005097
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005098 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005099 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005100 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005102 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005103 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005104 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005105 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005106 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005107
Willy Tarreau5c620922011-05-11 19:56:11 +02005108 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005109 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005110 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005111 * modes are already handled by the stream sock layer. We must not do
5112 * this in content-length mode because it could present the MSG_MORE
5113 * flag with the last block of forwarded data, which would cause an
5114 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005115 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005116 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005117 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005118
Willy Tarreau610ecce2010-01-04 21:15:02 +01005119 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005120 return 0;
5121
Willy Tarreaud98cf932009-12-27 22:54:55 +01005122 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005123 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005124 if (s->listener->counters)
5125 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005126 return_bad_req_stats_ok:
5127 txn->req.msg_state = HTTP_MSG_ERROR;
5128 if (txn->status) {
5129 /* Note: we don't send any error if some data were already sent */
5130 stream_int_retnclose(req->prod, NULL);
5131 } else {
5132 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005133 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005134 }
5135 req->analysers = 0;
5136 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005137
5138 if (!(s->flags & SN_ERR_MASK))
5139 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005140 if (!(s->flags & SN_FINST_MASK)) {
5141 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5142 s->flags |= SN_FINST_H;
5143 else
5144 s->flags |= SN_FINST_D;
5145 }
5146 return 0;
5147
5148 aborted_xfer:
5149 txn->req.msg_state = HTTP_MSG_ERROR;
5150 if (txn->status) {
5151 /* Note: we don't send any error if some data were already sent */
5152 stream_int_retnclose(req->prod, NULL);
5153 } else {
5154 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005155 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005156 }
5157 req->analysers = 0;
5158 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5159
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005160 s->fe->fe_counters.srv_aborts++;
5161 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005162 if (objt_server(s->target))
5163 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005164
5165 if (!(s->flags & SN_ERR_MASK))
5166 s->flags |= SN_ERR_SRVCL;
5167 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 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005173 return 0;
5174}
5175
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005176/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5177 * processing can continue on next analysers, or zero if it either needs more
5178 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5179 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5180 * when it has nothing left to do, and may remove any analyser when it wants to
5181 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005182 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005183int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005184{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005185 struct http_txn *txn = &s->txn;
5186 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005187 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005188 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005189 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005190 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005191
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005192 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 +02005193 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005194 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005195 rep,
5196 rep->rex, rep->wex,
5197 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005198 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005199 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005200
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005201 /*
5202 * Now parse the partial (or complete) lines.
5203 * We will check the response syntax, and also join multi-line
5204 * headers. An index of all the lines will be elaborated while
5205 * parsing.
5206 *
5207 * For the parsing, we use a 28 states FSM.
5208 *
5209 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005210 * rep->buf->p = beginning of response
5211 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5212 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005213 * msg->eol = end of current header or line (LF or CRLF)
5214 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005215 */
5216
Willy Tarreau83e3af02009-12-28 17:39:57 +01005217 /* There's a protected area at the end of the buffer for rewriting
5218 * purposes. We don't want to start to parse the request if the
5219 * protected area is affected, because we may have to move processed
5220 * data later, which is much more complicated.
5221 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005222 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005223 if (unlikely(!channel_reserved(rep))) {
5224 /* some data has still not left the buffer, wake us once that's done */
5225 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5226 goto abort_response;
5227 channel_dont_close(rep);
5228 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005229 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005230 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005231 }
5232
Willy Tarreau379357a2013-06-08 12:55:46 +02005233 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5234 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5235 buffer_slow_realign(rep->buf);
5236
Willy Tarreau9b28e032012-10-12 23:49:43 +02005237 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005238 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005239 }
5240
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005241 /* 1: we might have to print this header in debug mode */
5242 if (unlikely((global.mode & MODE_DEBUG) &&
5243 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005244 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005245 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005246
Willy Tarreau9b28e032012-10-12 23:49:43 +02005247 sol = rep->buf->p;
5248 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005250
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005251 sol += hdr_idx_first_pos(&txn->hdr_idx);
5252 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005253
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005254 while (cur_idx) {
5255 eol = sol + txn->hdr_idx.v[cur_idx].len;
5256 debug_hdr("srvhdr", s, sol, eol);
5257 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5258 cur_idx = txn->hdr_idx.v[cur_idx].next;
5259 }
5260 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005261
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005262 /*
5263 * Now we quickly check if we have found a full valid response.
5264 * If not so, we check the FD and buffer states before leaving.
5265 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005266 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005267 * responses are checked first.
5268 *
5269 * Depending on whether the client is still there or not, we
5270 * may send an error response back or not. Note that normally
5271 * we should only check for HTTP status there, and check I/O
5272 * errors somewhere else.
5273 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005274
Willy Tarreau655dce92009-11-08 13:10:58 +01005275 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005276 /* Invalid response */
5277 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5278 /* we detected a parsing error. We want to archive this response
5279 * in the dedicated proxy area for later troubleshooting.
5280 */
5281 hdr_response_bad:
5282 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005283 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005284
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005285 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005286 if (objt_server(s->target)) {
5287 objt_server(s->target)->counters.failed_resp++;
5288 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005289 }
Willy Tarreau64648412010-03-05 10:41:54 +01005290 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005291 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292 rep->analysers = 0;
5293 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005294 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005295 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005296 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005297
5298 if (!(s->flags & SN_ERR_MASK))
5299 s->flags |= SN_ERR_PRXCOND;
5300 if (!(s->flags & SN_FINST_MASK))
5301 s->flags |= SN_FINST_H;
5302
5303 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005304 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005305
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005306 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005307 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005308 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005309 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005310 goto hdr_response_bad;
5311 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005312
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005314 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005315 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005316 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005317 else if (txn->flags & TX_NOT_FIRST)
5318 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005319
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005320 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005321 if (objt_server(s->target)) {
5322 objt_server(s->target)->counters.failed_resp++;
5323 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005324 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005325
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005326 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005327 rep->analysers = 0;
5328 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005329 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005330 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005331 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005332
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005333 if (!(s->flags & SN_ERR_MASK))
5334 s->flags |= SN_ERR_SRVCL;
5335 if (!(s->flags & SN_FINST_MASK))
5336 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005337 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005338 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005339
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005340 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005341 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005343 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005344 else if (txn->flags & TX_NOT_FIRST)
5345 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005346
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005347 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005348 if (objt_server(s->target)) {
5349 objt_server(s->target)->counters.failed_resp++;
5350 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005351 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005352
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005353 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005354 rep->analysers = 0;
5355 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005356 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005357 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005358 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005359
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005360 if (!(s->flags & SN_ERR_MASK))
5361 s->flags |= SN_ERR_SRVTO;
5362 if (!(s->flags & SN_FINST_MASK))
5363 s->flags |= SN_FINST_H;
5364 return 0;
5365 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005366
Willy Tarreauf003d372012-11-26 13:35:37 +01005367 /* client abort with an abortonclose */
5368 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5369 s->fe->fe_counters.cli_aborts++;
5370 s->be->be_counters.cli_aborts++;
5371 if (objt_server(s->target))
5372 objt_server(s->target)->counters.cli_aborts++;
5373
5374 rep->analysers = 0;
5375 channel_auto_close(rep);
5376
5377 txn->status = 400;
5378 bi_erase(rep);
5379 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5380
5381 if (!(s->flags & SN_ERR_MASK))
5382 s->flags |= SN_ERR_CLICL;
5383 if (!(s->flags & SN_FINST_MASK))
5384 s->flags |= SN_FINST_H;
5385
5386 /* process_session() will take care of the error */
5387 return 0;
5388 }
5389
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005390 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005391 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005392 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005393 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005394 else if (txn->flags & TX_NOT_FIRST)
5395 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005396
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005397 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005398 if (objt_server(s->target)) {
5399 objt_server(s->target)->counters.failed_resp++;
5400 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005401 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005402
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005403 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005404 rep->analysers = 0;
5405 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005406 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005407 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005408 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005409
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005410 if (!(s->flags & SN_ERR_MASK))
5411 s->flags |= SN_ERR_SRVCL;
5412 if (!(s->flags & SN_FINST_MASK))
5413 s->flags |= SN_FINST_H;
5414 return 0;
5415 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005416
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005417 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005418 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005420 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005421 else if (txn->flags & TX_NOT_FIRST)
5422 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005423
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005424 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005425 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005426 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005427
5428 if (!(s->flags & SN_ERR_MASK))
5429 s->flags |= SN_ERR_CLICL;
5430 if (!(s->flags & SN_FINST_MASK))
5431 s->flags |= SN_FINST_H;
5432
5433 /* process_session() will take care of the error */
5434 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005435 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005436
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005437 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005438 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005439 return 0;
5440 }
5441
5442 /* More interesting part now : we know that we have a complete
5443 * response which at least looks like HTTP. We have an indicator
5444 * of each header's length, so we can parse them quickly.
5445 */
5446
5447 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005448 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005449
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005450 /*
5451 * 1: get the status code
5452 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005453 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005454 if (n < 1 || n > 5)
5455 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005456 /* when the client triggers a 4xx from the server, it's most often due
5457 * to a missing object or permission. These events should be tracked
5458 * because if they happen often, it may indicate a brute force or a
5459 * vulnerability scan.
5460 */
5461 if (n == 4)
5462 session_inc_http_err_ctr(s);
5463
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005464 if (objt_server(s->target))
5465 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005466
Willy Tarreau5b154472009-12-21 20:11:07 +01005467 /* check if the response is HTTP/1.1 or above */
5468 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005469 ((rep->buf->p[5] > '1') ||
5470 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005471 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005472
5473 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005474 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 +01005475
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005476 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005477 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005478
Willy Tarreau9b28e032012-10-12 23:49:43 +02005479 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005480
Willy Tarreau39650402010-03-15 19:44:39 +01005481 /* Adjust server's health based on status code. Note: status codes 501
5482 * and 505 are triggered on demand by client request, so we must not
5483 * count them as server failures.
5484 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005485 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005486 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005487 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005488 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005489 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005490 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005491
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005492 /*
5493 * 2: check for cacheability.
5494 */
5495
5496 switch (txn->status) {
5497 case 200:
5498 case 203:
5499 case 206:
5500 case 300:
5501 case 301:
5502 case 410:
5503 /* RFC2616 @13.4:
5504 * "A response received with a status code of
5505 * 200, 203, 206, 300, 301 or 410 MAY be stored
5506 * by a cache (...) unless a cache-control
5507 * directive prohibits caching."
5508 *
5509 * RFC2616 @9.5: POST method :
5510 * "Responses to this method are not cacheable,
5511 * unless the response includes appropriate
5512 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005513 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005514 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005515 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005516 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5517 break;
5518 default:
5519 break;
5520 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005521
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005522 /*
5523 * 3: we may need to capture headers
5524 */
5525 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005526 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005527 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 txn->rsp.cap, s->fe->rsp_cap);
5529
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005530 /* 4: determine the transfer-length.
5531 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5532 * the presence of a message-body in a RESPONSE and its transfer length
5533 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005534 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005535 * All responses to the HEAD request method MUST NOT include a
5536 * message-body, even though the presence of entity-header fields
5537 * might lead one to believe they do. All 1xx (informational), 204
5538 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5539 * message-body. All other responses do include a message-body,
5540 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005541 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005542 * 1. Any response which "MUST NOT" include a message-body (such as the
5543 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5544 * always terminated by the first empty line after the header fields,
5545 * regardless of the entity-header fields present in the message.
5546 *
5547 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5548 * the "chunked" transfer-coding (Section 6.2) is used, the
5549 * transfer-length is defined by the use of this transfer-coding.
5550 * If a Transfer-Encoding header field is present and the "chunked"
5551 * transfer-coding is not present, the transfer-length is defined by
5552 * the sender closing the connection.
5553 *
5554 * 3. If a Content-Length header field is present, its decimal value in
5555 * OCTETs represents both the entity-length and the transfer-length.
5556 * If a message is received with both a Transfer-Encoding header
5557 * field and a Content-Length header field, the latter MUST be ignored.
5558 *
5559 * 4. If the message uses the media type "multipart/byteranges", and
5560 * the transfer-length is not otherwise specified, then this self-
5561 * delimiting media type defines the transfer-length. This media
5562 * type MUST NOT be used unless the sender knows that the recipient
5563 * can parse it; the presence in a request of a Range header with
5564 * multiple byte-range specifiers from a 1.1 client implies that the
5565 * client can parse multipart/byteranges responses.
5566 *
5567 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005568 */
5569
5570 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005571 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005572 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005573 * FIXME: should we parse anyway and return an error on chunked encoding ?
5574 */
5575 if (txn->meth == HTTP_METH_HEAD ||
5576 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005577 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005578 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005579 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005580 goto skip_content_length;
5581 }
5582
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005583 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005584 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005585 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005586 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005587 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005588 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5589 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005590 /* bad transfer-encoding (chunked followed by something else) */
5591 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005592 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005593 break;
5594 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005595 }
5596
5597 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5598 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005599 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005600 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005601 signed long long cl;
5602
Willy Tarreauad14f752011-09-02 20:33:27 +02005603 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005604 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005605 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005606 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005607
Willy Tarreauad14f752011-09-02 20:33:27 +02005608 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005609 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005610 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005611 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005612
Willy Tarreauad14f752011-09-02 20:33:27 +02005613 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005614 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005615 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005616 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005617
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005618 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005619 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005620 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005621 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005622
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005623 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005624 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005625 }
5626
William Lallemand82fe75c2012-10-23 10:25:10 +02005627 if (s->fe->comp || s->be->comp)
5628 select_compression_response_header(s, rep->buf);
5629
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005630 /* FIXME: we should also implement the multipart/byterange method.
5631 * For now on, we resort to close mode in this case (unknown length).
5632 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005633skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005634
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005635 /* end of job, return OK */
5636 rep->analysers &= ~an_bit;
5637 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005638 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005639 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005640
5641 abort_keep_alive:
5642 /* A keep-alive request to the server failed on a network error.
5643 * The client is required to retry. We need to close without returning
5644 * any other information so that the client retries.
5645 */
5646 txn->status = 0;
5647 rep->analysers = 0;
5648 s->req->analysers = 0;
5649 channel_auto_close(rep);
5650 s->logs.logwait = 0;
5651 s->logs.level = 0;
5652 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5653 bi_erase(rep);
5654 stream_int_retnclose(rep->cons, NULL);
5655 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005656}
5657
5658/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005659 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5660 * and updates t->rep->analysers. It might make sense to explode it into several
5661 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005662 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005663int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005664{
5665 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666 struct http_msg *msg = &txn->rsp;
5667 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005668 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005669 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005670
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005671 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 +02005672 now_ms, __FUNCTION__,
5673 t,
5674 rep,
5675 rep->rex, rep->wex,
5676 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005677 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005678 rep->analysers);
5679
Willy Tarreau655dce92009-11-08 13:10:58 +01005680 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005681 return 0;
5682
5683 rep->analysers &= ~an_bit;
5684 rep->analyse_exp = TICK_ETERNITY;
5685
Willy Tarreau5b154472009-12-21 20:11:07 +01005686 /* Now we have to check if we need to modify the Connection header.
5687 * This is more difficult on the response than it is on the request,
5688 * because we can have two different HTTP versions and we don't know
5689 * how the client will interprete a response. For instance, let's say
5690 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5691 * HTTP/1.1 response without any header. Maybe it will bound itself to
5692 * HTTP/1.0 because it only knows about it, and will consider the lack
5693 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5694 * the lack of header as a keep-alive. Thus we will use two flags
5695 * indicating how a request MAY be understood by the client. In case
5696 * of multiple possibilities, we'll fix the header to be explicit. If
5697 * ambiguous cases such as both close and keepalive are seen, then we
5698 * will fall back to explicit close. Note that we won't take risks with
5699 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005700 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005701 */
5702
Willy Tarreaudc008c52010-02-01 16:20:08 +01005703 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5704 txn->status == 101)) {
5705 /* Either we've established an explicit tunnel, or we're
5706 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005707 * to understand the next protocols. We have to switch to tunnel
5708 * mode, so that we transfer the request and responses then let
5709 * this protocol pass unmodified. When we later implement specific
5710 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005711 * header which contains information about that protocol for
5712 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005713 */
5714 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5715 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005716 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5717 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005718 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5719 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005720 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005721
Willy Tarreau70dffda2014-01-30 03:07:23 +01005722 /* this situation happens when combining pretend-keepalive with httpclose. */
5723 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5724 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5725 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5726 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5727
Willy Tarreau60466522010-01-18 19:08:45 +01005728 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005729 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005730 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5731 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005732
Willy Tarreau60466522010-01-18 19:08:45 +01005733 /* now adjust header transformations depending on current state */
5734 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5735 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5736 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005737 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005738 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005739 }
Willy Tarreau60466522010-01-18 19:08:45 +01005740 else { /* SCL / KAL */
5741 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005742 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005743 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005744 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005745
Willy Tarreau60466522010-01-18 19:08:45 +01005746 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005747 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005748
Willy Tarreau60466522010-01-18 19:08:45 +01005749 /* Some keep-alive responses are converted to Server-close if
5750 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005751 */
Willy Tarreau60466522010-01-18 19:08:45 +01005752 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5753 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005754 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005755 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005756 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005757 }
5758
Willy Tarreau7959a552013-09-23 16:44:27 +02005759 /* we want to have the response time before we start processing it */
5760 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5761
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005762 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005763 /*
5764 * 3: we will have to evaluate the filters.
5765 * As opposed to version 1.2, now they will be evaluated in the
5766 * filters order and not in the header order. This means that
5767 * each filter has to be validated among all headers.
5768 *
5769 * Filters are tried with ->be first, then with ->fe if it is
5770 * different from ->be.
5771 */
5772
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005773 cur_proxy = t->be;
5774 while (1) {
5775 struct proxy *rule_set = cur_proxy;
5776
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005777 /* evaluate http-response rules */
5778 if (!http_res_last_rule)
5779 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5780
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005781 /* try headers filters */
5782 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005783 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005784 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005785 if (objt_server(t->target)) {
5786 objt_server(t->target)->counters.failed_resp++;
5787 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005788 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005789 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005790 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005791 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005792 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005793 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005794 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005795 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005796 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005797 if (!(t->flags & SN_ERR_MASK))
5798 t->flags |= SN_ERR_PRXCOND;
5799 if (!(t->flags & SN_FINST_MASK))
5800 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005801 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005802 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005803 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005804
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005805 /* has the response been denied ? */
5806 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005807 if (objt_server(t->target))
5808 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005809
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005810 t->be->be_counters.denied_resp++;
5811 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005812 if (t->listener->counters)
5813 t->listener->counters->denied_resp++;
5814
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005815 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005817
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005818 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005819 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005820 if (txn->status < 200)
5821 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005822 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005823 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005824 ret = acl_pass(ret);
5825 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5826 ret = !ret;
5827 if (!ret)
5828 continue;
5829 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005830 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005831 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005832 }
5833
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005834 /* check whether we're already working on the frontend */
5835 if (cur_proxy == t->fe)
5836 break;
5837 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005838 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005839
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005840 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005841 * We may be facing a 100-continue response, in which case this
5842 * is not the right response, and we're waiting for the next one.
5843 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005844 * next one.
5845 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005846 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005847 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005848 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005849 msg->msg_state = HTTP_MSG_RPBEFORE;
5850 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005851 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005852 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5853 return 1;
5854 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005855 else if (unlikely(txn->status < 200))
5856 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005857
5858 /* we don't have any 1xx status code now */
5859
5860 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005861 * 4: check for server cookie.
5862 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005863 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5864 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005865 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005866
Willy Tarreaubaaee002006-06-26 02:48:02 +02005867
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005870 */
Willy Tarreau67402132012-05-31 20:40:20 +02005871 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005872 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005873
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005874 /*
5875 * 6: add server cookie in the response if needed
5876 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005877 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005878 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005879 (!(t->flags & SN_DIRECT) ||
5880 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5881 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5882 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5883 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005884 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005885 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005886 /* the server is known, it's not the one the client requested, or the
5887 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005888 * insert a set-cookie here, except if we want to insert only on POST
5889 * requests and this one isn't. Note that servers which don't have cookies
5890 * (eg: some backup servers) will return a full cookie removal request.
5891 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005892 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005893 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005894 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5895 t->be->cookie_name);
5896 }
5897 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005898 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005899
5900 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5901 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005902 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5903 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5904 trash.len += 5;
5905
Willy Tarreauef4f3912010-10-07 21:00:29 +02005906 if (t->be->cookie_maxlife) {
5907 /* emit first_date, which is either the original one or
5908 * the current date.
5909 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005910 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005911 s30tob64(txn->cookie_first_date ?
5912 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005913 (date.tv_sec+3) >> 2, trash.str + trash.len);
5914 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005915 }
5916 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005917 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005918 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005919
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005920 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005921 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005922
Willy Tarreau4992dd22012-05-31 21:02:17 +02005923 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005924 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005925
5926 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005927 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005928
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005929 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005930 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005931
Willy Tarreauf1348312010-10-07 15:54:11 +02005932 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005933 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005934 /* the server did not change, only the date was updated */
5935 txn->flags |= TX_SCK_UPDATED;
5936 else
5937 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005938
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005939 /* Here, we will tell an eventual cache on the client side that we don't
5940 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5941 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5942 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5943 */
Willy Tarreau67402132012-05-31 20:40:20 +02005944 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005945
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005946 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5947
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005948 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005949 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005950 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005951 }
5952 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005953
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005954 /*
5955 * 7: check if result will be cacheable with a cookie.
5956 * We'll block the response if security checks have caught
5957 * nasty things such as a cacheable cookie.
5958 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005959 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5960 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005961 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005962
5963 /* we're in presence of a cacheable response containing
5964 * a set-cookie header. We'll block it as requested by
5965 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005966 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005967 if (objt_server(t->target))
5968 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005969
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005970 t->be->be_counters.denied_resp++;
5971 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005972 if (t->listener->counters)
5973 t->listener->counters->denied_resp++;
5974
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005975 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005976 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005977 send_log(t->be, LOG_ALERT,
5978 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005979 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005980 goto return_srv_prx_502;
5981 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005982
5983 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005984 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005985 * If an "Upgrade" token is found, the header is left untouched in order
5986 * not to have to deal with some client bugs : some of them fail an upgrade
5987 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005988 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005989 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5990 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005991 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5992 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005993 unsigned int want_flags = 0;
5994
5995 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5996 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5997 /* we want a keep-alive response here. Keep-alive header
5998 * required if either side is not 1.1.
5999 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006000 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006001 want_flags |= TX_CON_KAL_SET;
6002 }
6003 else {
6004 /* we want a close response here. Close header required if
6005 * the server is 1.1, regardless of the client.
6006 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006007 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006008 want_flags |= TX_CON_CLO_SET;
6009 }
6010
6011 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006012 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006013 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006014
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006015 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006016 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006017 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006018 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006019
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006020 /*************************************************************
6021 * OK, that's finished for the headers. We have done what we *
6022 * could. Let's switch to the DATA state. *
6023 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006024
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006025 /* if the user wants to log as soon as possible, without counting
6026 * bytes from the server, then this is the right moment. We have
6027 * to temporarily assign bytes_out to log what we currently have.
6028 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006029 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006030 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6031 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006032 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006033 t->logs.bytes_out = 0;
6034 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006035
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036 /* Note: we must not try to cheat by jumping directly to DATA,
6037 * otherwise we would not let the client side wake up.
6038 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006039
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006040 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006041 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006042 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006043}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006044
Willy Tarreaud98cf932009-12-27 22:54:55 +01006045/* This function is an analyser which forwards response body (including chunk
6046 * sizes if any). It is called as soon as we must forward, even if we forward
6047 * zero byte. The only situation where it must not be called is when we're in
6048 * tunnel mode and we want to forward till the close. It's used both to forward
6049 * remaining data and to resync after end of body. It expects the msg_state to
6050 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6051 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006052 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006053 * bytes of pending data + the headers if not already done (between sol and sov).
6054 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006055 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006056int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006057{
6058 struct http_txn *txn = &s->txn;
6059 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02006060 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02006061 static struct buffer *tmpbuf = NULL;
6062 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006063 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006064 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006065
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006066 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6067 return 0;
6068
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006069 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006070 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006071 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006072 /* Output closed while we were sending data. We must abort and
6073 * wake the other side up.
6074 */
6075 msg->msg_state = HTTP_MSG_ERROR;
6076 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006077 return 1;
6078 }
6079
Willy Tarreau4fe41902010-06-07 22:27:41 +02006080 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006081 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006082
William Lallemand82fe75c2012-10-23 10:25:10 +02006083 /* this is the first time we need the compression buffer */
6084 if (s->comp_algo != NULL && tmpbuf == NULL) {
6085 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6086 goto aborted_xfer; /* no memory */
6087 }
6088
Willy Tarreaud98cf932009-12-27 22:54:55 +01006089 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006090 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006091 * rep->buf.p still points to the beginning of the message and msg->sol
6092 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006093 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006094 channel_forward(res, msg->sov);
6095 msg->next = 0;
6096 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006097
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006098 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006099 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006100 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006101 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006102 }
6103
William Lallemand82fe75c2012-10-23 10:25:10 +02006104 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006105 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006106 if (ret < 0) {
6107 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006108 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006109 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006110 compressing = 1;
6111 }
6112
Willy Tarreaud98cf932009-12-27 22:54:55 +01006113 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006114 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006115 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006116 if (s->comp_algo == NULL) {
6117 bytes = msg->sov - msg->sol;
6118 if (msg->chunk_len || bytes) {
6119 msg->sol = msg->sov;
6120 msg->next -= bytes; /* will be forwarded */
6121 msg->chunk_len += bytes;
6122 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6123 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006124 }
6125
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006126 switch (msg->msg_state - HTTP_MSG_DATA) {
6127 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006128 if (compressing) {
6129 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6130 if (ret < 0)
6131 goto aborted_xfer;
6132 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006133
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006134 if (res->to_forward || msg->chunk_len) {
6135 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006136 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006137 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006138
6139 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006140 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006141 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006142 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006143 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006144 if (compressing && consumed_data) {
6145 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6146 compressing = 0;
6147 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006148 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006149 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006150 /* fall through for HTTP_MSG_CHUNK_CRLF */
6151
6152 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6153 /* we want the CRLF after the data */
6154
6155 ret = http_skip_chunk_crlf(msg);
6156 if (ret == 0)
6157 goto missing_data;
6158 else if (ret < 0) {
6159 if (msg->err_pos >= 0)
6160 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6161 goto return_bad_res;
6162 }
6163 /* skipping data in buffer for compression */
6164 if (compressing) {
6165 b_adv(res->buf, msg->next);
6166 msg->next = 0;
6167 msg->sov = 0;
6168 msg->sol = 0;
6169 }
6170 /* we're in MSG_CHUNK_SIZE now, fall through */
6171
6172 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006173 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006174 * set ->sov and ->next to point to the body and switch to DATA or
6175 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006176 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006177
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006178 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006179 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006180 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006181 else if (ret < 0) {
6182 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006183 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006184 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006185 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006186 if (compressing) {
6187 if (likely(msg->chunk_len > 0)) {
6188 /* skipping data if we are in compression mode */
6189 b_adv(res->buf, msg->next);
6190 msg->next = 0;
6191 msg->sov = 0;
6192 msg->sol = 0;
6193 } else {
6194 if (consumed_data) {
6195 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6196 compressing = 0;
6197 }
6198 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006199 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006200 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006201 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006202
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006203 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6204 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006205 if (ret == 0)
6206 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006207 else if (ret < 0) {
6208 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006209 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006210 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006211 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006212 if (s->comp_algo != NULL) {
6213 /* forwarding trailers */
6214 channel_forward(res, msg->next);
6215 msg->next = 0;
6216 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006217 /* we're in HTTP_MSG_DONE now, but we might still have
6218 * some data pending, so let's loop over once.
6219 */
6220 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006221
6222 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006223 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006224
6225 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006226 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006227 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6228 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006229 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006230 if (http_resync_states(s)) {
6231 http_silent_debug(__LINE__, s);
6232 /* some state changes occurred, maybe the analyser
6233 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006234 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006235 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006236 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006237 /* response errors are most likely due to
6238 * the client aborting the transfer.
6239 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006240 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006241 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006242 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006243 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006244 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006245 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006246 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006247 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006248 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006249 }
6250 }
6251
Willy Tarreaud98cf932009-12-27 22:54:55 +01006252 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006253 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006254 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6255 compressing = 0;
6256 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006257
6258 if (res->flags & CF_SHUTW)
6259 goto aborted_xfer;
6260
6261 /* stop waiting for data if the input is closed before the end. If the
6262 * client side was already closed, it means that the client has aborted,
6263 * so we don't want to count this as a server abort. Otherwise it's a
6264 * server abort.
6265 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006266 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006267 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6268 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006269 if (!(s->flags & SN_ERR_MASK))
6270 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006271 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006272 if (objt_server(s->target))
6273 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006274 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006275 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006276
Willy Tarreau40dba092010-03-04 18:14:51 +01006277 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006278 if (!s->req->analysers)
6279 goto return_bad_res;
6280
Willy Tarreauea953162012-05-18 23:41:28 +02006281 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006282 if (s->comp_algo == NULL) {
6283 bytes = msg->sov - msg->sol;
6284 if (msg->chunk_len || bytes) {
6285 msg->sol = msg->sov;
6286 msg->next -= bytes; /* will be forwarded */
6287 msg->chunk_len += bytes;
6288 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6289 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006290 }
6291
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006292 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006293 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006294 * Similarly, with keep-alive on the client side, we don't want to forward a
6295 * close.
6296 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006297 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006298 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6299 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006300 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006301
Willy Tarreau5c620922011-05-11 19:56:11 +02006302 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006303 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006304 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006305 * modes are already handled by the stream sock layer. We must not do
6306 * this in content-length mode because it could present the MSG_MORE
6307 * flag with the last block of forwarded data, which would cause an
6308 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006309 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006310 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006311 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006312
Willy Tarreaud98cf932009-12-27 22:54:55 +01006313 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006314 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006315 return 0;
6316
Willy Tarreau40dba092010-03-04 18:14:51 +01006317 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006318 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006319 if (objt_server(s->target))
6320 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006321
6322 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006323 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006324 /* don't send any error message as we're in the body */
6325 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006326 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006327 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006328 if (objt_server(s->target))
6329 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006330
6331 if (!(s->flags & SN_ERR_MASK))
6332 s->flags |= SN_ERR_PRXCOND;
6333 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006334 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006335 return 0;
6336
6337 aborted_xfer:
6338 txn->rsp.msg_state = HTTP_MSG_ERROR;
6339 /* don't send any error message as we're in the body */
6340 stream_int_retnclose(res->cons, NULL);
6341 res->analysers = 0;
6342 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6343
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006344 s->fe->fe_counters.cli_aborts++;
6345 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006346 if (objt_server(s->target))
6347 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006348
6349 if (!(s->flags & SN_ERR_MASK))
6350 s->flags |= SN_ERR_CLICL;
6351 if (!(s->flags & SN_FINST_MASK))
6352 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006353 return 0;
6354}
6355
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006356/* Iterate the same filter through all request headers.
6357 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006358 * Since it can manage the switch to another backend, it updates the per-proxy
6359 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006361int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006362{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006363 char term;
6364 char *cur_ptr, *cur_end, *cur_next;
6365 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006366 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006367 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006368 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 last_hdr = 0;
6371
Willy Tarreau9b28e032012-10-12 23:49:43 +02006372 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006373 old_idx = 0;
6374
6375 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006376 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006377 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006378 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006379 (exp->action == ACT_ALLOW ||
6380 exp->action == ACT_DENY ||
6381 exp->action == ACT_TARPIT))
6382 return 0;
6383
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006384 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006385 if (!cur_idx)
6386 break;
6387
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006388 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 cur_ptr = cur_next;
6390 cur_end = cur_ptr + cur_hdr->len;
6391 cur_next = cur_end + cur_hdr->cr + 1;
6392
6393 /* Now we have one header between cur_ptr and cur_end,
6394 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006395 */
6396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006397 /* The annoying part is that pattern matching needs
6398 * that we modify the contents to null-terminate all
6399 * strings before testing them.
6400 */
6401
6402 term = *cur_end;
6403 *cur_end = '\0';
6404
6405 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6406 switch (exp->action) {
6407 case ACT_SETBE:
6408 /* It is not possible to jump a second time.
6409 * FIXME: should we return an HTTP/500 here so that
6410 * the admin knows there's a problem ?
6411 */
6412 if (t->be != t->fe)
6413 break;
6414
6415 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006416 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006417 last_hdr = 1;
6418 break;
6419
6420 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006421 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006422 last_hdr = 1;
6423 break;
6424
6425 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006426 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006427 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006428 break;
6429
6430 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006431 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006433 break;
6434
6435 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006436 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6437 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006438 /* FIXME: if the user adds a newline in the replacement, the
6439 * index will not be recalculated for now, and the new line
6440 * will not be counted as a new header.
6441 */
6442
6443 cur_end += delta;
6444 cur_next += delta;
6445 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006446 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 break;
6448
6449 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006450 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 cur_next += delta;
6452
Willy Tarreaufa355d42009-11-29 18:12:29 +01006453 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006454 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6455 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 cur_hdr->len = 0;
6457 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006458 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006459 break;
6460
6461 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006462 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 if (cur_end)
6464 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 /* keep the link from this header to next one in case of later
6467 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006468 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 old_idx = cur_idx;
6470 }
6471 return 0;
6472}
6473
6474
6475/* Apply the filter to the request line.
6476 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6477 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006478 * Since it can manage the switch to another backend, it updates the per-proxy
6479 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006481int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006482{
6483 char term;
6484 char *cur_ptr, *cur_end;
6485 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006486 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006487 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006488
Willy Tarreau3d300592007-03-18 18:34:41 +01006489 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006491 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006492 (exp->action == ACT_ALLOW ||
6493 exp->action == ACT_DENY ||
6494 exp->action == ACT_TARPIT))
6495 return 0;
6496 else if (exp->action == ACT_REMOVE)
6497 return 0;
6498
6499 done = 0;
6500
Willy Tarreau9b28e032012-10-12 23:49:43 +02006501 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006502 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503
6504 /* Now we have the request line between cur_ptr and cur_end */
6505
6506 /* The annoying part is that pattern matching needs
6507 * that we modify the contents to null-terminate all
6508 * strings before testing them.
6509 */
6510
6511 term = *cur_end;
6512 *cur_end = '\0';
6513
6514 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6515 switch (exp->action) {
6516 case ACT_SETBE:
6517 /* It is not possible to jump a second time.
6518 * FIXME: should we return an HTTP/500 here so that
6519 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006520 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006521 if (t->be != t->fe)
6522 break;
6523
6524 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006525 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 done = 1;
6527 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006530 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 done = 1;
6532 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006534 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006535 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 done = 1;
6537 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006540 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 done = 1;
6542 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006543
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006544 case ACT_REPLACE:
6545 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006546 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6547 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 /* FIXME: if the user adds a newline in the replacement, the
6549 * index will not be recalculated for now, and the new line
6550 * will not be counted as a new header.
6551 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006552
Willy Tarreaufa355d42009-11-29 18:12:29 +01006553 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006554 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006555 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 HTTP_MSG_RQMETH,
6557 cur_ptr, cur_end + 1,
6558 NULL, NULL);
6559 if (unlikely(!cur_end))
6560 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562 /* we have a full request and we know that we have either a CR
6563 * or an LF at <ptr>.
6564 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006565 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6566 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 /* there is no point trying this regex on headers */
6568 return 1;
6569 }
6570 }
6571 *cur_end = term; /* restore the string terminator */
6572 return done;
6573}
Willy Tarreau97de6242006-12-27 17:18:38 +01006574
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575
Willy Tarreau58f10d72006-12-04 02:26:12 +01006576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006577/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006578 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006580 * unparsable request. Since it can manage the switch to another backend, it
6581 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006582 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006583int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006584{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006585 struct http_txn *txn = &s->txn;
6586 struct hdr_exp *exp;
6587
6588 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006589 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006591 /*
6592 * The interleaving of transformations and verdicts
6593 * makes it difficult to decide to continue or stop
6594 * the evaluation.
6595 */
6596
Willy Tarreau6c123b12010-01-28 20:22:06 +01006597 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6598 break;
6599
Willy Tarreau3d300592007-03-18 18:34:41 +01006600 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006601 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006602 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006603 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006604
6605 /* if this filter had a condition, evaluate it now and skip to
6606 * next filter if the condition does not match.
6607 */
6608 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006609 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006610 ret = acl_pass(ret);
6611 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6612 ret = !ret;
6613
6614 if (!ret)
6615 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006616 }
6617
6618 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006619 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006620 if (unlikely(ret < 0))
6621 return -1;
6622
6623 if (likely(ret == 0)) {
6624 /* The filter did not match the request, it can be
6625 * iterated through all headers.
6626 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006627 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006628 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006630 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631}
6632
6633
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634
Willy Tarreau58f10d72006-12-04 02:26:12 +01006635/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006636 * Try to retrieve the server associated to the appsession.
6637 * If the server is found, it's assigned to the session.
6638 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006639void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006640 struct http_txn *txn = &t->txn;
6641 appsess *asession = NULL;
6642 char *sessid_temp = NULL;
6643
Cyril Bontéb21570a2009-11-29 20:04:48 +01006644 if (len > t->be->appsession_len) {
6645 len = t->be->appsession_len;
6646 }
6647
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006648 if (t->be->options2 & PR_O2_AS_REQL) {
6649 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006650 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006651 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006652 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006653 }
6654
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006655 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006656 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6657 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6658 return;
6659 }
6660
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006661 memcpy(txn->sessid, buf, len);
6662 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006663 }
6664
6665 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6666 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6667 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6668 return;
6669 }
6670
Cyril Bontéb21570a2009-11-29 20:04:48 +01006671 memcpy(sessid_temp, buf, len);
6672 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006673
6674 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6675 /* free previously allocated memory */
6676 pool_free2(apools.sessid, sessid_temp);
6677
6678 if (asession != NULL) {
6679 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6680 if (!(t->be->options2 & PR_O2_AS_REQL))
6681 asession->request_count++;
6682
6683 if (asession->serverid != NULL) {
6684 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006685
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006686 while (srv) {
6687 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006688 if ((srv->state & SRV_RUNNING) ||
6689 (t->be->options & PR_O_PERSIST) ||
6690 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006691 /* we found the server and it's usable */
6692 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006693 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006694 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006695 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006696
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006697 break;
6698 } else {
6699 txn->flags &= ~TX_CK_MASK;
6700 txn->flags |= TX_CK_DOWN;
6701 }
6702 }
6703 srv = srv->next;
6704 }
6705 }
6706 }
6707}
6708
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006709/* Find the end of a cookie value contained between <s> and <e>. It works the
6710 * same way as with headers above except that the semi-colon also ends a token.
6711 * See RFC2965 for more information. Note that it requires a valid header to
6712 * return a valid result.
6713 */
6714char *find_cookie_value_end(char *s, const char *e)
6715{
6716 int quoted, qdpair;
6717
6718 quoted = qdpair = 0;
6719 for (; s < e; s++) {
6720 if (qdpair) qdpair = 0;
6721 else if (quoted) {
6722 if (*s == '\\') qdpair = 1;
6723 else if (*s == '"') quoted = 0;
6724 }
6725 else if (*s == '"') quoted = 1;
6726 else if (*s == ',' || *s == ';') return s;
6727 }
6728 return s;
6729}
6730
6731/* Delete a value in a header between delimiters <from> and <next> in buffer
6732 * <buf>. The number of characters displaced is returned, and the pointer to
6733 * the first delimiter is updated if required. The function tries as much as
6734 * possible to respect the following principles :
6735 * - replace <from> delimiter by the <next> one unless <from> points to a
6736 * colon, in which case <next> is simply removed
6737 * - set exactly one space character after the new first delimiter, unless
6738 * there are not enough characters in the block being moved to do so.
6739 * - remove unneeded spaces before the previous delimiter and after the new
6740 * one.
6741 *
6742 * It is the caller's responsibility to ensure that :
6743 * - <from> points to a valid delimiter or the colon ;
6744 * - <next> points to a valid delimiter or the final CR/LF ;
6745 * - there are non-space chars before <from> ;
6746 * - there is a CR/LF at or after <next>.
6747 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006748int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006749{
6750 char *prev = *from;
6751
6752 if (*prev == ':') {
6753 /* We're removing the first value, preserve the colon and add a
6754 * space if possible.
6755 */
6756 if (!http_is_crlf[(unsigned char)*next])
6757 next++;
6758 prev++;
6759 if (prev < next)
6760 *prev++ = ' ';
6761
6762 while (http_is_spht[(unsigned char)*next])
6763 next++;
6764 } else {
6765 /* Remove useless spaces before the old delimiter. */
6766 while (http_is_spht[(unsigned char)*(prev-1)])
6767 prev--;
6768 *from = prev;
6769
6770 /* copy the delimiter and if possible a space if we're
6771 * not at the end of the line.
6772 */
6773 if (!http_is_crlf[(unsigned char)*next]) {
6774 *prev++ = *next++;
6775 if (prev + 1 < next)
6776 *prev++ = ' ';
6777 while (http_is_spht[(unsigned char)*next])
6778 next++;
6779 }
6780 }
6781 return buffer_replace2(buf, prev, next, NULL, 0);
6782}
6783
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006784/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006785 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006786 * desirable to call it only when needed. This code is quite complex because
6787 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6788 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006789 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006790void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006791{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006792 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006794 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006795 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6796 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006798 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006799 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006800 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006801
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006802 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006803 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006804 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006805
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006806 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006807 hdr_beg = hdr_next;
6808 hdr_end = hdr_beg + cur_hdr->len;
6809 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006810
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006811 /* We have one full header between hdr_beg and hdr_end, and the
6812 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006813 * "Cookie:" headers.
6814 */
6815
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006816 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006817 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006818 old_idx = cur_idx;
6819 continue;
6820 }
6821
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006822 del_from = NULL; /* nothing to be deleted */
6823 preserve_hdr = 0; /* assume we may kill the whole header */
6824
Willy Tarreau58f10d72006-12-04 02:26:12 +01006825 /* Now look for cookies. Conforming to RFC2109, we have to support
6826 * attributes whose name begin with a '$', and associate them with
6827 * the right cookie, if we want to delete this cookie.
6828 * So there are 3 cases for each cookie read :
6829 * 1) it's a special attribute, beginning with a '$' : ignore it.
6830 * 2) it's a server id cookie that we *MAY* want to delete : save
6831 * some pointers on it (last semi-colon, beginning of cookie...)
6832 * 3) it's an application cookie : we *MAY* have to delete a previous
6833 * "special" cookie.
6834 * At the end of loop, if a "special" cookie remains, we may have to
6835 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006836 * *MUST* delete it.
6837 *
6838 * Note: RFC2965 is unclear about the processing of spaces around
6839 * the equal sign in the ATTR=VALUE form. A careful inspection of
6840 * the RFC explicitly allows spaces before it, and not within the
6841 * tokens (attrs or values). An inspection of RFC2109 allows that
6842 * too but section 10.1.3 lets one think that spaces may be allowed
6843 * after the equal sign too, resulting in some (rare) buggy
6844 * implementations trying to do that. So let's do what servers do.
6845 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6846 * allowed quoted strings in values, with any possible character
6847 * after a backslash, including control chars and delimitors, which
6848 * causes parsing to become ambiguous. Browsers also allow spaces
6849 * within values even without quotes.
6850 *
6851 * We have to keep multiple pointers in order to support cookie
6852 * removal at the beginning, middle or end of header without
6853 * corrupting the header. All of these headers are valid :
6854 *
6855 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6856 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6857 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6858 * | | | | | | | | |
6859 * | | | | | | | | hdr_end <--+
6860 * | | | | | | | +--> next
6861 * | | | | | | +----> val_end
6862 * | | | | | +-----------> val_beg
6863 * | | | | +--------------> equal
6864 * | | | +----------------> att_end
6865 * | | +---------------------> att_beg
6866 * | +--------------------------> prev
6867 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868 */
6869
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6871 /* Iterate through all cookies on this line */
6872
6873 /* find att_beg */
6874 att_beg = prev + 1;
6875 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6876 att_beg++;
6877
6878 /* find att_end : this is the first character after the last non
6879 * space before the equal. It may be equal to hdr_end.
6880 */
6881 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 while (equal < hdr_end) {
6884 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006885 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 if (http_is_spht[(unsigned char)*equal++])
6887 continue;
6888 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889 }
6890
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6892 * is between <att_beg> and <equal>, both may be identical.
6893 */
6894
6895 /* look for end of cookie if there is an equal sign */
6896 if (equal < hdr_end && *equal == '=') {
6897 /* look for the beginning of the value */
6898 val_beg = equal + 1;
6899 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6900 val_beg++;
6901
6902 /* find the end of the value, respecting quotes */
6903 next = find_cookie_value_end(val_beg, hdr_end);
6904
6905 /* make val_end point to the first white space or delimitor after the value */
6906 val_end = next;
6907 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6908 val_end--;
6909 } else {
6910 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006912
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006913 /* We have nothing to do with attributes beginning with '$'. However,
6914 * they will automatically be removed if a header before them is removed,
6915 * since they're supposed to be linked together.
6916 */
6917 if (*att_beg == '$')
6918 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 /* Ignore cookies with no equal sign */
6921 if (equal == next) {
6922 /* This is not our cookie, so we must preserve it. But if we already
6923 * scheduled another cookie for removal, we cannot remove the
6924 * complete header, but we can remove the previous block itself.
6925 */
6926 preserve_hdr = 1;
6927 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006928 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006929 val_end += delta;
6930 next += delta;
6931 hdr_end += delta;
6932 hdr_next += delta;
6933 cur_hdr->len += delta;
6934 http_msg_move_end(&txn->req, delta);
6935 prev = del_from;
6936 del_from = NULL;
6937 }
6938 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 /* if there are spaces around the equal sign, we need to
6942 * strip them otherwise we'll get trouble for cookie captures,
6943 * or even for rewrites. Since this happens extremely rarely,
6944 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6947 int stripped_before = 0;
6948 int stripped_after = 0;
6949
6950 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006951 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 equal += stripped_before;
6953 val_beg += stripped_before;
6954 }
6955
6956 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006957 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006958 val_beg += stripped_after;
6959 stripped_before += stripped_after;
6960 }
6961
6962 val_end += stripped_before;
6963 next += stripped_before;
6964 hdr_end += stripped_before;
6965 hdr_next += stripped_before;
6966 cur_hdr->len += stripped_before;
6967 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006970
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006971 /* First, let's see if we want to capture this cookie. We check
6972 * that we don't already have a client side cookie, because we
6973 * can only capture one. Also as an optimisation, we ignore
6974 * cookies shorter than the declared name.
6975 */
6976 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6977 (val_end - att_beg >= t->fe->capture_namelen) &&
6978 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6979 int log_len = val_end - att_beg;
6980
6981 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6982 Alert("HTTP logging : out of memory.\n");
6983 } else {
6984 if (log_len > t->fe->capture_len)
6985 log_len = t->fe->capture_len;
6986 memcpy(txn->cli_cookie, att_beg, log_len);
6987 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006988 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006989 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006990
Willy Tarreaubca99692010-10-06 19:25:55 +02006991 /* Persistence cookies in passive, rewrite or insert mode have the
6992 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006993 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006994 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006995 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006996 * For cookies in prefix mode, the form is :
6997 *
6998 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 */
7000 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7001 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
7002 struct server *srv = t->be->srv;
7003 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7006 * have the server ID between val_beg and delim, and the original cookie between
7007 * delim+1 and val_end. Otherwise, delim==val_end :
7008 *
7009 * Cookie: NAME=SRV; # in all but prefix modes
7010 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7011 * | || || | |+-> next
7012 * | || || | +--> val_end
7013 * | || || +---------> delim
7014 * | || |+------------> val_beg
7015 * | || +-------------> att_end = equal
7016 * | |+-----------------> att_beg
7017 * | +------------------> prev
7018 * +-------------------------> hdr_beg
7019 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007020
Willy Tarreau67402132012-05-31 20:40:20 +02007021 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007022 for (delim = val_beg; delim < val_end; delim++)
7023 if (*delim == COOKIE_DELIM)
7024 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007025 } else {
7026 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007028 /* Now check if the cookie contains a date field, which would
7029 * appear after a vertical bar ('|') just after the server name
7030 * and before the delimiter.
7031 */
7032 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7033 if (vbar1) {
7034 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007035 * right is the last seen date. It is a base64 encoded
7036 * 30-bit value representing the UNIX date since the
7037 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007038 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007039 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007040 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007041 if (val_end - vbar1 >= 5) {
7042 val = b64tos30(vbar1);
7043 if (val > 0)
7044 txn->cookie_last_date = val << 2;
7045 }
7046 /* look for a second vertical bar */
7047 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7048 if (vbar1 && (val_end - vbar1 > 5)) {
7049 val = b64tos30(vbar1 + 1);
7050 if (val > 0)
7051 txn->cookie_first_date = val << 2;
7052 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007053 }
7054 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007055
Willy Tarreauf64d1412010-10-07 20:06:11 +02007056 /* if the cookie has an expiration date and the proxy wants to check
7057 * it, then we do that now. We first check if the cookie is too old,
7058 * then only if it has expired. We detect strict overflow because the
7059 * time resolution here is not great (4 seconds). Cookies with dates
7060 * in the future are ignored if their offset is beyond one day. This
7061 * allows an admin to fix timezone issues without expiring everyone
7062 * and at the same time avoids keeping unwanted side effects for too
7063 * long.
7064 */
7065 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007066 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7067 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007068 txn->flags &= ~TX_CK_MASK;
7069 txn->flags |= TX_CK_OLD;
7070 delim = val_beg; // let's pretend we have not found the cookie
7071 txn->cookie_first_date = 0;
7072 txn->cookie_last_date = 0;
7073 }
7074 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007075 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7076 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007077 txn->flags &= ~TX_CK_MASK;
7078 txn->flags |= TX_CK_EXPIRED;
7079 delim = val_beg; // let's pretend we have not found the cookie
7080 txn->cookie_first_date = 0;
7081 txn->cookie_last_date = 0;
7082 }
7083
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007084 /* Here, we'll look for the first running server which supports the cookie.
7085 * This allows to share a same cookie between several servers, for example
7086 * to dedicate backup servers to specific servers only.
7087 * However, to prevent clients from sticking to cookie-less backup server
7088 * when they have incidentely learned an empty cookie, we simply ignore
7089 * empty cookies and mark them as invalid.
7090 * The same behaviour is applied when persistence must be ignored.
7091 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007092 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007093 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007095 while (srv) {
7096 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7097 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7098 if ((srv->state & SRV_RUNNING) ||
7099 (t->be->options & PR_O_PERSIST) ||
7100 (t->flags & SN_FORCE_PRST)) {
7101 /* we found the server and we can use it */
7102 txn->flags &= ~TX_CK_MASK;
7103 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7104 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007105 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007106 break;
7107 } else {
7108 /* we found a server, but it's down,
7109 * mark it as such and go on in case
7110 * another one is available.
7111 */
7112 txn->flags &= ~TX_CK_MASK;
7113 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 srv = srv->next;
7117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118
Willy Tarreauf64d1412010-10-07 20:06:11 +02007119 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007120 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007121 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007122 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7123 txn->flags |= TX_CK_UNUSED;
7124 else
7125 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007126 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007127
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007128 /* depending on the cookie mode, we may have to either :
7129 * - delete the complete cookie if we're in insert+indirect mode, so that
7130 * the server never sees it ;
7131 * - remove the server id from the cookie value, and tag the cookie as an
7132 * application cookie so that it does not get accidentely removed later,
7133 * if we're in cookie prefix mode
7134 */
Willy Tarreau67402132012-05-31 20:40:20 +02007135 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007136 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007137
Willy Tarreau9b28e032012-10-12 23:49:43 +02007138 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007139 val_end += delta;
7140 next += delta;
7141 hdr_end += delta;
7142 hdr_next += delta;
7143 cur_hdr->len += delta;
7144 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 del_from = NULL;
7147 preserve_hdr = 1; /* we want to keep this cookie */
7148 }
7149 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007150 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007151 del_from = prev;
7152 }
7153 } else {
7154 /* This is not our cookie, so we must preserve it. But if we already
7155 * scheduled another cookie for removal, we cannot remove the
7156 * complete header, but we can remove the previous block itself.
7157 */
7158 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007160 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007161 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007162 if (att_beg >= del_from)
7163 att_beg += delta;
7164 if (att_end >= del_from)
7165 att_end += delta;
7166 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007167 val_end += delta;
7168 next += delta;
7169 hdr_end += delta;
7170 hdr_next += delta;
7171 cur_hdr->len += delta;
7172 http_msg_move_end(&txn->req, delta);
7173 prev = del_from;
7174 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007175 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007176 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007178 /* Look for the appsession cookie unless persistence must be ignored */
7179 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7180 int cmp_len, value_len;
7181 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007182
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007183 if (t->be->options2 & PR_O2_AS_PFX) {
7184 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7185 value_begin = att_beg + t->be->appsession_name_len;
7186 value_len = val_end - att_beg - t->be->appsession_name_len;
7187 } else {
7188 cmp_len = att_end - att_beg;
7189 value_begin = val_beg;
7190 value_len = val_end - val_beg;
7191 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007192
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007193 /* let's see if the cookie is our appcookie */
7194 if (cmp_len == t->be->appsession_name_len &&
7195 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7196 manage_client_side_appsession(t, value_begin, value_len);
7197 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198 }
7199
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007200 /* continue with next cookie on this header line */
7201 att_beg = next;
7202 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007203
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007204 /* There are no more cookies on this line.
7205 * We may still have one (or several) marked for deletion at the
7206 * end of the line. We must do this now in two ways :
7207 * - if some cookies must be preserved, we only delete from the
7208 * mark to the end of line ;
7209 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007210 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007211 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007212 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007213 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007214 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007215 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007216 cur_hdr->len += delta;
7217 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007218 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219
7220 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007221 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7222 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007224 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007225 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007226 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007227 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007228 }
7229
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007230 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007231 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007233}
7234
7235
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236/* Iterate the same filter through all response headers contained in <rtr>.
7237 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7238 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007239int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240{
7241 char term;
7242 char *cur_ptr, *cur_end, *cur_next;
7243 int cur_idx, old_idx, last_hdr;
7244 struct http_txn *txn = &t->txn;
7245 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007246 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247
7248 last_hdr = 0;
7249
Willy Tarreau9b28e032012-10-12 23:49:43 +02007250 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251 old_idx = 0;
7252
7253 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007254 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007256 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007257 (exp->action == ACT_ALLOW ||
7258 exp->action == ACT_DENY))
7259 return 0;
7260
7261 cur_idx = txn->hdr_idx.v[old_idx].next;
7262 if (!cur_idx)
7263 break;
7264
7265 cur_hdr = &txn->hdr_idx.v[cur_idx];
7266 cur_ptr = cur_next;
7267 cur_end = cur_ptr + cur_hdr->len;
7268 cur_next = cur_end + cur_hdr->cr + 1;
7269
7270 /* Now we have one header between cur_ptr and cur_end,
7271 * and the next header starts at cur_next.
7272 */
7273
7274 /* The annoying part is that pattern matching needs
7275 * that we modify the contents to null-terminate all
7276 * strings before testing them.
7277 */
7278
7279 term = *cur_end;
7280 *cur_end = '\0';
7281
7282 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7283 switch (exp->action) {
7284 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007285 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007286 last_hdr = 1;
7287 break;
7288
7289 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007290 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 last_hdr = 1;
7292 break;
7293
7294 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007295 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7296 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297 /* FIXME: if the user adds a newline in the replacement, the
7298 * index will not be recalculated for now, and the new line
7299 * will not be counted as a new header.
7300 */
7301
7302 cur_end += delta;
7303 cur_next += delta;
7304 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007305 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 break;
7307
7308 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007309 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310 cur_next += delta;
7311
Willy Tarreaufa355d42009-11-29 18:12:29 +01007312 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7314 txn->hdr_idx.used--;
7315 cur_hdr->len = 0;
7316 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007317 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318 break;
7319
7320 }
7321 }
7322 if (cur_end)
7323 *cur_end = term; /* restore the string terminator */
7324
7325 /* keep the link from this header to next one in case of later
7326 * removal of next header.
7327 */
7328 old_idx = cur_idx;
7329 }
7330 return 0;
7331}
7332
7333
7334/* Apply the filter to the status line in the response buffer <rtr>.
7335 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7336 * or -1 if a replacement resulted in an invalid status line.
7337 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007338int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339{
7340 char term;
7341 char *cur_ptr, *cur_end;
7342 int done;
7343 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007344 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345
7346
Willy Tarreau3d300592007-03-18 18:34:41 +01007347 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007349 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350 (exp->action == ACT_ALLOW ||
7351 exp->action == ACT_DENY))
7352 return 0;
7353 else if (exp->action == ACT_REMOVE)
7354 return 0;
7355
7356 done = 0;
7357
Willy Tarreau9b28e032012-10-12 23:49:43 +02007358 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007359 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360
7361 /* Now we have the status line between cur_ptr and cur_end */
7362
7363 /* The annoying part is that pattern matching needs
7364 * that we modify the contents to null-terminate all
7365 * strings before testing them.
7366 */
7367
7368 term = *cur_end;
7369 *cur_end = '\0';
7370
7371 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7372 switch (exp->action) {
7373 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007374 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 done = 1;
7376 break;
7377
7378 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007379 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380 done = 1;
7381 break;
7382
7383 case ACT_REPLACE:
7384 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007385 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7386 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387 /* FIXME: if the user adds a newline in the replacement, the
7388 * index will not be recalculated for now, and the new line
7389 * will not be counted as a new header.
7390 */
7391
Willy Tarreaufa355d42009-11-29 18:12:29 +01007392 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007393 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007394 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007395 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 cur_ptr, cur_end + 1,
7397 NULL, NULL);
7398 if (unlikely(!cur_end))
7399 return -1;
7400
7401 /* we have a full respnse and we know that we have either a CR
7402 * or an LF at <ptr>.
7403 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007404 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007405 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 /* there is no point trying this regex on headers */
7407 return 1;
7408 }
7409 }
7410 *cur_end = term; /* restore the string terminator */
7411 return done;
7412}
7413
7414
7415
7416/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007417 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7419 * unparsable response.
7420 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007421int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007422{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007423 struct http_txn *txn = &s->txn;
7424 struct hdr_exp *exp;
7425
7426 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 int ret;
7428
7429 /*
7430 * The interleaving of transformations and verdicts
7431 * makes it difficult to decide to continue or stop
7432 * the evaluation.
7433 */
7434
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007435 if (txn->flags & TX_SVDENY)
7436 break;
7437
Willy Tarreau3d300592007-03-18 18:34:41 +01007438 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007439 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7440 exp->action == ACT_PASS)) {
7441 exp = exp->next;
7442 continue;
7443 }
7444
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007445 /* if this filter had a condition, evaluate it now and skip to
7446 * next filter if the condition does not match.
7447 */
7448 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007449 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007450 ret = acl_pass(ret);
7451 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7452 ret = !ret;
7453 if (!ret)
7454 continue;
7455 }
7456
Willy Tarreaua15645d2007-03-18 16:22:39 +01007457 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007458 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007459 if (unlikely(ret < 0))
7460 return -1;
7461
7462 if (likely(ret == 0)) {
7463 /* The filter did not match the response, it can be
7464 * iterated through all headers.
7465 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007466 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007468 }
7469 return 0;
7470}
7471
7472
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007474 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007475 * desirable to call it only when needed. This function is also used when we
7476 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007478void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007479{
7480 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007481 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007482 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007483 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007484 char *hdr_beg, *hdr_end, *hdr_next;
7485 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007486
Willy Tarreaua15645d2007-03-18 16:22:39 +01007487 /* Iterate through the headers.
7488 * we start with the start line.
7489 */
7490 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007491 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007492
7493 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7494 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007495 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496
7497 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007498 hdr_beg = hdr_next;
7499 hdr_end = hdr_beg + cur_hdr->len;
7500 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 /* We have one full header between hdr_beg and hdr_end, and the
7503 * next header starts at hdr_next. We're only interested in
7504 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007505 */
7506
Willy Tarreau24581ba2010-08-31 22:39:35 +02007507 is_cookie2 = 0;
7508 prev = hdr_beg + 10;
7509 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007510 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007511 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7512 if (!val) {
7513 old_idx = cur_idx;
7514 continue;
7515 }
7516 is_cookie2 = 1;
7517 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518 }
7519
Willy Tarreau24581ba2010-08-31 22:39:35 +02007520 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7521 * <prev> points to the colon.
7522 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007523 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007524
Willy Tarreau24581ba2010-08-31 22:39:35 +02007525 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7526 * check-cache is enabled) and we are not interested in checking
7527 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007528 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007529 if (t->be->cookie_name == NULL &&
7530 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007531 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007532 return;
7533
Willy Tarreau24581ba2010-08-31 22:39:35 +02007534 /* OK so now we know we have to process this response cookie.
7535 * The format of the Set-Cookie header is slightly different
7536 * from the format of the Cookie header in that it does not
7537 * support the comma as a cookie delimiter (thus the header
7538 * cannot be folded) because the Expires attribute described in
7539 * the original Netscape's spec may contain an unquoted date
7540 * with a comma inside. We have to live with this because
7541 * many browsers don't support Max-Age and some browsers don't
7542 * support quoted strings. However the Set-Cookie2 header is
7543 * clean.
7544 *
7545 * We have to keep multiple pointers in order to support cookie
7546 * removal at the beginning, middle or end of header without
7547 * corrupting the header (in case of set-cookie2). A special
7548 * pointer, <scav> points to the beginning of the set-cookie-av
7549 * fields after the first semi-colon. The <next> pointer points
7550 * either to the end of line (set-cookie) or next unquoted comma
7551 * (set-cookie2). All of these headers are valid :
7552 *
7553 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7554 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7555 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7556 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7557 * | | | | | | | | | |
7558 * | | | | | | | | +-> next hdr_end <--+
7559 * | | | | | | | +------------> scav
7560 * | | | | | | +--------------> val_end
7561 * | | | | | +--------------------> val_beg
7562 * | | | | +----------------------> equal
7563 * | | | +------------------------> att_end
7564 * | | +----------------------------> att_beg
7565 * | +------------------------------> prev
7566 * +-----------------------------------------> hdr_beg
7567 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007568
Willy Tarreau24581ba2010-08-31 22:39:35 +02007569 for (; prev < hdr_end; prev = next) {
7570 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007571
Willy Tarreau24581ba2010-08-31 22:39:35 +02007572 /* find att_beg */
7573 att_beg = prev + 1;
7574 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7575 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007576
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 /* find att_end : this is the first character after the last non
7578 * space before the equal. It may be equal to hdr_end.
7579 */
7580 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007581
Willy Tarreau24581ba2010-08-31 22:39:35 +02007582 while (equal < hdr_end) {
7583 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7584 break;
7585 if (http_is_spht[(unsigned char)*equal++])
7586 continue;
7587 att_end = equal;
7588 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007589
Willy Tarreau24581ba2010-08-31 22:39:35 +02007590 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7591 * is between <att_beg> and <equal>, both may be identical.
7592 */
7593
7594 /* look for end of cookie if there is an equal sign */
7595 if (equal < hdr_end && *equal == '=') {
7596 /* look for the beginning of the value */
7597 val_beg = equal + 1;
7598 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7599 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600
Willy Tarreau24581ba2010-08-31 22:39:35 +02007601 /* find the end of the value, respecting quotes */
7602 next = find_cookie_value_end(val_beg, hdr_end);
7603
7604 /* make val_end point to the first white space or delimitor after the value */
7605 val_end = next;
7606 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7607 val_end--;
7608 } else {
7609 /* <equal> points to next comma, semi-colon or EOL */
7610 val_beg = val_end = next = equal;
7611 }
7612
7613 if (next < hdr_end) {
7614 /* Set-Cookie2 supports multiple cookies, and <next> points to
7615 * a colon or semi-colon before the end. So skip all attr-value
7616 * pairs and look for the next comma. For Set-Cookie, since
7617 * commas are permitted in values, skip to the end.
7618 */
7619 if (is_cookie2)
7620 next = find_hdr_value_end(next, hdr_end);
7621 else
7622 next = hdr_end;
7623 }
7624
7625 /* Now everything is as on the diagram above */
7626
7627 /* Ignore cookies with no equal sign */
7628 if (equal == val_end)
7629 continue;
7630
7631 /* If there are spaces around the equal sign, we need to
7632 * strip them otherwise we'll get trouble for cookie captures,
7633 * or even for rewrites. Since this happens extremely rarely,
7634 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007635 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007636 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7637 int stripped_before = 0;
7638 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639
Willy Tarreau24581ba2010-08-31 22:39:35 +02007640 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007641 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 equal += stripped_before;
7643 val_beg += stripped_before;
7644 }
7645
7646 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007647 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007648 val_beg += stripped_after;
7649 stripped_before += stripped_after;
7650 }
7651
7652 val_end += stripped_before;
7653 next += stripped_before;
7654 hdr_end += stripped_before;
7655 hdr_next += stripped_before;
7656 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007657 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007658 }
7659
7660 /* First, let's see if we want to capture this cookie. We check
7661 * that we don't already have a server side cookie, because we
7662 * can only capture one. Also as an optimisation, we ignore
7663 * cookies shorter than the declared name.
7664 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007665 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007666 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 (val_end - att_beg >= t->fe->capture_namelen) &&
7668 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7669 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007670 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671 Alert("HTTP logging : out of memory.\n");
7672 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007673 else {
7674 if (log_len > t->fe->capture_len)
7675 log_len = t->fe->capture_len;
7676 memcpy(txn->srv_cookie, att_beg, log_len);
7677 txn->srv_cookie[log_len] = 0;
7678 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 }
7680
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007681 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007683 if (!(t->flags & SN_IGNORE_PRST) &&
7684 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7685 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007686 /* assume passive cookie by default */
7687 txn->flags &= ~TX_SCK_MASK;
7688 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007689
7690 /* If the cookie is in insert mode on a known server, we'll delete
7691 * this occurrence because we'll insert another one later.
7692 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007693 * a direct access.
7694 */
Willy Tarreau67402132012-05-31 20:40:20 +02007695 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007696 /* The "preserve" flag was set, we don't want to touch the
7697 * server's cookie.
7698 */
7699 }
Willy Tarreau67402132012-05-31 20:40:20 +02007700 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7701 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007702 /* this cookie must be deleted */
7703 if (*prev == ':' && next == hdr_end) {
7704 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007705 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007706 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7707 txn->hdr_idx.used--;
7708 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007709 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007710 hdr_next += delta;
7711 http_msg_move_end(&txn->rsp, delta);
7712 /* note: while both invalid now, <next> and <hdr_end>
7713 * are still equal, so the for() will stop as expected.
7714 */
7715 } else {
7716 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007718 next = prev;
7719 hdr_end += delta;
7720 hdr_next += delta;
7721 cur_hdr->len += delta;
7722 http_msg_move_end(&txn->rsp, delta);
7723 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007724 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007725 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007726 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007727 }
Willy Tarreau67402132012-05-31 20:40:20 +02007728 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007729 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007730 * with this server since we know it.
7731 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007732 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007733 next += delta;
7734 hdr_end += delta;
7735 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007736 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007737 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007738
Willy Tarreauf1348312010-10-07 15:54:11 +02007739 txn->flags &= ~TX_SCK_MASK;
7740 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007741 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007742 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007744 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007745 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007746 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007747 next += delta;
7748 hdr_end += delta;
7749 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007750 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007751 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007752
Willy Tarreau827aee92011-03-10 16:55:02 +01007753 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007754 txn->flags &= ~TX_SCK_MASK;
7755 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007756 }
7757 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007758 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7759 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007760 int cmp_len, value_len;
7761 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007762
Cyril Bontéb21570a2009-11-29 20:04:48 +01007763 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007764 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7765 value_begin = att_beg + t->be->appsession_name_len;
7766 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007767 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007768 cmp_len = att_end - att_beg;
7769 value_begin = val_beg;
7770 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007771 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007772
Cyril Bonté17530c32010-04-06 21:11:10 +02007773 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007774 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7775 /* free a possibly previously allocated memory */
7776 pool_free2(apools.sessid, txn->sessid);
7777
Cyril Bontéb21570a2009-11-29 20:04:48 +01007778 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007779 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007780 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7781 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7782 return;
7783 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007784 memcpy(txn->sessid, value_begin, value_len);
7785 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007786 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007787 }
7788 /* that's done for this cookie, check the next one on the same
7789 * line when next != hdr_end (only if is_cookie2).
7790 */
7791 }
7792 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007794 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007795
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007796 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007797 appsess *asession = NULL;
7798 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007799 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007800 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007801 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007802 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7803 Alert("Not enough Memory process_srv():asession:calloc().\n");
7804 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7805 return;
7806 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007807 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7808
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007809 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7810 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7811 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007812 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007813 return;
7814 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007815 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007816 asession->sessid[t->be->appsession_len] = 0;
7817
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007818 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007819 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007820 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007821 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007822 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007823 return;
7824 }
7825 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007826 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007827
7828 asession->request_count = 0;
7829 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7830 }
7831
7832 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7833 asession->request_count++;
7834 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835}
7836
7837
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838/*
7839 * Check if response is cacheable or not. Updates t->flags.
7840 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007841void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007842{
7843 struct http_txn *txn = &t->txn;
7844 char *p1, *p2;
7845
7846 char *cur_ptr, *cur_end, *cur_next;
7847 int cur_idx;
7848
Willy Tarreau5df51872007-11-25 16:20:08 +01007849 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 return;
7851
7852 /* Iterate through the headers.
7853 * we start with the start line.
7854 */
7855 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007856 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007857
7858 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7859 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007860 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007861
7862 cur_hdr = &txn->hdr_idx.v[cur_idx];
7863 cur_ptr = cur_next;
7864 cur_end = cur_ptr + cur_hdr->len;
7865 cur_next = cur_end + cur_hdr->cr + 1;
7866
7867 /* We have one full header between cur_ptr and cur_end, and the
7868 * next header starts at cur_next. We're only interested in
7869 * "Cookie:" headers.
7870 */
7871
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007872 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7873 if (val) {
7874 if ((cur_end - (cur_ptr + val) >= 8) &&
7875 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7876 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7877 return;
7878 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879 }
7880
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007881 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7882 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 continue;
7884
7885 /* OK, right now we know we have a cache-control header at cur_ptr */
7886
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007887 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007888
7889 if (p1 >= cur_end) /* no more info */
7890 continue;
7891
7892 /* p1 is at the beginning of the value */
7893 p2 = p1;
7894
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007895 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 p2++;
7897
7898 /* we have a complete value between p1 and p2 */
7899 if (p2 < cur_end && *p2 == '=') {
7900 /* we have something of the form no-cache="set-cookie" */
7901 if ((cur_end - p1 >= 21) &&
7902 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7903 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007904 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905 continue;
7906 }
7907
7908 /* OK, so we know that either p2 points to the end of string or to a comma */
7909 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007910 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7912 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7913 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007914 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007915 return;
7916 }
7917
7918 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007919 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007920 continue;
7921 }
7922 }
7923}
7924
7925
Willy Tarreau58f10d72006-12-04 02:26:12 +01007926/*
7927 * Try to retrieve a known appsession in the URI, then the associated server.
7928 * If the server is found, it's assigned to the session.
7929 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007930void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007931{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007932 char *end_params, *first_param, *cur_param, *next_param;
7933 char separator;
7934 int value_len;
7935
7936 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007938 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007939 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007940 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007941 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007942
Cyril Bontéb21570a2009-11-29 20:04:48 +01007943 first_param = NULL;
7944 switch (mode) {
7945 case PR_O2_AS_M_PP:
7946 first_param = memchr(begin, ';', len);
7947 break;
7948 case PR_O2_AS_M_QS:
7949 first_param = memchr(begin, '?', len);
7950 break;
7951 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007952
Cyril Bontéb21570a2009-11-29 20:04:48 +01007953 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007955 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007956
Cyril Bontéb21570a2009-11-29 20:04:48 +01007957 switch (mode) {
7958 case PR_O2_AS_M_PP:
7959 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7960 end_params = (char *) begin + len;
7961 }
7962 separator = ';';
7963 break;
7964 case PR_O2_AS_M_QS:
7965 end_params = (char *) begin + len;
7966 separator = '&';
7967 break;
7968 default:
7969 /* unknown mode, shouldn't happen */
7970 return;
7971 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007972
Cyril Bontéb21570a2009-11-29 20:04:48 +01007973 cur_param = next_param = end_params;
7974 while (cur_param > first_param) {
7975 cur_param--;
7976 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7977 /* let's see if this is the appsession parameter */
7978 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7979 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7980 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7981 /* Cool... it's the right one */
7982 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7983 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7984 if (value_len > 0) {
7985 manage_client_side_appsession(t, cur_param, value_len);
7986 }
7987 break;
7988 }
7989 next_param = cur_param;
7990 }
7991 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007992#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007993 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007994 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007995#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007996}
7997
Willy Tarreaub2513902006-12-17 14:52:38 +01007998/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007999 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008000 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008001 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008002 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008003 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008004 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008005 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008006 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008007int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008008{
8009 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008010 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008011 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008012
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008013 if (!uri_auth)
8014 return 0;
8015
Cyril Bonté70be45d2010-10-12 00:14:35 +02008016 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008017 return 0;
8018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008019 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008020 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008021 return 0;
8022
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008023 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008024 return 0;
8025
Willy Tarreaub2513902006-12-17 14:52:38 +01008026 return 1;
8027}
8028
Willy Tarreau4076a152009-04-02 15:18:36 +02008029/*
8030 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008031 * By default it tries to report the error position as msg->err_pos. However if
8032 * this one is not set, it will then report msg->next, which is the last known
8033 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008034 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008035 */
8036void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008037 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008038 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008039{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008040 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008041 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008042
Willy Tarreau9b28e032012-10-12 23:49:43 +02008043 es->len = MIN(chn->buf->i, sizeof(es->buf));
8044 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008045 len1 = MIN(len1, es->len);
8046 len2 = es->len - len1; /* remaining data if buffer wraps */
8047
Willy Tarreau9b28e032012-10-12 23:49:43 +02008048 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008049 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008050 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008051
Willy Tarreau4076a152009-04-02 15:18:36 +02008052 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008053 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008054 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008055 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008056
Willy Tarreau4076a152009-04-02 15:18:36 +02008057 es->when = date; // user-visible date
8058 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008059 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008060 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008061 if (objt_conn(s->req->prod->end))
8062 es->src = __objt_conn(s->req->prod->end)->addr.from;
8063 else
8064 memset(&es->src, 0, sizeof(es->src));
8065
Willy Tarreau078272e2010-12-12 12:46:33 +01008066 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008067 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008068 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008069 es->s_flags = s->flags;
8070 es->t_flags = s->txn.flags;
8071 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008072 es->b_out = chn->buf->o;
8073 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008074 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008075 es->m_clen = msg->chunk_len;
8076 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008077}
Willy Tarreaub2513902006-12-17 14:52:38 +01008078
Willy Tarreau294c4732011-12-16 21:35:50 +01008079/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8080 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8081 * performed over the whole headers. Otherwise it must contain a valid header
8082 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8083 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8084 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8085 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008086 * -1. The value fetch stops at commas, so this function is suited for use with
8087 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008088 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008089 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008090unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008091 struct hdr_idx *idx, int occ,
8092 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008093{
Willy Tarreau294c4732011-12-16 21:35:50 +01008094 struct hdr_ctx local_ctx;
8095 char *ptr_hist[MAX_HDR_HISTORY];
8096 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008097 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008098 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008099
Willy Tarreau294c4732011-12-16 21:35:50 +01008100 if (!ctx) {
8101 local_ctx.idx = 0;
8102 ctx = &local_ctx;
8103 }
8104
Willy Tarreaubce70882009-09-07 11:51:47 +02008105 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008106 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008107 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008108 occ--;
8109 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008110 *vptr = ctx->line + ctx->val;
8111 *vlen = ctx->vlen;
8112 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008113 }
8114 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008115 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008116 }
8117
8118 /* negative occurrence, we scan all the list then walk back */
8119 if (-occ > MAX_HDR_HISTORY)
8120 return 0;
8121
Willy Tarreau294c4732011-12-16 21:35:50 +01008122 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008123 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008124 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8125 len_hist[hist_ptr] = ctx->vlen;
8126 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008127 hist_ptr = 0;
8128 found++;
8129 }
8130 if (-occ > found)
8131 return 0;
8132 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008133 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8134 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8135 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008136 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008137 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008138 if (hist_ptr >= MAX_HDR_HISTORY)
8139 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008140 *vptr = ptr_hist[hist_ptr];
8141 *vlen = len_hist[hist_ptr];
8142 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008143}
8144
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008145/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8146 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8147 * performed over the whole headers. Otherwise it must contain a valid header
8148 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8149 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8150 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8151 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8152 * -1. This function differs from http_get_hdr() in that it only returns full
8153 * line header values and does not stop at commas.
8154 * The return value is 0 if nothing was found, or non-zero otherwise.
8155 */
8156unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8157 struct hdr_idx *idx, int occ,
8158 struct hdr_ctx *ctx, char **vptr, int *vlen)
8159{
8160 struct hdr_ctx local_ctx;
8161 char *ptr_hist[MAX_HDR_HISTORY];
8162 int len_hist[MAX_HDR_HISTORY];
8163 unsigned int hist_ptr;
8164 int found;
8165
8166 if (!ctx) {
8167 local_ctx.idx = 0;
8168 ctx = &local_ctx;
8169 }
8170
8171 if (occ >= 0) {
8172 /* search from the beginning */
8173 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8174 occ--;
8175 if (occ <= 0) {
8176 *vptr = ctx->line + ctx->val;
8177 *vlen = ctx->vlen;
8178 return 1;
8179 }
8180 }
8181 return 0;
8182 }
8183
8184 /* negative occurrence, we scan all the list then walk back */
8185 if (-occ > MAX_HDR_HISTORY)
8186 return 0;
8187
8188 found = hist_ptr = 0;
8189 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8190 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8191 len_hist[hist_ptr] = ctx->vlen;
8192 if (++hist_ptr >= MAX_HDR_HISTORY)
8193 hist_ptr = 0;
8194 found++;
8195 }
8196 if (-occ > found)
8197 return 0;
8198 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8199 * find occurrence -occ, so we have to check [hist_ptr+occ].
8200 */
8201 hist_ptr += occ;
8202 if (hist_ptr >= MAX_HDR_HISTORY)
8203 hist_ptr -= MAX_HDR_HISTORY;
8204 *vptr = ptr_hist[hist_ptr];
8205 *vlen = len_hist[hist_ptr];
8206 return 1;
8207}
8208
Willy Tarreaubaaee002006-06-26 02:48:02 +02008209/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008210 * Print a debug line with a header. Always stop at the first CR or LF char,
8211 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8212 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008213 */
8214void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8215{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008216 int max;
8217 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008218 dir,
8219 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8220 objt_conn(t->req->cons->end) ? (unsigned short)objt_conn(t->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008221
8222 for (max = 0; start + max < end; max++)
8223 if (start[max] == '\r' || start[max] == '\n')
8224 break;
8225
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008226 UBOUND(max, trash.size - trash.len - 3);
8227 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8228 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008229 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008230}
8231
Willy Tarreau0937bc42009-12-22 15:03:09 +01008232/*
8233 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8234 * the required fields are properly allocated and that we only need to (re)init
8235 * them. This should be used before processing any new request.
8236 */
8237void http_init_txn(struct session *s)
8238{
8239 struct http_txn *txn = &s->txn;
8240 struct proxy *fe = s->fe;
8241
8242 txn->flags = 0;
8243 txn->status = -1;
8244
Willy Tarreauf64d1412010-10-07 20:06:11 +02008245 txn->cookie_first_date = 0;
8246 txn->cookie_last_date = 0;
8247
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008248 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008249 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008250 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008251 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008252 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008253 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008254 txn->req.chunk_len = 0LL;
8255 txn->req.body_len = 0LL;
8256 txn->rsp.chunk_len = 0LL;
8257 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008258 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8259 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008260 txn->req.chn = s->req;
8261 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008262
8263 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008264
8265 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8266 if (fe->options2 & PR_O2_REQBUG_OK)
8267 txn->req.err_pos = -1; /* let buggy requests pass */
8268
Willy Tarreau46023632010-01-07 22:51:47 +01008269 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008270 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8271
Willy Tarreau46023632010-01-07 22:51:47 +01008272 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008273 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8274
8275 if (txn->hdr_idx.v)
8276 hdr_idx_init(&txn->hdr_idx);
8277}
8278
8279/* to be used at the end of a transaction */
8280void http_end_txn(struct session *s)
8281{
8282 struct http_txn *txn = &s->txn;
8283
Willy Tarreau75195602014-03-11 15:48:55 +01008284 /* release any possible compression context */
8285 if (s->flags & SN_COMP_READY)
8286 s->comp_algo->end(&s->comp_ctx);
8287 s->comp_algo = NULL;
8288 s->flags &= ~SN_COMP_READY;
8289
Willy Tarreau0937bc42009-12-22 15:03:09 +01008290 /* these ones will have been dynamically allocated */
8291 pool_free2(pool2_requri, txn->uri);
8292 pool_free2(pool2_capture, txn->cli_cookie);
8293 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008294 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008295 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008296
William Lallemanda73203e2012-03-12 12:48:57 +01008297 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008298 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008299 txn->uri = NULL;
8300 txn->srv_cookie = NULL;
8301 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008302
8303 if (txn->req.cap) {
8304 struct cap_hdr *h;
8305 for (h = s->fe->req_cap; h; h = h->next)
8306 pool_free2(h->pool, txn->req.cap[h->index]);
8307 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8308 }
8309
8310 if (txn->rsp.cap) {
8311 struct cap_hdr *h;
8312 for (h = s->fe->rsp_cap; h; h = h->next)
8313 pool_free2(h->pool, txn->rsp.cap[h->index]);
8314 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8315 }
8316
Willy Tarreau0937bc42009-12-22 15:03:09 +01008317}
8318
8319/* to be used at the end of a transaction to prepare a new one */
8320void http_reset_txn(struct session *s)
8321{
8322 http_end_txn(s);
8323 http_init_txn(s);
8324
8325 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008326 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008327 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008328 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008329 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008330 /* re-init store persistence */
8331 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008332 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008333
Willy Tarreau0937bc42009-12-22 15:03:09 +01008334 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008335
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008336 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008337
Willy Tarreau739cfba2010-01-25 23:11:14 +01008338 /* We must trim any excess data from the response buffer, because we
8339 * may have blocked an invalid response from a server that we don't
8340 * want to accidentely forward once we disable the analysers, nor do
8341 * we want those data to come along with next response. A typical
8342 * example of such data would be from a buggy server responding to
8343 * a HEAD with some data, or sending more than the advertised
8344 * content-length.
8345 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008346 if (unlikely(s->rep->buf->i))
8347 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008348
Willy Tarreau0937bc42009-12-22 15:03:09 +01008349 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008350 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008351
Willy Tarreaud04e8582010-05-31 12:31:35 +02008352 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008353 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008354
8355 s->req->rex = TICK_ETERNITY;
8356 s->req->wex = TICK_ETERNITY;
8357 s->req->analyse_exp = TICK_ETERNITY;
8358 s->rep->rex = TICK_ETERNITY;
8359 s->rep->wex = TICK_ETERNITY;
8360 s->rep->analyse_exp = TICK_ETERNITY;
8361}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008362
Willy Tarreauff011f22011-01-06 17:51:27 +01008363void free_http_req_rules(struct list *r) {
8364 struct http_req_rule *tr, *pr;
8365
8366 list_for_each_entry_safe(pr, tr, r, list) {
8367 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008368 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008369 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008370
8371 free(pr);
8372 }
8373}
8374
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008375/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008376struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8377{
8378 struct http_req_rule *rule;
8379 int cur_arg;
8380
8381 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8382 if (!rule) {
8383 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008384 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008385 }
8386
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008387 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008388 rule->action = HTTP_REQ_ACT_ALLOW;
8389 cur_arg = 1;
8390 } else if (!strcmp(args[0], "deny")) {
8391 rule->action = HTTP_REQ_ACT_DENY;
8392 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008393 } else if (!strcmp(args[0], "tarpit")) {
8394 rule->action = HTTP_REQ_ACT_TARPIT;
8395 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008396 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008397 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008398 cur_arg = 1;
8399
8400 while(*args[cur_arg]) {
8401 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008402 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008403 cur_arg+=2;
8404 continue;
8405 } else
8406 break;
8407 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008408 } else if (!strcmp(args[0], "set-nice")) {
8409 rule->action = HTTP_REQ_ACT_SET_NICE;
8410 cur_arg = 1;
8411
8412 if (!*args[cur_arg] ||
8413 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8414 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8415 file, linenum, args[0]);
8416 goto out_err;
8417 }
8418 rule->arg.nice = atoi(args[cur_arg]);
8419 if (rule->arg.nice < -1024)
8420 rule->arg.nice = -1024;
8421 else if (rule->arg.nice > 1024)
8422 rule->arg.nice = 1024;
8423 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008424 } else if (!strcmp(args[0], "set-tos")) {
8425#ifdef IP_TOS
8426 char *err;
8427 rule->action = HTTP_REQ_ACT_SET_TOS;
8428 cur_arg = 1;
8429
8430 if (!*args[cur_arg] ||
8431 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8432 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8433 file, linenum, args[0]);
8434 goto out_err;
8435 }
8436
8437 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8438 if (err && *err != '\0') {
8439 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8440 file, linenum, err, args[0]);
8441 goto out_err;
8442 }
8443 cur_arg++;
8444#else
8445 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8446 goto out_err;
8447#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008448 } else if (!strcmp(args[0], "set-mark")) {
8449#ifdef SO_MARK
8450 char *err;
8451 rule->action = HTTP_REQ_ACT_SET_MARK;
8452 cur_arg = 1;
8453
8454 if (!*args[cur_arg] ||
8455 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8456 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8457 file, linenum, args[0]);
8458 goto out_err;
8459 }
8460
8461 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8462 if (err && *err != '\0') {
8463 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8464 file, linenum, err, args[0]);
8465 goto out_err;
8466 }
8467 cur_arg++;
8468 global.last_checks |= LSTCHK_NETADM;
8469#else
8470 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8471 goto out_err;
8472#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008473 } else if (!strcmp(args[0], "set-log-level")) {
8474 rule->action = HTTP_REQ_ACT_SET_LOGL;
8475 cur_arg = 1;
8476
8477 if (!*args[cur_arg] ||
8478 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8479 bad_log_level:
8480 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8481 file, linenum, args[0]);
8482 goto out_err;
8483 }
8484 if (strcmp(args[cur_arg], "silent") == 0)
8485 rule->arg.loglevel = -1;
8486 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8487 goto bad_log_level;
8488 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008489 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8490 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8491 cur_arg = 1;
8492
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008493 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8494 (*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 +01008495 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8496 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008497 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008498 }
8499
8500 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8501 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8502 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008503
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008504 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008505 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008506 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8507 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008508 free(proxy->conf.lfs_file);
8509 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8510 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008511 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008512 } else if (strcmp(args[0], "redirect") == 0) {
8513 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008514 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008515
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008516 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008517 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8518 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8519 goto out_err;
8520 }
8521
8522 /* this redirect rule might already contain a parsed condition which
8523 * we'll pass to the http-request rule.
8524 */
8525 rule->action = HTTP_REQ_ACT_REDIR;
8526 rule->arg.redir = redir;
8527 rule->cond = redir->cond;
8528 redir->cond = NULL;
8529 cur_arg = 2;
8530 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008531 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008532 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 +01008533 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008534 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008535 }
8536
8537 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8538 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008539 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008540
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008541 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8542 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8543 file, linenum, args[0], errmsg);
8544 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008545 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008546 }
8547 rule->cond = cond;
8548 }
8549 else if (*args[cur_arg]) {
8550 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8551 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8552 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008553 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008554 }
8555
8556 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008557 out_err:
8558 free(rule);
8559 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008560}
8561
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008562/* parse an "http-respose" rule */
8563struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8564{
8565 struct http_res_rule *rule;
8566 int cur_arg;
8567
8568 rule = calloc(1, sizeof(*rule));
8569 if (!rule) {
8570 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8571 goto out_err;
8572 }
8573
8574 if (!strcmp(args[0], "allow")) {
8575 rule->action = HTTP_RES_ACT_ALLOW;
8576 cur_arg = 1;
8577 } else if (!strcmp(args[0], "deny")) {
8578 rule->action = HTTP_RES_ACT_DENY;
8579 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008580 } else if (!strcmp(args[0], "set-nice")) {
8581 rule->action = HTTP_RES_ACT_SET_NICE;
8582 cur_arg = 1;
8583
8584 if (!*args[cur_arg] ||
8585 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8586 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8587 file, linenum, args[0]);
8588 goto out_err;
8589 }
8590 rule->arg.nice = atoi(args[cur_arg]);
8591 if (rule->arg.nice < -1024)
8592 rule->arg.nice = -1024;
8593 else if (rule->arg.nice > 1024)
8594 rule->arg.nice = 1024;
8595 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008596 } else if (!strcmp(args[0], "set-tos")) {
8597#ifdef IP_TOS
8598 char *err;
8599 rule->action = HTTP_RES_ACT_SET_TOS;
8600 cur_arg = 1;
8601
8602 if (!*args[cur_arg] ||
8603 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8604 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8605 file, linenum, args[0]);
8606 goto out_err;
8607 }
8608
8609 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8610 if (err && *err != '\0') {
8611 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8612 file, linenum, err, args[0]);
8613 goto out_err;
8614 }
8615 cur_arg++;
8616#else
8617 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8618 goto out_err;
8619#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008620 } else if (!strcmp(args[0], "set-mark")) {
8621#ifdef SO_MARK
8622 char *err;
8623 rule->action = HTTP_RES_ACT_SET_MARK;
8624 cur_arg = 1;
8625
8626 if (!*args[cur_arg] ||
8627 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8628 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8629 file, linenum, args[0]);
8630 goto out_err;
8631 }
8632
8633 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8634 if (err && *err != '\0') {
8635 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8636 file, linenum, err, args[0]);
8637 goto out_err;
8638 }
8639 cur_arg++;
8640 global.last_checks |= LSTCHK_NETADM;
8641#else
8642 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8643 goto out_err;
8644#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008645 } else if (!strcmp(args[0], "set-log-level")) {
8646 rule->action = HTTP_RES_ACT_SET_LOGL;
8647 cur_arg = 1;
8648
8649 if (!*args[cur_arg] ||
8650 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8651 bad_log_level:
8652 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8653 file, linenum, args[0]);
8654 goto out_err;
8655 }
8656 if (strcmp(args[cur_arg], "silent") == 0)
8657 rule->arg.loglevel = -1;
8658 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8659 goto bad_log_level;
8660 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008661 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8662 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8663 cur_arg = 1;
8664
8665 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8666 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8667 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8668 file, linenum, args[0]);
8669 goto out_err;
8670 }
8671
8672 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8673 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8674 LIST_INIT(&rule->arg.hdr_add.fmt);
8675
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008676 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008677 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008678 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8679 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008680 free(proxy->conf.lfs_file);
8681 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8682 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008683 cur_arg += 2;
8684 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008685 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 +02008686 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8687 goto out_err;
8688 }
8689
8690 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8691 struct acl_cond *cond;
8692 char *errmsg = NULL;
8693
8694 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8695 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8696 file, linenum, args[0], errmsg);
8697 free(errmsg);
8698 goto out_err;
8699 }
8700 rule->cond = cond;
8701 }
8702 else if (*args[cur_arg]) {
8703 Alert("parsing [%s:%d]: 'http-response %s' expects"
8704 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8705 file, linenum, args[0], args[cur_arg]);
8706 goto out_err;
8707 }
8708
8709 return rule;
8710 out_err:
8711 free(rule);
8712 return NULL;
8713}
8714
Willy Tarreau4baae242012-12-27 12:00:31 +01008715/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008716 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8717 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008718 */
8719struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008720 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008721{
8722 struct redirect_rule *rule;
8723 int cur_arg;
8724 int type = REDIRECT_TYPE_NONE;
8725 int code = 302;
8726 const char *destination = NULL;
8727 const char *cookie = NULL;
8728 int cookie_set = 0;
8729 unsigned int flags = REDIRECT_FLAG_NONE;
8730 struct acl_cond *cond = NULL;
8731
8732 cur_arg = 0;
8733 while (*(args[cur_arg])) {
8734 if (strcmp(args[cur_arg], "location") == 0) {
8735 if (!*args[cur_arg + 1])
8736 goto missing_arg;
8737
8738 type = REDIRECT_TYPE_LOCATION;
8739 cur_arg++;
8740 destination = args[cur_arg];
8741 }
8742 else if (strcmp(args[cur_arg], "prefix") == 0) {
8743 if (!*args[cur_arg + 1])
8744 goto missing_arg;
8745
8746 type = REDIRECT_TYPE_PREFIX;
8747 cur_arg++;
8748 destination = args[cur_arg];
8749 }
8750 else if (strcmp(args[cur_arg], "scheme") == 0) {
8751 if (!*args[cur_arg + 1])
8752 goto missing_arg;
8753
8754 type = REDIRECT_TYPE_SCHEME;
8755 cur_arg++;
8756 destination = args[cur_arg];
8757 }
8758 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8759 if (!*args[cur_arg + 1])
8760 goto missing_arg;
8761
8762 cur_arg++;
8763 cookie = args[cur_arg];
8764 cookie_set = 1;
8765 }
8766 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8767 if (!*args[cur_arg + 1])
8768 goto missing_arg;
8769
8770 cur_arg++;
8771 cookie = args[cur_arg];
8772 cookie_set = 0;
8773 }
8774 else if (strcmp(args[cur_arg], "code") == 0) {
8775 if (!*args[cur_arg + 1])
8776 goto missing_arg;
8777
8778 cur_arg++;
8779 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008780 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008781 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008782 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008783 args[cur_arg - 1], args[cur_arg]);
8784 return NULL;
8785 }
8786 }
8787 else if (!strcmp(args[cur_arg],"drop-query")) {
8788 flags |= REDIRECT_FLAG_DROP_QS;
8789 }
8790 else if (!strcmp(args[cur_arg],"append-slash")) {
8791 flags |= REDIRECT_FLAG_APPEND_SLASH;
8792 }
8793 else if (strcmp(args[cur_arg], "if") == 0 ||
8794 strcmp(args[cur_arg], "unless") == 0) {
8795 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8796 if (!cond) {
8797 memprintf(errmsg, "error in condition: %s", *errmsg);
8798 return NULL;
8799 }
8800 break;
8801 }
8802 else {
8803 memprintf(errmsg,
8804 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8805 args[cur_arg]);
8806 return NULL;
8807 }
8808 cur_arg++;
8809 }
8810
8811 if (type == REDIRECT_TYPE_NONE) {
8812 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8813 return NULL;
8814 }
8815
8816 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8817 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008818 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008819
8820 if (!use_fmt) {
8821 /* old-style static redirect rule */
8822 rule->rdr_str = strdup(destination);
8823 rule->rdr_len = strlen(destination);
8824 }
8825 else {
8826 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008827
8828 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8829 * if prefix == "/", we don't want to add anything, otherwise it
8830 * makes it hard for the user to configure a self-redirection.
8831 */
8832 proxy->conf.args.ctx = ARGC_RDR;
8833 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008834 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008835 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8836 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008837 free(curproxy->conf.lfs_file);
8838 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8839 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008840 }
8841 }
8842
Willy Tarreau4baae242012-12-27 12:00:31 +01008843 if (cookie) {
8844 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8845 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8846 */
8847 rule->cookie_len = strlen(cookie);
8848 if (cookie_set) {
8849 rule->cookie_str = malloc(rule->cookie_len + 10);
8850 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8851 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8852 rule->cookie_len += 9;
8853 } else {
8854 rule->cookie_str = malloc(rule->cookie_len + 21);
8855 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8856 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8857 rule->cookie_len += 20;
8858 }
8859 }
8860 rule->type = type;
8861 rule->code = code;
8862 rule->flags = flags;
8863 LIST_INIT(&rule->list);
8864 return rule;
8865
8866 missing_arg:
8867 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8868 return NULL;
8869}
8870
Willy Tarreau8797c062007-05-07 00:55:35 +02008871/************************************************************************/
8872/* The code below is dedicated to ACL parsing and matching */
8873/************************************************************************/
8874
8875
Willy Tarreau14174bc2012-04-16 14:34:04 +02008876/* This function ensures that the prerequisites for an L7 fetch are ready,
8877 * which means that a request or response is ready. If some data is missing,
8878 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008879 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8880 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008881 *
8882 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008883 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8884 * decide whether or not an HTTP message is present ;
8885 * 0 if the requested data cannot be fetched or if it is certain that
8886 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008887 * 1 if an HTTP message is ready
8888 */
8889static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008890smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008891 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008892{
8893 struct http_txn *txn = l7;
8894 struct http_msg *msg = &txn->req;
8895
8896 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8897 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8898 */
8899
8900 if (unlikely(!s || !txn))
8901 return 0;
8902
8903 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008904 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008905
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008906 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008907 if (unlikely(!s->req))
8908 return 0;
8909
Willy Tarreauaae75e32013-03-29 12:31:49 +01008910 /* If the buffer does not leave enough free space at the end,
8911 * we must first realign it.
8912 */
8913 if (s->req->buf->p > s->req->buf->data &&
8914 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8915 buffer_slow_realign(s->req->buf);
8916
Willy Tarreau14174bc2012-04-16 14:34:04 +02008917 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008918 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008919 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008920
8921 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008922 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008923 http_msg_analyzer(msg, &txn->hdr_idx);
8924
8925 /* Still no valid request ? */
8926 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008927 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008928 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008929 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008930 }
8931 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008932 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008933 return 0;
8934 }
8935
8936 /* OK we just got a valid HTTP request. We have some minor
8937 * preparation to perform so that further checks can rely
8938 * on HTTP tests.
8939 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008940
8941 /* If the request was parsed but was too large, we must absolutely
8942 * return an error so that it is not processed. At the moment this
8943 * cannot happen, but if the parsers are to change in the future,
8944 * we want this check to be maintained.
8945 */
8946 if (unlikely(s->req->buf->i + s->req->buf->p >
8947 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8948 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008949 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008950 return 1;
8951 }
8952
Willy Tarreau9b28e032012-10-12 23:49:43 +02008953 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008954 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8955 s->flags |= SN_REDIRECTABLE;
8956
Willy Tarreau506d0502013-07-06 13:29:24 +02008957 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8958 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008959 }
8960
Willy Tarreau506d0502013-07-06 13:29:24 +02008961 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008962 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008963 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008964
8965 /* otherwise everything's ready for the request */
8966 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008967 else {
8968 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008969 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8970 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008971 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008972 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008973 }
8974
8975 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008976 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008977 return 1;
8978}
Willy Tarreau8797c062007-05-07 00:55:35 +02008979
Willy Tarreauc0239e02012-04-16 14:42:55 +02008980#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008981 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 +02008982
Willy Tarreau24e32d82012-04-23 23:55:44 +02008983#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008984 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 +02008985
Willy Tarreau8797c062007-05-07 00:55:35 +02008986
8987/* 1. Check on METHOD
8988 * We use the pre-parsed method if it is known, and store its number as an
8989 * integer. If it is unknown, we use the pointer and the length.
8990 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01008991static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008992{
8993 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008994 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008995
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01008996 len = strlen(text);
8997 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008998
8999 pattern->val.i = meth;
9000 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009001 trash = get_trash_chunk();
9002 if (trash->size < len) {
9003 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9004 len, trash->size);
9005 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009006 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009007 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009008 pattern->len = len;
9009 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009010 else {
9011 pattern->ptr.str = NULL;
9012 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009013 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009014 return 1;
9015}
9016
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009017/* This function fetches the method of current HTTP request and stores
9018 * it in the global pattern struct as a chunk. There are two possibilities :
9019 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9020 * in <len> and <ptr> is NULL ;
9021 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9022 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009023 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009024 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009025static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009026smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009027 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009028{
9029 int meth;
9030 struct http_txn *txn = l7;
9031
Willy Tarreau24e32d82012-04-23 23:55:44 +02009032 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009033
Willy Tarreau8797c062007-05-07 00:55:35 +02009034 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009035 smp->type = SMP_T_METH;
9036 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009037 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009038 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9039 /* ensure the indexes are not affected */
9040 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009041 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009042 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9043 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009044 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009045 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009046 return 1;
9047}
9048
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009049/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009050static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009051{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009052 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009053 struct pattern_list *lst;
9054 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009055
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009056 list_for_each_entry(lst, &expr->patterns, list) {
9057 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009058
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009059 /* well-known method */
9060 if (pattern->val.i != HTTP_METH_OTHER) {
9061 if (smp->data.meth.meth == pattern->val.i)
9062 return pattern;
9063 else
9064 continue;
9065 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009066
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009067 /* Other method, we must compare the strings */
9068 if (pattern->len != smp->data.meth.str.len)
9069 continue;
9070
9071 icase = pattern->flags & PAT_F_IGNORE_CASE;
9072 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9073 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9074 return pattern;
9075 }
9076 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009077}
9078
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009079static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009080smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009081 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009082{
9083 struct http_txn *txn = l7;
9084 char *ptr;
9085 int len;
9086
Willy Tarreauc0239e02012-04-16 14:42:55 +02009087 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009088
Willy Tarreau8797c062007-05-07 00:55:35 +02009089 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009090 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009091
9092 while ((len-- > 0) && (*ptr++ != '/'));
9093 if (len <= 0)
9094 return 0;
9095
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009096 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009097 smp->data.str.str = ptr;
9098 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009099
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009100 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009101 return 1;
9102}
9103
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009104static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009105smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009106 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009107{
9108 struct http_txn *txn = l7;
9109 char *ptr;
9110 int len;
9111
Willy Tarreauc0239e02012-04-16 14:42:55 +02009112 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009113
Willy Tarreauf26b2522012-12-14 08:33:14 +01009114 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9115 return 0;
9116
Willy Tarreau8797c062007-05-07 00:55:35 +02009117 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009118 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009119
9120 while ((len-- > 0) && (*ptr++ != '/'));
9121 if (len <= 0)
9122 return 0;
9123
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009124 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009125 smp->data.str.str = ptr;
9126 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009127
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009128 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009129 return 1;
9130}
9131
9132/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009133static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009134smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009135 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009136{
9137 struct http_txn *txn = l7;
9138 char *ptr;
9139 int len;
9140
Willy Tarreauc0239e02012-04-16 14:42:55 +02009141 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009142
Willy Tarreauf26b2522012-12-14 08:33:14 +01009143 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9144 return 0;
9145
Willy Tarreau8797c062007-05-07 00:55:35 +02009146 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009147 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009148
Willy Tarreauf853c462012-04-23 18:53:56 +02009149 smp->type = SMP_T_UINT;
9150 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009151 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009152 return 1;
9153}
9154
9155/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009156static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009157smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009158 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009159{
9160 struct http_txn *txn = l7;
9161
Willy Tarreauc0239e02012-04-16 14:42:55 +02009162 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009163
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009164 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009165 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009166 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009167 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009168 return 1;
9169}
9170
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009171static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009172smp_fetch_url_ip(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)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009174{
9175 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009176 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009177
Willy Tarreauc0239e02012-04-16 14:42:55 +02009178 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009179
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009180 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9181 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009182 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009183
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009184 smp->type = SMP_T_IPV4;
9185 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009186 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009187 return 1;
9188}
9189
9190static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009191smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009192 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009193{
9194 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009195 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009196
Willy Tarreauc0239e02012-04-16 14:42:55 +02009197 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009198
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009199 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9200 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9201 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009202
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009203 smp->type = SMP_T_UINT;
9204 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009205 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009206 return 1;
9207}
9208
Willy Tarreau185b5c42012-04-26 15:11:51 +02009209/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9210 * Accepts an optional argument of type string containing the header field name,
9211 * and an optional argument of type signed or unsigned integer to request an
9212 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009213 * headers are considered from the first one. It does not stop on commas and
9214 * returns full lines instead (useful for User-Agent or Date for example).
9215 */
9216static int
9217smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009218 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009219{
9220 struct http_txn *txn = l7;
9221 struct hdr_idx *idx = &txn->hdr_idx;
9222 struct hdr_ctx *ctx = smp->ctx.a[0];
9223 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9224 int occ = 0;
9225 const char *name_str = NULL;
9226 int name_len = 0;
9227
9228 if (!ctx) {
9229 /* first call */
9230 ctx = &static_hdr_ctx;
9231 ctx->idx = 0;
9232 smp->ctx.a[0] = ctx;
9233 }
9234
9235 if (args) {
9236 if (args[0].type != ARGT_STR)
9237 return 0;
9238 name_str = args[0].data.str.str;
9239 name_len = args[0].data.str.len;
9240
9241 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9242 occ = args[1].data.uint;
9243 }
9244
9245 CHECK_HTTP_MESSAGE_FIRST();
9246
9247 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9248 /* search for header from the beginning */
9249 ctx->idx = 0;
9250
9251 if (!occ && !(opt & SMP_OPT_ITERATE))
9252 /* no explicit occurrence and single fetch => last header by default */
9253 occ = -1;
9254
9255 if (!occ)
9256 /* prepare to report multiple occurrences for ACL fetches */
9257 smp->flags |= SMP_F_NOT_LAST;
9258
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009259 smp->type = SMP_T_STR;
9260 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009261 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9262 return 1;
9263
9264 smp->flags &= ~SMP_F_NOT_LAST;
9265 return 0;
9266}
9267
9268/* 6. Check on HTTP header count. The number of occurrences is returned.
9269 * Accepts exactly 1 argument of type string. It does not stop on commas and
9270 * returns full lines instead (useful for User-Agent or Date for example).
9271 */
9272static int
9273smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009274 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009275{
9276 struct http_txn *txn = l7;
9277 struct hdr_idx *idx = &txn->hdr_idx;
9278 struct hdr_ctx ctx;
9279 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9280 int cnt;
9281
9282 if (!args || args->type != ARGT_STR)
9283 return 0;
9284
9285 CHECK_HTTP_MESSAGE_FIRST();
9286
9287 ctx.idx = 0;
9288 cnt = 0;
9289 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9290 cnt++;
9291
9292 smp->type = SMP_T_UINT;
9293 smp->data.uint = cnt;
9294 smp->flags = SMP_F_VOL_HDR;
9295 return 1;
9296}
9297
9298/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9299 * Accepts an optional argument of type string containing the header field name,
9300 * and an optional argument of type signed or unsigned integer to request an
9301 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009302 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009303 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009304static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009305smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009306 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009307{
9308 struct http_txn *txn = l7;
9309 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009310 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009311 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009312 int occ = 0;
9313 const char *name_str = NULL;
9314 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009315
Willy Tarreaua890d072013-04-02 12:01:06 +02009316 if (!ctx) {
9317 /* first call */
9318 ctx = &static_hdr_ctx;
9319 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009320 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009321 }
9322
Willy Tarreau185b5c42012-04-26 15:11:51 +02009323 if (args) {
9324 if (args[0].type != ARGT_STR)
9325 return 0;
9326 name_str = args[0].data.str.str;
9327 name_len = args[0].data.str.len;
9328
9329 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9330 occ = args[1].data.uint;
9331 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009332
Willy Tarreaue333ec92012-04-16 16:26:40 +02009333 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009334
Willy Tarreau185b5c42012-04-26 15:11:51 +02009335 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009336 /* search for header from the beginning */
9337 ctx->idx = 0;
9338
Willy Tarreau185b5c42012-04-26 15:11:51 +02009339 if (!occ && !(opt & SMP_OPT_ITERATE))
9340 /* no explicit occurrence and single fetch => last header by default */
9341 occ = -1;
9342
9343 if (!occ)
9344 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009345 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009346
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009347 smp->type = SMP_T_STR;
9348 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009349 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 +02009350 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009351
Willy Tarreau37406352012-04-23 16:16:37 +02009352 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009353 return 0;
9354}
9355
Willy Tarreauc11416f2007-06-17 16:58:38 +02009356/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009357 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009358 */
9359static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009360smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009361 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009362{
9363 struct http_txn *txn = l7;
9364 struct hdr_idx *idx = &txn->hdr_idx;
9365 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009366 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009367 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009368
Willy Tarreau24e32d82012-04-23 23:55:44 +02009369 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009370 return 0;
9371
Willy Tarreaue333ec92012-04-16 16:26:40 +02009372 CHECK_HTTP_MESSAGE_FIRST();
9373
Willy Tarreau33a7e692007-06-10 19:45:56 +02009374 ctx.idx = 0;
9375 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009376 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 +02009377 cnt++;
9378
Willy Tarreauf853c462012-04-23 18:53:56 +02009379 smp->type = SMP_T_UINT;
9380 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009381 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009382 return 1;
9383}
9384
Willy Tarreau185b5c42012-04-26 15:11:51 +02009385/* Fetch an HTTP header's integer value. The integer value is returned. It
9386 * takes a mandatory argument of type string and an optional one of type int
9387 * to designate a specific occurrence. It returns an unsigned integer, which
9388 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009389 */
9390static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009391smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009392 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009393{
Willy Tarreauef38c392013-07-22 16:29:32 +02009394 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009395
Willy Tarreauf853c462012-04-23 18:53:56 +02009396 if (ret > 0) {
9397 smp->type = SMP_T_UINT;
9398 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9399 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009400
Willy Tarreaud53e2422012-04-16 17:21:11 +02009401 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009402}
9403
Cyril Bonté69fa9922012-10-25 00:01:06 +02009404/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9405 * and an optional one of type int to designate a specific occurrence.
9406 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009407 */
9408static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009409smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009410 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009411{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009412 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009413
Willy Tarreauef38c392013-07-22 16:29:32 +02009414 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009415 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9416 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009417 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009418 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009419 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009420 if (smp->data.str.len < temp->size - 1) {
9421 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9422 temp->str[smp->data.str.len] = '\0';
9423 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9424 smp->type = SMP_T_IPV6;
9425 break;
9426 }
9427 }
9428 }
9429
Willy Tarreaud53e2422012-04-16 17:21:11 +02009430 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009431 if (!(smp->flags & SMP_F_NOT_LAST))
9432 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009433 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009434 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009435}
9436
Willy Tarreau737b0c12007-06-10 21:28:46 +02009437/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9438 * the first '/' after the possible hostname, and ends before the possible '?'.
9439 */
9440static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009441smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009442 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009443{
9444 struct http_txn *txn = l7;
9445 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009446
Willy Tarreauc0239e02012-04-16 14:42:55 +02009447 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009448
Willy Tarreau9b28e032012-10-12 23:49:43 +02009449 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009450 ptr = http_get_path(txn);
9451 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009452 return 0;
9453
9454 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009455 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009456 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009457
9458 while (ptr < end && *ptr != '?')
9459 ptr++;
9460
Willy Tarreauf853c462012-04-23 18:53:56 +02009461 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009462 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009463 return 1;
9464}
9465
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009466/* This produces a concatenation of the first occurrence of the Host header
9467 * followed by the path component if it begins with a slash ('/'). This means
9468 * that '*' will not be added, resulting in exactly the first Host entry.
9469 * If no Host header is found, then the path is returned as-is. The returned
9470 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009471 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009472 */
9473static int
9474smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009475 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009476{
9477 struct http_txn *txn = l7;
9478 char *ptr, *end, *beg;
9479 struct hdr_ctx ctx;
9480
9481 CHECK_HTTP_MESSAGE_FIRST();
9482
9483 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009484 if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009485 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009486 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009487
9488 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009489 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009490 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009491 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009492 smp->data.str.len = ctx.vlen;
9493
9494 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009495 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009496 beg = http_get_path(txn);
9497 if (!beg)
9498 beg = end;
9499
9500 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9501
9502 if (beg < ptr && *beg == '/') {
9503 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9504 smp->data.str.len += ptr - beg;
9505 }
9506
9507 smp->flags = SMP_F_VOL_1ST;
9508 return 1;
9509}
9510
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009511/* This produces a 32-bit hash of the concatenation of the first occurrence of
9512 * the Host header followed by the path component if it begins with a slash ('/').
9513 * This means that '*' will not be added, resulting in exactly the first Host
9514 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009515 * is hashed using the path hash followed by a full avalanche hash and provides a
9516 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009517 * high-traffic sites without having to store whole paths.
9518 */
9519static int
9520smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009521 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009522{
9523 struct http_txn *txn = l7;
9524 struct hdr_ctx ctx;
9525 unsigned int hash = 0;
9526 char *ptr, *beg, *end;
9527 int len;
9528
9529 CHECK_HTTP_MESSAGE_FIRST();
9530
9531 ctx.idx = 0;
9532 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9533 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9534 ptr = ctx.line + ctx.val;
9535 len = ctx.vlen;
9536 while (len--)
9537 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9538 }
9539
9540 /* now retrieve the path */
9541 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9542 beg = http_get_path(txn);
9543 if (!beg)
9544 beg = end;
9545
9546 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9547
9548 if (beg < ptr && *beg == '/') {
9549 while (beg < ptr)
9550 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9551 }
9552 hash = full_hash(hash);
9553
9554 smp->type = SMP_T_UINT;
9555 smp->data.uint = hash;
9556 smp->flags = SMP_F_VOL_1ST;
9557 return 1;
9558}
9559
Willy Tarreau4a550602012-12-09 14:53:32 +01009560/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009561 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9562 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9563 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009564 * that in environments where IPv6 is insignificant, truncating the output to
9565 * 8 bytes would still work.
9566 */
9567static int
9568smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009569 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009570{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009571 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009572 struct connection *cli_conn = objt_conn(l4->si[0].end);
9573
9574 if (!cli_conn)
9575 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009576
Willy Tarreauef38c392013-07-22 16:29:32 +02009577 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009578 return 0;
9579
Willy Tarreau47ca5452012-12-23 20:22:19 +01009580 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009581 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9582 temp->len += sizeof(smp->data.uint);
9583
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009584 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009585 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009586 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009587 temp->len += 4;
9588 break;
9589 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009590 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009591 temp->len += 16;
9592 break;
9593 default:
9594 return 0;
9595 }
9596
9597 smp->data.str = *temp;
9598 smp->type = SMP_T_BIN;
9599 return 1;
9600}
9601
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009602static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009603smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009604 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009605{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009606 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9607 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9608 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009609
Willy Tarreau24e32d82012-04-23 23:55:44 +02009610 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009611
Willy Tarreauf853c462012-04-23 18:53:56 +02009612 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009613 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009614 return 1;
9615}
9616
Willy Tarreau7f18e522010-10-22 20:04:13 +02009617/* return a valid test if the current request is the first one on the connection */
9618static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009619smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009620 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009621{
9622 if (!s)
9623 return 0;
9624
Willy Tarreauf853c462012-04-23 18:53:56 +02009625 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009626 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009627 return 1;
9628}
9629
Willy Tarreau34db1082012-04-19 17:16:54 +02009630/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009631static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009632smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009633 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009634{
9635
Willy Tarreau24e32d82012-04-23 23:55:44 +02009636 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009637 return 0;
9638
Willy Tarreauc0239e02012-04-16 14:42:55 +02009639 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009640
Willy Tarreauc0239e02012-04-16 14:42:55 +02009641 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009642 return 0;
9643
Willy Tarreauf853c462012-04-23 18:53:56 +02009644 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009645 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009646 return 1;
9647}
Willy Tarreau8797c062007-05-07 00:55:35 +02009648
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009649/* Accepts exactly 1 argument of type userlist */
9650static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009651smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009652 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009653{
9654
9655 if (!args || args->type != ARGT_USR)
9656 return 0;
9657
9658 CHECK_HTTP_MESSAGE_FIRST();
9659
9660 if (!get_http_auth(l4))
9661 return 0;
9662
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009663 /* if the user does not belong to the userlist or has a wrong password,
9664 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009665 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009666 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009667 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9668 return 0;
9669
9670 /* pat_match_auth() will need the user list */
9671 smp->ctx.a[0] = args->data.usr;
9672
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009673 smp->type = SMP_T_STR;
9674 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009675 smp->data.str.str = l4->txn.auth.user;
9676 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009677
9678 return 1;
9679}
9680
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009681/* Try to find the next occurrence of a cookie name in a cookie header value.
9682 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9683 * the cookie value is returned into *value and *value_l, and the function
9684 * returns a pointer to the next pointer to search from if the value was found.
9685 * Otherwise if the cookie was not found, NULL is returned and neither value
9686 * nor value_l are touched. The input <hdr> string should first point to the
9687 * header's value, and the <hdr_end> pointer must point to the first character
9688 * not part of the value. <list> must be non-zero if value may represent a list
9689 * of values (cookie headers). This makes it faster to abort parsing when no
9690 * list is expected.
9691 */
9692static char *
9693extract_cookie_value(char *hdr, const char *hdr_end,
9694 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009695 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009696{
9697 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9698 char *next;
9699
9700 /* we search at least a cookie name followed by an equal, and more
9701 * generally something like this :
9702 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9703 */
9704 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9705 /* Iterate through all cookies on this line */
9706
9707 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9708 att_beg++;
9709
9710 /* find att_end : this is the first character after the last non
9711 * space before the equal. It may be equal to hdr_end.
9712 */
9713 equal = att_end = att_beg;
9714
9715 while (equal < hdr_end) {
9716 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9717 break;
9718 if (http_is_spht[(unsigned char)*equal++])
9719 continue;
9720 att_end = equal;
9721 }
9722
9723 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9724 * is between <att_beg> and <equal>, both may be identical.
9725 */
9726
9727 /* look for end of cookie if there is an equal sign */
9728 if (equal < hdr_end && *equal == '=') {
9729 /* look for the beginning of the value */
9730 val_beg = equal + 1;
9731 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9732 val_beg++;
9733
9734 /* find the end of the value, respecting quotes */
9735 next = find_cookie_value_end(val_beg, hdr_end);
9736
9737 /* make val_end point to the first white space or delimitor after the value */
9738 val_end = next;
9739 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9740 val_end--;
9741 } else {
9742 val_beg = val_end = next = equal;
9743 }
9744
9745 /* We have nothing to do with attributes beginning with '$'. However,
9746 * they will automatically be removed if a header before them is removed,
9747 * since they're supposed to be linked together.
9748 */
9749 if (*att_beg == '$')
9750 continue;
9751
9752 /* Ignore cookies with no equal sign */
9753 if (equal == next)
9754 continue;
9755
9756 /* Now we have the cookie name between att_beg and att_end, and
9757 * its value between val_beg and val_end.
9758 */
9759
9760 if (att_end - att_beg == cookie_name_l &&
9761 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9762 /* let's return this value and indicate where to go on from */
9763 *value = val_beg;
9764 *value_l = val_end - val_beg;
9765 return next + 1;
9766 }
9767
9768 /* Set-Cookie headers only have the name in the first attr=value part */
9769 if (!list)
9770 break;
9771 }
9772
9773 return NULL;
9774}
9775
William Lallemanda43ba4e2014-01-28 18:14:25 +01009776/* Fetch a captured HTTP request header. The index is the position of
9777 * the "capture" option in the configuration file
9778 */
9779static int
9780smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9781 const struct arg *args, struct sample *smp, const char *kw)
9782{
9783 struct proxy *fe = l4->fe;
9784 struct http_txn *txn = l7;
9785 int idx;
9786
9787 if (!args || args->type != ARGT_UINT)
9788 return 0;
9789
9790 idx = args->data.uint;
9791
9792 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9793 return 0;
9794
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009795 smp->type = SMP_T_STR;
9796 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009797 smp->data.str.str = txn->req.cap[idx];
9798 smp->data.str.len = strlen(txn->req.cap[idx]);
9799
9800 return 1;
9801}
9802
9803/* Fetch a captured HTTP response header. The index is the position of
9804 * the "capture" option in the configuration file
9805 */
9806static int
9807smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9808 const struct arg *args, struct sample *smp, const char *kw)
9809{
9810 struct proxy *fe = l4->fe;
9811 struct http_txn *txn = l7;
9812 int idx;
9813
9814 if (!args || args->type != ARGT_UINT)
9815 return 0;
9816
9817 idx = args->data.uint;
9818
9819 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9820 return 0;
9821
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009822 smp->type = SMP_T_STR;
9823 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009824 smp->data.str.str = txn->rsp.cap[idx];
9825 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9826
9827 return 1;
9828}
9829
William Lallemand65ad6e12014-01-31 15:08:02 +01009830/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9831static int
9832smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9833 const struct arg *args, struct sample *smp, const char *kw)
9834{
9835 struct chunk *temp;
9836 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009837 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009838
9839 if (!txn->uri)
9840 return 0;
9841
William Lallemand96a77852014-02-05 00:30:02 +01009842 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009843
William Lallemand96a77852014-02-05 00:30:02 +01009844 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9845 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009846
William Lallemand96a77852014-02-05 00:30:02 +01009847 temp = get_trash_chunk();
9848 temp->str = txn->uri;
9849 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009850 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009851 smp->type = SMP_T_STR;
9852 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009853
9854 return 1;
9855
9856}
9857
9858/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9859static int
9860smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9861 const struct arg *args, struct sample *smp, const char *kw)
9862{
9863 struct chunk *temp;
9864 struct http_txn *txn = l7;
9865 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009866
9867 if (!txn->uri)
9868 return 0;
William Lallemand96a77852014-02-05 00:30:02 +01009869
William Lallemand65ad6e12014-01-31 15:08:02 +01009870 ptr = txn->uri;
9871
9872 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9873 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +01009874
William Lallemand65ad6e12014-01-31 15:08:02 +01009875 if (!*ptr)
9876 return 0;
9877
9878 ptr++; /* skip the space */
9879
9880 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +01009881 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +01009882 if (!ptr)
9883 return 0;
9884 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
9885 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009886
9887 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009888 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009889 smp->type = SMP_T_STR;
9890 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009891
9892 return 1;
9893}
9894
9895
Willy Tarreaue333ec92012-04-16 16:26:40 +02009896/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009897 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009898 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009899 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009900 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009901 * Accepts exactly 1 argument of type string. If the input options indicate
9902 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009903 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009904 */
9905static int
Willy Tarreau51539362012-05-08 12:46:28 +02009906smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009907 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009908{
9909 struct http_txn *txn = l7;
9910 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009911 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009912 const struct http_msg *msg;
9913 const char *hdr_name;
9914 int hdr_name_len;
9915 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009916 int occ = 0;
9917 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009918
Willy Tarreau24e32d82012-04-23 23:55:44 +02009919 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009920 return 0;
9921
Willy Tarreaua890d072013-04-02 12:01:06 +02009922 if (!ctx) {
9923 /* first call */
9924 ctx = &static_hdr_ctx;
9925 ctx->idx = 0;
9926 smp->ctx.a[2] = ctx;
9927 }
9928
Willy Tarreaue333ec92012-04-16 16:26:40 +02009929 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009930
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009931 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009932 msg = &txn->req;
9933 hdr_name = "Cookie";
9934 hdr_name_len = 6;
9935 } else {
9936 msg = &txn->rsp;
9937 hdr_name = "Set-Cookie";
9938 hdr_name_len = 10;
9939 }
9940
Willy Tarreau28376d62012-04-26 21:26:10 +02009941 if (!occ && !(opt & SMP_OPT_ITERATE))
9942 /* no explicit occurrence and single fetch => last cookie by default */
9943 occ = -1;
9944
9945 /* OK so basically here, either we want only one value and it's the
9946 * last one, or we want to iterate over all of them and we fetch the
9947 * next one.
9948 */
9949
Willy Tarreau9b28e032012-10-12 23:49:43 +02009950 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009951 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009952 /* search for the header from the beginning, we must first initialize
9953 * the search parameters.
9954 */
Willy Tarreau37406352012-04-23 16:16:37 +02009955 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009956 ctx->idx = 0;
9957 }
9958
Willy Tarreau28376d62012-04-26 21:26:10 +02009959 smp->flags |= SMP_F_VOL_HDR;
9960
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009961 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009962 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9963 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009964 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9965 goto out;
9966
Willy Tarreau24e32d82012-04-23 23:55:44 +02009967 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009968 continue;
9969
Willy Tarreau37406352012-04-23 16:16:37 +02009970 smp->ctx.a[0] = ctx->line + ctx->val;
9971 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009972 }
9973
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009974 smp->type = SMP_T_STR;
9975 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +02009976 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009977 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009978 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009979 &smp->data.str.str,
9980 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009981 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009982 found = 1;
9983 if (occ >= 0) {
9984 /* one value was returned into smp->data.str.{str,len} */
9985 smp->flags |= SMP_F_NOT_LAST;
9986 return 1;
9987 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009988 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009989 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009990 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009991 /* all cookie headers and values were scanned. If we're looking for the
9992 * last occurrence, we may return it now.
9993 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009994 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009995 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009996 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009997}
9998
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009999/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010000 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010001 * multiple cookies may be parsed on the same line. The returned sample is of
10002 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010003 */
10004static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010005smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010006 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010007{
10008 struct http_txn *txn = l7;
10009 struct hdr_idx *idx = &txn->hdr_idx;
10010 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010011 const struct http_msg *msg;
10012 const char *hdr_name;
10013 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010014 int cnt;
10015 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010016 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010017
Willy Tarreau24e32d82012-04-23 23:55:44 +020010018 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010019 return 0;
10020
Willy Tarreaue333ec92012-04-16 16:26:40 +020010021 CHECK_HTTP_MESSAGE_FIRST();
10022
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010023 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010024 msg = &txn->req;
10025 hdr_name = "Cookie";
10026 hdr_name_len = 6;
10027 } else {
10028 msg = &txn->rsp;
10029 hdr_name = "Set-Cookie";
10030 hdr_name_len = 10;
10031 }
10032
Willy Tarreau9b28e032012-10-12 23:49:43 +020010033 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010034 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010035 ctx.idx = 0;
10036 cnt = 0;
10037
10038 while (1) {
10039 /* Note: val_beg == NULL every time we need to fetch a new header */
10040 if (!val_beg) {
10041 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10042 break;
10043
Willy Tarreau24e32d82012-04-23 23:55:44 +020010044 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010045 continue;
10046
10047 val_beg = ctx.line + ctx.val;
10048 val_end = val_beg + ctx.vlen;
10049 }
10050
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010051 smp->type = SMP_T_STR;
10052 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010053 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010054 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010055 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010056 &smp->data.str.str,
10057 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010058 cnt++;
10059 }
10060 }
10061
Willy Tarreaub169eba2013-12-16 15:14:43 +010010062 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010063 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010064 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010065 return 1;
10066}
10067
Willy Tarreau51539362012-05-08 12:46:28 +020010068/* Fetch an cookie's integer value. The integer value is returned. It
10069 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10070 */
10071static int
10072smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010073 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010074{
Willy Tarreauef38c392013-07-22 16:29:32 +020010075 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010076
10077 if (ret > 0) {
10078 smp->type = SMP_T_UINT;
10079 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10080 }
10081
10082 return ret;
10083}
10084
Willy Tarreau8797c062007-05-07 00:55:35 +020010085/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010086/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010087/************************************************************************/
10088
David Cournapeau16023ee2010-12-23 20:55:41 +090010089/*
10090 * Given a path string and its length, find the position of beginning of the
10091 * query string. Returns NULL if no query string is found in the path.
10092 *
10093 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10094 *
10095 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10096 */
bedis4c75cca2012-10-05 08:38:24 +020010097static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010098{
10099 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010100
bedis4c75cca2012-10-05 08:38:24 +020010101 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010102 return p ? p + 1 : NULL;
10103}
10104
bedis4c75cca2012-10-05 08:38:24 +020010105static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010106{
bedis4c75cca2012-10-05 08:38:24 +020010107 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010108}
10109
10110/*
10111 * Given a url parameter, find the starting position of the first occurence,
10112 * or NULL if the parameter is not found.
10113 *
10114 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10115 * the function will return query_string+8.
10116 */
10117static char*
10118find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010119 char* url_param_name, size_t url_param_name_l,
10120 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010121{
10122 char *pos, *last;
10123
10124 pos = query_string;
10125 last = query_string + query_string_l - url_param_name_l - 1;
10126
10127 while (pos <= last) {
10128 if (pos[url_param_name_l] == '=') {
10129 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10130 return pos;
10131 pos += url_param_name_l + 1;
10132 }
bedis4c75cca2012-10-05 08:38:24 +020010133 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010134 pos++;
10135 pos++;
10136 }
10137 return NULL;
10138}
10139
10140/*
10141 * Given a url parameter name, returns its value and size into *value and
10142 * *value_l respectively, and returns non-zero. If the parameter is not found,
10143 * zero is returned and value/value_l are not touched.
10144 */
10145static int
10146find_url_param_value(char* path, size_t path_l,
10147 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010148 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010149{
10150 char *query_string, *qs_end;
10151 char *arg_start;
10152 char *value_start, *value_end;
10153
bedis4c75cca2012-10-05 08:38:24 +020010154 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010155 if (!query_string)
10156 return 0;
10157
10158 qs_end = path + path_l;
10159 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010160 url_param_name, url_param_name_l,
10161 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010162 if (!arg_start)
10163 return 0;
10164
10165 value_start = arg_start + url_param_name_l + 1;
10166 value_end = value_start;
10167
bedis4c75cca2012-10-05 08:38:24 +020010168 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010169 value_end++;
10170
10171 *value = value_start;
10172 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010173 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010174}
10175
10176static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010177smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010178 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010179{
bedis4c75cca2012-10-05 08:38:24 +020010180 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010181 struct http_txn *txn = l7;
10182 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010183
bedis4c75cca2012-10-05 08:38:24 +020010184 if (!args || args[0].type != ARGT_STR ||
10185 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010186 return 0;
10187
10188 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010189
bedis4c75cca2012-10-05 08:38:24 +020010190 if (args[1].type)
10191 delim = *args[1].data.str.str;
10192
Willy Tarreau9b28e032012-10-12 23:49:43 +020010193 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010194 args->data.str.str, args->data.str.len,
10195 &smp->data.str.str, &smp->data.str.len,
10196 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010197 return 0;
10198
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010199 smp->type = SMP_T_STR;
10200 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010201 return 1;
10202}
10203
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010204/* Return the signed integer value for the specified url parameter (see url_param
10205 * above).
10206 */
10207static int
10208smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010209 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010210{
Willy Tarreauef38c392013-07-22 16:29:32 +020010211 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010212
10213 if (ret > 0) {
10214 smp->type = SMP_T_UINT;
10215 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10216 }
10217
10218 return ret;
10219}
10220
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010221/* This produces a 32-bit hash of the concatenation of the first occurrence of
10222 * the Host header followed by the path component if it begins with a slash ('/').
10223 * This means that '*' will not be added, resulting in exactly the first Host
10224 * entry. If no Host header is found, then the path is used. The resulting value
10225 * is hashed using the url hash followed by a full avalanche hash and provides a
10226 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10227 * high-traffic sites without having to store whole paths.
10228 * this differs from the base32 functions in that it includes the url parameters
10229 * as well as the path
10230 */
10231static int
10232smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010233 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010234{
10235 struct http_txn *txn = l7;
10236 struct hdr_ctx ctx;
10237 unsigned int hash = 0;
10238 char *ptr, *beg, *end;
10239 int len;
10240
10241 CHECK_HTTP_MESSAGE_FIRST();
10242
10243 ctx.idx = 0;
10244 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10245 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10246 ptr = ctx.line + ctx.val;
10247 len = ctx.vlen;
10248 while (len--)
10249 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10250 }
10251
10252 /* now retrieve the path */
10253 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10254 beg = http_get_path(txn);
10255 if (!beg)
10256 beg = end;
10257
10258 for (ptr = beg; ptr < end ; ptr++);
10259
10260 if (beg < ptr && *beg == '/') {
10261 while (beg < ptr)
10262 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10263 }
10264 hash = full_hash(hash);
10265
10266 smp->type = SMP_T_UINT;
10267 smp->data.uint = hash;
10268 smp->flags = SMP_F_VOL_1ST;
10269 return 1;
10270}
10271
10272/* This concatenates the source address with the 32-bit hash of the Host and
10273 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10274 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10275 * on the source address length. The URL hash is stored before the address so
10276 * that in environments where IPv6 is insignificant, truncating the output to
10277 * 8 bytes would still work.
10278 */
10279static int
10280smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010281 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010282{
10283 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010284 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010285
Willy Tarreaue155ec22013-11-18 18:33:22 +010010286 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010287 return 0;
10288
10289 temp = get_trash_chunk();
10290 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10291 temp->len += sizeof(smp->data.uint);
10292
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010293 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010294 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010295 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010296 temp->len += 4;
10297 break;
10298 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010299 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010300 temp->len += 16;
10301 break;
10302 default:
10303 return 0;
10304 }
10305
10306 smp->data.str = *temp;
10307 smp->type = SMP_T_BIN;
10308 return 1;
10309}
10310
Willy Tarreau185b5c42012-04-26 15:11:51 +020010311/* This function is used to validate the arguments passed to any "hdr" fetch
10312 * keyword. These keywords support an optional positive or negative occurrence
10313 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10314 * is assumed that the types are already the correct ones. Returns 0 on error,
10315 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10316 * error message in case of error, that the caller is responsible for freeing.
10317 * The initial location must either be freeable or NULL.
10318 */
10319static int val_hdr(struct arg *arg, char **err_msg)
10320{
10321 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010322 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010323 return 0;
10324 }
10325 return 1;
10326}
10327
Willy Tarreau276fae92013-07-25 14:36:01 +020010328/* takes an UINT value on input supposed to represent the time since EPOCH,
10329 * adds an optional offset found in args[0] and emits a string representing
10330 * the date in RFC-1123/5322 format.
10331 */
10332static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10333{
10334 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10335 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10336 struct chunk *temp;
10337 struct tm *tm;
10338 time_t curr_date = smp->data.uint;
10339
10340 /* add offset */
10341 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10342 curr_date += args[0].data.sint;
10343
10344 tm = gmtime(&curr_date);
10345
10346 temp = get_trash_chunk();
10347 temp->len = snprintf(temp->str, temp->size - temp->len,
10348 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10349 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10350 tm->tm_hour, tm->tm_min, tm->tm_sec);
10351
10352 smp->data.str = *temp;
10353 smp->type = SMP_T_STR;
10354 return 1;
10355}
10356
Willy Tarreau4a568972010-05-12 08:08:50 +020010357/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010358/* All supported ACL keywords must be declared here. */
10359/************************************************************************/
10360
10361/* Note: must not be declared <const> as its list will be overwritten.
10362 * Please take care of keeping this list alphabetically sorted.
10363 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010364static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010365 { "base", "base", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10366 { "base_beg", "base", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10367 { "base_dir", "base", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10368 { "base_dom", "base", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10369 { "base_end", "base", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10370 { "base_len", "base", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10371 { "base_reg", "base", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10372 { "base_sub", "base", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010373
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010374 { "cook", "req.cook", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10375 { "cook_beg", "req.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10376 { "cook_dir", "req.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10377 { "cook_dom", "req.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10378 { "cook_end", "req.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10379 { "cook_len", "req.cook", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10380 { "cook_reg", "req.cook", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10381 { "cook_sub", "req.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010382
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010383 { "hdr", "req.hdr", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10384 { "hdr_beg", "req.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10385 { "hdr_dir", "req.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10386 { "hdr_dom", "req.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10387 { "hdr_end", "req.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10388 { "hdr_len", "req.hdr", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10389 { "hdr_reg", "req.hdr", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10390 { "hdr_sub", "req.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010391
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010392 { "http_auth_group", NULL, pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010393
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010394 { "method", NULL, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010395
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010396 { "path", "path", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10397 { "path_beg", "path", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10398 { "path_dir", "path", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10399 { "path_dom", "path", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10400 { "path_end", "path", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10401 { "path_len", "path", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10402 { "path_reg", "path", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10403 { "path_sub", "path", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010404
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010405 { "req_ver", "req.ver", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10406 { "resp_ver", "res.ver", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010407
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010408 { "scook", "res.cook", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10409 { "scook_beg", "res.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10410 { "scook_dir", "res.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10411 { "scook_dom", "res.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10412 { "scook_end", "res.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10413 { "scook_len", "res.cook", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10414 { "scook_reg", "res.cook", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10415 { "scook_sub", "res.cook", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010416
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010417 { "shdr", "res.hdr", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10418 { "shdr_beg", "res.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10419 { "shdr_dir", "res.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10420 { "shdr_dom", "res.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10421 { "shdr_end", "res.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10422 { "shdr_len", "res.hdr", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10423 { "shdr_reg", "res.hdr", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10424 { "shdr_sub", "res.hdr", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010425
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010426 { "url", "url", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10427 { "url_beg", "url", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10428 { "url_dir", "url", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10429 { "url_dom", "url", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10430 { "url_end", "url", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10431 { "url_len", "url", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10432 { "url_reg", "url", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10433 { "url_sub", "url", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010434
Thierry FOURNIERe369ca22014-01-29 16:24:55 +010010435 { "urlp", "urlp", pat_parse_str, pat_idx_tree_str, pat_del_tree_str, pat_prune_ptr, pat_match_str },
10436 { "urlp_beg", "urlp", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_beg },
10437 { "urlp_dir", "urlp", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dir },
10438 { "urlp_dom", "urlp", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_dom },
10439 { "urlp_end", "urlp", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_end },
10440 { "urlp_len", "urlp", pat_parse_int, pat_idx_list_val, pat_del_list_val, pat_prune_val, pat_match_len },
10441 { "urlp_reg", "urlp", pat_parse_reg, pat_idx_list_reg, pat_del_list_reg, pat_prune_reg, pat_match_reg },
10442 { "urlp_sub", "urlp", pat_parse_str, pat_idx_list_str, pat_del_list_ptr, pat_prune_ptr, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010443
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010444 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010445}};
10446
10447/************************************************************************/
10448/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010449/************************************************************************/
10450/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010451static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010452 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010453 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10454 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10455
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010456 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10457 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemand65ad6e12014-01-31 15:08:02 +010010458
William Lallemanda43ba4e2014-01-28 18:14:25 +010010459 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010460 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
10461 { "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 +010010462
Willy Tarreau409bcde2013-01-08 00:31:00 +010010463 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10464 * are only here to match the ACL's name, are request-only and are used
10465 * for ACL compatibility only.
10466 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010467 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10468 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010469 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10470 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10471
10472 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10473 * only here to match the ACL's name, are request-only and are used for
10474 * ACL compatibility only.
10475 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010476 { "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 +010010477 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10478 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10479 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10480
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010481 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010482 { "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 +010010483 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010484 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010485 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010486
10487 /* HTTP protocol on the request path */
10488 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010489 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010490
10491 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010492 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10493 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010494
10495 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010496 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10497 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010498
Willy Tarreau18ed2562013-01-14 15:56:36 +010010499 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010500 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010501 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10502 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10503
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010504 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010505 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010506 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010507 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10508 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10509 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10510
10511 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010512 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010513 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10514 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10515
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010516 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010517 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010518 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010519 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10520 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10521 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10522
Willy Tarreau409bcde2013-01-08 00:31:00 +010010523 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010524 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010525 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10526 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010527 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010528
10529 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010530 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010531 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10532 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10533 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10534
10535 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010536 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010537 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10538 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010539 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10540 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010541 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10542 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010543 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10544 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010545}};
10546
Willy Tarreau8797c062007-05-07 00:55:35 +020010547
Willy Tarreau276fae92013-07-25 14:36:01 +020010548/* Note: must not be declared <const> as its list will be overwritten */
10549static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10550 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10551 { NULL, NULL, 0, 0, 0 },
10552}};
10553
Willy Tarreau8797c062007-05-07 00:55:35 +020010554__attribute__((constructor))
10555static void __http_protocol_init(void)
10556{
10557 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010558 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010559 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010560}
10561
10562
Willy Tarreau58f10d72006-12-04 02:26:12 +010010563/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010564 * Local variables:
10565 * c-indent-level: 8
10566 * c-basic-offset: 8
10567 * End:
10568 */