blob: 4c78af814134b46f795c59e6eeb0a7ac9489b972 [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 Tarreau0558a022014-03-13 15:48:45 +01001734 /* Assumes msg->sol points to the first of either CR or LF.
1735 * Sets ->sov and ->next to the total header length, ->eoh to
1736 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1737 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 EXPECT_LF_HERE(ptr, http_msg_invalid);
1739 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001740 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001741 msg->eoh = msg->sol;
1742 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001743 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001744 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001746
1747 case HTTP_MSG_ERROR:
1748 /* this may only happen if we call http_msg_analyser() twice with an error */
1749 break;
1750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001752#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1754 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001755#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001756 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 }
1758 http_msg_ood:
1759 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001760 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001761 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 http_msg_invalid:
1765 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001766 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 return;
1769}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001770
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001771/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1772 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1773 * nothing is done and 1 is returned.
1774 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001775static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776{
1777 int delta;
1778 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001779 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001780
1781 if (msg->sl.rq.v_l != 0)
1782 return 1;
1783
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001784 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1785 if (txn->meth != HTTP_METH_GET)
1786 return 0;
1787
Willy Tarreau9b28e032012-10-12 23:49:43 +02001788 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 delta = 0;
1790
1791 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001792 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1793 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001794 }
1795 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001796 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001797 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001798 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001799 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001800 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001801 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001802 NULL, NULL);
1803 if (unlikely(!cur_end))
1804 return 0;
1805
1806 /* we have a full HTTP/1.0 request now and we know that
1807 * we have either a CR or an LF at <ptr>.
1808 */
1809 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1810 return 1;
1811}
1812
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001813/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001814 * and "keep-alive" values. If we already know that some headers may safely
1815 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001816 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1817 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001818 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1820 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1821 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001822 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001823 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001824void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001825{
Willy Tarreau5b154472009-12-21 20:11:07 +01001826 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001827 const char *hdr_val = "Connection";
1828 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001829
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001831 return;
1832
Willy Tarreau88d349d2010-01-25 12:15:43 +01001833 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1834 hdr_val = "Proxy-Connection";
1835 hdr_len = 16;
1836 }
1837
Willy Tarreau5b154472009-12-21 20:11:07 +01001838 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001839 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001840 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1842 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001843 if (to_del & 2)
1844 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001845 else
1846 txn->flags |= TX_CON_KAL_SET;
1847 }
1848 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1849 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001850 if (to_del & 1)
1851 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001852 else
1853 txn->flags |= TX_CON_CLO_SET;
1854 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001855 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1856 txn->flags |= TX_HDR_CONN_UPG;
1857 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001858 }
1859
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001860 txn->flags |= TX_HDR_CONN_PRS;
1861 return;
1862}
Willy Tarreau5b154472009-12-21 20:11:07 +01001863
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001864/* Apply desired changes on the Connection: header. Values may be removed and/or
1865 * added depending on the <wanted> flags, which are exclusively composed of
1866 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1867 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1868 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001869void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870{
1871 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001872 const char *hdr_val = "Connection";
1873 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874
1875 ctx.idx = 0;
1876
Willy Tarreau88d349d2010-01-25 12:15:43 +01001877
1878 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1879 hdr_val = "Proxy-Connection";
1880 hdr_len = 16;
1881 }
1882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001884 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001885 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1886 if (wanted & TX_CON_KAL_SET)
1887 txn->flags |= TX_CON_KAL_SET;
1888 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001889 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001890 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001891 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1892 if (wanted & TX_CON_CLO_SET)
1893 txn->flags |= TX_CON_CLO_SET;
1894 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001895 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001896 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001897 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898
1899 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1900 return;
1901
1902 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1903 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: close";
1905 hdr_len = 17;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: close";
1908 hdr_len = 23;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912
1913 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1914 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001915 hdr_val = "Connection: keep-alive";
1916 hdr_len = 22;
1917 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1918 hdr_val = "Proxy-Connection: keep-alive";
1919 hdr_len = 28;
1920 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001921 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001922 }
1923 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001924}
1925
Willy Tarreaua458b672012-03-05 11:17:50 +01001926/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreau877e78d2013-04-07 18:48:08 +02001927 * first byte of body, and increments msg->sov by the number of bytes parsed.
1928 * so that we know we can forward ->sov bytes.
Willy Tarreau115acb92009-12-26 13:56:06 +01001929 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001931 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001932static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001933{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001934 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001935 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001936 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001937 const char *end = buf->data + buf->size;
1938 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 unsigned int chunk = 0;
1940
1941 /* The chunk size is in the following form, though we are only
1942 * interested in the size and CRLF :
1943 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1944 */
1945 while (1) {
1946 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001947 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001948 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001950 if (c < 0) /* not a hex digit anymore */
1951 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001952 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001953 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001954 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001955 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001956 chunk = (chunk << 4) + c;
1957 }
1958
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001960 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001961 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962
1963 while (http_is_spht[(unsigned char)*ptr]) {
1964 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001965 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001966 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001967 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001968 }
1969
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 /* Up to there, we know that at least one byte is present at *ptr. Check
1971 * for the end of chunk size.
1972 */
1973 while (1) {
1974 if (likely(HTTP_IS_CRLF(*ptr))) {
1975 /* we now have a CR or an LF at ptr */
1976 if (likely(*ptr == '\r')) {
1977 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001978 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001979 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001980 return 0;
1981 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001982
Willy Tarreaud98cf932009-12-27 22:54:55 +01001983 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001984 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001986 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987 /* done */
1988 break;
1989 }
1990 else if (*ptr == ';') {
1991 /* chunk extension, ends at next CRLF */
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 Tarreau115acb92009-12-26 13:56:06 +01001995 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001996
1997 while (!HTTP_IS_CRLF(*ptr)) {
1998 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001999 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002000 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 return 0;
2002 }
2003 /* we have a CRLF now, loop above */
2004 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002007 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002008 }
2009
Willy Tarreaud98cf932009-12-27 22:54:55 +01002010 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01002011 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002013 */
Willy Tarreau0161d622013-04-02 01:26:55 +02002014 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002015 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01002016 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002017 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002018 msg->chunk_len = chunk;
2019 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002021 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002022 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002023 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002024 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002025}
2026
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002027/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002028 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029 * the trailers is found, it is automatically scheduled to be forwarded,
2030 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2031 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01002032 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01002033 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01002034 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01002035 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2036 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau877e78d2013-04-07 18:48:08 +02002037 * forwarding, and that msg->sov exactly matches the length of trailers already
2038 * parsed and not forwarded. It is also important to note that this function is
2039 * designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002041static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002043 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002044
Willy Tarreaua458b672012-03-05 11:17:50 +01002045 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002047 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002048 const char *ptr = b_ptr(buf, msg->next);
2049 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002050 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051
2052 /* scan current line and stop at LF or CRLF */
2053 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002054 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002055 return 0;
2056
2057 if (*ptr == '\n') {
2058 if (!p1)
2059 p1 = ptr;
2060 p2 = ptr;
2061 break;
2062 }
2063
2064 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002065 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002066 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002067 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002068 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002069 p1 = ptr;
2070 }
2071
2072 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 if (ptr >= buf->data + buf->size)
2074 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 }
2076
2077 /* after LF; point to beginning of next line */
2078 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002079 if (p2 >= buf->data + buf->size)
2080 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002083 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002084 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002085
2086 /* schedule this line for forwarding */
2087 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002088 if (msg->sov >= buf->size)
2089 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002090
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002091 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002092 /* LF/CRLF at beginning of line => end of trailers at p2.
2093 * Everything was scheduled for forwarding, there's nothing
2094 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002095 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002096 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097 msg->msg_state = HTTP_MSG_DONE;
2098 return 1;
2099 }
2100 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002101 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102 }
2103}
2104
Willy Tarreau54d23df2012-10-25 19:04:45 +02002105/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau877e78d2013-04-07 18:48:08 +02002107 * and ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002108 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002109 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2110 * not enough data are available, the function does not change anything and
2111 * returns zero. If a parse error is encountered, the function returns < 0 and
2112 * does not change anything. Note: this function is designed to parse wrapped
2113 * CRLF at the end of the buffer.
2114 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002115static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002117 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002118 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 int bytes;
2120
2121 /* NB: we'll check data availabilty at the end. It's not a
2122 * problem because whatever we match first will be checked
2123 * against the correct length.
2124 */
2125 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002126 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127 if (*ptr == '\r') {
2128 bytes++;
2129 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 if (ptr >= buf->data + buf->size)
2131 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002132 }
2133
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002134 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002135 return 0;
2136
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002137 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002138 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002139 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002140 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141
2142 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002143 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002144 ptr = buf->data;
Willy Tarreau877e78d2013-04-07 18:48:08 +02002145 /* prepare the CRLF to be forwarded (->sov) */
Willy Tarreauea1175a2012-03-05 15:52:30 +01002146 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2148 return 1;
2149}
Willy Tarreau5b154472009-12-21 20:11:07 +01002150
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002151/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2152 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2153 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2154 * Unparsable qvalues return 1000 as "q=1.000".
2155 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002156int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002157{
2158 int q = 1000;
2159
2160 if (!isdigit(*qvalue))
2161 goto out;
2162 q = (*qvalue++ - '0') * 1000;
2163
2164 if (*qvalue++ != '.')
2165 goto out;
2166
2167 if (!isdigit(*qvalue))
2168 goto out;
2169 q += (*qvalue++ - '0') * 100;
2170
2171 if (!isdigit(*qvalue))
2172 goto out;
2173 q += (*qvalue++ - '0') * 10;
2174
2175 if (!isdigit(*qvalue))
2176 goto out;
2177 q += (*qvalue++ - '0') * 1;
2178 out:
2179 if (q > 1000)
2180 q = 1000;
Thierry FOURNIERad903512014-04-11 17:51:01 +02002181 if (*end)
2182 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002183 return q;
2184}
William Lallemand82fe75c2012-10-23 10:25:10 +02002185
2186/*
2187 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002188 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002189int select_compression_request_header(struct session *s, struct buffer *req)
2190{
2191 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002192 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002193 struct hdr_ctx ctx;
2194 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002195 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002196
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002197 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2198 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002199 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2200 */
2201 ctx.idx = 0;
2202 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2203 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002204 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2205 (ctx.vlen < 31 ||
2206 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2207 ctx.line[ctx.val + 30] < '6' ||
2208 (ctx.line[ctx.val + 30] == '6' &&
2209 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2210 s->comp_algo = NULL;
2211 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002212 }
2213
William Lallemand82fe75c2012-10-23 10:25:10 +02002214 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002215 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002216 int best_q = 0;
2217
William Lallemand82fe75c2012-10-23 10:25:10 +02002218 ctx.idx = 0;
2219 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002220 const char *qval;
2221 int q;
2222 int toklen;
2223
2224 /* try to isolate the token from the optional q-value */
2225 toklen = 0;
2226 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2227 toklen++;
2228
2229 qval = ctx.line + ctx.val + toklen;
2230 while (1) {
2231 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2232 qval++;
2233
2234 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2235 qval = NULL;
2236 break;
2237 }
2238 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002239
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002240 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2241 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002242
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002243 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2244 qval = NULL;
2245 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002246 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002247 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2248 break;
2249
2250 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2251 qval++;
2252 }
2253
2254 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002255 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002256
2257 if (q <= best_q)
2258 continue;
2259
2260 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2261 if (*(ctx.line + ctx.val) == '*' ||
2262 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2263 s->comp_algo = comp_algo;
2264 best_q = q;
2265 break;
2266 }
2267 }
2268 }
2269 }
2270
2271 /* remove all occurrences of the header when "compression offload" is set */
2272 if (s->comp_algo) {
2273 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2274 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2275 ctx.idx = 0;
2276 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2277 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002278 }
2279 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002280 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002281 }
2282
2283 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002284 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2285 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002286 if (comp_algo->add_data == identity_add_data) {
2287 s->comp_algo = comp_algo;
2288 return 1;
2289 }
2290 }
2291 }
2292
2293 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002294 return 0;
2295}
2296
2297/*
2298 * Selects a comression algorithm depending of the server response.
2299 */
2300int select_compression_response_header(struct session *s, struct buffer *res)
2301{
2302 struct http_txn *txn = &s->txn;
2303 struct http_msg *msg = &txn->rsp;
2304 struct hdr_ctx ctx;
2305 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002306
2307 /* no common compression algorithm was found in request header */
2308 if (s->comp_algo == NULL)
2309 goto fail;
2310
2311 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002312 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002313 goto fail;
2314
William Lallemandd3002612012-11-26 14:34:47 +01002315 /* 200 only */
2316 if (txn->status != 200)
2317 goto fail;
2318
William Lallemand82fe75c2012-10-23 10:25:10 +02002319 /* Content-Length is null */
2320 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2321 goto fail;
2322
Willy Tarreau667c2a32013-04-09 08:13:58 +02002323 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2324 if (msg->flags & HTTP_MSGF_TE_CHNK)
2325 goto fail;
2326
William Lallemand82fe75c2012-10-23 10:25:10 +02002327 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002329 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2330 goto fail;
2331
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002332 /* no compression when Cache-Control: no-transform is present in the message */
2333 ctx.idx = 0;
2334 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2335 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2336 goto fail;
2337 }
2338
William Lallemand82fe75c2012-10-23 10:25:10 +02002339 comp_type = NULL;
2340
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002341 /* we don't want to compress multipart content-types, nor content-types that are
2342 * not listed in the "compression type" directive if any. If no content-type was
2343 * found but configuration requires one, we don't compress either. Backend has
2344 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002346 ctx.idx = 0;
2347 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2348 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2349 goto fail;
2350
2351 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2352 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002353 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002354 if (ctx.vlen >= comp_type->name_len &&
2355 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002356 /* this Content-Type should be compressed */
2357 break;
2358 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002359 /* this Content-Type should not be compressed */
2360 if (comp_type == NULL)
2361 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002362 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002363 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002364 else { /* no content-type header */
2365 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2366 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002367 }
2368
William Lallemandd85f9172012-11-09 17:05:39 +01002369 /* limit compression rate */
2370 if (global.comp_rate_lim > 0)
2371 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2372 goto fail;
2373
William Lallemand072a2bf2012-11-20 17:01:01 +01002374 /* limit cpu usage */
2375 if (idle_pct < compress_min_idle)
2376 goto fail;
2377
William Lallemand4c49fae2012-11-07 15:00:23 +01002378 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002379 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002380 goto fail;
2381
William Lallemandec3e3892012-11-12 17:02:18 +01002382 s->flags |= SN_COMP_READY;
2383
William Lallemand82fe75c2012-10-23 10:25:10 +02002384 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002385 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002386 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2387 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2388
2389 /* add Transfer-Encoding header */
2390 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2391 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2392
2393 /*
2394 * Add Content-Encoding header when it's not identity encoding.
2395 * RFC 2616 : Identity encoding: This content-coding is used only in the
2396 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2397 * header.
2398 */
2399 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002400 trash.len = 18;
2401 memcpy(trash.str, "Content-Encoding: ", trash.len);
2402 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2403 trash.len += s->comp_algo->name_len;
2404 trash.str[trash.len] = '\0';
2405 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002406 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002407 return 1;
2408
2409fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002410 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002411 return 0;
2412}
2413
2414
Willy Tarreaud787e662009-07-07 10:14:51 +02002415/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2416 * processing can continue on next analysers, or zero if it either needs more
2417 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2418 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2419 * when it has nothing left to do, and may remove any analyser when it wants to
2420 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002421 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002422int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002423{
Willy Tarreau59234e92008-11-30 23:51:27 +01002424 /*
2425 * We will parse the partial (or complete) lines.
2426 * We will check the request syntax, and also join multi-line
2427 * headers. An index of all the lines will be elaborated while
2428 * parsing.
2429 *
2430 * For the parsing, we use a 28 states FSM.
2431 *
2432 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002433 * req->buf->p = beginning of request
2434 * req->buf->p + msg->eoh = end of processed headers / start of current one
2435 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002436 * msg->eol = end of current header or line (LF or CRLF)
2437 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002438 *
2439 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002440 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002441 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2442 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002443 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002446 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002447 struct http_txn *txn = &s->txn;
2448 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002449 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002450
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002451 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 +01002452 now_ms, __FUNCTION__,
2453 s,
2454 req,
2455 req->rex, req->wex,
2456 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002457 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002458 req->analysers);
2459
Willy Tarreau52a0c602009-08-16 22:45:38 +02002460 /* we're speaking HTTP here, so let's speak HTTP to the client */
2461 s->srv_error = http_return_srv_error;
2462
Willy Tarreau83e3af02009-12-28 17:39:57 +01002463 /* There's a protected area at the end of the buffer for rewriting
2464 * purposes. We don't want to start to parse the request if the
2465 * protected area is affected, because we may have to move processed
2466 * data later, which is much more complicated.
2467 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002468 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002469 if (txn->flags & TX_NOT_FIRST) {
2470 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002471 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002472 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002473 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002474 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002475 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002476 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002477 return 0;
2478 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002479 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2480 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2481 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002482 }
2483
Willy Tarreau065e8332010-01-08 00:30:20 +01002484 /* Note that we have the same problem with the response ; we
2485 * may want to send a redirect, error or anything which requires
2486 * some spare space. So we'll ensure that we have at least
2487 * maxrewrite bytes available in the response buffer before
2488 * processing that one. This will only affect pipelined
2489 * keep-alive requests.
2490 */
2491 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002492 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002493 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2494 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2495 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002496 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002497 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002498 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002499 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002500 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002501 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002502 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002503 return 0;
2504 }
2505 }
2506
Willy Tarreau9b28e032012-10-12 23:49:43 +02002507 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002508 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002509 }
2510
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 /* 1: we might have to print this header in debug mode */
2512 if (unlikely((global.mode & MODE_DEBUG) &&
2513 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002514 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002516
Willy Tarreau9b28e032012-10-12 23:49:43 +02002517 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002518 /* this is a bit complex : in case of error on the request line,
2519 * we know that rq.l is still zero, so we display only the part
2520 * up to the end of the line (truncated by debug_hdr).
2521 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002522 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002524
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 sol += hdr_idx_first_pos(&txn->hdr_idx);
2526 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 while (cur_idx) {
2529 eol = sol + txn->hdr_idx.v[cur_idx].len;
2530 debug_hdr("clihdr", s, sol, eol);
2531 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2532 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002533 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002534 }
2535
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 /*
2538 * Now we quickly check if we have found a full valid request.
2539 * If not so, we check the FD and buffer states before leaving.
2540 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002541 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002542 * requests are checked first. When waiting for a second request
2543 * on a keep-alive session, if we encounter and error, close, t/o,
2544 * we note the error in the session flags but don't set any state.
2545 * Since the error will be noted there, it will not be counted by
2546 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002547 * Last, we may increase some tracked counters' http request errors on
2548 * the cases that are deliberately the client's fault. For instance,
2549 * a timeout or connection reset is not counted as an error. However
2550 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002552
Willy Tarreau655dce92009-11-08 13:10:58 +01002553 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002554 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002556 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002557 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002558 session_inc_http_req_ctr(s);
2559 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002560 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002562 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002563
Willy Tarreau59234e92008-11-30 23:51:27 +01002564 /* 1: Since we are in header mode, if there's no space
2565 * left for headers, we won't be able to free more
2566 * later, so the session will never terminate. We
2567 * must terminate it now.
2568 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002569 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002570 /* FIXME: check if URI is set and return Status
2571 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002572 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002573 session_inc_http_req_ctr(s);
2574 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002575 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002576 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002577 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002578 goto return_bad_req;
2579 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002580
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002582 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002583 if (!(s->flags & SN_ERR_MASK))
2584 s->flags |= SN_ERR_CLICL;
2585
Willy Tarreaufcffa692010-01-10 14:21:19 +01002586 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002587 goto failed_keep_alive;
2588
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002590 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002591 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002592 session_inc_http_err_ctr(s);
2593 }
2594
Willy Tarreaudc979f22012-12-04 10:39:01 +01002595 txn->status = 400;
2596 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 msg->msg_state = HTTP_MSG_ERROR;
2598 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002599
Willy Tarreauda7ff642010-06-23 11:44:09 +02002600 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002601 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002602 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002603 if (s->listener->counters)
2604 s->listener->counters->failed_req++;
2605
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 if (!(s->flags & SN_FINST_MASK))
2607 s->flags |= SN_FINST_R;
2608 return 0;
2609 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002610
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002612 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002613 if (!(s->flags & SN_ERR_MASK))
2614 s->flags |= SN_ERR_CLITO;
2615
Willy Tarreaufcffa692010-01-10 14:21:19 +01002616 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002617 goto failed_keep_alive;
2618
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002620 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002621 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002622 session_inc_http_err_ctr(s);
2623 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002625 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 msg->msg_state = HTTP_MSG_ERROR;
2627 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002628
Willy Tarreauda7ff642010-06-23 11:44:09 +02002629 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002630 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002631 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002632 if (s->listener->counters)
2633 s->listener->counters->failed_req++;
2634
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 if (!(s->flags & SN_FINST_MASK))
2636 s->flags |= SN_FINST_R;
2637 return 0;
2638 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002639
Willy Tarreau59234e92008-11-30 23:51:27 +01002640 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002641 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002642 if (!(s->flags & SN_ERR_MASK))
2643 s->flags |= SN_ERR_CLICL;
2644
Willy Tarreaufcffa692010-01-10 14:21:19 +01002645 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002646 goto failed_keep_alive;
2647
Willy Tarreau4076a152009-04-02 15:18:36 +02002648 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002649 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002651 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 msg->msg_state = HTTP_MSG_ERROR;
2653 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002654
Willy Tarreauda7ff642010-06-23 11:44:09 +02002655 session_inc_http_err_ctr(s);
2656 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002657 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002658 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002659 if (s->listener->counters)
2660 s->listener->counters->failed_req++;
2661
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 if (!(s->flags & SN_FINST_MASK))
2663 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002664 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002665 }
2666
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002667 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002668 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2669 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002670#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002671 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 +01002672 /* We need more data, we have to re-enable quick-ack in case we
2673 * previously disabled it, otherwise we might cause the client
2674 * to delay next data.
2675 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002676 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002677 }
2678#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002679
Willy Tarreaufcffa692010-01-10 14:21:19 +01002680 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2681 /* If the client starts to talk, let's fall back to
2682 * request timeout processing.
2683 */
2684 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002685 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002686 }
2687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002689 if (!tick_isset(req->analyse_exp)) {
2690 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2691 (txn->flags & TX_WAIT_NEXT_RQ) &&
2692 tick_isset(s->be->timeout.httpka))
2693 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2694 else
2695 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2696 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002697
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 /* we're not ready yet */
2699 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002700
2701 failed_keep_alive:
2702 /* Here we process low-level errors for keep-alive requests. In
2703 * short, if the request is not the first one and it experiences
2704 * a timeout, read error or shutdown, we just silently close so
2705 * that the client can try again.
2706 */
2707 txn->status = 0;
2708 msg->msg_state = HTTP_MSG_RQBEFORE;
2709 req->analysers = 0;
2710 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002711 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002712 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002713 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002714 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002715 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002716
Willy Tarreaud787e662009-07-07 10:14:51 +02002717 /* OK now we have a complete HTTP request with indexed headers. Let's
2718 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002719 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002720 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002721 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002722 * byte after the last LF. msg->sov points to the first byte of data.
2723 * msg->eol cannot be trusted because it may have been left uninitialized
2724 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002725 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002726
Willy Tarreauda7ff642010-06-23 11:44:09 +02002727 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002728 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2729
Willy Tarreaub16a5742010-01-10 14:46:16 +01002730 if (txn->flags & TX_WAIT_NEXT_RQ) {
2731 /* kill the pending keep-alive timeout */
2732 txn->flags &= ~TX_WAIT_NEXT_RQ;
2733 req->analyse_exp = TICK_ETERNITY;
2734 }
2735
2736
Willy Tarreaud787e662009-07-07 10:14:51 +02002737 /* Maybe we found in invalid header name while we were configured not
2738 * to block on that, so we have to capture it now.
2739 */
2740 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002741 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002742
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 /*
2744 * 1: identify the method
2745 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002746 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002747
2748 /* we can make use of server redirect on GET and HEAD */
2749 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2750 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002751
Willy Tarreau59234e92008-11-30 23:51:27 +01002752 /*
2753 * 2: check if the URI matches the monitor_uri.
2754 * We have to do this for every request which gets in, because
2755 * the monitor-uri is defined by the frontend.
2756 */
2757 if (unlikely((s->fe->monitor_uri_len != 0) &&
2758 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002759 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 s->fe->monitor_uri,
2761 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002762 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002763 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002764 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002766
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002768 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002772 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002773
Willy Tarreau59234e92008-11-30 23:51:27 +01002774 ret = acl_pass(ret);
2775 if (cond->pol == ACL_COND_UNLESS)
2776 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002777
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 if (ret) {
2779 /* we fail this request, let's return 503 service unavail */
2780 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002781 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002782 if (!(s->flags & SN_ERR_MASK))
2783 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002784 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002785 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002786 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002787
Willy Tarreau59234e92008-11-30 23:51:27 +01002788 /* nothing to fail, let's reply normaly */
2789 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002790 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002791 if (!(s->flags & SN_ERR_MASK))
2792 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002793 goto return_prx_cond;
2794 }
2795
2796 /*
2797 * 3: Maybe we have to copy the original REQURI for the logs ?
2798 * Note: we cannot log anymore if the request has been
2799 * classified as invalid.
2800 */
2801 if (unlikely(s->logs.logwait & LW_REQ)) {
2802 /* we have a complete HTTP request that we must log */
2803 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2804 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002805
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 if (urilen >= REQURI_LEN)
2807 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002808 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002810
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002811 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 s->do_log(s);
2813 } else {
2814 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002815 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002816 }
Willy Tarreau06619262006-12-17 08:37:22 +01002817
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002819 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002820 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002821
Willy Tarreau5b154472009-12-21 20:11:07 +01002822 /* ... and check if the request is HTTP/1.1 or above */
2823 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002824 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2825 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2826 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002827 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002828
2829 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002830 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 +01002831
Willy Tarreau88d349d2010-01-25 12:15:43 +01002832 /* if the frontend has "option http-use-proxy-header", we'll check if
2833 * we have what looks like a proxied connection instead of a connection,
2834 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2835 * Note that this is *not* RFC-compliant, however browsers and proxies
2836 * happen to do that despite being non-standard :-(
2837 * We consider that a request not beginning with either '/' or '*' is
2838 * a proxied connection, which covers both "scheme://location" and
2839 * CONNECT ip:port.
2840 */
2841 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002842 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002843 txn->flags |= TX_USE_PX_CONN;
2844
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002845 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002846 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002847
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002849 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002850 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002852
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002853 /* 6: determine the transfer-length.
2854 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2855 * the presence of a message-body in a REQUEST and its transfer length
2856 * must be determined that way (in order of precedence) :
2857 * 1. The presence of a message-body in a request is signaled by the
2858 * inclusion of a Content-Length or Transfer-Encoding header field
2859 * in the request's header fields. When a request message contains
2860 * both a message-body of non-zero length and a method that does
2861 * not define any semantics for that request message-body, then an
2862 * origin server SHOULD either ignore the message-body or respond
2863 * with an appropriate error message (e.g., 413). A proxy or
2864 * gateway, when presented the same request, SHOULD either forward
2865 * the request inbound with the message- body or ignore the
2866 * message-body when determining a response.
2867 *
2868 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2869 * and the "chunked" transfer-coding (Section 6.2) is used, the
2870 * transfer-length is defined by the use of this transfer-coding.
2871 * If a Transfer-Encoding header field is present and the "chunked"
2872 * transfer-coding is not present, the transfer-length is defined
2873 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002874 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002875 * 3. If a Content-Length header field is present, its decimal value in
2876 * OCTETs represents both the entity-length and the transfer-length.
2877 * If a message is received with both a Transfer-Encoding header
2878 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002879 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002880 * 4. By the server closing the connection. (Closing the connection
2881 * cannot be used to indicate the end of a request body, since that
2882 * would leave no possibility for the server to send back a response.)
2883 *
2884 * Whenever a transfer-coding is applied to a message-body, the set of
2885 * transfer-codings MUST include "chunked", unless the message indicates
2886 * it is terminated by closing the connection. When the "chunked"
2887 * transfer-coding is used, it MUST be the last transfer-coding applied
2888 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002889 */
2890
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002891 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002892 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002893 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002894 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002895 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002896 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002897 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2898 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002899 /* bad transfer-encoding (chunked followed by something else) */
2900 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002901 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002902 break;
2903 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002904 }
2905
Willy Tarreau32b47f42009-10-18 20:55:02 +02002906 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002907 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002908 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002909 signed long long cl;
2910
Willy Tarreauad14f752011-09-02 20:33:27 +02002911 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002912 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002913 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002914 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002915
Willy Tarreauad14f752011-09-02 20:33:27 +02002916 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002917 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002918 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002919 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002920
Willy Tarreauad14f752011-09-02 20:33:27 +02002921 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002922 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002923 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002924 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002925
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002926 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002927 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002928 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002929 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002930
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002931 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002932 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002933 }
2934
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002935 /* bodyless requests have a known length */
2936 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002937 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002938
Willy Tarreaud787e662009-07-07 10:14:51 +02002939 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002940 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002941 req->analyse_exp = TICK_ETERNITY;
2942 return 1;
2943
2944 return_bad_req:
2945 /* We centralize bad requests processing here */
2946 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2947 /* we detected a parsing error. We want to archive this request
2948 * in the dedicated proxy area for later troubleshooting.
2949 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002950 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002951 }
2952
2953 txn->req.msg_state = HTTP_MSG_ERROR;
2954 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002955 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002956
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002957 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002958 if (s->listener->counters)
2959 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002960
2961 return_prx_cond:
2962 if (!(s->flags & SN_ERR_MASK))
2963 s->flags |= SN_ERR_PRXCOND;
2964 if (!(s->flags & SN_FINST_MASK))
2965 s->flags |= SN_FINST_R;
2966
2967 req->analysers = 0;
2968 req->analyse_exp = TICK_ETERNITY;
2969 return 0;
2970}
2971
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002972
Willy Tarreau347a35d2013-11-22 17:51:09 +01002973/* This function prepares an applet to handle the stats. It can deal with the
2974 * "100-continue" expectation, check that admin rules are met for POST requests,
2975 * and program a response message if something was unexpected. It cannot fail
2976 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002977 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002978 * s->target which is supposed to already point to the stats applet. The caller
2979 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002980 */
2981int http_handle_stats(struct session *s, struct channel *req)
2982{
2983 struct stats_admin_rule *stats_admin_rule;
2984 struct stream_interface *si = s->rep->prod;
2985 struct http_txn *txn = &s->txn;
2986 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002987 struct uri_auth *uri_auth = s->be->uri_auth;
2988 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002989 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002990
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002991 appctx = si_appctx(si);
2992 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2993 appctx->st1 = appctx->st2 = 0;
2994 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2995 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002996
2997 uri = msg->chn->buf->p + msg->sl.rq.u;
2998 lookup = uri + uri_auth->uri_len;
2999
3000 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3001 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003002 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003003 break;
3004 }
3005 }
3006
3007 if (uri_auth->refresh) {
3008 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3009 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003010 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003011 break;
3012 }
3013 }
3014 }
3015
3016 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3017 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003018 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003019 break;
3020 }
3021 }
3022
3023 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3024 if (memcmp(h, ";st=", 4) == 0) {
3025 int i;
3026 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003027 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003028 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3029 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003030 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003031 break;
3032 }
3033 }
3034 break;
3035 }
3036 }
3037
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003038 appctx->ctx.stats.scope_str = 0;
3039 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003040 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3041 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3042 int itx = 0;
3043 const char *h2;
3044 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3045 const char *err;
3046
3047 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3048 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003049 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003050 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3051 itx++;
3052 h++;
3053 }
3054
3055 if (itx > STAT_SCOPE_TXT_MAXLEN)
3056 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003057 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003059 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003060 memcpy(scope_txt, h2, itx);
3061 scope_txt[itx] = '\0';
3062 err = invalid_char(scope_txt);
3063 if (err) {
3064 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003065 appctx->ctx.stats.scope_str = 0;
3066 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003067 }
3068 break;
3069 }
3070 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003071
3072 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003073 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003074 int ret = 1;
3075
3076 if (stats_admin_rule->cond) {
3077 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3078 ret = acl_pass(ret);
3079 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3080 ret = !ret;
3081 }
3082
3083 if (ret) {
3084 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003085 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003086 break;
3087 }
3088 }
3089
3090 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003091 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003092 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003093 if (msg->msg_state < HTTP_MSG_100_SENT) {
3094 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3095 * send an HTTP/1.1 100 Continue intermediate response.
3096 */
3097 if (msg->flags & HTTP_MSGF_VER_11) {
3098 struct hdr_ctx ctx;
3099 ctx.idx = 0;
3100 /* Expect is allowed in 1.1, look for it */
3101 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3102 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3103 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3104 }
3105 }
3106 msg->msg_state = HTTP_MSG_100_SENT;
3107 s->logs.tv_request = now; /* update the request timer to reflect full request */
3108 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003109 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003110 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003111 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003112 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3113 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003114 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003115 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003116 else {
3117 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003118 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003119 }
3120
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003121 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003122 return 1;
3123}
3124
Lukas Tribus67db8df2013-06-23 17:37:13 +02003125/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3126 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3127 */
3128static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3129{
3130#ifdef IP_TOS
3131 if (from.ss_family == AF_INET)
3132 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3133#endif
3134#ifdef IPV6_TCLASS
3135 if (from.ss_family == AF_INET6) {
3136 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3137 /* v4-mapped addresses need IP_TOS */
3138 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3139 else
3140 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3141 }
3142#endif
3143}
3144
Willy Tarreau20b0de52012-12-24 15:45:22 +01003145/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003146 * transaction <txn>. Returns the first rule that prevents further processing
3147 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3148 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3149 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003150 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003151static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003152http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003153{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003154 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003155 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003156 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003157
Willy Tarreauff011f22011-01-06 17:51:27 +01003158 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003159 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003160 continue;
3161
Willy Tarreau96257ec2012-12-27 10:46:37 +01003162 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003163 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003164 int ret;
3165
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003166 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003167 ret = acl_pass(ret);
3168
Willy Tarreauff011f22011-01-06 17:51:27 +01003169 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003170 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003171
3172 if (!ret) /* condition not matched */
3173 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003174 }
3175
Willy Tarreau20b0de52012-12-24 15:45:22 +01003176
Willy Tarreau96257ec2012-12-27 10:46:37 +01003177 switch (rule->action) {
3178 case HTTP_REQ_ACT_ALLOW:
3179 return NULL; /* "allow" rules are OK */
3180
3181 case HTTP_REQ_ACT_DENY:
3182 txn->flags |= TX_CLDENY;
3183 return rule;
3184
Willy Tarreauccbcc372012-12-27 12:37:57 +01003185 case HTTP_REQ_ACT_TARPIT:
3186 txn->flags |= TX_CLTARPIT;
3187 return rule;
3188
Willy Tarreau96257ec2012-12-27 10:46:37 +01003189 case HTTP_REQ_ACT_AUTH:
3190 return rule;
3191
Willy Tarreau81499eb2012-12-27 12:19:02 +01003192 case HTTP_REQ_ACT_REDIR:
3193 return rule;
3194
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003195 case HTTP_REQ_ACT_SET_NICE:
3196 s->task->nice = rule->arg.nice;
3197 break;
3198
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003199 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003200 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003201 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003202 break;
3203
Willy Tarreau51347ed2013-06-11 19:34:13 +02003204 case HTTP_REQ_ACT_SET_MARK:
3205#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003206 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003207 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003208#endif
3209 break;
3210
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003211 case HTTP_REQ_ACT_SET_LOGL:
3212 s->logs.level = rule->arg.loglevel;
3213 break;
3214
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003215 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003216 case HTTP_REQ_ACT_SET_HDR:
3217 ctx.idx = 0;
3218 /* remove all occurrences of the header */
3219 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3220 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3221 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003222 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003223 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3224 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003225 /* now fall through to header addition */
3226
3227 case HTTP_REQ_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->req, &txn->hdr_idx, trash.str, trash.len);
3235 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003236 }
3237 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003238
3239 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003240 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003241}
3242
Willy Tarreau71241ab2012-12-27 11:30:54 +01003243
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003244/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3245 * transaction <txn>. Returns the first rule that prevents further processing
3246 * of the response (deny, ...) or NULL if it executed all rules or stopped
3247 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3248 * rule.
3249 */
3250static struct http_res_rule *
3251http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3252{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003253 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003254 struct http_res_rule *rule;
3255 struct hdr_ctx ctx;
3256
3257 list_for_each_entry(rule, rules, list) {
3258 if (rule->action >= HTTP_RES_ACT_MAX)
3259 continue;
3260
3261 /* check optional condition */
3262 if (rule->cond) {
3263 int ret;
3264
3265 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3266 ret = acl_pass(ret);
3267
3268 if (rule->cond->pol == ACL_COND_UNLESS)
3269 ret = !ret;
3270
3271 if (!ret) /* condition not matched */
3272 continue;
3273 }
3274
3275
3276 switch (rule->action) {
3277 case HTTP_RES_ACT_ALLOW:
3278 return NULL; /* "allow" rules are OK */
3279
3280 case HTTP_RES_ACT_DENY:
3281 txn->flags |= TX_SVDENY;
3282 return rule;
3283
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003284 case HTTP_RES_ACT_SET_NICE:
3285 s->task->nice = rule->arg.nice;
3286 break;
3287
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003288 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003289 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003290 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003291 break;
3292
Willy Tarreau51347ed2013-06-11 19:34:13 +02003293 case HTTP_RES_ACT_SET_MARK:
3294#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003295 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003296 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003297#endif
3298 break;
3299
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003300 case HTTP_RES_ACT_SET_LOGL:
3301 s->logs.level = rule->arg.loglevel;
3302 break;
3303
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003304 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003305 case HTTP_RES_ACT_SET_HDR:
3306 ctx.idx = 0;
3307 /* remove all occurrences of the header */
3308 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3309 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3310 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3311 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003312 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3313 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003314 /* now fall through to header addition */
3315
3316 case HTTP_RES_ACT_ADD_HDR:
3317 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3318 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3319 trash.len = rule->arg.hdr_add.name_len;
3320 trash.str[trash.len++] = ':';
3321 trash.str[trash.len++] = ' ';
3322 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3323 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3324 break;
3325 }
3326 }
3327
3328 /* we reached the end of the rules, nothing to report */
3329 return NULL;
3330}
3331
3332
Willy Tarreau71241ab2012-12-27 11:30:54 +01003333/* Perform an HTTP redirect based on the information in <rule>. The function
3334 * returns non-zero on success, or zero in case of a, irrecoverable error such
3335 * as too large a request to build a valid response.
3336 */
3337static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3338{
3339 struct http_msg *msg = &txn->req;
3340 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003341 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003342
3343 /* build redirect message */
3344 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003345 case 308:
3346 msg_fmt = HTTP_308;
3347 break;
3348 case 307:
3349 msg_fmt = HTTP_307;
3350 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003351 case 303:
3352 msg_fmt = HTTP_303;
3353 break;
3354 case 301:
3355 msg_fmt = HTTP_301;
3356 break;
3357 case 302:
3358 default:
3359 msg_fmt = HTTP_302;
3360 break;
3361 }
3362
3363 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3364 return 0;
3365
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003366 location = trash.str + trash.len;
3367
Willy Tarreau71241ab2012-12-27 11:30:54 +01003368 switch(rule->type) {
3369 case REDIRECT_TYPE_SCHEME: {
3370 const char *path;
3371 const char *host;
3372 struct hdr_ctx ctx;
3373 int pathlen;
3374 int hostlen;
3375
3376 host = "";
3377 hostlen = 0;
3378 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003379 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003380 host = ctx.line + ctx.val;
3381 hostlen = ctx.vlen;
3382 }
3383
3384 path = http_get_path(txn);
3385 /* build message using path */
3386 if (path) {
3387 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3388 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3389 int qs = 0;
3390 while (qs < pathlen) {
3391 if (path[qs] == '?') {
3392 pathlen = qs;
3393 break;
3394 }
3395 qs++;
3396 }
3397 }
3398 } else {
3399 path = "/";
3400 pathlen = 1;
3401 }
3402
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003403 if (rule->rdr_str) { /* this is an old "redirect" rule */
3404 /* check if we can add scheme + "://" + host + path */
3405 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3406 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003407
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003408 /* add scheme */
3409 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3410 trash.len += rule->rdr_len;
3411 }
3412 else {
3413 /* add scheme with executing log format */
3414 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003415
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003416 /* check if we can add scheme + "://" + host + path */
3417 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3418 return 0;
3419 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003420 /* add "://" */
3421 memcpy(trash.str + trash.len, "://", 3);
3422 trash.len += 3;
3423
3424 /* add host */
3425 memcpy(trash.str + trash.len, host, hostlen);
3426 trash.len += hostlen;
3427
3428 /* add path */
3429 memcpy(trash.str + trash.len, path, pathlen);
3430 trash.len += pathlen;
3431
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003432 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003433 if (trash.len && trash.str[trash.len - 1] != '/' &&
3434 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3435 if (trash.len > trash.size - 5)
3436 return 0;
3437 trash.str[trash.len] = '/';
3438 trash.len++;
3439 }
3440
3441 break;
3442 }
3443 case REDIRECT_TYPE_PREFIX: {
3444 const char *path;
3445 int pathlen;
3446
3447 path = http_get_path(txn);
3448 /* build message using path */
3449 if (path) {
3450 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3451 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3452 int qs = 0;
3453 while (qs < pathlen) {
3454 if (path[qs] == '?') {
3455 pathlen = qs;
3456 break;
3457 }
3458 qs++;
3459 }
3460 }
3461 } else {
3462 path = "/";
3463 pathlen = 1;
3464 }
3465
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003466 if (rule->rdr_str) { /* this is an old "redirect" rule */
3467 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3468 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003469
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003470 /* add prefix. Note that if prefix == "/", we don't want to
3471 * add anything, otherwise it makes it hard for the user to
3472 * configure a self-redirection.
3473 */
3474 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3475 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3476 trash.len += rule->rdr_len;
3477 }
3478 }
3479 else {
3480 /* add prefix with executing log format */
3481 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3482
3483 /* Check length */
3484 if (trash.len + pathlen > trash.size - 4)
3485 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003486 }
3487
3488 /* add path */
3489 memcpy(trash.str + trash.len, path, pathlen);
3490 trash.len += pathlen;
3491
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003492 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003493 if (trash.len && trash.str[trash.len - 1] != '/' &&
3494 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3495 if (trash.len > trash.size - 5)
3496 return 0;
3497 trash.str[trash.len] = '/';
3498 trash.len++;
3499 }
3500
3501 break;
3502 }
3503 case REDIRECT_TYPE_LOCATION:
3504 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003505 if (rule->rdr_str) { /* this is an old "redirect" rule */
3506 if (trash.len + rule->rdr_len > trash.size - 4)
3507 return 0;
3508
3509 /* add location */
3510 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3511 trash.len += rule->rdr_len;
3512 }
3513 else {
3514 /* add location with executing log format */
3515 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003516
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003517 /* Check left length */
3518 if (trash.len > trash.size - 4)
3519 return 0;
3520 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003521 break;
3522 }
3523
3524 if (rule->cookie_len) {
3525 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3526 trash.len += 14;
3527 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3528 trash.len += rule->cookie_len;
3529 memcpy(trash.str + trash.len, "\r\n", 2);
3530 trash.len += 2;
3531 }
3532
3533 /* add end of headers and the keep-alive/close status.
3534 * We may choose to set keep-alive if the Location begins
3535 * with a slash, because the client will come back to the
3536 * same server.
3537 */
3538 txn->status = rule->code;
3539 /* let's log the request time */
3540 s->logs.tv_request = now;
3541
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003542 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003543 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3544 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3545 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3546 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3547 /* keep-alive possible */
3548 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3549 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3550 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3551 trash.len += 30;
3552 } else {
3553 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3554 trash.len += 24;
3555 }
3556 }
3557 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3558 trash.len += 4;
3559 bo_inject(txn->rsp.chn, trash.str, trash.len);
3560 /* "eat" the request */
3561 bi_fast_delete(txn->req.chn->buf, msg->sov);
3562 msg->sov = 0;
3563 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3564 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3565 txn->req.msg_state = HTTP_MSG_CLOSED;
3566 txn->rsp.msg_state = HTTP_MSG_DONE;
3567 } else {
3568 /* keep-alive not possible */
3569 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3570 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3571 trash.len += 29;
3572 } else {
3573 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3574 trash.len += 23;
3575 }
3576 stream_int_retnclose(txn->req.chn->prod, &trash);
3577 txn->req.chn->analysers = 0;
3578 }
3579
3580 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003581 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003582 if (!(s->flags & SN_FINST_MASK))
3583 s->flags |= SN_FINST_R;
3584
3585 return 1;
3586}
3587
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003588/* This stream analyser runs all HTTP request processing which is common to
3589 * frontends and backends, which means blocking ACLs, filters, connection-close,
3590 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003591 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003592 * either needs more data or wants to immediately abort the request (eg: deny,
3593 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003594 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003595int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003596{
Willy Tarreaud787e662009-07-07 10:14:51 +02003597 struct http_txn *txn = &s->txn;
3598 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003599 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003600 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003602 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003603
Willy Tarreau655dce92009-11-08 13:10:58 +01003604 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003605 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003606 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003607 return 0;
3608 }
3609
Willy Tarreau3a816292009-07-07 10:55:49 +02003610 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003611 req->analyse_exp = TICK_ETERNITY;
3612
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003613 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 +02003614 now_ms, __FUNCTION__,
3615 s,
3616 req,
3617 req->rex, req->wex,
3618 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003619 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003620 req->analysers);
3621
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003622 /* first check whether we have some ACLs set to block this request */
3623 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003624 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003625
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003626 ret = acl_pass(ret);
3627 if (cond->pol == ACL_COND_UNLESS)
3628 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003629
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003630 if (ret) {
3631 txn->status = 403;
3632 /* let's log the request time */
3633 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003634 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003635 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003636 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003637 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003638 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003639
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003640 /* just in case we have some per-backend tracking */
3641 session_inc_be_http_req_ctr(s);
3642
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003643 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003644 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003645
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003646 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003647 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003648 if (stats_check_uri(s->rep->prod, txn, px)) {
3649 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003650 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3651 txn->status = 500;
3652 s->logs.tv_request = now;
3653 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003654
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003655 if (!(s->flags & SN_ERR_MASK))
3656 s->flags |= SN_ERR_RESOURCE;
3657 goto return_prx_cond;
3658 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003659 /* parse the whole stats request and extract the relevant information */
3660 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003661 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 +01003662 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003663 }
3664
Willy Tarreau3b44e722013-11-16 10:28:23 +01003665 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3666 * blocked by an http-request rule.
3667 */
3668 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003669 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003671 }
Willy Tarreau06619262006-12-17 08:37:22 +01003672
Willy Tarreau3b44e722013-11-16 10:28:23 +01003673 /* return a 403 if either rule has blocked */
3674 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003675 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003676 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003677 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003678 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003679 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003680 s->fe->fe_counters.denied_req++;
3681 if (s->fe != s->be)
3682 s->be->be_counters.denied_req++;
3683 if (s->listener->counters)
3684 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003685 goto return_prx_cond;
3686 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003687
3688 /* When a connection is tarpitted, we use the tarpit timeout,
3689 * which may be the same as the connect timeout if unspecified.
3690 * If unset, then set it to zero because we really want it to
3691 * eventually expire. We build the tarpit as an analyser.
3692 */
3693 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003694 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003695 /* wipe the request out so that we can drop the connection early
3696 * if the client closes first.
3697 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003698 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003699 req->analysers = 0; /* remove switching rules etc... */
3700 req->analysers |= AN_REQ_HTTP_TARPIT;
3701 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3702 if (!req->analyse_exp)
3703 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003704 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003705 s->fe->fe_counters.denied_req++;
3706 if (s->fe != s->be)
3707 s->be->be_counters.denied_req++;
3708 if (s->listener->counters)
3709 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003710 return 1;
3711 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003712 }
Willy Tarreau06619262006-12-17 08:37:22 +01003713
Willy Tarreau70dffda2014-01-30 03:07:23 +01003714 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003715 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003716 * Option httpclose by itself sets tunnel mode where headers are mangled.
3717 * However, if another mode is set, it will affect it (eg: server-close/
3718 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3719 * if FE and BE have the same settings (common). The method consists in
3720 * checking if options changed between the two calls (implying that either
3721 * one is non-null, or one of them is non-null and we are there for the first
3722 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003723 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003724
Willy Tarreau416ce612014-01-31 15:45:34 +01003725 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003726 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003727 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003728
Willy Tarreau70dffda2014-01-30 03:07:23 +01003729 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3730 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3731 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3732 tmp = TX_CON_WANT_TUN;
3733
3734 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3735 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3736 tmp = TX_CON_WANT_TUN;
3737 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003738
3739 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003740 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3741 /* option httpclose + server_close => forceclose */
3742 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3743 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3744 tmp = TX_CON_WANT_CLO;
3745 else
3746 tmp = TX_CON_WANT_SCL;
3747 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003748
3749 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3750 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003751 tmp = TX_CON_WANT_CLO;
3752
Willy Tarreau5b154472009-12-21 20:11:07 +01003753 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3754 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003755
Willy Tarreau416ce612014-01-31 15:45:34 +01003756 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3757 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003758 /* parse the Connection header and possibly clean it */
3759 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003760 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003761 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3762 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003763 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003764 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003765 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003766 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003767 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003768
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003769 /* check if client or config asks for explicit close in KAL/SCL */
3770 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3771 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3772 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003773 (!(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 +01003774 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003775 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003776 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3777 }
Willy Tarreau78599912009-10-17 20:12:21 +02003778
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003779 /* we can be blocked here because the request needs to be authenticated,
3780 * either to pass or to access stats.
3781 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003782 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003783 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003784
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003785 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003786 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003787
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003788 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003789 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003790 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003791 /* on 401 we still count one error, because normal browsing
3792 * won't significantly increase the counter but brute force
3793 * attempts will.
3794 */
3795 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003796 goto return_prx_cond;
3797 }
3798
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003799 /* add request headers from the rule sets in the same order */
3800 list_for_each_entry(wl, &px->req_add, list) {
3801 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003802 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003803 ret = acl_pass(ret);
3804 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3805 ret = !ret;
3806 if (!ret)
3807 continue;
3808 }
3809
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003810 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003811 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003812 }
3813
3814 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3815 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3816 goto return_bad_req;
3817 req->analyse_exp = TICK_ETERNITY;
3818 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003819 }
3820
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003821 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003822 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003823 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3824 s->fe->fe_counters.intercepted_req++;
3825
3826 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3827 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3828 if (!(s->flags & SN_FINST_MASK))
3829 s->flags |= SN_FINST_R;
3830
3831 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003832 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003833 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003834 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003835
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003836 /* check whether we have some ACLs set to redirect this request */
3837 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003838 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003839 int ret;
3840
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003841 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003842 ret = acl_pass(ret);
3843 if (rule->cond->pol == ACL_COND_UNLESS)
3844 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003845 if (!ret)
3846 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003847 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003848 if (!http_apply_redirect_rule(rule, s, txn))
3849 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003850
Willy Tarreau71241ab2012-12-27 11:30:54 +01003851 req->analyse_exp = TICK_ETERNITY;
3852 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003853 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003854
Willy Tarreau2be39392010-01-03 17:24:51 +01003855 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3856 * If this happens, then the data will not come immediately, so we must
3857 * send all what we have without waiting. Note that due to the small gain
3858 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003859 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003860 * itself once used.
3861 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003862 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003863
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003864 /* that's OK for us now, let's move on to next analysers */
3865 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003866
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003867 return_bad_req:
3868 /* We centralize bad requests processing here */
3869 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3870 /* we detected a parsing error. We want to archive this request
3871 * in the dedicated proxy area for later troubleshooting.
3872 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003873 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003874 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003875
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003876 txn->req.msg_state = HTTP_MSG_ERROR;
3877 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003878 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003879
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003880 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003881 if (s->listener->counters)
3882 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003883
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003884 return_prx_cond:
3885 if (!(s->flags & SN_ERR_MASK))
3886 s->flags |= SN_ERR_PRXCOND;
3887 if (!(s->flags & SN_FINST_MASK))
3888 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003889
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003890 req->analysers = 0;
3891 req->analyse_exp = TICK_ETERNITY;
3892 return 0;
3893}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003894
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003895/* This function performs all the processing enabled for the current request.
3896 * It returns 1 if the processing can continue on next analysers, or zero if it
3897 * needs more data, encounters an error, or wants to immediately abort the
3898 * request. It relies on buffers flags, and updates s->req->analysers.
3899 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003900int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003901{
3902 struct http_txn *txn = &s->txn;
3903 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003904 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003905
Willy Tarreau655dce92009-11-08 13:10:58 +01003906 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003907 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003908 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003909 return 0;
3910 }
3911
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003912 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 +02003913 now_ms, __FUNCTION__,
3914 s,
3915 req,
3916 req->rex, req->wex,
3917 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003918 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003919 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003920
William Lallemand82fe75c2012-10-23 10:25:10 +02003921 if (s->fe->comp || s->be->comp)
3922 select_compression_request_header(s, req->buf);
3923
Willy Tarreau59234e92008-11-30 23:51:27 +01003924 /*
3925 * Right now, we know that we have processed the entire headers
3926 * and that unwanted requests have been filtered out. We can do
3927 * whatever we want with the remaining request. Also, now we
3928 * may have separate values for ->fe, ->be.
3929 */
Willy Tarreau06619262006-12-17 08:37:22 +01003930
Willy Tarreau59234e92008-11-30 23:51:27 +01003931 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003932 * If HTTP PROXY is set we simply get remote server address parsing
3933 * incoming request. Note that this requires that a connection is
3934 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003935 */
3936 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003937 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003938 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003939
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003940 /* Note that for now we don't reuse existing proxy connections */
3941 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003942 txn->req.msg_state = HTTP_MSG_ERROR;
3943 txn->status = 500;
3944 req->analysers = 0;
3945 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3946
3947 if (!(s->flags & SN_ERR_MASK))
3948 s->flags |= SN_ERR_RESOURCE;
3949 if (!(s->flags & SN_FINST_MASK))
3950 s->flags |= SN_FINST_R;
3951
3952 return 0;
3953 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003954
3955 path = http_get_path(txn);
3956 url2sa(req->buf->p + msg->sl.rq.u,
3957 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003958 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003959 /* if the path was found, we have to remove everything between
3960 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3961 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3962 * u_l characters by a single "/".
3963 */
3964 if (path) {
3965 char *cur_ptr = req->buf->p;
3966 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3967 int delta;
3968
3969 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3970 http_msg_move_end(&txn->req, delta);
3971 cur_end += delta;
3972 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3973 goto return_bad_req;
3974 }
3975 else {
3976 char *cur_ptr = req->buf->p;
3977 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3978 int delta;
3979
3980 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3981 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3982 http_msg_move_end(&txn->req, delta);
3983 cur_end += delta;
3984 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3985 goto return_bad_req;
3986 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003987 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003988
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003990 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003991 * Note that doing so might move headers in the request, but
3992 * the fields will stay coherent and the URI will not move.
3993 * This should only be performed in the backend.
3994 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003995 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003996 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3997 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003998
Willy Tarreau59234e92008-11-30 23:51:27 +01003999 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004000 * 8: the appsession cookie was looked up very early in 1.2,
4001 * so let's do the same now.
4002 */
4003
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004004 /* It needs to look into the URI unless persistence must be ignored */
4005 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004006 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 +01004007 }
4008
William Lallemanda73203e2012-03-12 12:48:57 +01004009 /* add unique-id if "header-unique-id" is specified */
4010
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004011 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4012 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4013 goto return_bad_req;
4014 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004015 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004016 }
William Lallemanda73203e2012-03-12 12:48:57 +01004017
4018 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004019 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4020 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004021 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004022 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004023 goto return_bad_req;
4024 }
4025
Cyril Bontéb21570a2009-11-29 20:04:48 +01004026 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004027 * 9: add X-Forwarded-For if either the frontend or the backend
4028 * asks for it.
4029 */
4030 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004031 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004032 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004033 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4034 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004035 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004036 /* The header is set to be added only if none is present
4037 * and we found it, so don't do anything.
4038 */
4039 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004040 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004041 /* Add an X-Forwarded-For header unless the source IP is
4042 * in the 'except' network range.
4043 */
4044 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004045 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004046 != s->fe->except_net.s_addr) &&
4047 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004048 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004049 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004050 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004051 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004052 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004053
4054 /* Note: we rely on the backend to get the header name to be used for
4055 * x-forwarded-for, because the header is really meant for the backends.
4056 * However, if the backend did not specify any option, we have to rely
4057 * on the frontend's header name.
4058 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004059 if (s->be->fwdfor_hdr_len) {
4060 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004061 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004062 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004063 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004064 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004065 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004066 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004067
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004068 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004069 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004070 }
4071 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004072 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004073 /* FIXME: for the sake of completeness, we should also support
4074 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004075 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004076 int len;
4077 char pn[INET6_ADDRSTRLEN];
4078 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004079 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004080 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004081
Willy Tarreau59234e92008-11-30 23:51:27 +01004082 /* Note: we rely on the backend to get the header name to be used for
4083 * x-forwarded-for, because the header is really meant for the backends.
4084 * However, if the backend did not specify any option, we have to rely
4085 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004086 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004087 if (s->be->fwdfor_hdr_len) {
4088 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004089 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004090 } else {
4091 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004092 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004093 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004094 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004095
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004096 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004097 goto return_bad_req;
4098 }
4099 }
4100
4101 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004102 * 10: add X-Original-To if either the frontend or the backend
4103 * asks for it.
4104 */
4105 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4106
4107 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004108 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004109 /* Add an X-Original-To header unless the destination IP is
4110 * in the 'except' network range.
4111 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004112 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004113
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004114 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004115 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004116 (((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 +02004117 != s->fe->except_to.s_addr) &&
4118 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004119 (((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 +02004120 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004121 int len;
4122 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004123 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004124
4125 /* Note: we rely on the backend to get the header name to be used for
4126 * x-original-to, because the header is really meant for the backends.
4127 * However, if the backend did not specify any option, we have to rely
4128 * on the frontend's header name.
4129 */
4130 if (s->be->orgto_hdr_len) {
4131 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004132 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004133 } else {
4134 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004135 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004136 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004137 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004138
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004139 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004140 goto return_bad_req;
4141 }
4142 }
4143 }
4144
Willy Tarreau50fc7772012-11-11 22:19:57 +01004145 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4146 * If an "Upgrade" token is found, the header is left untouched in order not to have
4147 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4148 * "Upgrade" is present in the Connection header.
4149 */
4150 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4151 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004152 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4153 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004154 unsigned int want_flags = 0;
4155
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004156 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004157 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004158 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4159 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004160 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004161 want_flags |= TX_CON_CLO_SET;
4162 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004163 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004164 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4165 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004166 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004167 want_flags |= TX_CON_KAL_SET;
4168 }
4169
4170 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004171 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004172 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004173
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004174
Willy Tarreau522d6c02009-12-06 18:49:18 +01004175 /* If we have no server assigned yet and we're balancing on url_param
4176 * with a POST request, we may be interested in checking the body for
4177 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004178 */
4179 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4180 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004181 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004182 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004183 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004184 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004185
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004186 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004187 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004188#ifdef TCP_QUICKACK
4189 /* We expect some data from the client. Unless we know for sure
4190 * we already have a full request, we have to re-enable quick-ack
4191 * in case we previously disabled it, otherwise we might cause
4192 * the client to delay further data.
4193 */
4194 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004195 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004196 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004197 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004198 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004199#endif
4200 }
Willy Tarreau03945942009-12-22 16:50:27 +01004201
Willy Tarreau59234e92008-11-30 23:51:27 +01004202 /*************************************************************
4203 * OK, that's finished for the headers. We have done what we *
4204 * could. Let's switch to the DATA state. *
4205 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004206 req->analyse_exp = TICK_ETERNITY;
4207 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004208
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004209 /* if the server closes the connection, we want to immediately react
4210 * and close the socket to save packets and syscalls.
4211 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004212 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4213 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004214
Willy Tarreau59234e92008-11-30 23:51:27 +01004215 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004216 /* OK let's go on with the BODY now */
4217 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004218
Willy Tarreau59234e92008-11-30 23:51:27 +01004219 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004220 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004221 /* we detected a parsing error. We want to archive this request
4222 * in the dedicated proxy area for later troubleshooting.
4223 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004224 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004225 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004226
Willy Tarreau59234e92008-11-30 23:51:27 +01004227 txn->req.msg_state = HTTP_MSG_ERROR;
4228 txn->status = 400;
4229 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004230 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004231
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004232 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004233 if (s->listener->counters)
4234 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004235
Willy Tarreau59234e92008-11-30 23:51:27 +01004236 if (!(s->flags & SN_ERR_MASK))
4237 s->flags |= SN_ERR_PRXCOND;
4238 if (!(s->flags & SN_FINST_MASK))
4239 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004240 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004241}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004242
Willy Tarreau60b85b02008-11-30 23:28:40 +01004243/* This function is an analyser which processes the HTTP tarpit. It always
4244 * returns zero, at the beginning because it prevents any other processing
4245 * from occurring, and at the end because it terminates the request.
4246 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004247int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004248{
4249 struct http_txn *txn = &s->txn;
4250
4251 /* This connection is being tarpitted. The CLIENT side has
4252 * already set the connect expiration date to the right
4253 * timeout. We just have to check that the client is still
4254 * there and that the timeout has not expired.
4255 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004256 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004257 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004258 !tick_is_expired(req->analyse_exp, now_ms))
4259 return 0;
4260
4261 /* We will set the queue timer to the time spent, just for
4262 * logging purposes. We fake a 500 server error, so that the
4263 * attacker will not suspect his connection has been tarpitted.
4264 * It will not cause trouble to the logs because we can exclude
4265 * the tarpitted connections by filtering on the 'PT' status flags.
4266 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004267 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4268
4269 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004270 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004271 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004272
4273 req->analysers = 0;
4274 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004275
Willy Tarreau60b85b02008-11-30 23:28:40 +01004276 if (!(s->flags & SN_ERR_MASK))
4277 s->flags |= SN_ERR_PRXCOND;
4278 if (!(s->flags & SN_FINST_MASK))
4279 s->flags |= SN_FINST_T;
4280 return 0;
4281}
4282
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004283/* This function is an analyser which waits for the HTTP request body. It waits
4284 * for either the buffer to be full, or the full advertised contents to have
4285 * reached the buffer. It must only be called after the standard HTTP request
4286 * processing has occurred, because it expects the request to be parsed and will
4287 * look for the Expect header. It may send a 100-Continue interim response. It
4288 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4289 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4290 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004291 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004292int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004293{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004294 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004295 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004296
4297 /* We have to parse the HTTP request body to find any required data.
4298 * "balance url_param check_post" should have been the only way to get
4299 * into this. We were brought here after HTTP header analysis, so all
4300 * related structures are ready.
4301 */
4302
Willy Tarreau890988f2014-04-10 11:59:33 +02004303 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4304 /* This is the first call */
4305 if (msg->msg_state < HTTP_MSG_BODY)
4306 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004307
Willy Tarreau890988f2014-04-10 11:59:33 +02004308 if (msg->msg_state < HTTP_MSG_100_SENT) {
4309 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4310 * send an HTTP/1.1 100 Continue intermediate response.
4311 */
4312 if (msg->flags & HTTP_MSGF_VER_11) {
4313 struct hdr_ctx ctx;
4314 ctx.idx = 0;
4315 /* Expect is allowed in 1.1, look for it */
4316 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4317 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4318 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4319 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004320 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004321 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004322 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004323
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004324 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004325 * req->buf->p still points to the beginning of the message. We
4326 * must save the body in msg->next because it survives buffer
4327 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004328 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004329 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004330
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004331 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004332 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4333 else
4334 msg->msg_state = HTTP_MSG_DATA;
4335 }
4336
Willy Tarreau890988f2014-04-10 11:59:33 +02004337 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4338 /* We're in content-length mode, we just have to wait for enough data. */
4339 if (req->buf->i - msg->sov < msg->body_len)
4340 goto missing_data;
4341
4342 /* OK we have everything we need now */
4343 goto http_end;
4344 }
4345
4346 /* OK here we're parsing a chunked-encoded message */
4347
Willy Tarreau522d6c02009-12-06 18:49:18 +01004348 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004349 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004350 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004351 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004352 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004353 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004354
Willy Tarreau115acb92009-12-26 13:56:06 +01004355 if (!ret)
4356 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004357 else if (ret < 0) {
4358 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004359 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004360 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004361 }
4362
Willy Tarreaud98cf932009-12-27 22:54:55 +01004363 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004364 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004365 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004366 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004367 if (msg->msg_state == HTTP_MSG_TRAILERS)
4368 goto http_end;
4369
Willy Tarreau226071e2014-04-10 11:55:45 +02004370 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004371 goto http_end;
4372
4373 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004374 /* we get here if we need to wait for more data. If the buffer is full,
4375 * we have the maximum we can expect.
4376 */
4377 if (buffer_full(req->buf, global.tune.maxrewrite))
4378 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004379
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004380 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004381 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004382 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004383
4384 if (!(s->flags & SN_ERR_MASK))
4385 s->flags |= SN_ERR_CLITO;
4386 if (!(s->flags & SN_FINST_MASK))
4387 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004388 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004389 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004390
4391 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004392 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004393 /* Not enough data. We'll re-use the http-request
4394 * timeout here. Ideally, we should set the timeout
4395 * relative to the accept() date. We just set the
4396 * request timeout once at the beginning of the
4397 * request.
4398 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004399 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004400 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004401 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004402 return 0;
4403 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004404
4405 http_end:
4406 /* The situation will not evolve, so let's give up on the analysis. */
4407 s->logs.tv_request = now; /* update the request timer to reflect full request */
4408 req->analysers &= ~an_bit;
4409 req->analyse_exp = TICK_ETERNITY;
4410 return 1;
4411
4412 return_bad_req: /* let's centralize all bad requests */
4413 txn->req.msg_state = HTTP_MSG_ERROR;
4414 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004415 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004416
Willy Tarreau79ebac62010-06-07 13:47:49 +02004417 if (!(s->flags & SN_ERR_MASK))
4418 s->flags |= SN_ERR_PRXCOND;
4419 if (!(s->flags & SN_FINST_MASK))
4420 s->flags |= SN_FINST_R;
4421
Willy Tarreau522d6c02009-12-06 18:49:18 +01004422 return_err_msg:
4423 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004424 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004425 if (s->listener->counters)
4426 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004427 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004428}
4429
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004430/* send a server's name with an outgoing request over an established connection.
4431 * Note: this function is designed to be called once the request has been scheduled
4432 * for being forwarded. This is the reason why it rewinds the buffer before
4433 * proceeding.
4434 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004435int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004436
4437 struct hdr_ctx ctx;
4438
Mark Lamourinec2247f02012-01-04 13:02:01 -05004439 char *hdr_name = be->server_id_hdr_name;
4440 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004441 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004442 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004443 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004444
William Lallemandd9e90662012-01-30 17:27:17 +01004445 ctx.idx = 0;
4446
Willy Tarreau9b28e032012-10-12 23:49:43 +02004447 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004448 if (old_o) {
4449 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004450 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004451 txn->req.next += old_o;
4452 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004453 }
4454
Willy Tarreau9b28e032012-10-12 23:49:43 +02004455 old_i = chn->buf->i;
4456 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 -05004457 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004458 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004459 }
4460
4461 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004462 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004463 memcpy(hdr_val, hdr_name, hdr_name_len);
4464 hdr_val += hdr_name_len;
4465 *hdr_val++ = ':';
4466 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004467 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4468 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004469
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004470 if (old_o) {
4471 /* If this was a forwarded request, we must readjust the amount of
4472 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004473 * variations. Note that the current state is >= HTTP_MSG_BODY,
4474 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004475 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004476 old_o += chn->buf->i - old_i;
4477 b_adv(chn->buf, old_o);
4478 txn->req.next -= old_o;
4479 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004480 }
4481
Mark Lamourinec2247f02012-01-04 13:02:01 -05004482 return 0;
4483}
4484
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485/* Terminate current transaction and prepare a new one. This is very tricky
4486 * right now but it works.
4487 */
4488void http_end_txn_clean_session(struct session *s)
4489{
Willy Tarreau068621e2013-12-23 15:11:25 +01004490 int prev_status = s->txn.status;
4491
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492 /* FIXME: We need a more portable way of releasing a backend's and a
4493 * server's connections. We need a safer way to reinitialize buffer
4494 * flags. We also need a more accurate method for computing per-request
4495 * data.
4496 */
4497 http_silent_debug(__LINE__, s);
4498
Willy Tarreau4213a112013-12-15 10:25:42 +01004499 /* unless we're doing keep-alive, we want to quickly close the connection
4500 * to the server.
4501 */
4502 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4503 !si_conn_ready(s->req->cons)) {
4504 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4505 si_shutr(s->req->cons);
4506 si_shutw(s->req->cons);
4507 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508
4509 http_silent_debug(__LINE__, s);
4510
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004511 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004512 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004513 if (unlikely(s->srv_conn))
4514 sess_change_server(s, NULL);
4515 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004516
4517 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4518 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004519 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520
4521 if (s->txn.status) {
4522 int n;
4523
4524 n = s->txn.status / 100;
4525 if (n < 1 || n > 5)
4526 n = 0;
4527
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004528 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004529 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004530 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004531 s->fe->fe_counters.p.http.comp_rsp++;
4532 }
Willy Tarreau24657792010-02-26 10:30:28 +01004533 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004534 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004535 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004536 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004537 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004538 s->be->be_counters.p.http.comp_rsp++;
4539 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540 }
4541
4542 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004543 s->logs.bytes_in -= s->req->buf->i;
4544 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545
4546 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004547 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 !(s->flags & SN_MONITOR) &&
4549 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4550 s->do_log(s);
4551 }
4552
4553 s->logs.accept_date = date; /* user-visible date for logging */
4554 s->logs.tv_accept = now; /* corrected date for internal use */
4555 tv_zero(&s->logs.tv_request);
4556 s->logs.t_queue = -1;
4557 s->logs.t_connect = -1;
4558 s->logs.t_data = -1;
4559 s->logs.t_close = 0;
4560 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4561 s->logs.srv_queue_size = 0; /* we will get this number soon */
4562
Willy Tarreau9b28e032012-10-12 23:49:43 +02004563 s->logs.bytes_in = s->req->total = s->req->buf->i;
4564 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565
4566 if (s->pend_pos)
4567 pendconn_free(s->pend_pos);
4568
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004569 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 if (s->flags & SN_CURR_SESS) {
4571 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004572 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004574 if (may_dequeue_tasks(objt_server(s->target), s->be))
4575 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 }
4577
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004578 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004579
Willy Tarreau4213a112013-12-15 10:25:42 +01004580 /* only release our endpoint if we don't intend to reuse the
4581 * connection.
4582 */
4583 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4584 !si_conn_ready(s->req->cons)) {
4585 si_release_endpoint(s->req->cons);
4586 }
4587
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004590 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004591 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004592 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004593 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4594 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 +02004595 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 +01004596 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004597
Willy Tarreau610ecce2010-01-04 21:15:02 +01004598 s->txn.meth = 0;
4599 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004600 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004601
4602 if (prev_status == 401 || prev_status == 407) {
4603 /* In HTTP keep-alive mode, if we receive a 401, we still have
4604 * a chance of being able to send the visitor again to the same
4605 * server over the same connection. This is required by some
4606 * broken protocols such as NTLM, and anyway whenever there is
4607 * an opportunity for sending the challenge to the proper place,
4608 * it's better to do it (at least it helps with debugging).
4609 */
4610 s->txn.flags |= TX_PREFER_LAST;
4611 }
4612
Willy Tarreauee55dc02010-06-01 10:56:34 +02004613 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004614 s->req->cons->flags |= SI_FL_INDEP_STR;
4615
Willy Tarreau96e31212011-05-30 18:10:30 +02004616 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004617 s->req->flags |= CF_NEVER_WAIT;
4618 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004619 }
4620
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621 /* if the request buffer is not empty, it means we're
4622 * about to process another request, so send pending
4623 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004624 * Just don't do this if the buffer is close to be full,
4625 * because the request will wait for it to flush a little
4626 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004628 if (s->req->buf->i) {
4629 if (s->rep->buf->o &&
4630 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4631 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004632 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004633 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004634
4635 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004636 channel_auto_read(s->req);
4637 channel_auto_close(s->req);
4638 channel_auto_read(s->rep);
4639 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004640
Willy Tarreau27375622013-12-17 00:00:28 +01004641 /* we're in keep-alive with an idle connection, monitor it */
4642 si_idle_conn(s->req->cons);
4643
Willy Tarreau342b11c2010-11-24 16:22:09 +01004644 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645 s->rep->analysers = 0;
4646
4647 http_silent_debug(__LINE__, s);
4648}
4649
4650
4651/* This function updates the request state machine according to the response
4652 * state machine and buffer flags. It returns 1 if it changes anything (flag
4653 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4654 * it is only used to find when a request/response couple is complete. Both
4655 * this function and its equivalent should loop until both return zero. It
4656 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4657 */
4658int http_sync_req_state(struct session *s)
4659{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004660 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004661 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004662 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004663 unsigned int old_state = txn->req.msg_state;
4664
4665 http_silent_debug(__LINE__, s);
4666 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4667 return 0;
4668
4669 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004670 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004671 * We can shut the read side unless we want to abort_on_close,
4672 * or we have a POST request. The issue with POST requests is
4673 * that some browsers still send a CRLF after the request, and
4674 * this CRLF must be read so that it does not remain in the kernel
4675 * buffers, otherwise a close could cause an RST on some systems
4676 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004677 * Note that if we're using keep-alive on the client side, we'd
4678 * rather poll now and keep the polling enabled for the whole
4679 * session's life than enabling/disabling it between each
4680 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004681 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004682 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4683 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4684 !(s->be->options & PR_O_ABRT_CLOSE) &&
4685 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004686 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687
Willy Tarreau40f151a2012-12-20 12:10:09 +01004688 /* if the server closes the connection, we want to immediately react
4689 * and close the socket to save packets and syscalls.
4690 */
4691 chn->cons->flags |= SI_FL_NOHALF;
4692
Willy Tarreau610ecce2010-01-04 21:15:02 +01004693 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4694 goto wait_other_side;
4695
4696 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4697 /* The server has not finished to respond, so we
4698 * don't want to move in order not to upset it.
4699 */
4700 goto wait_other_side;
4701 }
4702
4703 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4704 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004705 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004706 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004707 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 goto wait_other_side;
4709 }
4710
4711 /* When we get here, it means that both the request and the
4712 * response have finished receiving. Depending on the connection
4713 * mode, we'll have to wait for the last bytes to leave in either
4714 * direction, and sometimes for a close to be effective.
4715 */
4716
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004717 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4718 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004719 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4720 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004721 }
4722 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4723 /* Option forceclose is set, or either side wants to close,
4724 * let's enforce it now that we're not expecting any new
4725 * data to come. The caller knows the session is complete
4726 * once both states are CLOSED.
4727 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004728 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4729 channel_shutr_now(chn);
4730 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004731 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004732 }
4733 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004734 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4735 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004736 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004737 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4738 channel_auto_read(chn);
4739 txn->req.msg_state = HTTP_MSG_TUNNEL;
4740 chn->flags |= CF_NEVER_WAIT;
4741 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 }
4743
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004744 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004746 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004747
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004748 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004749 txn->req.msg_state = HTTP_MSG_CLOSING;
4750 goto http_msg_closing;
4751 }
4752 else {
4753 txn->req.msg_state = HTTP_MSG_CLOSED;
4754 goto http_msg_closed;
4755 }
4756 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004757 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004758 }
4759
4760 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4761 http_msg_closing:
4762 /* nothing else to forward, just waiting for the output buffer
4763 * to be empty and for the shutw_now to take effect.
4764 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004765 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004766 txn->req.msg_state = HTTP_MSG_CLOSED;
4767 goto http_msg_closed;
4768 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004769 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770 txn->req.msg_state = HTTP_MSG_ERROR;
4771 goto wait_other_side;
4772 }
4773 }
4774
4775 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4776 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004777 /* see above in MSG_DONE why we only do this in these states */
4778 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4779 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4780 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004781 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 goto wait_other_side;
4783 }
4784
4785 wait_other_side:
4786 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004787 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004788}
4789
4790
4791/* This function updates the response state machine according to the request
4792 * state machine and buffer flags. It returns 1 if it changes anything (flag
4793 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4794 * it is only used to find when a request/response couple is complete. Both
4795 * this function and its equivalent should loop until both return zero. It
4796 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4797 */
4798int http_sync_res_state(struct session *s)
4799{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004800 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004802 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004803 unsigned int old_state = txn->rsp.msg_state;
4804
4805 http_silent_debug(__LINE__, s);
4806 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4807 return 0;
4808
4809 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4810 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004811 * still monitor the server connection for a possible close
4812 * while the request is being uploaded, so we don't disable
4813 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004814 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004815 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004816
4817 if (txn->req.msg_state == HTTP_MSG_ERROR)
4818 goto wait_other_side;
4819
4820 if (txn->req.msg_state < HTTP_MSG_DONE) {
4821 /* The client seems to still be sending data, probably
4822 * because we got an error response during an upload.
4823 * We have the choice of either breaking the connection
4824 * or letting it pass through. Let's do the later.
4825 */
4826 goto wait_other_side;
4827 }
4828
4829 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4830 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004831 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004832 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004833 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004834 goto wait_other_side;
4835 }
4836
4837 /* When we get here, it means that both the request and the
4838 * response have finished receiving. Depending on the connection
4839 * mode, we'll have to wait for the last bytes to leave in either
4840 * direction, and sometimes for a close to be effective.
4841 */
4842
4843 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4844 /* Server-close mode : shut read and wait for the request
4845 * side to close its output buffer. The caller will detect
4846 * when we're in DONE and the other is in CLOSED and will
4847 * catch that for the final cleanup.
4848 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004849 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4850 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004851 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004852 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4853 /* Option forceclose is set, or either side wants to close,
4854 * let's enforce it now that we're not expecting any new
4855 * data to come. The caller knows the session is complete
4856 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004857 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004858 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4859 channel_shutr_now(chn);
4860 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004861 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004862 }
4863 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004864 /* The last possible modes are keep-alive and tunnel. Tunnel will
4865 * need to forward remaining data. Keep-alive will need to monitor
4866 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004868 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004869 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004870 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4871 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004872 }
4873
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004874 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004876 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004877 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4878 goto http_msg_closing;
4879 }
4880 else {
4881 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4882 goto http_msg_closed;
4883 }
4884 }
4885 goto wait_other_side;
4886 }
4887
4888 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4889 http_msg_closing:
4890 /* nothing else to forward, just waiting for the output buffer
4891 * to be empty and for the shutw_now to take effect.
4892 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004893 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4895 goto http_msg_closed;
4896 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004897 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004898 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004899 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004900 if (objt_server(s->target))
4901 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004902 goto wait_other_side;
4903 }
4904 }
4905
4906 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4907 http_msg_closed:
4908 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004909 bi_erase(chn);
4910 channel_auto_close(chn);
4911 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 goto wait_other_side;
4913 }
4914
4915 wait_other_side:
4916 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004917 /* We force the response to leave immediately if we're waiting for the
4918 * other side, since there is no pending shutdown to push it out.
4919 */
4920 if (!channel_is_empty(chn))
4921 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004922 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004923}
4924
4925
4926/* Resync the request and response state machines. Return 1 if either state
4927 * changes.
4928 */
4929int http_resync_states(struct session *s)
4930{
4931 struct http_txn *txn = &s->txn;
4932 int old_req_state = txn->req.msg_state;
4933 int old_res_state = txn->rsp.msg_state;
4934
4935 http_silent_debug(__LINE__, s);
4936 http_sync_req_state(s);
4937 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004938 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 if (!http_sync_res_state(s))
4940 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004941 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004942 if (!http_sync_req_state(s))
4943 break;
4944 }
4945 http_silent_debug(__LINE__, s);
4946 /* OK, both state machines agree on a compatible state.
4947 * There are a few cases we're interested in :
4948 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4949 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4950 * directions, so let's simply disable both analysers.
4951 * - HTTP_MSG_CLOSED on the response only means we must abort the
4952 * request.
4953 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4954 * with server-close mode means we've completed one request and we
4955 * must re-initialize the server connection.
4956 */
4957
4958 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4959 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4960 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4961 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4962 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004963 channel_auto_close(s->req);
4964 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004966 channel_auto_close(s->rep);
4967 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004968 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004969 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4970 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004971 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004972 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004973 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004974 channel_auto_close(s->rep);
4975 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004976 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004977 channel_abort(s->req);
4978 channel_auto_close(s->req);
4979 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004980 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004981 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004982 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4983 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004984 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004985 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4986 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4987 /* server-close/keep-alive: terminate this transaction,
4988 * possibly killing the server connection and reinitialize
4989 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 */
4991 http_end_txn_clean_session(s);
4992 }
4993
4994 http_silent_debug(__LINE__, s);
4995 return txn->req.msg_state != old_req_state ||
4996 txn->rsp.msg_state != old_res_state;
4997}
4998
Willy Tarreaud98cf932009-12-27 22:54:55 +01004999/* This function is an analyser which forwards request body (including chunk
5000 * sizes if any). It is called as soon as we must forward, even if we forward
5001 * zero byte. The only situation where it must not be called is when we're in
5002 * tunnel mode and we want to forward till the close. It's used both to forward
5003 * remaining data and to resync after end of body. It expects the msg_state to
5004 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5005 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005006 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005007 * bytes of pending data + the headers if not already done (between sol and sov).
5008 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005009 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005010int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005011{
5012 struct http_txn *txn = &s->txn;
5013 struct http_msg *msg = &s->txn.req;
5014
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005015 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5016 return 0;
5017
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005018 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005019 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005020 /* Output closed while we were sending data. We must abort and
5021 * wake the other side up.
5022 */
5023 msg->msg_state = HTTP_MSG_ERROR;
5024 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005025 return 1;
5026 }
5027
Willy Tarreau80a92c02014-03-12 10:41:13 +01005028 /* Some post-connect processing might want us to refrain from starting to
5029 * forward data. Currently, the only reason for this is "balance url_param"
5030 * whichs need to parse/process the request after we've enabled forwarding.
5031 */
5032 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5033 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5034 channel_auto_connect(req);
5035 goto missing_data;
5036 }
5037 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5038 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005039
5040 /* Note that we don't have to send 100-continue back because we don't
5041 * need the data to complete our job, and it's up to the server to
5042 * decide whether to return 100, 417 or anything else in return of
5043 * an "Expect: 100-continue" header.
5044 */
5045
5046 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005047 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02005048 * req->buf->p still points to the beginning of the message. We
5049 * must save the body in msg->next because it survives buffer
5050 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005051 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005052 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005053
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005054 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005055 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005056 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005057 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005058 }
5059
Willy Tarreau80a92c02014-03-12 10:41:13 +01005060 /* in most states, we should abort in case of early close */
5061 channel_auto_close(req);
5062
Willy Tarreaud98cf932009-12-27 22:54:55 +01005063 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005064 http_silent_debug(__LINE__, s);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005065
5066 /* we may have some pending data starting at req->buf->p */
5067 if (msg->chunk_len || msg->sov) {
5068 msg->chunk_len += msg->sov;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005069 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005070 msg->next -= msg->sov;
5071 msg->sov = 0;
Willy Tarreau638cd022010-01-03 07:42:04 +01005072 }
Willy Tarreau5523b322009-12-29 12:05:52 +01005073
Willy Tarreaucaabe412010-01-03 23:08:28 +01005074 if (msg->msg_state == HTTP_MSG_DATA) {
5075 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005076 if (req->to_forward) {
5077 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005078 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005079 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005080
5081 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005082 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005083 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005084 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005085 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005086 }
5087 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005088 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005089 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005090 * TRAILERS state.
5091 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005092 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005093
Willy Tarreau54d23df2012-10-25 19:04:45 +02005094 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005095 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005096 else if (ret < 0) {
5097 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005098 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005099 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005100 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005101 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005102 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005103 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005104 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005105 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005106 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005107
5108 if (ret == 0)
5109 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005110 else if (ret < 0) {
5111 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005112 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005113 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005114 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005115 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005116 /* we're in MSG_CHUNK_SIZE now */
5117 }
5118 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005119 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005120
5121 if (ret == 0)
5122 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005123 else if (ret < 0) {
5124 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005125 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005126 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005127 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005128 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005129 /* we're in HTTP_MSG_DONE now */
5130 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005132 int old_state = msg->msg_state;
5133
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005135 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005136 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5137 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005138 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139 if (http_resync_states(s)) {
5140 /* some state changes occurred, maybe the analyser
5141 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005142 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005143 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005144 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005145 /* request errors are most likely due to
5146 * the server aborting the transfer.
5147 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005148 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005149 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005150 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005151 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005152 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005153 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005155 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005156
5157 /* If "option abortonclose" is set on the backend, we
5158 * want to monitor the client's connection and forward
5159 * any shutdown notification to the server, which will
5160 * decide whether to close or to go on processing the
5161 * request.
5162 */
5163 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005164 channel_auto_read(req);
5165 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005166 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005167 else if (s->txn.meth == HTTP_METH_POST) {
5168 /* POST requests may require to read extra CRLF
5169 * sent by broken browsers and which could cause
5170 * an RST to be sent upon close on some systems
5171 * (eg: Linux).
5172 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005173 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005174 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005175
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005177 }
5178 }
5179
Willy Tarreaud98cf932009-12-27 22:54:55 +01005180 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005181 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005182 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005183 if (!(s->flags & SN_ERR_MASK))
5184 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005185 if (!(s->flags & SN_FINST_MASK)) {
5186 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5187 s->flags |= SN_FINST_H;
5188 else
5189 s->flags |= SN_FINST_D;
5190 }
5191
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005192 s->fe->fe_counters.cli_aborts++;
5193 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005194 if (objt_server(s->target))
5195 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005196
5197 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005198 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005199
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005200 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005201 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005202 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005203
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005204 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005205 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005206 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005207 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005208 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005209
Willy Tarreau5c620922011-05-11 19:56:11 +02005210 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005211 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005212 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005213 * modes are already handled by the stream sock layer. We must not do
5214 * this in content-length mode because it could present the MSG_MORE
5215 * flag with the last block of forwarded data, which would cause an
5216 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005217 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005218 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005219 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005220
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005222 return 0;
5223
Willy Tarreaud98cf932009-12-27 22:54:55 +01005224 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005225 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005226 if (s->listener->counters)
5227 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005228 return_bad_req_stats_ok:
5229 txn->req.msg_state = HTTP_MSG_ERROR;
5230 if (txn->status) {
5231 /* Note: we don't send any error if some data were already sent */
5232 stream_int_retnclose(req->prod, NULL);
5233 } else {
5234 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005235 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005236 }
5237 req->analysers = 0;
5238 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005239
5240 if (!(s->flags & SN_ERR_MASK))
5241 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005242 if (!(s->flags & SN_FINST_MASK)) {
5243 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5244 s->flags |= SN_FINST_H;
5245 else
5246 s->flags |= SN_FINST_D;
5247 }
5248 return 0;
5249
5250 aborted_xfer:
5251 txn->req.msg_state = HTTP_MSG_ERROR;
5252 if (txn->status) {
5253 /* Note: we don't send any error if some data were already sent */
5254 stream_int_retnclose(req->prod, NULL);
5255 } else {
5256 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005257 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005258 }
5259 req->analysers = 0;
5260 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5261
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005262 s->fe->fe_counters.srv_aborts++;
5263 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005264 if (objt_server(s->target))
5265 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005266
5267 if (!(s->flags & SN_ERR_MASK))
5268 s->flags |= SN_ERR_SRVCL;
5269 if (!(s->flags & SN_FINST_MASK)) {
5270 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5271 s->flags |= SN_FINST_H;
5272 else
5273 s->flags |= SN_FINST_D;
5274 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005275 return 0;
5276}
5277
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005278/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5279 * processing can continue on next analysers, or zero if it either needs more
5280 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5281 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5282 * when it has nothing left to do, and may remove any analyser when it wants to
5283 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005284 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005285int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005286{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005287 struct http_txn *txn = &s->txn;
5288 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005289 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005290 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005291 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005292 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005293
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005294 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 +02005295 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005296 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005297 rep,
5298 rep->rex, rep->wex,
5299 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005300 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005301 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005302
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005303 /*
5304 * Now parse the partial (or complete) lines.
5305 * We will check the response syntax, and also join multi-line
5306 * headers. An index of all the lines will be elaborated while
5307 * parsing.
5308 *
5309 * For the parsing, we use a 28 states FSM.
5310 *
5311 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005312 * rep->buf->p = beginning of response
5313 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5314 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005315 * msg->eol = end of current header or line (LF or CRLF)
5316 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005317 */
5318
Willy Tarreau83e3af02009-12-28 17:39:57 +01005319 /* There's a protected area at the end of the buffer for rewriting
5320 * purposes. We don't want to start to parse the request if the
5321 * protected area is affected, because we may have to move processed
5322 * data later, which is much more complicated.
5323 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005324 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005325 if (unlikely(!channel_reserved(rep))) {
5326 /* some data has still not left the buffer, wake us once that's done */
5327 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5328 goto abort_response;
5329 channel_dont_close(rep);
5330 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005331 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005332 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005333 }
5334
Willy Tarreau379357a2013-06-08 12:55:46 +02005335 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5336 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5337 buffer_slow_realign(rep->buf);
5338
Willy Tarreau9b28e032012-10-12 23:49:43 +02005339 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005340 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005341 }
5342
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005343 /* 1: we might have to print this header in debug mode */
5344 if (unlikely((global.mode & MODE_DEBUG) &&
5345 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005346 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005347 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005348
Willy Tarreau9b28e032012-10-12 23:49:43 +02005349 sol = rep->buf->p;
5350 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005351 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005352
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005353 sol += hdr_idx_first_pos(&txn->hdr_idx);
5354 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005355
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005356 while (cur_idx) {
5357 eol = sol + txn->hdr_idx.v[cur_idx].len;
5358 debug_hdr("srvhdr", s, sol, eol);
5359 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5360 cur_idx = txn->hdr_idx.v[cur_idx].next;
5361 }
5362 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005363
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005364 /*
5365 * Now we quickly check if we have found a full valid response.
5366 * If not so, we check the FD and buffer states before leaving.
5367 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005368 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005369 * responses are checked first.
5370 *
5371 * Depending on whether the client is still there or not, we
5372 * may send an error response back or not. Note that normally
5373 * we should only check for HTTP status there, and check I/O
5374 * errors somewhere else.
5375 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005376
Willy Tarreau655dce92009-11-08 13:10:58 +01005377 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005378 /* Invalid response */
5379 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5380 /* we detected a parsing error. We want to archive this response
5381 * in the dedicated proxy area for later troubleshooting.
5382 */
5383 hdr_response_bad:
5384 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005385 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005386
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005387 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005388 if (objt_server(s->target)) {
5389 objt_server(s->target)->counters.failed_resp++;
5390 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005391 }
Willy Tarreau64648412010-03-05 10:41:54 +01005392 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005393 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005394 rep->analysers = 0;
5395 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005396 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005397 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005398 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005399
5400 if (!(s->flags & SN_ERR_MASK))
5401 s->flags |= SN_ERR_PRXCOND;
5402 if (!(s->flags & SN_FINST_MASK))
5403 s->flags |= SN_FINST_H;
5404
5405 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005406 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005407
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005408 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005409 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005410 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005411 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005412 goto hdr_response_bad;
5413 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005414
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005416 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005417 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005418 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005419 else if (txn->flags & TX_NOT_FIRST)
5420 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005421
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005422 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005423 if (objt_server(s->target)) {
5424 objt_server(s->target)->counters.failed_resp++;
5425 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005426 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005427
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005428 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429 rep->analysers = 0;
5430 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005431 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005432 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005433 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005434
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005435 if (!(s->flags & SN_ERR_MASK))
5436 s->flags |= SN_ERR_SRVCL;
5437 if (!(s->flags & SN_FINST_MASK))
5438 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005439 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005440 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005441
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005442 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005443 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005444 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005445 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005446 else if (txn->flags & TX_NOT_FIRST)
5447 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005448
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005449 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005450 if (objt_server(s->target)) {
5451 objt_server(s->target)->counters.failed_resp++;
5452 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005453 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005454
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005455 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005456 rep->analysers = 0;
5457 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005458 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005459 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005460 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005461
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005462 if (!(s->flags & SN_ERR_MASK))
5463 s->flags |= SN_ERR_SRVTO;
5464 if (!(s->flags & SN_FINST_MASK))
5465 s->flags |= SN_FINST_H;
5466 return 0;
5467 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005468
Willy Tarreauf003d372012-11-26 13:35:37 +01005469 /* client abort with an abortonclose */
5470 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5471 s->fe->fe_counters.cli_aborts++;
5472 s->be->be_counters.cli_aborts++;
5473 if (objt_server(s->target))
5474 objt_server(s->target)->counters.cli_aborts++;
5475
5476 rep->analysers = 0;
5477 channel_auto_close(rep);
5478
5479 txn->status = 400;
5480 bi_erase(rep);
5481 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5482
5483 if (!(s->flags & SN_ERR_MASK))
5484 s->flags |= SN_ERR_CLICL;
5485 if (!(s->flags & SN_FINST_MASK))
5486 s->flags |= SN_FINST_H;
5487
5488 /* process_session() will take care of the error */
5489 return 0;
5490 }
5491
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005492 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005493 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005494 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005495 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005496 else if (txn->flags & TX_NOT_FIRST)
5497 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005498
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005499 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005500 if (objt_server(s->target)) {
5501 objt_server(s->target)->counters.failed_resp++;
5502 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005503 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005504
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005505 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005506 rep->analysers = 0;
5507 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005508 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005509 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005510 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005511
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005512 if (!(s->flags & SN_ERR_MASK))
5513 s->flags |= SN_ERR_SRVCL;
5514 if (!(s->flags & SN_FINST_MASK))
5515 s->flags |= SN_FINST_H;
5516 return 0;
5517 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005518
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005519 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005520 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005521 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005522 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005523 else if (txn->flags & TX_NOT_FIRST)
5524 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005525
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005526 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005527 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005528 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005529
5530 if (!(s->flags & SN_ERR_MASK))
5531 s->flags |= SN_ERR_CLICL;
5532 if (!(s->flags & SN_FINST_MASK))
5533 s->flags |= SN_FINST_H;
5534
5535 /* process_session() will take care of the error */
5536 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005537 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005538
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005539 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005540 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005541 return 0;
5542 }
5543
5544 /* More interesting part now : we know that we have a complete
5545 * response which at least looks like HTTP. We have an indicator
5546 * of each header's length, so we can parse them quickly.
5547 */
5548
5549 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005550 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005551
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005552 /*
5553 * 1: get the status code
5554 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005555 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005556 if (n < 1 || n > 5)
5557 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005558 /* when the client triggers a 4xx from the server, it's most often due
5559 * to a missing object or permission. These events should be tracked
5560 * because if they happen often, it may indicate a brute force or a
5561 * vulnerability scan.
5562 */
5563 if (n == 4)
5564 session_inc_http_err_ctr(s);
5565
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005566 if (objt_server(s->target))
5567 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005568
Willy Tarreau5b154472009-12-21 20:11:07 +01005569 /* check if the response is HTTP/1.1 or above */
5570 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005571 ((rep->buf->p[5] > '1') ||
5572 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005573 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005574
5575 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005576 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 +01005577
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005578 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005579 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005580
Willy Tarreau9b28e032012-10-12 23:49:43 +02005581 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005582
Willy Tarreau39650402010-03-15 19:44:39 +01005583 /* Adjust server's health based on status code. Note: status codes 501
5584 * and 505 are triggered on demand by client request, so we must not
5585 * count them as server failures.
5586 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005587 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005588 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005589 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005590 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005591 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005592 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005593
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005594 /*
5595 * 2: check for cacheability.
5596 */
5597
5598 switch (txn->status) {
5599 case 200:
5600 case 203:
5601 case 206:
5602 case 300:
5603 case 301:
5604 case 410:
5605 /* RFC2616 @13.4:
5606 * "A response received with a status code of
5607 * 200, 203, 206, 300, 301 or 410 MAY be stored
5608 * by a cache (...) unless a cache-control
5609 * directive prohibits caching."
5610 *
5611 * RFC2616 @9.5: POST method :
5612 * "Responses to this method are not cacheable,
5613 * unless the response includes appropriate
5614 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005615 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005616 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005617 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005618 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5619 break;
5620 default:
5621 break;
5622 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005624 /*
5625 * 3: we may need to capture headers
5626 */
5627 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005628 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005629 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005630 txn->rsp.cap, s->fe->rsp_cap);
5631
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005632 /* 4: determine the transfer-length.
5633 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5634 * the presence of a message-body in a RESPONSE and its transfer length
5635 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005636 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005637 * All responses to the HEAD request method MUST NOT include a
5638 * message-body, even though the presence of entity-header fields
5639 * might lead one to believe they do. All 1xx (informational), 204
5640 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5641 * message-body. All other responses do include a message-body,
5642 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005643 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005644 * 1. Any response which "MUST NOT" include a message-body (such as the
5645 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5646 * always terminated by the first empty line after the header fields,
5647 * regardless of the entity-header fields present in the message.
5648 *
5649 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5650 * the "chunked" transfer-coding (Section 6.2) is used, the
5651 * transfer-length is defined by the use of this transfer-coding.
5652 * If a Transfer-Encoding header field is present and the "chunked"
5653 * transfer-coding is not present, the transfer-length is defined by
5654 * the sender closing the connection.
5655 *
5656 * 3. If a Content-Length header field is present, its decimal value in
5657 * OCTETs represents both the entity-length and the transfer-length.
5658 * If a message is received with both a Transfer-Encoding header
5659 * field and a Content-Length header field, the latter MUST be ignored.
5660 *
5661 * 4. If the message uses the media type "multipart/byteranges", and
5662 * the transfer-length is not otherwise specified, then this self-
5663 * delimiting media type defines the transfer-length. This media
5664 * type MUST NOT be used unless the sender knows that the recipient
5665 * can parse it; the presence in a request of a Range header with
5666 * multiple byte-range specifiers from a 1.1 client implies that the
5667 * client can parse multipart/byteranges responses.
5668 *
5669 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005670 */
5671
5672 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005673 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005674 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005675 * FIXME: should we parse anyway and return an error on chunked encoding ?
5676 */
5677 if (txn->meth == HTTP_METH_HEAD ||
5678 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005679 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005680 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005681 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005682 goto skip_content_length;
5683 }
5684
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005685 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005686 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005687 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005688 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005689 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005690 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5691 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005692 /* bad transfer-encoding (chunked followed by something else) */
5693 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005694 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005695 break;
5696 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005697 }
5698
5699 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5700 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005701 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005702 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005703 signed long long cl;
5704
Willy Tarreauad14f752011-09-02 20:33:27 +02005705 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005706 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005707 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005708 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005709
Willy Tarreauad14f752011-09-02 20:33:27 +02005710 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005711 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005712 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005713 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005714
Willy Tarreauad14f752011-09-02 20:33:27 +02005715 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005716 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005717 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005718 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005719
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005720 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005721 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005722 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005723 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005724
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005725 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005726 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005727 }
5728
William Lallemand82fe75c2012-10-23 10:25:10 +02005729 if (s->fe->comp || s->be->comp)
5730 select_compression_response_header(s, rep->buf);
5731
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005732 /* FIXME: we should also implement the multipart/byterange method.
5733 * For now on, we resort to close mode in this case (unknown length).
5734 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005735skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005736
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005737 /* end of job, return OK */
5738 rep->analysers &= ~an_bit;
5739 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005740 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005741 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005742
5743 abort_keep_alive:
5744 /* A keep-alive request to the server failed on a network error.
5745 * The client is required to retry. We need to close without returning
5746 * any other information so that the client retries.
5747 */
5748 txn->status = 0;
5749 rep->analysers = 0;
5750 s->req->analysers = 0;
5751 channel_auto_close(rep);
5752 s->logs.logwait = 0;
5753 s->logs.level = 0;
5754 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5755 bi_erase(rep);
5756 stream_int_retnclose(rep->cons, NULL);
5757 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005758}
5759
5760/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005761 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5762 * and updates t->rep->analysers. It might make sense to explode it into several
5763 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005764 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005765int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005766{
5767 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 struct http_msg *msg = &txn->rsp;
5769 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005770 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005771 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005772
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005773 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 +02005774 now_ms, __FUNCTION__,
5775 t,
5776 rep,
5777 rep->rex, rep->wex,
5778 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005779 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005780 rep->analysers);
5781
Willy Tarreau655dce92009-11-08 13:10:58 +01005782 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005783 return 0;
5784
5785 rep->analysers &= ~an_bit;
5786 rep->analyse_exp = TICK_ETERNITY;
5787
Willy Tarreau5b154472009-12-21 20:11:07 +01005788 /* Now we have to check if we need to modify the Connection header.
5789 * This is more difficult on the response than it is on the request,
5790 * because we can have two different HTTP versions and we don't know
5791 * how the client will interprete a response. For instance, let's say
5792 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5793 * HTTP/1.1 response without any header. Maybe it will bound itself to
5794 * HTTP/1.0 because it only knows about it, and will consider the lack
5795 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5796 * the lack of header as a keep-alive. Thus we will use two flags
5797 * indicating how a request MAY be understood by the client. In case
5798 * of multiple possibilities, we'll fix the header to be explicit. If
5799 * ambiguous cases such as both close and keepalive are seen, then we
5800 * will fall back to explicit close. Note that we won't take risks with
5801 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005802 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005803 */
5804
Willy Tarreaudc008c52010-02-01 16:20:08 +01005805 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5806 txn->status == 101)) {
5807 /* Either we've established an explicit tunnel, or we're
5808 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005809 * to understand the next protocols. We have to switch to tunnel
5810 * mode, so that we transfer the request and responses then let
5811 * this protocol pass unmodified. When we later implement specific
5812 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005813 * header which contains information about that protocol for
5814 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005815 */
5816 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5817 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005818 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5819 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005820 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5821 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005822 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005823
Willy Tarreau70dffda2014-01-30 03:07:23 +01005824 /* this situation happens when combining pretend-keepalive with httpclose. */
5825 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5826 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5827 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5828 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5829
Willy Tarreau60466522010-01-18 19:08:45 +01005830 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005831 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005832 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5833 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005834
Willy Tarreau60466522010-01-18 19:08:45 +01005835 /* now adjust header transformations depending on current state */
5836 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5837 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5838 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005839 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005840 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005841 }
Willy Tarreau60466522010-01-18 19:08:45 +01005842 else { /* SCL / KAL */
5843 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005844 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005845 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005847
Willy Tarreau60466522010-01-18 19:08:45 +01005848 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005849 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005850
Willy Tarreau60466522010-01-18 19:08:45 +01005851 /* Some keep-alive responses are converted to Server-close if
5852 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005853 */
Willy Tarreau60466522010-01-18 19:08:45 +01005854 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5855 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005856 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005857 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005858 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005859 }
5860
Willy Tarreau7959a552013-09-23 16:44:27 +02005861 /* we want to have the response time before we start processing it */
5862 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005864 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005865 /*
5866 * 3: we will have to evaluate the filters.
5867 * As opposed to version 1.2, now they will be evaluated in the
5868 * filters order and not in the header order. This means that
5869 * each filter has to be validated among all headers.
5870 *
5871 * Filters are tried with ->be first, then with ->fe if it is
5872 * different from ->be.
5873 */
5874
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005875 cur_proxy = t->be;
5876 while (1) {
5877 struct proxy *rule_set = cur_proxy;
5878
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005879 /* evaluate http-response rules */
5880 if (!http_res_last_rule)
5881 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5882
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005883 /* try headers filters */
5884 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005885 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005886 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005887 if (objt_server(t->target)) {
5888 objt_server(t->target)->counters.failed_resp++;
5889 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005890 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005891 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005892 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005893 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005894 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005895 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005896 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005897 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005898 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005899 if (!(t->flags & SN_ERR_MASK))
5900 t->flags |= SN_ERR_PRXCOND;
5901 if (!(t->flags & SN_FINST_MASK))
5902 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005903 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005904 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005905 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005906
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005907 /* has the response been denied ? */
5908 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005909 if (objt_server(t->target))
5910 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005911
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005912 t->be->be_counters.denied_resp++;
5913 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005914 if (t->listener->counters)
5915 t->listener->counters->denied_resp++;
5916
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005917 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005918 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005919
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005920 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005921 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005922 if (txn->status < 200)
5923 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005924 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005925 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005926 ret = acl_pass(ret);
5927 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5928 ret = !ret;
5929 if (!ret)
5930 continue;
5931 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005932 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005933 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005934 }
5935
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005936 /* check whether we're already working on the frontend */
5937 if (cur_proxy == t->fe)
5938 break;
5939 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005940 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005941
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005942 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005943 * We may be facing a 100-continue response, in which case this
5944 * is not the right response, and we're waiting for the next one.
5945 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005946 * next one.
5947 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005948 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005949 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005950 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005951 msg->msg_state = HTTP_MSG_RPBEFORE;
5952 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005953 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005954 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5955 return 1;
5956 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005957 else if (unlikely(txn->status < 200))
5958 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005959
5960 /* we don't have any 1xx status code now */
5961
5962 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005963 * 4: check for server cookie.
5964 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005965 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5966 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005967 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005968
Willy Tarreaubaaee002006-06-26 02:48:02 +02005969
Willy Tarreaua15645d2007-03-18 16:22:39 +01005970 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005971 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005972 */
Willy Tarreau67402132012-05-31 20:40:20 +02005973 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005974 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005975
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005976 /*
5977 * 6: add server cookie in the response if needed
5978 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005979 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005980 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005981 (!(t->flags & SN_DIRECT) ||
5982 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5983 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5984 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5985 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005986 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005987 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005988 /* the server is known, it's not the one the client requested, or the
5989 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005990 * insert a set-cookie here, except if we want to insert only on POST
5991 * requests and this one isn't. Note that servers which don't have cookies
5992 * (eg: some backup servers) will return a full cookie removal request.
5993 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005994 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005995 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005996 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5997 t->be->cookie_name);
5998 }
5999 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006000 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006001
6002 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
6003 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006004 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6005 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6006 trash.len += 5;
6007
Willy Tarreauef4f3912010-10-07 21:00:29 +02006008 if (t->be->cookie_maxlife) {
6009 /* emit first_date, which is either the original one or
6010 * the current date.
6011 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006012 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006013 s30tob64(txn->cookie_first_date ?
6014 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006015 (date.tv_sec+3) >> 2, trash.str + trash.len);
6016 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006017 }
6018 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006019 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02006020 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006021
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006022 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006023 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006024
Willy Tarreau4992dd22012-05-31 21:02:17 +02006025 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006026 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006027
6028 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006029 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006031 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006032 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006033
Willy Tarreauf1348312010-10-07 15:54:11 +02006034 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006035 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02006036 /* the server did not change, only the date was updated */
6037 txn->flags |= TX_SCK_UPDATED;
6038 else
6039 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006040
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006041 /* Here, we will tell an eventual cache on the client side that we don't
6042 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6043 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6044 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
6045 */
Willy Tarreau67402132012-05-31 20:40:20 +02006046 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02006047
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006048 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6049
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006050 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01006051 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006052 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006053 }
6054 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006055
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006056 /*
6057 * 7: check if result will be cacheable with a cookie.
6058 * We'll block the response if security checks have caught
6059 * nasty things such as a cacheable cookie.
6060 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006061 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6062 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006063 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006064
6065 /* we're in presence of a cacheable response containing
6066 * a set-cookie header. We'll block it as requested by
6067 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006068 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006069 if (objt_server(t->target))
6070 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006071
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006072 t->be->be_counters.denied_resp++;
6073 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006074 if (t->listener->counters)
6075 t->listener->counters->denied_resp++;
6076
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006077 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006078 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006079 send_log(t->be, LOG_ALERT,
6080 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006081 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006082 goto return_srv_prx_502;
6083 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006084
6085 /*
Willy Tarreau60466522010-01-18 19:08:45 +01006086 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01006087 * If an "Upgrade" token is found, the header is left untouched in order
6088 * not to have to deal with some client bugs : some of them fail an upgrade
6089 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006090 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006091 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6092 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01006093 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6094 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006095 unsigned int want_flags = 0;
6096
6097 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6098 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6099 /* we want a keep-alive response here. Keep-alive header
6100 * required if either side is not 1.1.
6101 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006102 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006103 want_flags |= TX_CON_KAL_SET;
6104 }
6105 else {
6106 /* we want a close response here. Close header required if
6107 * the server is 1.1, regardless of the client.
6108 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006109 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006110 want_flags |= TX_CON_CLO_SET;
6111 }
6112
6113 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006114 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006115 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006116
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006117 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006118 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006119 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006120 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006121
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006122 /*************************************************************
6123 * OK, that's finished for the headers. We have done what we *
6124 * could. Let's switch to the DATA state. *
6125 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006126
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006127 /* if the user wants to log as soon as possible, without counting
6128 * bytes from the server, then this is the right moment. We have
6129 * to temporarily assign bytes_out to log what we currently have.
6130 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006131 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006132 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6133 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006134 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006135 t->logs.bytes_out = 0;
6136 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006137
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006138 /* Note: we must not try to cheat by jumping directly to DATA,
6139 * otherwise we would not let the client side wake up.
6140 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006141
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006142 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006143 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006144 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006145}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006146
Willy Tarreaud98cf932009-12-27 22:54:55 +01006147/* This function is an analyser which forwards response body (including chunk
6148 * sizes if any). It is called as soon as we must forward, even if we forward
6149 * zero byte. The only situation where it must not be called is when we're in
6150 * tunnel mode and we want to forward till the close. It's used both to forward
6151 * remaining data and to resync after end of body. It expects the msg_state to
6152 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6153 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006154 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006155 * bytes of pending data + the headers if not already done (between sol and sov).
6156 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006157 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006158int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006159{
6160 struct http_txn *txn = &s->txn;
6161 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006162 static struct buffer *tmpbuf = NULL;
6163 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006164 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006165 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006166
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006167 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6168 return 0;
6169
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006170 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006171 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006172 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006173 /* Output closed while we were sending data. We must abort and
6174 * wake the other side up.
6175 */
6176 msg->msg_state = HTTP_MSG_ERROR;
6177 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006178 return 1;
6179 }
6180
Willy Tarreau4fe41902010-06-07 22:27:41 +02006181 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006182 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006183
William Lallemand82fe75c2012-10-23 10:25:10 +02006184 /* this is the first time we need the compression buffer */
6185 if (s->comp_algo != NULL && tmpbuf == NULL) {
6186 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6187 goto aborted_xfer; /* no memory */
6188 }
6189
Willy Tarreaud98cf932009-12-27 22:54:55 +01006190 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006191 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02006192 * res->buf.p still points to the beginning of the message. We
6193 * forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006194 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006195 channel_forward(res, msg->sov);
6196 msg->next = 0;
6197 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006198
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006199 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006200 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006201 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006202 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006203 }
6204
William Lallemand82fe75c2012-10-23 10:25:10 +02006205 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006206 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006207 if (ret < 0) {
6208 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006209 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006210 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006211 compressing = 1;
6212 }
6213
Willy Tarreaud98cf932009-12-27 22:54:55 +01006214 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006215 http_silent_debug(__LINE__, s);
Willy Tarreau877e78d2013-04-07 18:48:08 +02006216
6217 /* we may have some pending data starting at res->buf->p */
William Lallemand82fe75c2012-10-23 10:25:10 +02006218 if (s->comp_algo == NULL) {
Willy Tarreau877e78d2013-04-07 18:48:08 +02006219 if (msg->chunk_len || msg->sov) {
6220 msg->chunk_len += msg->sov;
William Lallemand82fe75c2012-10-23 10:25:10 +02006221 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreau877e78d2013-04-07 18:48:08 +02006222 msg->next -= msg->sov;
6223 msg->sov = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02006224 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006225 }
6226
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006227 switch (msg->msg_state - HTTP_MSG_DATA) {
6228 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006229 if (compressing) {
6230 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6231 if (ret < 0)
6232 goto aborted_xfer;
6233 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006234
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006235 if (res->to_forward || msg->chunk_len) {
6236 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006237 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006238 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006239
6240 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006241 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006242 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006243 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006244 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006245 if (compressing && consumed_data) {
6246 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6247 compressing = 0;
6248 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006249 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006250 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006251 /* fall through for HTTP_MSG_CHUNK_CRLF */
6252
6253 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6254 /* we want the CRLF after the data */
6255
6256 ret = http_skip_chunk_crlf(msg);
6257 if (ret == 0)
6258 goto missing_data;
6259 else if (ret < 0) {
6260 if (msg->err_pos >= 0)
6261 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6262 goto return_bad_res;
6263 }
6264 /* skipping data in buffer for compression */
6265 if (compressing) {
6266 b_adv(res->buf, msg->next);
6267 msg->next = 0;
6268 msg->sov = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006269 }
6270 /* we're in MSG_CHUNK_SIZE now, fall through */
6271
6272 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006273 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006274 * set ->sov and ->next to point to the body and switch to DATA or
6275 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006276 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006277
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006278 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006279 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006280 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006281 else if (ret < 0) {
6282 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006283 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006284 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006285 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006286 if (compressing) {
6287 if (likely(msg->chunk_len > 0)) {
6288 /* skipping data if we are in compression mode */
6289 b_adv(res->buf, msg->next);
6290 msg->next = 0;
6291 msg->sov = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006292 } else {
6293 if (consumed_data) {
6294 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6295 compressing = 0;
6296 }
6297 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006298 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006299 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006300 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006301
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006302 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6303 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006304 if (ret == 0)
6305 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006306 else if (ret < 0) {
6307 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006308 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006309 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006310 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006311 if (s->comp_algo != NULL) {
6312 /* forwarding trailers */
6313 channel_forward(res, msg->next);
6314 msg->next = 0;
6315 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006316 /* we're in HTTP_MSG_DONE now, but we might still have
6317 * some data pending, so let's loop over once.
6318 */
6319 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006320
6321 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006322 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006323
6324 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006325 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006326 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6327 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006328 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006329 if (http_resync_states(s)) {
6330 http_silent_debug(__LINE__, s);
6331 /* some state changes occurred, maybe the analyser
6332 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006333 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006334 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006335 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006336 /* response errors are most likely due to
6337 * the client aborting the transfer.
6338 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006339 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006340 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006341 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006342 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006343 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006344 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006345 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006346 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006347 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006348 }
6349 }
6350
Willy Tarreaud98cf932009-12-27 22:54:55 +01006351 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006352 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006353 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6354 compressing = 0;
6355 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006356
6357 if (res->flags & CF_SHUTW)
6358 goto aborted_xfer;
6359
6360 /* stop waiting for data if the input is closed before the end. If the
6361 * client side was already closed, it means that the client has aborted,
6362 * so we don't want to count this as a server abort. Otherwise it's a
6363 * server abort.
6364 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006365 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006366 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6367 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006368 if (!(s->flags & SN_ERR_MASK))
6369 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006370 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006371 if (objt_server(s->target))
6372 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006373 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006374 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006375
Willy Tarreau40dba092010-03-04 18:14:51 +01006376 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006377 if (!s->req->analysers)
6378 goto return_bad_res;
6379
Willy Tarreau877e78d2013-04-07 18:48:08 +02006380 /* forward any pending data starting at res->buf->p */
William Lallemand82fe75c2012-10-23 10:25:10 +02006381 if (s->comp_algo == NULL) {
Willy Tarreau877e78d2013-04-07 18:48:08 +02006382 if (msg->chunk_len || msg->sov) {
6383 msg->chunk_len += msg->sov;
William Lallemand82fe75c2012-10-23 10:25:10 +02006384 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreau877e78d2013-04-07 18:48:08 +02006385 msg->next -= msg->sov;
6386 msg->sov = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02006387 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006388 }
6389
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006390 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006391 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006392 * Similarly, with keep-alive on the client side, we don't want to forward a
6393 * close.
6394 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006395 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006396 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6397 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006398 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006399
Willy Tarreau5c620922011-05-11 19:56:11 +02006400 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006401 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006402 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006403 * modes are already handled by the stream sock layer. We must not do
6404 * this in content-length mode because it could present the MSG_MORE
6405 * flag with the last block of forwarded data, which would cause an
6406 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006407 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006408 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006409 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006410
Willy Tarreaud98cf932009-12-27 22:54:55 +01006411 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006412 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006413 return 0;
6414
Willy Tarreau40dba092010-03-04 18:14:51 +01006415 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006416 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006417 if (objt_server(s->target))
6418 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006419
6420 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006421 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006422 /* don't send any error message as we're in the body */
6423 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006424 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006425 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006426 if (objt_server(s->target))
6427 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006428
6429 if (!(s->flags & SN_ERR_MASK))
6430 s->flags |= SN_ERR_PRXCOND;
6431 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006432 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006433 return 0;
6434
6435 aborted_xfer:
6436 txn->rsp.msg_state = HTTP_MSG_ERROR;
6437 /* don't send any error message as we're in the body */
6438 stream_int_retnclose(res->cons, NULL);
6439 res->analysers = 0;
6440 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6441
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006442 s->fe->fe_counters.cli_aborts++;
6443 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006444 if (objt_server(s->target))
6445 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006446
6447 if (!(s->flags & SN_ERR_MASK))
6448 s->flags |= SN_ERR_CLICL;
6449 if (!(s->flags & SN_FINST_MASK))
6450 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006451 return 0;
6452}
6453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454/* Iterate the same filter through all request headers.
6455 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006456 * Since it can manage the switch to another backend, it updates the per-proxy
6457 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006458 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006459int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006461 char term;
6462 char *cur_ptr, *cur_end, *cur_next;
6463 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006464 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006466 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 last_hdr = 0;
6469
Willy Tarreau9b28e032012-10-12 23:49:43 +02006470 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006471 old_idx = 0;
6472
6473 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006474 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006476 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 (exp->action == ACT_ALLOW ||
6478 exp->action == ACT_DENY ||
6479 exp->action == ACT_TARPIT))
6480 return 0;
6481
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006482 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 if (!cur_idx)
6484 break;
6485
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006486 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006487 cur_ptr = cur_next;
6488 cur_end = cur_ptr + cur_hdr->len;
6489 cur_next = cur_end + cur_hdr->cr + 1;
6490
6491 /* Now we have one header between cur_ptr and cur_end,
6492 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006493 */
6494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006495 /* The annoying part is that pattern matching needs
6496 * that we modify the contents to null-terminate all
6497 * strings before testing them.
6498 */
6499
6500 term = *cur_end;
6501 *cur_end = '\0';
6502
6503 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6504 switch (exp->action) {
6505 case ACT_SETBE:
6506 /* It is not possible to jump a second time.
6507 * FIXME: should we return an HTTP/500 here so that
6508 * the admin knows there's a problem ?
6509 */
6510 if (t->be != t->fe)
6511 break;
6512
6513 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006514 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006515 last_hdr = 1;
6516 break;
6517
6518 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006519 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 last_hdr = 1;
6521 break;
6522
6523 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006524 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 break;
6527
6528 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006529 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 break;
6532
6533 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006534 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6535 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 /* FIXME: if the user adds a newline in the replacement, the
6537 * index will not be recalculated for now, and the new line
6538 * will not be counted as a new header.
6539 */
6540
6541 cur_end += delta;
6542 cur_next += delta;
6543 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006544 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006545 break;
6546
6547 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006548 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006549 cur_next += delta;
6550
Willy Tarreaufa355d42009-11-29 18:12:29 +01006551 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006552 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6553 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006554 cur_hdr->len = 0;
6555 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006556 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006557 break;
6558
6559 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561 if (cur_end)
6562 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 /* keep the link from this header to next one in case of later
6565 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006566 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 old_idx = cur_idx;
6568 }
6569 return 0;
6570}
6571
6572
6573/* Apply the filter to the request line.
6574 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6575 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576 * Since it can manage the switch to another backend, it updates the per-proxy
6577 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006578 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006579int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580{
6581 char term;
6582 char *cur_ptr, *cur_end;
6583 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006584 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006585 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006586
Willy Tarreau3d300592007-03-18 18:34:41 +01006587 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006588 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006589 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006590 (exp->action == ACT_ALLOW ||
6591 exp->action == ACT_DENY ||
6592 exp->action == ACT_TARPIT))
6593 return 0;
6594 else if (exp->action == ACT_REMOVE)
6595 return 0;
6596
6597 done = 0;
6598
Willy Tarreau9b28e032012-10-12 23:49:43 +02006599 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006600 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006601
6602 /* Now we have the request line between cur_ptr and cur_end */
6603
6604 /* The annoying part is that pattern matching needs
6605 * that we modify the contents to null-terminate all
6606 * strings before testing them.
6607 */
6608
6609 term = *cur_end;
6610 *cur_end = '\0';
6611
6612 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6613 switch (exp->action) {
6614 case ACT_SETBE:
6615 /* It is not possible to jump a second time.
6616 * FIXME: should we return an HTTP/500 here so that
6617 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006618 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006619 if (t->be != t->fe)
6620 break;
6621
6622 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006623 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006624 done = 1;
6625 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006627 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006628 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006629 done = 1;
6630 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006632 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006633 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006634 done = 1;
6635 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006637 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006638 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006639 done = 1;
6640 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006642 case ACT_REPLACE:
6643 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006644 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6645 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 /* FIXME: if the user adds a newline in the replacement, the
6647 * index will not be recalculated for now, and the new line
6648 * will not be counted as a new header.
6649 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006650
Willy Tarreaufa355d42009-11-29 18:12:29 +01006651 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006652 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006653 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006654 HTTP_MSG_RQMETH,
6655 cur_ptr, cur_end + 1,
6656 NULL, NULL);
6657 if (unlikely(!cur_end))
6658 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006660 /* we have a full request and we know that we have either a CR
6661 * or an LF at <ptr>.
6662 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006663 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6664 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006665 /* there is no point trying this regex on headers */
6666 return 1;
6667 }
6668 }
6669 *cur_end = term; /* restore the string terminator */
6670 return done;
6671}
Willy Tarreau97de6242006-12-27 17:18:38 +01006672
Willy Tarreau58f10d72006-12-04 02:26:12 +01006673
Willy Tarreau58f10d72006-12-04 02:26:12 +01006674
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006675/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006676 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006677 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006678 * unparsable request. Since it can manage the switch to another backend, it
6679 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006680 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006681int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006682{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006683 struct http_txn *txn = &s->txn;
6684 struct hdr_exp *exp;
6685
6686 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006687 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006689 /*
6690 * The interleaving of transformations and verdicts
6691 * makes it difficult to decide to continue or stop
6692 * the evaluation.
6693 */
6694
Willy Tarreau6c123b12010-01-28 20:22:06 +01006695 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6696 break;
6697
Willy Tarreau3d300592007-03-18 18:34:41 +01006698 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006699 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006700 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006701 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006702
6703 /* if this filter had a condition, evaluate it now and skip to
6704 * next filter if the condition does not match.
6705 */
6706 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006707 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006708 ret = acl_pass(ret);
6709 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6710 ret = !ret;
6711
6712 if (!ret)
6713 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006714 }
6715
6716 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006717 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006718 if (unlikely(ret < 0))
6719 return -1;
6720
6721 if (likely(ret == 0)) {
6722 /* The filter did not match the request, it can be
6723 * iterated through all headers.
6724 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006725 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006726 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006727 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006728 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006729}
6730
6731
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732
Willy Tarreau58f10d72006-12-04 02:26:12 +01006733/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006734 * Try to retrieve the server associated to the appsession.
6735 * If the server is found, it's assigned to the session.
6736 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006737void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006738 struct http_txn *txn = &t->txn;
6739 appsess *asession = NULL;
6740 char *sessid_temp = NULL;
6741
Cyril Bontéb21570a2009-11-29 20:04:48 +01006742 if (len > t->be->appsession_len) {
6743 len = t->be->appsession_len;
6744 }
6745
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006746 if (t->be->options2 & PR_O2_AS_REQL) {
6747 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006748 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006749 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006750 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006751 }
6752
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006753 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006754 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6755 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6756 return;
6757 }
6758
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006759 memcpy(txn->sessid, buf, len);
6760 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006761 }
6762
6763 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6764 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6765 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6766 return;
6767 }
6768
Cyril Bontéb21570a2009-11-29 20:04:48 +01006769 memcpy(sessid_temp, buf, len);
6770 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006771
6772 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6773 /* free previously allocated memory */
6774 pool_free2(apools.sessid, sessid_temp);
6775
6776 if (asession != NULL) {
6777 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6778 if (!(t->be->options2 & PR_O2_AS_REQL))
6779 asession->request_count++;
6780
6781 if (asession->serverid != NULL) {
6782 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006783
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006784 while (srv) {
6785 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006786 if ((srv->state & SRV_RUNNING) ||
6787 (t->be->options & PR_O_PERSIST) ||
6788 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006789 /* we found the server and it's usable */
6790 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006791 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006792 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006793 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006794
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006795 break;
6796 } else {
6797 txn->flags &= ~TX_CK_MASK;
6798 txn->flags |= TX_CK_DOWN;
6799 }
6800 }
6801 srv = srv->next;
6802 }
6803 }
6804 }
6805}
6806
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006807/* Find the end of a cookie value contained between <s> and <e>. It works the
6808 * same way as with headers above except that the semi-colon also ends a token.
6809 * See RFC2965 for more information. Note that it requires a valid header to
6810 * return a valid result.
6811 */
6812char *find_cookie_value_end(char *s, const char *e)
6813{
6814 int quoted, qdpair;
6815
6816 quoted = qdpair = 0;
6817 for (; s < e; s++) {
6818 if (qdpair) qdpair = 0;
6819 else if (quoted) {
6820 if (*s == '\\') qdpair = 1;
6821 else if (*s == '"') quoted = 0;
6822 }
6823 else if (*s == '"') quoted = 1;
6824 else if (*s == ',' || *s == ';') return s;
6825 }
6826 return s;
6827}
6828
6829/* Delete a value in a header between delimiters <from> and <next> in buffer
6830 * <buf>. The number of characters displaced is returned, and the pointer to
6831 * the first delimiter is updated if required. The function tries as much as
6832 * possible to respect the following principles :
6833 * - replace <from> delimiter by the <next> one unless <from> points to a
6834 * colon, in which case <next> is simply removed
6835 * - set exactly one space character after the new first delimiter, unless
6836 * there are not enough characters in the block being moved to do so.
6837 * - remove unneeded spaces before the previous delimiter and after the new
6838 * one.
6839 *
6840 * It is the caller's responsibility to ensure that :
6841 * - <from> points to a valid delimiter or the colon ;
6842 * - <next> points to a valid delimiter or the final CR/LF ;
6843 * - there are non-space chars before <from> ;
6844 * - there is a CR/LF at or after <next>.
6845 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006846int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006847{
6848 char *prev = *from;
6849
6850 if (*prev == ':') {
6851 /* We're removing the first value, preserve the colon and add a
6852 * space if possible.
6853 */
6854 if (!http_is_crlf[(unsigned char)*next])
6855 next++;
6856 prev++;
6857 if (prev < next)
6858 *prev++ = ' ';
6859
6860 while (http_is_spht[(unsigned char)*next])
6861 next++;
6862 } else {
6863 /* Remove useless spaces before the old delimiter. */
6864 while (http_is_spht[(unsigned char)*(prev-1)])
6865 prev--;
6866 *from = prev;
6867
6868 /* copy the delimiter and if possible a space if we're
6869 * not at the end of the line.
6870 */
6871 if (!http_is_crlf[(unsigned char)*next]) {
6872 *prev++ = *next++;
6873 if (prev + 1 < next)
6874 *prev++ = ' ';
6875 while (http_is_spht[(unsigned char)*next])
6876 next++;
6877 }
6878 }
6879 return buffer_replace2(buf, prev, next, NULL, 0);
6880}
6881
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006882/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006883 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006884 * desirable to call it only when needed. This code is quite complex because
6885 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6886 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006887 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006888void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006890 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006892 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006893 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6894 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006895
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006897 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006898 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006899
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006900 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006901 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006902 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006903
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006904 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 hdr_beg = hdr_next;
6906 hdr_end = hdr_beg + cur_hdr->len;
6907 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 /* We have one full header between hdr_beg and hdr_end, and the
6910 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006911 * "Cookie:" headers.
6912 */
6913
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006915 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006916 old_idx = cur_idx;
6917 continue;
6918 }
6919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 del_from = NULL; /* nothing to be deleted */
6921 preserve_hdr = 0; /* assume we may kill the whole header */
6922
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923 /* Now look for cookies. Conforming to RFC2109, we have to support
6924 * attributes whose name begin with a '$', and associate them with
6925 * the right cookie, if we want to delete this cookie.
6926 * So there are 3 cases for each cookie read :
6927 * 1) it's a special attribute, beginning with a '$' : ignore it.
6928 * 2) it's a server id cookie that we *MAY* want to delete : save
6929 * some pointers on it (last semi-colon, beginning of cookie...)
6930 * 3) it's an application cookie : we *MAY* have to delete a previous
6931 * "special" cookie.
6932 * At the end of loop, if a "special" cookie remains, we may have to
6933 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006934 * *MUST* delete it.
6935 *
6936 * Note: RFC2965 is unclear about the processing of spaces around
6937 * the equal sign in the ATTR=VALUE form. A careful inspection of
6938 * the RFC explicitly allows spaces before it, and not within the
6939 * tokens (attrs or values). An inspection of RFC2109 allows that
6940 * too but section 10.1.3 lets one think that spaces may be allowed
6941 * after the equal sign too, resulting in some (rare) buggy
6942 * implementations trying to do that. So let's do what servers do.
6943 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6944 * allowed quoted strings in values, with any possible character
6945 * after a backslash, including control chars and delimitors, which
6946 * causes parsing to become ambiguous. Browsers also allow spaces
6947 * within values even without quotes.
6948 *
6949 * We have to keep multiple pointers in order to support cookie
6950 * removal at the beginning, middle or end of header without
6951 * corrupting the header. All of these headers are valid :
6952 *
6953 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6954 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6955 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6956 * | | | | | | | | |
6957 * | | | | | | | | hdr_end <--+
6958 * | | | | | | | +--> next
6959 * | | | | | | +----> val_end
6960 * | | | | | +-----------> val_beg
6961 * | | | | +--------------> equal
6962 * | | | +----------------> att_end
6963 * | | +---------------------> att_beg
6964 * | +--------------------------> prev
6965 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006966 */
6967
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006968 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6969 /* Iterate through all cookies on this line */
6970
6971 /* find att_beg */
6972 att_beg = prev + 1;
6973 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6974 att_beg++;
6975
6976 /* find att_end : this is the first character after the last non
6977 * space before the equal. It may be equal to hdr_end.
6978 */
6979 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006980
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 while (equal < hdr_end) {
6982 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006983 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006984 if (http_is_spht[(unsigned char)*equal++])
6985 continue;
6986 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006987 }
6988
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006989 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6990 * is between <att_beg> and <equal>, both may be identical.
6991 */
6992
6993 /* look for end of cookie if there is an equal sign */
6994 if (equal < hdr_end && *equal == '=') {
6995 /* look for the beginning of the value */
6996 val_beg = equal + 1;
6997 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6998 val_beg++;
6999
7000 /* find the end of the value, respecting quotes */
7001 next = find_cookie_value_end(val_beg, hdr_end);
7002
7003 /* make val_end point to the first white space or delimitor after the value */
7004 val_end = next;
7005 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7006 val_end--;
7007 } else {
7008 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007009 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007010
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007011 /* We have nothing to do with attributes beginning with '$'. However,
7012 * they will automatically be removed if a header before them is removed,
7013 * since they're supposed to be linked together.
7014 */
7015 if (*att_beg == '$')
7016 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007017
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007018 /* Ignore cookies with no equal sign */
7019 if (equal == next) {
7020 /* This is not our cookie, so we must preserve it. But if we already
7021 * scheduled another cookie for removal, we cannot remove the
7022 * complete header, but we can remove the previous block itself.
7023 */
7024 preserve_hdr = 1;
7025 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007026 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 val_end += delta;
7028 next += delta;
7029 hdr_end += delta;
7030 hdr_next += delta;
7031 cur_hdr->len += delta;
7032 http_msg_move_end(&txn->req, delta);
7033 prev = del_from;
7034 del_from = NULL;
7035 }
7036 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007037 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007038
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007039 /* if there are spaces around the equal sign, we need to
7040 * strip them otherwise we'll get trouble for cookie captures,
7041 * or even for rewrites. Since this happens extremely rarely,
7042 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007044 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7045 int stripped_before = 0;
7046 int stripped_after = 0;
7047
7048 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007049 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007050 equal += stripped_before;
7051 val_beg += stripped_before;
7052 }
7053
7054 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007055 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007056 val_beg += stripped_after;
7057 stripped_before += stripped_after;
7058 }
7059
7060 val_end += stripped_before;
7061 next += stripped_before;
7062 hdr_end += stripped_before;
7063 hdr_next += stripped_before;
7064 cur_hdr->len += stripped_before;
7065 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007067 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007069 /* First, let's see if we want to capture this cookie. We check
7070 * that we don't already have a client side cookie, because we
7071 * can only capture one. Also as an optimisation, we ignore
7072 * cookies shorter than the declared name.
7073 */
7074 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7075 (val_end - att_beg >= t->fe->capture_namelen) &&
7076 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7077 int log_len = val_end - att_beg;
7078
7079 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7080 Alert("HTTP logging : out of memory.\n");
7081 } else {
7082 if (log_len > t->fe->capture_len)
7083 log_len = t->fe->capture_len;
7084 memcpy(txn->cli_cookie, att_beg, log_len);
7085 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007088
Willy Tarreaubca99692010-10-06 19:25:55 +02007089 /* Persistence cookies in passive, rewrite or insert mode have the
7090 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007091 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007092 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007093 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007094 * For cookies in prefix mode, the form is :
7095 *
7096 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 */
7098 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7099 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
7100 struct server *srv = t->be->srv;
7101 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7104 * have the server ID between val_beg and delim, and the original cookie between
7105 * delim+1 and val_end. Otherwise, delim==val_end :
7106 *
7107 * Cookie: NAME=SRV; # in all but prefix modes
7108 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7109 * | || || | |+-> next
7110 * | || || | +--> val_end
7111 * | || || +---------> delim
7112 * | || |+------------> val_beg
7113 * | || +-------------> att_end = equal
7114 * | |+-----------------> att_beg
7115 * | +------------------> prev
7116 * +-------------------------> hdr_beg
7117 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118
Willy Tarreau67402132012-05-31 20:40:20 +02007119 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007120 for (delim = val_beg; delim < val_end; delim++)
7121 if (*delim == COOKIE_DELIM)
7122 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007123 } else {
7124 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007125 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007126 /* Now check if the cookie contains a date field, which would
7127 * appear after a vertical bar ('|') just after the server name
7128 * and before the delimiter.
7129 */
7130 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7131 if (vbar1) {
7132 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007133 * right is the last seen date. It is a base64 encoded
7134 * 30-bit value representing the UNIX date since the
7135 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007136 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007137 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007138 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007139 if (val_end - vbar1 >= 5) {
7140 val = b64tos30(vbar1);
7141 if (val > 0)
7142 txn->cookie_last_date = val << 2;
7143 }
7144 /* look for a second vertical bar */
7145 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7146 if (vbar1 && (val_end - vbar1 > 5)) {
7147 val = b64tos30(vbar1 + 1);
7148 if (val > 0)
7149 txn->cookie_first_date = val << 2;
7150 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007151 }
7152 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007153
Willy Tarreauf64d1412010-10-07 20:06:11 +02007154 /* if the cookie has an expiration date and the proxy wants to check
7155 * it, then we do that now. We first check if the cookie is too old,
7156 * then only if it has expired. We detect strict overflow because the
7157 * time resolution here is not great (4 seconds). Cookies with dates
7158 * in the future are ignored if their offset is beyond one day. This
7159 * allows an admin to fix timezone issues without expiring everyone
7160 * and at the same time avoids keeping unwanted side effects for too
7161 * long.
7162 */
7163 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007164 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7165 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007166 txn->flags &= ~TX_CK_MASK;
7167 txn->flags |= TX_CK_OLD;
7168 delim = val_beg; // let's pretend we have not found the cookie
7169 txn->cookie_first_date = 0;
7170 txn->cookie_last_date = 0;
7171 }
7172 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007173 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7174 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007175 txn->flags &= ~TX_CK_MASK;
7176 txn->flags |= TX_CK_EXPIRED;
7177 delim = val_beg; // let's pretend we have not found the cookie
7178 txn->cookie_first_date = 0;
7179 txn->cookie_last_date = 0;
7180 }
7181
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007182 /* Here, we'll look for the first running server which supports the cookie.
7183 * This allows to share a same cookie between several servers, for example
7184 * to dedicate backup servers to specific servers only.
7185 * However, to prevent clients from sticking to cookie-less backup server
7186 * when they have incidentely learned an empty cookie, we simply ignore
7187 * empty cookies and mark them as invalid.
7188 * The same behaviour is applied when persistence must be ignored.
7189 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007190 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007191 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007192
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007193 while (srv) {
7194 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7195 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7196 if ((srv->state & SRV_RUNNING) ||
7197 (t->be->options & PR_O_PERSIST) ||
7198 (t->flags & SN_FORCE_PRST)) {
7199 /* we found the server and we can use it */
7200 txn->flags &= ~TX_CK_MASK;
7201 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7202 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007203 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007204 break;
7205 } else {
7206 /* we found a server, but it's down,
7207 * mark it as such and go on in case
7208 * another one is available.
7209 */
7210 txn->flags &= ~TX_CK_MASK;
7211 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007212 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007213 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007214 srv = srv->next;
7215 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007216
Willy Tarreauf64d1412010-10-07 20:06:11 +02007217 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007218 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007219 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007220 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7221 txn->flags |= TX_CK_UNUSED;
7222 else
7223 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007224 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007225
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007226 /* depending on the cookie mode, we may have to either :
7227 * - delete the complete cookie if we're in insert+indirect mode, so that
7228 * the server never sees it ;
7229 * - remove the server id from the cookie value, and tag the cookie as an
7230 * application cookie so that it does not get accidentely removed later,
7231 * if we're in cookie prefix mode
7232 */
Willy Tarreau67402132012-05-31 20:40:20 +02007233 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007234 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007235
Willy Tarreau9b28e032012-10-12 23:49:43 +02007236 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007237 val_end += delta;
7238 next += delta;
7239 hdr_end += delta;
7240 hdr_next += delta;
7241 cur_hdr->len += delta;
7242 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007244 del_from = NULL;
7245 preserve_hdr = 1; /* we want to keep this cookie */
7246 }
7247 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007248 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007249 del_from = prev;
7250 }
7251 } else {
7252 /* This is not our cookie, so we must preserve it. But if we already
7253 * scheduled another cookie for removal, we cannot remove the
7254 * complete header, but we can remove the previous block itself.
7255 */
7256 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007258 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007259 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007260 if (att_beg >= del_from)
7261 att_beg += delta;
7262 if (att_end >= del_from)
7263 att_end += delta;
7264 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007265 val_end += delta;
7266 next += delta;
7267 hdr_end += delta;
7268 hdr_next += delta;
7269 cur_hdr->len += delta;
7270 http_msg_move_end(&txn->req, delta);
7271 prev = del_from;
7272 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007273 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007274 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007275
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007276 /* Look for the appsession cookie unless persistence must be ignored */
7277 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7278 int cmp_len, value_len;
7279 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007280
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007281 if (t->be->options2 & PR_O2_AS_PFX) {
7282 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7283 value_begin = att_beg + t->be->appsession_name_len;
7284 value_len = val_end - att_beg - t->be->appsession_name_len;
7285 } else {
7286 cmp_len = att_end - att_beg;
7287 value_begin = val_beg;
7288 value_len = val_end - val_beg;
7289 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007290
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007291 /* let's see if the cookie is our appcookie */
7292 if (cmp_len == t->be->appsession_name_len &&
7293 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7294 manage_client_side_appsession(t, value_begin, value_len);
7295 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007296 }
7297
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007298 /* continue with next cookie on this header line */
7299 att_beg = next;
7300 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007302 /* There are no more cookies on this line.
7303 * We may still have one (or several) marked for deletion at the
7304 * end of the line. We must do this now in two ways :
7305 * - if some cookies must be preserved, we only delete from the
7306 * mark to the end of line ;
7307 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007308 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007309 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007311 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007312 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007313 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007314 cur_hdr->len += delta;
7315 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007316 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317
7318 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007319 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7320 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007322 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007323 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007324 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007325 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007326 }
7327
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007328 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007329 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007330 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007331}
7332
7333
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334/* Iterate the same filter through all response headers contained in <rtr>.
7335 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7336 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007337int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007338{
7339 char term;
7340 char *cur_ptr, *cur_end, *cur_next;
7341 int cur_idx, old_idx, last_hdr;
7342 struct http_txn *txn = &t->txn;
7343 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007344 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345
7346 last_hdr = 0;
7347
Willy Tarreau9b28e032012-10-12 23:49:43 +02007348 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349 old_idx = 0;
7350
7351 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007352 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007354 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 (exp->action == ACT_ALLOW ||
7356 exp->action == ACT_DENY))
7357 return 0;
7358
7359 cur_idx = txn->hdr_idx.v[old_idx].next;
7360 if (!cur_idx)
7361 break;
7362
7363 cur_hdr = &txn->hdr_idx.v[cur_idx];
7364 cur_ptr = cur_next;
7365 cur_end = cur_ptr + cur_hdr->len;
7366 cur_next = cur_end + cur_hdr->cr + 1;
7367
7368 /* Now we have one header between cur_ptr and cur_end,
7369 * and the next header starts at cur_next.
7370 */
7371
7372 /* The annoying part is that pattern matching needs
7373 * that we modify the contents to null-terminate all
7374 * strings before testing them.
7375 */
7376
7377 term = *cur_end;
7378 *cur_end = '\0';
7379
7380 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7381 switch (exp->action) {
7382 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007383 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384 last_hdr = 1;
7385 break;
7386
7387 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007388 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389 last_hdr = 1;
7390 break;
7391
7392 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007393 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7394 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007395 /* FIXME: if the user adds a newline in the replacement, the
7396 * index will not be recalculated for now, and the new line
7397 * will not be counted as a new header.
7398 */
7399
7400 cur_end += delta;
7401 cur_next += delta;
7402 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007403 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007404 break;
7405
7406 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007407 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 cur_next += delta;
7409
Willy Tarreaufa355d42009-11-29 18:12:29 +01007410 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007411 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7412 txn->hdr_idx.used--;
7413 cur_hdr->len = 0;
7414 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007415 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 break;
7417
7418 }
7419 }
7420 if (cur_end)
7421 *cur_end = term; /* restore the string terminator */
7422
7423 /* keep the link from this header to next one in case of later
7424 * removal of next header.
7425 */
7426 old_idx = cur_idx;
7427 }
7428 return 0;
7429}
7430
7431
7432/* Apply the filter to the status line in the response buffer <rtr>.
7433 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7434 * or -1 if a replacement resulted in an invalid status line.
7435 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007436int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007437{
7438 char term;
7439 char *cur_ptr, *cur_end;
7440 int done;
7441 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007442 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443
7444
Willy Tarreau3d300592007-03-18 18:34:41 +01007445 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007446 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007447 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007448 (exp->action == ACT_ALLOW ||
7449 exp->action == ACT_DENY))
7450 return 0;
7451 else if (exp->action == ACT_REMOVE)
7452 return 0;
7453
7454 done = 0;
7455
Willy Tarreau9b28e032012-10-12 23:49:43 +02007456 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007457 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007458
7459 /* Now we have the status line between cur_ptr and cur_end */
7460
7461 /* The annoying part is that pattern matching needs
7462 * that we modify the contents to null-terminate all
7463 * strings before testing them.
7464 */
7465
7466 term = *cur_end;
7467 *cur_end = '\0';
7468
7469 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7470 switch (exp->action) {
7471 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007472 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473 done = 1;
7474 break;
7475
7476 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007477 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007478 done = 1;
7479 break;
7480
7481 case ACT_REPLACE:
7482 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007483 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7484 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485 /* FIXME: if the user adds a newline in the replacement, the
7486 * index will not be recalculated for now, and the new line
7487 * will not be counted as a new header.
7488 */
7489
Willy Tarreaufa355d42009-11-29 18:12:29 +01007490 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007491 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007492 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007493 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007494 cur_ptr, cur_end + 1,
7495 NULL, NULL);
7496 if (unlikely(!cur_end))
7497 return -1;
7498
7499 /* we have a full respnse and we know that we have either a CR
7500 * or an LF at <ptr>.
7501 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007502 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007503 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504 /* there is no point trying this regex on headers */
7505 return 1;
7506 }
7507 }
7508 *cur_end = term; /* restore the string terminator */
7509 return done;
7510}
7511
7512
7513
7514/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007515 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007516 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7517 * unparsable response.
7518 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007519int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007521 struct http_txn *txn = &s->txn;
7522 struct hdr_exp *exp;
7523
7524 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525 int ret;
7526
7527 /*
7528 * The interleaving of transformations and verdicts
7529 * makes it difficult to decide to continue or stop
7530 * the evaluation.
7531 */
7532
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007533 if (txn->flags & TX_SVDENY)
7534 break;
7535
Willy Tarreau3d300592007-03-18 18:34:41 +01007536 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007537 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7538 exp->action == ACT_PASS)) {
7539 exp = exp->next;
7540 continue;
7541 }
7542
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007543 /* if this filter had a condition, evaluate it now and skip to
7544 * next filter if the condition does not match.
7545 */
7546 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007547 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007548 ret = acl_pass(ret);
7549 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7550 ret = !ret;
7551 if (!ret)
7552 continue;
7553 }
7554
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007556 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557 if (unlikely(ret < 0))
7558 return -1;
7559
7560 if (likely(ret == 0)) {
7561 /* The filter did not match the response, it can be
7562 * iterated through all headers.
7563 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007564 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007565 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566 }
7567 return 0;
7568}
7569
7570
Willy Tarreaua15645d2007-03-18 16:22:39 +01007571/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007572 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007573 * desirable to call it only when needed. This function is also used when we
7574 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007575 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007576void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007577{
7578 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007579 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007580 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007581 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007582 char *hdr_beg, *hdr_end, *hdr_next;
7583 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584
Willy Tarreaua15645d2007-03-18 16:22:39 +01007585 /* Iterate through the headers.
7586 * we start with the start line.
7587 */
7588 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007589 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007590
7591 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7592 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007593 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007594
7595 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007596 hdr_beg = hdr_next;
7597 hdr_end = hdr_beg + cur_hdr->len;
7598 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007599
Willy Tarreau24581ba2010-08-31 22:39:35 +02007600 /* We have one full header between hdr_beg and hdr_end, and the
7601 * next header starts at hdr_next. We're only interested in
7602 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603 */
7604
Willy Tarreau24581ba2010-08-31 22:39:35 +02007605 is_cookie2 = 0;
7606 prev = hdr_beg + 10;
7607 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007608 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007609 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7610 if (!val) {
7611 old_idx = cur_idx;
7612 continue;
7613 }
7614 is_cookie2 = 1;
7615 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616 }
7617
Willy Tarreau24581ba2010-08-31 22:39:35 +02007618 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7619 * <prev> points to the colon.
7620 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007621 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007622
Willy Tarreau24581ba2010-08-31 22:39:35 +02007623 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7624 * check-cache is enabled) and we are not interested in checking
7625 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007626 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007627 if (t->be->cookie_name == NULL &&
7628 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007629 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007630 return;
7631
Willy Tarreau24581ba2010-08-31 22:39:35 +02007632 /* OK so now we know we have to process this response cookie.
7633 * The format of the Set-Cookie header is slightly different
7634 * from the format of the Cookie header in that it does not
7635 * support the comma as a cookie delimiter (thus the header
7636 * cannot be folded) because the Expires attribute described in
7637 * the original Netscape's spec may contain an unquoted date
7638 * with a comma inside. We have to live with this because
7639 * many browsers don't support Max-Age and some browsers don't
7640 * support quoted strings. However the Set-Cookie2 header is
7641 * clean.
7642 *
7643 * We have to keep multiple pointers in order to support cookie
7644 * removal at the beginning, middle or end of header without
7645 * corrupting the header (in case of set-cookie2). A special
7646 * pointer, <scav> points to the beginning of the set-cookie-av
7647 * fields after the first semi-colon. The <next> pointer points
7648 * either to the end of line (set-cookie) or next unquoted comma
7649 * (set-cookie2). All of these headers are valid :
7650 *
7651 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7652 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7653 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7654 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7655 * | | | | | | | | | |
7656 * | | | | | | | | +-> next hdr_end <--+
7657 * | | | | | | | +------------> scav
7658 * | | | | | | +--------------> val_end
7659 * | | | | | +--------------------> val_beg
7660 * | | | | +----------------------> equal
7661 * | | | +------------------------> att_end
7662 * | | +----------------------------> att_beg
7663 * | +------------------------------> prev
7664 * +-----------------------------------------> hdr_beg
7665 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007666
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 for (; prev < hdr_end; prev = next) {
7668 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007669
Willy Tarreau24581ba2010-08-31 22:39:35 +02007670 /* find att_beg */
7671 att_beg = prev + 1;
7672 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7673 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674
Willy Tarreau24581ba2010-08-31 22:39:35 +02007675 /* find att_end : this is the first character after the last non
7676 * space before the equal. It may be equal to hdr_end.
7677 */
7678 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679
Willy Tarreau24581ba2010-08-31 22:39:35 +02007680 while (equal < hdr_end) {
7681 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7682 break;
7683 if (http_is_spht[(unsigned char)*equal++])
7684 continue;
7685 att_end = equal;
7686 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687
Willy Tarreau24581ba2010-08-31 22:39:35 +02007688 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7689 * is between <att_beg> and <equal>, both may be identical.
7690 */
7691
7692 /* look for end of cookie if there is an equal sign */
7693 if (equal < hdr_end && *equal == '=') {
7694 /* look for the beginning of the value */
7695 val_beg = equal + 1;
7696 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7697 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007698
Willy Tarreau24581ba2010-08-31 22:39:35 +02007699 /* find the end of the value, respecting quotes */
7700 next = find_cookie_value_end(val_beg, hdr_end);
7701
7702 /* make val_end point to the first white space or delimitor after the value */
7703 val_end = next;
7704 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7705 val_end--;
7706 } else {
7707 /* <equal> points to next comma, semi-colon or EOL */
7708 val_beg = val_end = next = equal;
7709 }
7710
7711 if (next < hdr_end) {
7712 /* Set-Cookie2 supports multiple cookies, and <next> points to
7713 * a colon or semi-colon before the end. So skip all attr-value
7714 * pairs and look for the next comma. For Set-Cookie, since
7715 * commas are permitted in values, skip to the end.
7716 */
7717 if (is_cookie2)
7718 next = find_hdr_value_end(next, hdr_end);
7719 else
7720 next = hdr_end;
7721 }
7722
7723 /* Now everything is as on the diagram above */
7724
7725 /* Ignore cookies with no equal sign */
7726 if (equal == val_end)
7727 continue;
7728
7729 /* If there are spaces around the equal sign, we need to
7730 * strip them otherwise we'll get trouble for cookie captures,
7731 * or even for rewrites. Since this happens extremely rarely,
7732 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007733 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007734 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7735 int stripped_before = 0;
7736 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007737
Willy Tarreau24581ba2010-08-31 22:39:35 +02007738 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007739 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007740 equal += stripped_before;
7741 val_beg += stripped_before;
7742 }
7743
7744 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007745 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007746 val_beg += stripped_after;
7747 stripped_before += stripped_after;
7748 }
7749
7750 val_end += stripped_before;
7751 next += stripped_before;
7752 hdr_end += stripped_before;
7753 hdr_next += stripped_before;
7754 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007755 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007756 }
7757
7758 /* First, let's see if we want to capture this cookie. We check
7759 * that we don't already have a server side cookie, because we
7760 * can only capture one. Also as an optimisation, we ignore
7761 * cookies shorter than the declared name.
7762 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007763 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007764 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007765 (val_end - att_beg >= t->fe->capture_namelen) &&
7766 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7767 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007768 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769 Alert("HTTP logging : out of memory.\n");
7770 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007771 else {
7772 if (log_len > t->fe->capture_len)
7773 log_len = t->fe->capture_len;
7774 memcpy(txn->srv_cookie, att_beg, log_len);
7775 txn->srv_cookie[log_len] = 0;
7776 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777 }
7778
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007779 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007780 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007781 if (!(t->flags & SN_IGNORE_PRST) &&
7782 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7783 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007784 /* assume passive cookie by default */
7785 txn->flags &= ~TX_SCK_MASK;
7786 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787
7788 /* If the cookie is in insert mode on a known server, we'll delete
7789 * this occurrence because we'll insert another one later.
7790 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007791 * a direct access.
7792 */
Willy Tarreau67402132012-05-31 20:40:20 +02007793 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007794 /* The "preserve" flag was set, we don't want to touch the
7795 * server's cookie.
7796 */
7797 }
Willy Tarreau67402132012-05-31 20:40:20 +02007798 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7799 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007800 /* this cookie must be deleted */
7801 if (*prev == ':' && next == hdr_end) {
7802 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007803 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007804 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7805 txn->hdr_idx.used--;
7806 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007807 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007808 hdr_next += delta;
7809 http_msg_move_end(&txn->rsp, delta);
7810 /* note: while both invalid now, <next> and <hdr_end>
7811 * are still equal, so the for() will stop as expected.
7812 */
7813 } else {
7814 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007815 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007816 next = prev;
7817 hdr_end += delta;
7818 hdr_next += delta;
7819 cur_hdr->len += delta;
7820 http_msg_move_end(&txn->rsp, delta);
7821 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007822 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007823 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007824 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 }
Willy Tarreau67402132012-05-31 20:40:20 +02007826 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007827 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828 * with this server since we know it.
7829 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007830 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007831 next += delta;
7832 hdr_end += delta;
7833 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007834 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007835 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007836
Willy Tarreauf1348312010-10-07 15:54:11 +02007837 txn->flags &= ~TX_SCK_MASK;
7838 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007840 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007842 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007844 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007845 next += delta;
7846 hdr_end += delta;
7847 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007849 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850
Willy Tarreau827aee92011-03-10 16:55:02 +01007851 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007852 txn->flags &= ~TX_SCK_MASK;
7853 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007854 }
7855 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007856 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7857 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007858 int cmp_len, value_len;
7859 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860
Cyril Bontéb21570a2009-11-29 20:04:48 +01007861 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007862 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7863 value_begin = att_beg + t->be->appsession_name_len;
7864 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007865 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007866 cmp_len = att_end - att_beg;
7867 value_begin = val_beg;
7868 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007869 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007870
Cyril Bonté17530c32010-04-06 21:11:10 +02007871 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007872 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7873 /* free a possibly previously allocated memory */
7874 pool_free2(apools.sessid, txn->sessid);
7875
Cyril Bontéb21570a2009-11-29 20:04:48 +01007876 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007877 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007878 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7879 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7880 return;
7881 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007882 memcpy(txn->sessid, value_begin, value_len);
7883 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007885 }
7886 /* that's done for this cookie, check the next one on the same
7887 * line when next != hdr_end (only if is_cookie2).
7888 */
7889 }
7890 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007892 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007893
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007894 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007895 appsess *asession = NULL;
7896 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007897 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007898 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007899 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007900 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7901 Alert("Not enough Memory process_srv():asession:calloc().\n");
7902 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7903 return;
7904 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007905 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7906
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007907 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7908 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7909 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007910 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007911 return;
7912 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007913 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007914 asession->sessid[t->be->appsession_len] = 0;
7915
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007916 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007917 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007918 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007919 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007920 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007921 return;
7922 }
7923 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007924 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007925
7926 asession->request_count = 0;
7927 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7928 }
7929
7930 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7931 asession->request_count++;
7932 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007933}
7934
7935
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936/*
7937 * Check if response is cacheable or not. Updates t->flags.
7938 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007939void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940{
7941 struct http_txn *txn = &t->txn;
7942 char *p1, *p2;
7943
7944 char *cur_ptr, *cur_end, *cur_next;
7945 int cur_idx;
7946
Willy Tarreau5df51872007-11-25 16:20:08 +01007947 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007948 return;
7949
7950 /* Iterate through the headers.
7951 * we start with the start line.
7952 */
7953 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007954 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955
7956 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7957 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007958 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007959
7960 cur_hdr = &txn->hdr_idx.v[cur_idx];
7961 cur_ptr = cur_next;
7962 cur_end = cur_ptr + cur_hdr->len;
7963 cur_next = cur_end + cur_hdr->cr + 1;
7964
7965 /* We have one full header between cur_ptr and cur_end, and the
7966 * next header starts at cur_next. We're only interested in
7967 * "Cookie:" headers.
7968 */
7969
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007970 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7971 if (val) {
7972 if ((cur_end - (cur_ptr + val) >= 8) &&
7973 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7974 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7975 return;
7976 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977 }
7978
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007979 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7980 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981 continue;
7982
7983 /* OK, right now we know we have a cache-control header at cur_ptr */
7984
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007985 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986
7987 if (p1 >= cur_end) /* no more info */
7988 continue;
7989
7990 /* p1 is at the beginning of the value */
7991 p2 = p1;
7992
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007993 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 p2++;
7995
7996 /* we have a complete value between p1 and p2 */
7997 if (p2 < cur_end && *p2 == '=') {
7998 /* we have something of the form no-cache="set-cookie" */
7999 if ((cur_end - p1 >= 21) &&
8000 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8001 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008002 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003 continue;
8004 }
8005
8006 /* OK, so we know that either p2 points to the end of string or to a comma */
8007 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008008 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8010 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8011 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008012 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 return;
8014 }
8015
8016 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008017 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 continue;
8019 }
8020 }
8021}
8022
8023
Willy Tarreau58f10d72006-12-04 02:26:12 +01008024/*
8025 * Try to retrieve a known appsession in the URI, then the associated server.
8026 * If the server is found, it's assigned to the session.
8027 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008028void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008029{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008030 char *end_params, *first_param, *cur_param, *next_param;
8031 char separator;
8032 int value_len;
8033
8034 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008035
Willy Tarreaue2e27a52007-04-01 00:01:37 +02008036 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02008037 (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 +01008038 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008039 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008040
Cyril Bontéb21570a2009-11-29 20:04:48 +01008041 first_param = NULL;
8042 switch (mode) {
8043 case PR_O2_AS_M_PP:
8044 first_param = memchr(begin, ';', len);
8045 break;
8046 case PR_O2_AS_M_QS:
8047 first_param = memchr(begin, '?', len);
8048 break;
8049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008050
Cyril Bontéb21570a2009-11-29 20:04:48 +01008051 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008052 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008053 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008054
Cyril Bontéb21570a2009-11-29 20:04:48 +01008055 switch (mode) {
8056 case PR_O2_AS_M_PP:
8057 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8058 end_params = (char *) begin + len;
8059 }
8060 separator = ';';
8061 break;
8062 case PR_O2_AS_M_QS:
8063 end_params = (char *) begin + len;
8064 separator = '&';
8065 break;
8066 default:
8067 /* unknown mode, shouldn't happen */
8068 return;
8069 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008070
Cyril Bontéb21570a2009-11-29 20:04:48 +01008071 cur_param = next_param = end_params;
8072 while (cur_param > first_param) {
8073 cur_param--;
8074 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8075 /* let's see if this is the appsession parameter */
8076 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
8077 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
8078 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
8079 /* Cool... it's the right one */
8080 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8081 value_len = MIN(t->be->appsession_len, next_param - cur_param);
8082 if (value_len > 0) {
8083 manage_client_side_appsession(t, cur_param, value_len);
8084 }
8085 break;
8086 }
8087 next_param = cur_param;
8088 }
8089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008090#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008091 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02008092 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008093#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008094}
8095
Willy Tarreaub2513902006-12-17 14:52:38 +01008096/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008097 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008098 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008099 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008100 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008101 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008102 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008103 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008104 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008105int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008106{
8107 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008108 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008109 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008110
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008111 if (!uri_auth)
8112 return 0;
8113
Cyril Bonté70be45d2010-10-12 00:14:35 +02008114 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008115 return 0;
8116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008117 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008118 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008119 return 0;
8120
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008121 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008122 return 0;
8123
Willy Tarreaub2513902006-12-17 14:52:38 +01008124 return 1;
8125}
8126
Willy Tarreau4076a152009-04-02 15:18:36 +02008127/*
8128 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008129 * By default it tries to report the error position as msg->err_pos. However if
8130 * this one is not set, it will then report msg->next, which is the last known
8131 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008132 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008133 */
8134void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008135 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008136 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008137{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008138 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008139 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008140
Willy Tarreau9b28e032012-10-12 23:49:43 +02008141 es->len = MIN(chn->buf->i, sizeof(es->buf));
8142 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008143 len1 = MIN(len1, es->len);
8144 len2 = es->len - len1; /* remaining data if buffer wraps */
8145
Willy Tarreau9b28e032012-10-12 23:49:43 +02008146 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008147 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008148 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008149
Willy Tarreau4076a152009-04-02 15:18:36 +02008150 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008151 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008152 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008153 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008154
Willy Tarreau4076a152009-04-02 15:18:36 +02008155 es->when = date; // user-visible date
8156 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008157 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008158 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008159 if (objt_conn(s->req->prod->end))
8160 es->src = __objt_conn(s->req->prod->end)->addr.from;
8161 else
8162 memset(&es->src, 0, sizeof(es->src));
8163
Willy Tarreau078272e2010-12-12 12:46:33 +01008164 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008165 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008166 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008167 es->s_flags = s->flags;
8168 es->t_flags = s->txn.flags;
8169 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008170 es->b_out = chn->buf->o;
8171 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008172 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008173 es->m_clen = msg->chunk_len;
8174 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008175}
Willy Tarreaub2513902006-12-17 14:52:38 +01008176
Willy Tarreau294c4732011-12-16 21:35:50 +01008177/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8178 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8179 * performed over the whole headers. Otherwise it must contain a valid header
8180 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8181 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8182 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8183 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008184 * -1. The value fetch stops at commas, so this function is suited for use with
8185 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008186 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008187 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008188unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008189 struct hdr_idx *idx, int occ,
8190 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008191{
Willy Tarreau294c4732011-12-16 21:35:50 +01008192 struct hdr_ctx local_ctx;
8193 char *ptr_hist[MAX_HDR_HISTORY];
8194 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008195 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008196 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008197
Willy Tarreau294c4732011-12-16 21:35:50 +01008198 if (!ctx) {
8199 local_ctx.idx = 0;
8200 ctx = &local_ctx;
8201 }
8202
Willy Tarreaubce70882009-09-07 11:51:47 +02008203 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008204 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008205 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008206 occ--;
8207 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008208 *vptr = ctx->line + ctx->val;
8209 *vlen = ctx->vlen;
8210 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008211 }
8212 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008213 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008214 }
8215
8216 /* negative occurrence, we scan all the list then walk back */
8217 if (-occ > MAX_HDR_HISTORY)
8218 return 0;
8219
Willy Tarreau294c4732011-12-16 21:35:50 +01008220 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008221 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008222 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8223 len_hist[hist_ptr] = ctx->vlen;
8224 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008225 hist_ptr = 0;
8226 found++;
8227 }
8228 if (-occ > found)
8229 return 0;
8230 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008231 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8232 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8233 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008234 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008235 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008236 if (hist_ptr >= MAX_HDR_HISTORY)
8237 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008238 *vptr = ptr_hist[hist_ptr];
8239 *vlen = len_hist[hist_ptr];
8240 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008241}
8242
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008243/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8244 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8245 * performed over the whole headers. Otherwise it must contain a valid header
8246 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8247 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8248 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8249 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8250 * -1. This function differs from http_get_hdr() in that it only returns full
8251 * line header values and does not stop at commas.
8252 * The return value is 0 if nothing was found, or non-zero otherwise.
8253 */
8254unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8255 struct hdr_idx *idx, int occ,
8256 struct hdr_ctx *ctx, char **vptr, int *vlen)
8257{
8258 struct hdr_ctx local_ctx;
8259 char *ptr_hist[MAX_HDR_HISTORY];
8260 int len_hist[MAX_HDR_HISTORY];
8261 unsigned int hist_ptr;
8262 int found;
8263
8264 if (!ctx) {
8265 local_ctx.idx = 0;
8266 ctx = &local_ctx;
8267 }
8268
8269 if (occ >= 0) {
8270 /* search from the beginning */
8271 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8272 occ--;
8273 if (occ <= 0) {
8274 *vptr = ctx->line + ctx->val;
8275 *vlen = ctx->vlen;
8276 return 1;
8277 }
8278 }
8279 return 0;
8280 }
8281
8282 /* negative occurrence, we scan all the list then walk back */
8283 if (-occ > MAX_HDR_HISTORY)
8284 return 0;
8285
8286 found = hist_ptr = 0;
8287 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8288 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8289 len_hist[hist_ptr] = ctx->vlen;
8290 if (++hist_ptr >= MAX_HDR_HISTORY)
8291 hist_ptr = 0;
8292 found++;
8293 }
8294 if (-occ > found)
8295 return 0;
8296 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8297 * find occurrence -occ, so we have to check [hist_ptr+occ].
8298 */
8299 hist_ptr += occ;
8300 if (hist_ptr >= MAX_HDR_HISTORY)
8301 hist_ptr -= MAX_HDR_HISTORY;
8302 *vptr = ptr_hist[hist_ptr];
8303 *vlen = len_hist[hist_ptr];
8304 return 1;
8305}
8306
Willy Tarreaubaaee002006-06-26 02:48:02 +02008307/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008308 * Print a debug line with a header. Always stop at the first CR or LF char,
8309 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8310 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008311 */
8312void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8313{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008314 int max;
8315 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008316 dir,
8317 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8318 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 +02008319
8320 for (max = 0; start + max < end; max++)
8321 if (start[max] == '\r' || start[max] == '\n')
8322 break;
8323
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008324 UBOUND(max, trash.size - trash.len - 3);
8325 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8326 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008327 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008328}
8329
Willy Tarreau0937bc42009-12-22 15:03:09 +01008330/*
8331 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8332 * the required fields are properly allocated and that we only need to (re)init
8333 * them. This should be used before processing any new request.
8334 */
8335void http_init_txn(struct session *s)
8336{
8337 struct http_txn *txn = &s->txn;
8338 struct proxy *fe = s->fe;
8339
8340 txn->flags = 0;
8341 txn->status = -1;
8342
Willy Tarreauf64d1412010-10-07 20:06:11 +02008343 txn->cookie_first_date = 0;
8344 txn->cookie_last_date = 0;
8345
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008346 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008347 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008348 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008349 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008350 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008351 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008352 txn->req.chunk_len = 0LL;
8353 txn->req.body_len = 0LL;
8354 txn->rsp.chunk_len = 0LL;
8355 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008356 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8357 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008358 txn->req.chn = s->req;
8359 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008360
8361 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008362
8363 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8364 if (fe->options2 & PR_O2_REQBUG_OK)
8365 txn->req.err_pos = -1; /* let buggy requests pass */
8366
Willy Tarreau46023632010-01-07 22:51:47 +01008367 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008368 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8369
Willy Tarreau46023632010-01-07 22:51:47 +01008370 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008371 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8372
8373 if (txn->hdr_idx.v)
8374 hdr_idx_init(&txn->hdr_idx);
8375}
8376
8377/* to be used at the end of a transaction */
8378void http_end_txn(struct session *s)
8379{
8380 struct http_txn *txn = &s->txn;
8381
Willy Tarreau75195602014-03-11 15:48:55 +01008382 /* release any possible compression context */
8383 if (s->flags & SN_COMP_READY)
8384 s->comp_algo->end(&s->comp_ctx);
8385 s->comp_algo = NULL;
8386 s->flags &= ~SN_COMP_READY;
8387
Willy Tarreau0937bc42009-12-22 15:03:09 +01008388 /* these ones will have been dynamically allocated */
8389 pool_free2(pool2_requri, txn->uri);
8390 pool_free2(pool2_capture, txn->cli_cookie);
8391 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008392 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008393 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008394
William Lallemanda73203e2012-03-12 12:48:57 +01008395 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008396 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008397 txn->uri = NULL;
8398 txn->srv_cookie = NULL;
8399 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008400
8401 if (txn->req.cap) {
8402 struct cap_hdr *h;
8403 for (h = s->fe->req_cap; h; h = h->next)
8404 pool_free2(h->pool, txn->req.cap[h->index]);
8405 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8406 }
8407
8408 if (txn->rsp.cap) {
8409 struct cap_hdr *h;
8410 for (h = s->fe->rsp_cap; h; h = h->next)
8411 pool_free2(h->pool, txn->rsp.cap[h->index]);
8412 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8413 }
8414
Willy Tarreau0937bc42009-12-22 15:03:09 +01008415}
8416
8417/* to be used at the end of a transaction to prepare a new one */
8418void http_reset_txn(struct session *s)
8419{
8420 http_end_txn(s);
8421 http_init_txn(s);
8422
8423 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008424 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008425 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008426 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008427 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008428 /* re-init store persistence */
8429 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008430 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008431
Willy Tarreau0937bc42009-12-22 15:03:09 +01008432 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008433
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008434 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008435
Willy Tarreau739cfba2010-01-25 23:11:14 +01008436 /* We must trim any excess data from the response buffer, because we
8437 * may have blocked an invalid response from a server that we don't
8438 * want to accidentely forward once we disable the analysers, nor do
8439 * we want those data to come along with next response. A typical
8440 * example of such data would be from a buggy server responding to
8441 * a HEAD with some data, or sending more than the advertised
8442 * content-length.
8443 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008444 if (unlikely(s->rep->buf->i))
8445 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008446
Willy Tarreau0937bc42009-12-22 15:03:09 +01008447 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008448 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008449
Willy Tarreaud04e8582010-05-31 12:31:35 +02008450 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008451 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008452
8453 s->req->rex = TICK_ETERNITY;
8454 s->req->wex = TICK_ETERNITY;
8455 s->req->analyse_exp = TICK_ETERNITY;
8456 s->rep->rex = TICK_ETERNITY;
8457 s->rep->wex = TICK_ETERNITY;
8458 s->rep->analyse_exp = TICK_ETERNITY;
8459}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008460
Willy Tarreauff011f22011-01-06 17:51:27 +01008461void free_http_req_rules(struct list *r) {
8462 struct http_req_rule *tr, *pr;
8463
8464 list_for_each_entry_safe(pr, tr, r, list) {
8465 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008466 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008467 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008468
8469 free(pr);
8470 }
8471}
8472
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008473/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008474struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8475{
8476 struct http_req_rule *rule;
8477 int cur_arg;
8478
8479 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8480 if (!rule) {
8481 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008482 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008483 }
8484
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008485 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008486 rule->action = HTTP_REQ_ACT_ALLOW;
8487 cur_arg = 1;
8488 } else if (!strcmp(args[0], "deny")) {
8489 rule->action = HTTP_REQ_ACT_DENY;
8490 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008491 } else if (!strcmp(args[0], "tarpit")) {
8492 rule->action = HTTP_REQ_ACT_TARPIT;
8493 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008494 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008495 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008496 cur_arg = 1;
8497
8498 while(*args[cur_arg]) {
8499 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008500 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008501 cur_arg+=2;
8502 continue;
8503 } else
8504 break;
8505 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008506 } else if (!strcmp(args[0], "set-nice")) {
8507 rule->action = HTTP_REQ_ACT_SET_NICE;
8508 cur_arg = 1;
8509
8510 if (!*args[cur_arg] ||
8511 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8512 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8513 file, linenum, args[0]);
8514 goto out_err;
8515 }
8516 rule->arg.nice = atoi(args[cur_arg]);
8517 if (rule->arg.nice < -1024)
8518 rule->arg.nice = -1024;
8519 else if (rule->arg.nice > 1024)
8520 rule->arg.nice = 1024;
8521 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008522 } else if (!strcmp(args[0], "set-tos")) {
8523#ifdef IP_TOS
8524 char *err;
8525 rule->action = HTTP_REQ_ACT_SET_TOS;
8526 cur_arg = 1;
8527
8528 if (!*args[cur_arg] ||
8529 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8530 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8531 file, linenum, args[0]);
8532 goto out_err;
8533 }
8534
8535 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8536 if (err && *err != '\0') {
8537 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8538 file, linenum, err, args[0]);
8539 goto out_err;
8540 }
8541 cur_arg++;
8542#else
8543 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8544 goto out_err;
8545#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008546 } else if (!strcmp(args[0], "set-mark")) {
8547#ifdef SO_MARK
8548 char *err;
8549 rule->action = HTTP_REQ_ACT_SET_MARK;
8550 cur_arg = 1;
8551
8552 if (!*args[cur_arg] ||
8553 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8554 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8555 file, linenum, args[0]);
8556 goto out_err;
8557 }
8558
8559 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8560 if (err && *err != '\0') {
8561 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8562 file, linenum, err, args[0]);
8563 goto out_err;
8564 }
8565 cur_arg++;
8566 global.last_checks |= LSTCHK_NETADM;
8567#else
8568 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8569 goto out_err;
8570#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008571 } else if (!strcmp(args[0], "set-log-level")) {
8572 rule->action = HTTP_REQ_ACT_SET_LOGL;
8573 cur_arg = 1;
8574
8575 if (!*args[cur_arg] ||
8576 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8577 bad_log_level:
8578 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8579 file, linenum, args[0]);
8580 goto out_err;
8581 }
8582 if (strcmp(args[cur_arg], "silent") == 0)
8583 rule->arg.loglevel = -1;
8584 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8585 goto bad_log_level;
8586 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008587 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8588 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8589 cur_arg = 1;
8590
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008591 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8592 (*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 +01008593 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8594 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008595 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008596 }
8597
8598 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8599 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8600 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008601
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008602 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008603 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008604 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8605 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008606 free(proxy->conf.lfs_file);
8607 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8608 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008609 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008610 } else if (strcmp(args[0], "del-header") == 0) {
8611 rule->action = HTTP_REQ_ACT_DEL_HDR;
8612 cur_arg = 1;
8613
8614 if (!*args[cur_arg] ||
8615 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8616 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8617 file, linenum, args[0]);
8618 goto out_err;
8619 }
8620
8621 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8622 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8623
8624 proxy->conf.args.ctx = ARGC_HRQ;
8625 free(proxy->conf.lfs_file);
8626 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8627 proxy->conf.lfs_line = proxy->conf.args.line;
8628 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008629 } else if (strcmp(args[0], "redirect") == 0) {
8630 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008631 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008632
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008633 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008634 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8635 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8636 goto out_err;
8637 }
8638
8639 /* this redirect rule might already contain a parsed condition which
8640 * we'll pass to the http-request rule.
8641 */
8642 rule->action = HTTP_REQ_ACT_REDIR;
8643 rule->arg.redir = redir;
8644 rule->cond = redir->cond;
8645 redir->cond = NULL;
8646 cur_arg = 2;
8647 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008648 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008649 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 +01008650 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008651 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008652 }
8653
8654 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8655 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008656 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008657
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008658 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8659 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8660 file, linenum, args[0], errmsg);
8661 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008662 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008663 }
8664 rule->cond = cond;
8665 }
8666 else if (*args[cur_arg]) {
8667 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8668 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8669 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008670 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008671 }
8672
8673 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008674 out_err:
8675 free(rule);
8676 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008677}
8678
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008679/* parse an "http-respose" rule */
8680struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8681{
8682 struct http_res_rule *rule;
8683 int cur_arg;
8684
8685 rule = calloc(1, sizeof(*rule));
8686 if (!rule) {
8687 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8688 goto out_err;
8689 }
8690
8691 if (!strcmp(args[0], "allow")) {
8692 rule->action = HTTP_RES_ACT_ALLOW;
8693 cur_arg = 1;
8694 } else if (!strcmp(args[0], "deny")) {
8695 rule->action = HTTP_RES_ACT_DENY;
8696 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008697 } else if (!strcmp(args[0], "set-nice")) {
8698 rule->action = HTTP_RES_ACT_SET_NICE;
8699 cur_arg = 1;
8700
8701 if (!*args[cur_arg] ||
8702 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8703 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8704 file, linenum, args[0]);
8705 goto out_err;
8706 }
8707 rule->arg.nice = atoi(args[cur_arg]);
8708 if (rule->arg.nice < -1024)
8709 rule->arg.nice = -1024;
8710 else if (rule->arg.nice > 1024)
8711 rule->arg.nice = 1024;
8712 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008713 } else if (!strcmp(args[0], "set-tos")) {
8714#ifdef IP_TOS
8715 char *err;
8716 rule->action = HTTP_RES_ACT_SET_TOS;
8717 cur_arg = 1;
8718
8719 if (!*args[cur_arg] ||
8720 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8721 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8722 file, linenum, args[0]);
8723 goto out_err;
8724 }
8725
8726 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8727 if (err && *err != '\0') {
8728 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8729 file, linenum, err, args[0]);
8730 goto out_err;
8731 }
8732 cur_arg++;
8733#else
8734 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8735 goto out_err;
8736#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008737 } else if (!strcmp(args[0], "set-mark")) {
8738#ifdef SO_MARK
8739 char *err;
8740 rule->action = HTTP_RES_ACT_SET_MARK;
8741 cur_arg = 1;
8742
8743 if (!*args[cur_arg] ||
8744 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8745 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8746 file, linenum, args[0]);
8747 goto out_err;
8748 }
8749
8750 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8751 if (err && *err != '\0') {
8752 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8753 file, linenum, err, args[0]);
8754 goto out_err;
8755 }
8756 cur_arg++;
8757 global.last_checks |= LSTCHK_NETADM;
8758#else
8759 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8760 goto out_err;
8761#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008762 } else if (!strcmp(args[0], "set-log-level")) {
8763 rule->action = HTTP_RES_ACT_SET_LOGL;
8764 cur_arg = 1;
8765
8766 if (!*args[cur_arg] ||
8767 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8768 bad_log_level:
8769 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8770 file, linenum, args[0]);
8771 goto out_err;
8772 }
8773 if (strcmp(args[cur_arg], "silent") == 0)
8774 rule->arg.loglevel = -1;
8775 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8776 goto bad_log_level;
8777 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008778 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8779 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8780 cur_arg = 1;
8781
8782 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8783 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8784 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8785 file, linenum, args[0]);
8786 goto out_err;
8787 }
8788
8789 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8790 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8791 LIST_INIT(&rule->arg.hdr_add.fmt);
8792
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008793 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008794 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008795 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8796 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008797 free(proxy->conf.lfs_file);
8798 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8799 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008800 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008801 } else if (strcmp(args[0], "del-header") == 0) {
8802 rule->action = HTTP_RES_ACT_DEL_HDR;
8803 cur_arg = 1;
8804
8805 if (!*args[cur_arg] ||
8806 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8807 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8808 file, linenum, args[0]);
8809 goto out_err;
8810 }
8811
8812 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8813 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8814
8815 proxy->conf.args.ctx = ARGC_HRS;
8816 free(proxy->conf.lfs_file);
8817 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8818 proxy->conf.lfs_line = proxy->conf.args.line;
8819 cur_arg += 1;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008820 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008821 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 +02008822 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8823 goto out_err;
8824 }
8825
8826 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8827 struct acl_cond *cond;
8828 char *errmsg = NULL;
8829
8830 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8831 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8832 file, linenum, args[0], errmsg);
8833 free(errmsg);
8834 goto out_err;
8835 }
8836 rule->cond = cond;
8837 }
8838 else if (*args[cur_arg]) {
8839 Alert("parsing [%s:%d]: 'http-response %s' expects"
8840 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8841 file, linenum, args[0], args[cur_arg]);
8842 goto out_err;
8843 }
8844
8845 return rule;
8846 out_err:
8847 free(rule);
8848 return NULL;
8849}
8850
Willy Tarreau4baae242012-12-27 12:00:31 +01008851/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008852 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8853 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008854 */
8855struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008856 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008857{
8858 struct redirect_rule *rule;
8859 int cur_arg;
8860 int type = REDIRECT_TYPE_NONE;
8861 int code = 302;
8862 const char *destination = NULL;
8863 const char *cookie = NULL;
8864 int cookie_set = 0;
8865 unsigned int flags = REDIRECT_FLAG_NONE;
8866 struct acl_cond *cond = NULL;
8867
8868 cur_arg = 0;
8869 while (*(args[cur_arg])) {
8870 if (strcmp(args[cur_arg], "location") == 0) {
8871 if (!*args[cur_arg + 1])
8872 goto missing_arg;
8873
8874 type = REDIRECT_TYPE_LOCATION;
8875 cur_arg++;
8876 destination = args[cur_arg];
8877 }
8878 else if (strcmp(args[cur_arg], "prefix") == 0) {
8879 if (!*args[cur_arg + 1])
8880 goto missing_arg;
8881
8882 type = REDIRECT_TYPE_PREFIX;
8883 cur_arg++;
8884 destination = args[cur_arg];
8885 }
8886 else if (strcmp(args[cur_arg], "scheme") == 0) {
8887 if (!*args[cur_arg + 1])
8888 goto missing_arg;
8889
8890 type = REDIRECT_TYPE_SCHEME;
8891 cur_arg++;
8892 destination = args[cur_arg];
8893 }
8894 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8895 if (!*args[cur_arg + 1])
8896 goto missing_arg;
8897
8898 cur_arg++;
8899 cookie = args[cur_arg];
8900 cookie_set = 1;
8901 }
8902 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8903 if (!*args[cur_arg + 1])
8904 goto missing_arg;
8905
8906 cur_arg++;
8907 cookie = args[cur_arg];
8908 cookie_set = 0;
8909 }
8910 else if (strcmp(args[cur_arg], "code") == 0) {
8911 if (!*args[cur_arg + 1])
8912 goto missing_arg;
8913
8914 cur_arg++;
8915 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008916 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008917 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008918 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008919 args[cur_arg - 1], args[cur_arg]);
8920 return NULL;
8921 }
8922 }
8923 else if (!strcmp(args[cur_arg],"drop-query")) {
8924 flags |= REDIRECT_FLAG_DROP_QS;
8925 }
8926 else if (!strcmp(args[cur_arg],"append-slash")) {
8927 flags |= REDIRECT_FLAG_APPEND_SLASH;
8928 }
8929 else if (strcmp(args[cur_arg], "if") == 0 ||
8930 strcmp(args[cur_arg], "unless") == 0) {
8931 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8932 if (!cond) {
8933 memprintf(errmsg, "error in condition: %s", *errmsg);
8934 return NULL;
8935 }
8936 break;
8937 }
8938 else {
8939 memprintf(errmsg,
8940 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8941 args[cur_arg]);
8942 return NULL;
8943 }
8944 cur_arg++;
8945 }
8946
8947 if (type == REDIRECT_TYPE_NONE) {
8948 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8949 return NULL;
8950 }
8951
8952 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8953 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008954 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008955
8956 if (!use_fmt) {
8957 /* old-style static redirect rule */
8958 rule->rdr_str = strdup(destination);
8959 rule->rdr_len = strlen(destination);
8960 }
8961 else {
8962 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008963
8964 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8965 * if prefix == "/", we don't want to add anything, otherwise it
8966 * makes it hard for the user to configure a self-redirection.
8967 */
8968 proxy->conf.args.ctx = ARGC_RDR;
8969 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008970 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008971 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8972 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008973 free(curproxy->conf.lfs_file);
8974 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8975 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008976 }
8977 }
8978
Willy Tarreau4baae242012-12-27 12:00:31 +01008979 if (cookie) {
8980 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8981 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8982 */
8983 rule->cookie_len = strlen(cookie);
8984 if (cookie_set) {
8985 rule->cookie_str = malloc(rule->cookie_len + 10);
8986 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8987 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8988 rule->cookie_len += 9;
8989 } else {
8990 rule->cookie_str = malloc(rule->cookie_len + 21);
8991 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8992 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8993 rule->cookie_len += 20;
8994 }
8995 }
8996 rule->type = type;
8997 rule->code = code;
8998 rule->flags = flags;
8999 LIST_INIT(&rule->list);
9000 return rule;
9001
9002 missing_arg:
9003 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9004 return NULL;
9005}
9006
Willy Tarreau8797c062007-05-07 00:55:35 +02009007/************************************************************************/
9008/* The code below is dedicated to ACL parsing and matching */
9009/************************************************************************/
9010
9011
Willy Tarreau14174bc2012-04-16 14:34:04 +02009012/* This function ensures that the prerequisites for an L7 fetch are ready,
9013 * which means that a request or response is ready. If some data is missing,
9014 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009015 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9016 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009017 *
9018 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009019 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9020 * decide whether or not an HTTP message is present ;
9021 * 0 if the requested data cannot be fetched or if it is certain that
9022 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009023 * 1 if an HTTP message is ready
9024 */
9025static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009026smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009027 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009028{
9029 struct http_txn *txn = l7;
9030 struct http_msg *msg = &txn->req;
9031
9032 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9033 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9034 */
9035
9036 if (unlikely(!s || !txn))
9037 return 0;
9038
9039 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009040 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009041
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009042 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009043 if (unlikely(!s->req))
9044 return 0;
9045
Willy Tarreauaae75e32013-03-29 12:31:49 +01009046 /* If the buffer does not leave enough free space at the end,
9047 * we must first realign it.
9048 */
9049 if (s->req->buf->p > s->req->buf->data &&
9050 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9051 buffer_slow_realign(s->req->buf);
9052
Willy Tarreau14174bc2012-04-16 14:34:04 +02009053 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009054 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009055 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009056
9057 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009058 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009059 http_msg_analyzer(msg, &txn->hdr_idx);
9060
9061 /* Still no valid request ? */
9062 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009063 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009064 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009065 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009066 }
9067 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009068 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009069 return 0;
9070 }
9071
9072 /* OK we just got a valid HTTP request. We have some minor
9073 * preparation to perform so that further checks can rely
9074 * on HTTP tests.
9075 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009076
9077 /* If the request was parsed but was too large, we must absolutely
9078 * return an error so that it is not processed. At the moment this
9079 * cannot happen, but if the parsers are to change in the future,
9080 * we want this check to be maintained.
9081 */
9082 if (unlikely(s->req->buf->i + s->req->buf->p >
9083 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9084 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009085 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009086 return 1;
9087 }
9088
Willy Tarreau9b28e032012-10-12 23:49:43 +02009089 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009090 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9091 s->flags |= SN_REDIRECTABLE;
9092
Willy Tarreau506d0502013-07-06 13:29:24 +02009093 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9094 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009095 }
9096
Willy Tarreau506d0502013-07-06 13:29:24 +02009097 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009098 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009099 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009100
9101 /* otherwise everything's ready for the request */
9102 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009103 else {
9104 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009105 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9106 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009107 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009108 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009109 }
9110
9111 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009112 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009113 return 1;
9114}
Willy Tarreau8797c062007-05-07 00:55:35 +02009115
Willy Tarreauc0239e02012-04-16 14:42:55 +02009116#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009117 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 +02009118
Willy Tarreau24e32d82012-04-23 23:55:44 +02009119#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009120 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 +02009121
Willy Tarreau8797c062007-05-07 00:55:35 +02009122
9123/* 1. Check on METHOD
9124 * We use the pre-parsed method if it is known, and store its number as an
9125 * integer. If it is unknown, we use the pointer and the length.
9126 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009127static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009128{
9129 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009130 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009131
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009132 len = strlen(text);
9133 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009134
9135 pattern->val.i = meth;
9136 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009137 trash = get_trash_chunk();
9138 if (trash->size < len) {
9139 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9140 len, trash->size);
9141 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009142 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009143 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009144 pattern->len = len;
9145 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009146 else {
9147 pattern->ptr.str = NULL;
9148 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009149 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009150 return 1;
9151}
9152
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009153/* This function fetches the method of current HTTP request and stores
9154 * it in the global pattern struct as a chunk. There are two possibilities :
9155 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9156 * in <len> and <ptr> is NULL ;
9157 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9158 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009159 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009160 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009161static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009162smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009163 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009164{
9165 int meth;
9166 struct http_txn *txn = l7;
9167
Willy Tarreau24e32d82012-04-23 23:55:44 +02009168 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009169
Willy Tarreau8797c062007-05-07 00:55:35 +02009170 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009171 smp->type = SMP_T_METH;
9172 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009173 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009174 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9175 /* ensure the indexes are not affected */
9176 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009177 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009178 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9179 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009180 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009181 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009182 return 1;
9183}
9184
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009185/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009186static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009187{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009188 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009189 struct pattern_list *lst;
9190 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009191
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009192 list_for_each_entry(lst, &expr->patterns, list) {
9193 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009194
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009195 /* well-known method */
9196 if (pattern->val.i != HTTP_METH_OTHER) {
9197 if (smp->data.meth.meth == pattern->val.i)
9198 return pattern;
9199 else
9200 continue;
9201 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009202
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009203 /* Other method, we must compare the strings */
9204 if (pattern->len != smp->data.meth.str.len)
9205 continue;
9206
9207 icase = pattern->flags & PAT_F_IGNORE_CASE;
9208 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9209 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9210 return pattern;
9211 }
9212 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009213}
9214
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009215static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009216smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009217 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009218{
9219 struct http_txn *txn = l7;
9220 char *ptr;
9221 int len;
9222
Willy Tarreauc0239e02012-04-16 14:42:55 +02009223 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009224
Willy Tarreau8797c062007-05-07 00:55:35 +02009225 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009226 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009227
9228 while ((len-- > 0) && (*ptr++ != '/'));
9229 if (len <= 0)
9230 return 0;
9231
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009232 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009233 smp->data.str.str = ptr;
9234 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009235
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009236 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009237 return 1;
9238}
9239
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009240static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009241smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009242 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009243{
9244 struct http_txn *txn = l7;
9245 char *ptr;
9246 int len;
9247
Willy Tarreauc0239e02012-04-16 14:42:55 +02009248 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009249
Willy Tarreauf26b2522012-12-14 08:33:14 +01009250 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9251 return 0;
9252
Willy Tarreau8797c062007-05-07 00:55:35 +02009253 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009254 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009255
9256 while ((len-- > 0) && (*ptr++ != '/'));
9257 if (len <= 0)
9258 return 0;
9259
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009260 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009261 smp->data.str.str = ptr;
9262 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009263
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009264 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009265 return 1;
9266}
9267
9268/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009269static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009270smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009271 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009272{
9273 struct http_txn *txn = l7;
9274 char *ptr;
9275 int len;
9276
Willy Tarreauc0239e02012-04-16 14:42:55 +02009277 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009278
Willy Tarreauf26b2522012-12-14 08:33:14 +01009279 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9280 return 0;
9281
Willy Tarreau8797c062007-05-07 00:55:35 +02009282 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009283 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009284
Willy Tarreauf853c462012-04-23 18:53:56 +02009285 smp->type = SMP_T_UINT;
9286 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009287 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009288 return 1;
9289}
9290
9291/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009292static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009293smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009294 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009295{
9296 struct http_txn *txn = l7;
9297
Willy Tarreauc0239e02012-04-16 14:42:55 +02009298 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009299
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009300 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009301 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009302 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009303 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009304 return 1;
9305}
9306
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009307static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009308smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009309 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009310{
9311 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009312 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009313
Willy Tarreauc0239e02012-04-16 14:42:55 +02009314 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009315
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009316 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009317 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009318 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009319
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009320 smp->type = SMP_T_IPV4;
9321 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009322 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009323 return 1;
9324}
9325
9326static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009327smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009328 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009329{
9330 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009331 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009332
Willy Tarreauc0239e02012-04-16 14:42:55 +02009333 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009334
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009335 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009336 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9337 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009338
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009339 smp->type = SMP_T_UINT;
9340 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009341 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009342 return 1;
9343}
9344
Willy Tarreau185b5c42012-04-26 15:11:51 +02009345/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9346 * Accepts an optional argument of type string containing the header field name,
9347 * and an optional argument of type signed or unsigned integer to request an
9348 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009349 * headers are considered from the first one. It does not stop on commas and
9350 * returns full lines instead (useful for User-Agent or Date for example).
9351 */
9352static int
9353smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009354 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009355{
9356 struct http_txn *txn = l7;
9357 struct hdr_idx *idx = &txn->hdr_idx;
9358 struct hdr_ctx *ctx = smp->ctx.a[0];
9359 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9360 int occ = 0;
9361 const char *name_str = NULL;
9362 int name_len = 0;
9363
9364 if (!ctx) {
9365 /* first call */
9366 ctx = &static_hdr_ctx;
9367 ctx->idx = 0;
9368 smp->ctx.a[0] = ctx;
9369 }
9370
9371 if (args) {
9372 if (args[0].type != ARGT_STR)
9373 return 0;
9374 name_str = args[0].data.str.str;
9375 name_len = args[0].data.str.len;
9376
9377 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9378 occ = args[1].data.uint;
9379 }
9380
9381 CHECK_HTTP_MESSAGE_FIRST();
9382
9383 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9384 /* search for header from the beginning */
9385 ctx->idx = 0;
9386
9387 if (!occ && !(opt & SMP_OPT_ITERATE))
9388 /* no explicit occurrence and single fetch => last header by default */
9389 occ = -1;
9390
9391 if (!occ)
9392 /* prepare to report multiple occurrences for ACL fetches */
9393 smp->flags |= SMP_F_NOT_LAST;
9394
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009395 smp->type = SMP_T_STR;
9396 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009397 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9398 return 1;
9399
9400 smp->flags &= ~SMP_F_NOT_LAST;
9401 return 0;
9402}
9403
9404/* 6. Check on HTTP header count. The number of occurrences is returned.
9405 * Accepts exactly 1 argument of type string. It does not stop on commas and
9406 * returns full lines instead (useful for User-Agent or Date for example).
9407 */
9408static int
9409smp_fetch_fhdr_cnt(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 Tarreau04ff9f12013-06-10 18:39:42 +02009411{
9412 struct http_txn *txn = l7;
9413 struct hdr_idx *idx = &txn->hdr_idx;
9414 struct hdr_ctx ctx;
9415 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9416 int cnt;
9417
9418 if (!args || args->type != ARGT_STR)
9419 return 0;
9420
9421 CHECK_HTTP_MESSAGE_FIRST();
9422
9423 ctx.idx = 0;
9424 cnt = 0;
9425 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9426 cnt++;
9427
9428 smp->type = SMP_T_UINT;
9429 smp->data.uint = cnt;
9430 smp->flags = SMP_F_VOL_HDR;
9431 return 1;
9432}
9433
9434/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9435 * Accepts an optional argument of type string containing the header field name,
9436 * and an optional argument of type signed or unsigned integer to request an
9437 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009438 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009439 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009440static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009441smp_fetch_hdr(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 Tarreau33a7e692007-06-10 19:45:56 +02009443{
9444 struct http_txn *txn = l7;
9445 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009446 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009447 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009448 int occ = 0;
9449 const char *name_str = NULL;
9450 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009451
Willy Tarreaua890d072013-04-02 12:01:06 +02009452 if (!ctx) {
9453 /* first call */
9454 ctx = &static_hdr_ctx;
9455 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009456 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009457 }
9458
Willy Tarreau185b5c42012-04-26 15:11:51 +02009459 if (args) {
9460 if (args[0].type != ARGT_STR)
9461 return 0;
9462 name_str = args[0].data.str.str;
9463 name_len = args[0].data.str.len;
9464
9465 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9466 occ = args[1].data.uint;
9467 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009468
Willy Tarreaue333ec92012-04-16 16:26:40 +02009469 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009470
Willy Tarreau185b5c42012-04-26 15:11:51 +02009471 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009472 /* search for header from the beginning */
9473 ctx->idx = 0;
9474
Willy Tarreau185b5c42012-04-26 15:11:51 +02009475 if (!occ && !(opt & SMP_OPT_ITERATE))
9476 /* no explicit occurrence and single fetch => last header by default */
9477 occ = -1;
9478
9479 if (!occ)
9480 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009481 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009482
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009483 smp->type = SMP_T_STR;
9484 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009485 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 +02009486 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009487
Willy Tarreau37406352012-04-23 16:16:37 +02009488 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009489 return 0;
9490}
9491
Willy Tarreauc11416f2007-06-17 16:58:38 +02009492/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009493 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009494 */
9495static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009496smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009497 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009498{
9499 struct http_txn *txn = l7;
9500 struct hdr_idx *idx = &txn->hdr_idx;
9501 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009502 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009503 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009504
Willy Tarreau24e32d82012-04-23 23:55:44 +02009505 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009506 return 0;
9507
Willy Tarreaue333ec92012-04-16 16:26:40 +02009508 CHECK_HTTP_MESSAGE_FIRST();
9509
Willy Tarreau33a7e692007-06-10 19:45:56 +02009510 ctx.idx = 0;
9511 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009512 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 +02009513 cnt++;
9514
Willy Tarreauf853c462012-04-23 18:53:56 +02009515 smp->type = SMP_T_UINT;
9516 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009517 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009518 return 1;
9519}
9520
Willy Tarreau185b5c42012-04-26 15:11:51 +02009521/* Fetch an HTTP header's integer value. The integer value is returned. It
9522 * takes a mandatory argument of type string and an optional one of type int
9523 * to designate a specific occurrence. It returns an unsigned integer, which
9524 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009525 */
9526static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009527smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009528 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009529{
Willy Tarreauef38c392013-07-22 16:29:32 +02009530 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009531
Willy Tarreauf853c462012-04-23 18:53:56 +02009532 if (ret > 0) {
9533 smp->type = SMP_T_UINT;
9534 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9535 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009536
Willy Tarreaud53e2422012-04-16 17:21:11 +02009537 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009538}
9539
Cyril Bonté69fa9922012-10-25 00:01:06 +02009540/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9541 * and an optional one of type int to designate a specific occurrence.
9542 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009543 */
9544static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009545smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009546 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009547{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009548 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009549
Willy Tarreauef38c392013-07-22 16:29:32 +02009550 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009551 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9552 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009553 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009554 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009555 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009556 if (smp->data.str.len < temp->size - 1) {
9557 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9558 temp->str[smp->data.str.len] = '\0';
9559 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9560 smp->type = SMP_T_IPV6;
9561 break;
9562 }
9563 }
9564 }
9565
Willy Tarreaud53e2422012-04-16 17:21:11 +02009566 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009567 if (!(smp->flags & SMP_F_NOT_LAST))
9568 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009569 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009570 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009571}
9572
Willy Tarreau737b0c12007-06-10 21:28:46 +02009573/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9574 * the first '/' after the possible hostname, and ends before the possible '?'.
9575 */
9576static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009577smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009578 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009579{
9580 struct http_txn *txn = l7;
9581 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009582
Willy Tarreauc0239e02012-04-16 14:42:55 +02009583 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009584
Willy Tarreau9b28e032012-10-12 23:49:43 +02009585 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009586 ptr = http_get_path(txn);
9587 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009588 return 0;
9589
9590 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009591 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009592 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009593
9594 while (ptr < end && *ptr != '?')
9595 ptr++;
9596
Willy Tarreauf853c462012-04-23 18:53:56 +02009597 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009598 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009599 return 1;
9600}
9601
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009602/* This produces a concatenation of the first occurrence of the Host header
9603 * followed by the path component if it begins with a slash ('/'). This means
9604 * that '*' will not be added, resulting in exactly the first Host entry.
9605 * If no Host header is found, then the path is returned as-is. The returned
9606 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009607 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009608 */
9609static int
9610smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009611 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009612{
9613 struct http_txn *txn = l7;
9614 char *ptr, *end, *beg;
9615 struct hdr_ctx ctx;
9616
9617 CHECK_HTTP_MESSAGE_FIRST();
9618
9619 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009620 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009621 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009622
9623 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009624 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009625 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009626 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009627 smp->data.str.len = ctx.vlen;
9628
9629 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009630 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009631 beg = http_get_path(txn);
9632 if (!beg)
9633 beg = end;
9634
9635 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9636
9637 if (beg < ptr && *beg == '/') {
9638 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9639 smp->data.str.len += ptr - beg;
9640 }
9641
9642 smp->flags = SMP_F_VOL_1ST;
9643 return 1;
9644}
9645
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009646/* This produces a 32-bit hash of the concatenation of the first occurrence of
9647 * the Host header followed by the path component if it begins with a slash ('/').
9648 * This means that '*' will not be added, resulting in exactly the first Host
9649 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009650 * is hashed using the path hash followed by a full avalanche hash and provides a
9651 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009652 * high-traffic sites without having to store whole paths.
9653 */
9654static int
9655smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009656 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009657{
9658 struct http_txn *txn = l7;
9659 struct hdr_ctx ctx;
9660 unsigned int hash = 0;
9661 char *ptr, *beg, *end;
9662 int len;
9663
9664 CHECK_HTTP_MESSAGE_FIRST();
9665
9666 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009667 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009668 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9669 ptr = ctx.line + ctx.val;
9670 len = ctx.vlen;
9671 while (len--)
9672 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9673 }
9674
9675 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009676 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009677 beg = http_get_path(txn);
9678 if (!beg)
9679 beg = end;
9680
9681 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9682
9683 if (beg < ptr && *beg == '/') {
9684 while (beg < ptr)
9685 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9686 }
9687 hash = full_hash(hash);
9688
9689 smp->type = SMP_T_UINT;
9690 smp->data.uint = hash;
9691 smp->flags = SMP_F_VOL_1ST;
9692 return 1;
9693}
9694
Willy Tarreau4a550602012-12-09 14:53:32 +01009695/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009696 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9697 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9698 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009699 * that in environments where IPv6 is insignificant, truncating the output to
9700 * 8 bytes would still work.
9701 */
9702static int
9703smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009704 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009705{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009706 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009707 struct connection *cli_conn = objt_conn(l4->si[0].end);
9708
9709 if (!cli_conn)
9710 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009711
Willy Tarreauef38c392013-07-22 16:29:32 +02009712 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009713 return 0;
9714
Willy Tarreau47ca5452012-12-23 20:22:19 +01009715 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009716 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9717 temp->len += sizeof(smp->data.uint);
9718
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009719 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009720 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009721 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009722 temp->len += 4;
9723 break;
9724 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009725 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009726 temp->len += 16;
9727 break;
9728 default:
9729 return 0;
9730 }
9731
9732 smp->data.str = *temp;
9733 smp->type = SMP_T_BIN;
9734 return 1;
9735}
9736
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009737static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009738smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009739 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009740{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009741 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9742 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9743 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009744
Willy Tarreau24e32d82012-04-23 23:55:44 +02009745 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009746
Willy Tarreauf853c462012-04-23 18:53:56 +02009747 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009748 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009749 return 1;
9750}
9751
Willy Tarreau7f18e522010-10-22 20:04:13 +02009752/* return a valid test if the current request is the first one on the connection */
9753static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009754smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009755 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009756{
9757 if (!s)
9758 return 0;
9759
Willy Tarreauf853c462012-04-23 18:53:56 +02009760 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009761 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009762 return 1;
9763}
9764
Willy Tarreau34db1082012-04-19 17:16:54 +02009765/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009766static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009767smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009768 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009769{
9770
Willy Tarreau24e32d82012-04-23 23:55:44 +02009771 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009772 return 0;
9773
Willy Tarreauc0239e02012-04-16 14:42:55 +02009774 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009775
Willy Tarreauc0239e02012-04-16 14:42:55 +02009776 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009777 return 0;
9778
Willy Tarreauf853c462012-04-23 18:53:56 +02009779 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009780 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009781 return 1;
9782}
Willy Tarreau8797c062007-05-07 00:55:35 +02009783
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009784/* Accepts exactly 1 argument of type userlist */
9785static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009786smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009787 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009788{
9789
9790 if (!args || args->type != ARGT_USR)
9791 return 0;
9792
9793 CHECK_HTTP_MESSAGE_FIRST();
9794
9795 if (!get_http_auth(l4))
9796 return 0;
9797
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009798 /* if the user does not belong to the userlist or has a wrong password,
9799 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009800 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009801 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009802 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9803 return 0;
9804
9805 /* pat_match_auth() will need the user list */
9806 smp->ctx.a[0] = args->data.usr;
9807
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009808 smp->type = SMP_T_STR;
9809 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009810 smp->data.str.str = l4->txn.auth.user;
9811 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009812
9813 return 1;
9814}
9815
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009816/* Try to find the next occurrence of a cookie name in a cookie header value.
9817 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9818 * the cookie value is returned into *value and *value_l, and the function
9819 * returns a pointer to the next pointer to search from if the value was found.
9820 * Otherwise if the cookie was not found, NULL is returned and neither value
9821 * nor value_l are touched. The input <hdr> string should first point to the
9822 * header's value, and the <hdr_end> pointer must point to the first character
9823 * not part of the value. <list> must be non-zero if value may represent a list
9824 * of values (cookie headers). This makes it faster to abort parsing when no
9825 * list is expected.
9826 */
9827static char *
9828extract_cookie_value(char *hdr, const char *hdr_end,
9829 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009830 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009831{
9832 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9833 char *next;
9834
9835 /* we search at least a cookie name followed by an equal, and more
9836 * generally something like this :
9837 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9838 */
9839 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9840 /* Iterate through all cookies on this line */
9841
9842 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9843 att_beg++;
9844
9845 /* find att_end : this is the first character after the last non
9846 * space before the equal. It may be equal to hdr_end.
9847 */
9848 equal = att_end = att_beg;
9849
9850 while (equal < hdr_end) {
9851 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9852 break;
9853 if (http_is_spht[(unsigned char)*equal++])
9854 continue;
9855 att_end = equal;
9856 }
9857
9858 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9859 * is between <att_beg> and <equal>, both may be identical.
9860 */
9861
9862 /* look for end of cookie if there is an equal sign */
9863 if (equal < hdr_end && *equal == '=') {
9864 /* look for the beginning of the value */
9865 val_beg = equal + 1;
9866 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9867 val_beg++;
9868
9869 /* find the end of the value, respecting quotes */
9870 next = find_cookie_value_end(val_beg, hdr_end);
9871
9872 /* make val_end point to the first white space or delimitor after the value */
9873 val_end = next;
9874 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9875 val_end--;
9876 } else {
9877 val_beg = val_end = next = equal;
9878 }
9879
9880 /* We have nothing to do with attributes beginning with '$'. However,
9881 * they will automatically be removed if a header before them is removed,
9882 * since they're supposed to be linked together.
9883 */
9884 if (*att_beg == '$')
9885 continue;
9886
9887 /* Ignore cookies with no equal sign */
9888 if (equal == next)
9889 continue;
9890
9891 /* Now we have the cookie name between att_beg and att_end, and
9892 * its value between val_beg and val_end.
9893 */
9894
9895 if (att_end - att_beg == cookie_name_l &&
9896 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9897 /* let's return this value and indicate where to go on from */
9898 *value = val_beg;
9899 *value_l = val_end - val_beg;
9900 return next + 1;
9901 }
9902
9903 /* Set-Cookie headers only have the name in the first attr=value part */
9904 if (!list)
9905 break;
9906 }
9907
9908 return NULL;
9909}
9910
William Lallemanda43ba4e2014-01-28 18:14:25 +01009911/* Fetch a captured HTTP request header. The index is the position of
9912 * the "capture" option in the configuration file
9913 */
9914static int
9915smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9916 const struct arg *args, struct sample *smp, const char *kw)
9917{
9918 struct proxy *fe = l4->fe;
9919 struct http_txn *txn = l7;
9920 int idx;
9921
9922 if (!args || args->type != ARGT_UINT)
9923 return 0;
9924
9925 idx = args->data.uint;
9926
9927 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9928 return 0;
9929
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009930 smp->type = SMP_T_STR;
9931 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009932 smp->data.str.str = txn->req.cap[idx];
9933 smp->data.str.len = strlen(txn->req.cap[idx]);
9934
9935 return 1;
9936}
9937
9938/* Fetch a captured HTTP response header. The index is the position of
9939 * the "capture" option in the configuration file
9940 */
9941static int
9942smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9943 const struct arg *args, struct sample *smp, const char *kw)
9944{
9945 struct proxy *fe = l4->fe;
9946 struct http_txn *txn = l7;
9947 int idx;
9948
9949 if (!args || args->type != ARGT_UINT)
9950 return 0;
9951
9952 idx = args->data.uint;
9953
9954 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9955 return 0;
9956
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009957 smp->type = SMP_T_STR;
9958 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009959 smp->data.str.str = txn->rsp.cap[idx];
9960 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9961
9962 return 1;
9963}
9964
William Lallemand65ad6e12014-01-31 15:08:02 +01009965/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9966static int
9967smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9968 const struct arg *args, struct sample *smp, const char *kw)
9969{
9970 struct chunk *temp;
9971 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009972 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009973
9974 if (!txn->uri)
9975 return 0;
9976
William Lallemand96a77852014-02-05 00:30:02 +01009977 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009978
William Lallemand96a77852014-02-05 00:30:02 +01009979 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9980 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009981
William Lallemand96a77852014-02-05 00:30:02 +01009982 temp = get_trash_chunk();
9983 temp->str = txn->uri;
9984 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009985 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009986 smp->type = SMP_T_STR;
9987 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009988
9989 return 1;
9990
9991}
9992
9993/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9994static int
9995smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9996 const struct arg *args, struct sample *smp, const char *kw)
9997{
9998 struct chunk *temp;
9999 struct http_txn *txn = l7;
10000 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010001
10002 if (!txn->uri)
10003 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010004
William Lallemand65ad6e12014-01-31 15:08:02 +010010005 ptr = txn->uri;
10006
10007 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10008 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010009
William Lallemand65ad6e12014-01-31 15:08:02 +010010010 if (!*ptr)
10011 return 0;
10012
10013 ptr++; /* skip the space */
10014
10015 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010016 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010017 if (!ptr)
10018 return 0;
10019 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10020 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010021
10022 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010023 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010024 smp->type = SMP_T_STR;
10025 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010026
10027 return 1;
10028}
10029
10030
Willy Tarreaue333ec92012-04-16 16:26:40 +020010031/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010032 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010033 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010034 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010035 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010036 * Accepts exactly 1 argument of type string. If the input options indicate
10037 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010038 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010039 */
10040static int
Willy Tarreau51539362012-05-08 12:46:28 +020010041smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010042 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010043{
10044 struct http_txn *txn = l7;
10045 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010046 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010047 const struct http_msg *msg;
10048 const char *hdr_name;
10049 int hdr_name_len;
10050 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010051 int occ = 0;
10052 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010053
Willy Tarreau24e32d82012-04-23 23:55:44 +020010054 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010055 return 0;
10056
Willy Tarreaua890d072013-04-02 12:01:06 +020010057 if (!ctx) {
10058 /* first call */
10059 ctx = &static_hdr_ctx;
10060 ctx->idx = 0;
10061 smp->ctx.a[2] = ctx;
10062 }
10063
Willy Tarreaue333ec92012-04-16 16:26:40 +020010064 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010065
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010066 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010067 msg = &txn->req;
10068 hdr_name = "Cookie";
10069 hdr_name_len = 6;
10070 } else {
10071 msg = &txn->rsp;
10072 hdr_name = "Set-Cookie";
10073 hdr_name_len = 10;
10074 }
10075
Willy Tarreau28376d62012-04-26 21:26:10 +020010076 if (!occ && !(opt & SMP_OPT_ITERATE))
10077 /* no explicit occurrence and single fetch => last cookie by default */
10078 occ = -1;
10079
10080 /* OK so basically here, either we want only one value and it's the
10081 * last one, or we want to iterate over all of them and we fetch the
10082 * next one.
10083 */
10084
Willy Tarreau9b28e032012-10-12 23:49:43 +020010085 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010086 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010087 /* search for the header from the beginning, we must first initialize
10088 * the search parameters.
10089 */
Willy Tarreau37406352012-04-23 16:16:37 +020010090 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010091 ctx->idx = 0;
10092 }
10093
Willy Tarreau28376d62012-04-26 21:26:10 +020010094 smp->flags |= SMP_F_VOL_HDR;
10095
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010096 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010097 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10098 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010099 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10100 goto out;
10101
Willy Tarreau24e32d82012-04-23 23:55:44 +020010102 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010103 continue;
10104
Willy Tarreau37406352012-04-23 16:16:37 +020010105 smp->ctx.a[0] = ctx->line + ctx->val;
10106 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010107 }
10108
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010109 smp->type = SMP_T_STR;
10110 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010111 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010112 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010113 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010114 &smp->data.str.str,
10115 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010116 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010117 found = 1;
10118 if (occ >= 0) {
10119 /* one value was returned into smp->data.str.{str,len} */
10120 smp->flags |= SMP_F_NOT_LAST;
10121 return 1;
10122 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010123 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010124 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010125 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010126 /* all cookie headers and values were scanned. If we're looking for the
10127 * last occurrence, we may return it now.
10128 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010129 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010130 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010131 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010132}
10133
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010134/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010135 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010136 * multiple cookies may be parsed on the same line. The returned sample is of
10137 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010138 */
10139static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010140smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010141 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010142{
10143 struct http_txn *txn = l7;
10144 struct hdr_idx *idx = &txn->hdr_idx;
10145 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010146 const struct http_msg *msg;
10147 const char *hdr_name;
10148 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010149 int cnt;
10150 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010151 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010152
Willy Tarreau24e32d82012-04-23 23:55:44 +020010153 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010154 return 0;
10155
Willy Tarreaue333ec92012-04-16 16:26:40 +020010156 CHECK_HTTP_MESSAGE_FIRST();
10157
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010158 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010159 msg = &txn->req;
10160 hdr_name = "Cookie";
10161 hdr_name_len = 6;
10162 } else {
10163 msg = &txn->rsp;
10164 hdr_name = "Set-Cookie";
10165 hdr_name_len = 10;
10166 }
10167
Willy Tarreau9b28e032012-10-12 23:49:43 +020010168 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010169 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010170 ctx.idx = 0;
10171 cnt = 0;
10172
10173 while (1) {
10174 /* Note: val_beg == NULL every time we need to fetch a new header */
10175 if (!val_beg) {
10176 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10177 break;
10178
Willy Tarreau24e32d82012-04-23 23:55:44 +020010179 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010180 continue;
10181
10182 val_beg = ctx.line + ctx.val;
10183 val_end = val_beg + ctx.vlen;
10184 }
10185
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010186 smp->type = SMP_T_STR;
10187 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010188 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010189 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010190 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010191 &smp->data.str.str,
10192 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010193 cnt++;
10194 }
10195 }
10196
Willy Tarreaub169eba2013-12-16 15:14:43 +010010197 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010198 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010199 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010200 return 1;
10201}
10202
Willy Tarreau51539362012-05-08 12:46:28 +020010203/* Fetch an cookie's integer value. The integer value is returned. It
10204 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10205 */
10206static int
10207smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010208 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010209{
Willy Tarreauef38c392013-07-22 16:29:32 +020010210 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010211
10212 if (ret > 0) {
10213 smp->type = SMP_T_UINT;
10214 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10215 }
10216
10217 return ret;
10218}
10219
Willy Tarreau8797c062007-05-07 00:55:35 +020010220/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010221/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010222/************************************************************************/
10223
David Cournapeau16023ee2010-12-23 20:55:41 +090010224/*
10225 * Given a path string and its length, find the position of beginning of the
10226 * query string. Returns NULL if no query string is found in the path.
10227 *
10228 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10229 *
10230 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10231 */
bedis4c75cca2012-10-05 08:38:24 +020010232static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010233{
10234 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010235
bedis4c75cca2012-10-05 08:38:24 +020010236 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010237 return p ? p + 1 : NULL;
10238}
10239
bedis4c75cca2012-10-05 08:38:24 +020010240static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010241{
bedis4c75cca2012-10-05 08:38:24 +020010242 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010243}
10244
10245/*
10246 * Given a url parameter, find the starting position of the first occurence,
10247 * or NULL if the parameter is not found.
10248 *
10249 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10250 * the function will return query_string+8.
10251 */
10252static char*
10253find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010254 char* url_param_name, size_t url_param_name_l,
10255 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010256{
10257 char *pos, *last;
10258
10259 pos = query_string;
10260 last = query_string + query_string_l - url_param_name_l - 1;
10261
10262 while (pos <= last) {
10263 if (pos[url_param_name_l] == '=') {
10264 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10265 return pos;
10266 pos += url_param_name_l + 1;
10267 }
bedis4c75cca2012-10-05 08:38:24 +020010268 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010269 pos++;
10270 pos++;
10271 }
10272 return NULL;
10273}
10274
10275/*
10276 * Given a url parameter name, returns its value and size into *value and
10277 * *value_l respectively, and returns non-zero. If the parameter is not found,
10278 * zero is returned and value/value_l are not touched.
10279 */
10280static int
10281find_url_param_value(char* path, size_t path_l,
10282 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010283 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010284{
10285 char *query_string, *qs_end;
10286 char *arg_start;
10287 char *value_start, *value_end;
10288
bedis4c75cca2012-10-05 08:38:24 +020010289 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010290 if (!query_string)
10291 return 0;
10292
10293 qs_end = path + path_l;
10294 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010295 url_param_name, url_param_name_l,
10296 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010297 if (!arg_start)
10298 return 0;
10299
10300 value_start = arg_start + url_param_name_l + 1;
10301 value_end = value_start;
10302
bedis4c75cca2012-10-05 08:38:24 +020010303 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010304 value_end++;
10305
10306 *value = value_start;
10307 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010308 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010309}
10310
10311static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010312smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010313 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010314{
bedis4c75cca2012-10-05 08:38:24 +020010315 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010316 struct http_txn *txn = l7;
10317 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010318
bedis4c75cca2012-10-05 08:38:24 +020010319 if (!args || args[0].type != ARGT_STR ||
10320 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010321 return 0;
10322
10323 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010324
bedis4c75cca2012-10-05 08:38:24 +020010325 if (args[1].type)
10326 delim = *args[1].data.str.str;
10327
Willy Tarreau9b28e032012-10-12 23:49:43 +020010328 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010329 args->data.str.str, args->data.str.len,
10330 &smp->data.str.str, &smp->data.str.len,
10331 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010332 return 0;
10333
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010334 smp->type = SMP_T_STR;
10335 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010336 return 1;
10337}
10338
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010339/* Return the signed integer value for the specified url parameter (see url_param
10340 * above).
10341 */
10342static int
10343smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010344 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010345{
Willy Tarreauef38c392013-07-22 16:29:32 +020010346 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010347
10348 if (ret > 0) {
10349 smp->type = SMP_T_UINT;
10350 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10351 }
10352
10353 return ret;
10354}
10355
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010356/* This produces a 32-bit hash of the concatenation of the first occurrence of
10357 * the Host header followed by the path component if it begins with a slash ('/').
10358 * This means that '*' will not be added, resulting in exactly the first Host
10359 * entry. If no Host header is found, then the path is used. The resulting value
10360 * is hashed using the url hash followed by a full avalanche hash and provides a
10361 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10362 * high-traffic sites without having to store whole paths.
10363 * this differs from the base32 functions in that it includes the url parameters
10364 * as well as the path
10365 */
10366static int
10367smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010368 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010369{
10370 struct http_txn *txn = l7;
10371 struct hdr_ctx ctx;
10372 unsigned int hash = 0;
10373 char *ptr, *beg, *end;
10374 int len;
10375
10376 CHECK_HTTP_MESSAGE_FIRST();
10377
10378 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010379 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010380 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10381 ptr = ctx.line + ctx.val;
10382 len = ctx.vlen;
10383 while (len--)
10384 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10385 }
10386
10387 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010388 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010389 beg = http_get_path(txn);
10390 if (!beg)
10391 beg = end;
10392
10393 for (ptr = beg; ptr < end ; ptr++);
10394
10395 if (beg < ptr && *beg == '/') {
10396 while (beg < ptr)
10397 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10398 }
10399 hash = full_hash(hash);
10400
10401 smp->type = SMP_T_UINT;
10402 smp->data.uint = hash;
10403 smp->flags = SMP_F_VOL_1ST;
10404 return 1;
10405}
10406
10407/* This concatenates the source address with the 32-bit hash of the Host and
10408 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10409 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10410 * on the source address length. The URL hash is stored before the address so
10411 * that in environments where IPv6 is insignificant, truncating the output to
10412 * 8 bytes would still work.
10413 */
10414static int
10415smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010416 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010417{
10418 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010419 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010420
Willy Tarreaue155ec22013-11-18 18:33:22 +010010421 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010422 return 0;
10423
10424 temp = get_trash_chunk();
10425 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10426 temp->len += sizeof(smp->data.uint);
10427
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010428 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010429 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010430 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010431 temp->len += 4;
10432 break;
10433 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010434 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010435 temp->len += 16;
10436 break;
10437 default:
10438 return 0;
10439 }
10440
10441 smp->data.str = *temp;
10442 smp->type = SMP_T_BIN;
10443 return 1;
10444}
10445
Willy Tarreau185b5c42012-04-26 15:11:51 +020010446/* This function is used to validate the arguments passed to any "hdr" fetch
10447 * keyword. These keywords support an optional positive or negative occurrence
10448 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10449 * is assumed that the types are already the correct ones. Returns 0 on error,
10450 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10451 * error message in case of error, that the caller is responsible for freeing.
10452 * The initial location must either be freeable or NULL.
10453 */
10454static int val_hdr(struct arg *arg, char **err_msg)
10455{
10456 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010457 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010458 return 0;
10459 }
10460 return 1;
10461}
10462
Willy Tarreau276fae92013-07-25 14:36:01 +020010463/* takes an UINT value on input supposed to represent the time since EPOCH,
10464 * adds an optional offset found in args[0] and emits a string representing
10465 * the date in RFC-1123/5322 format.
10466 */
10467static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10468{
10469 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10470 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10471 struct chunk *temp;
10472 struct tm *tm;
10473 time_t curr_date = smp->data.uint;
10474
10475 /* add offset */
10476 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10477 curr_date += args[0].data.sint;
10478
10479 tm = gmtime(&curr_date);
10480
10481 temp = get_trash_chunk();
10482 temp->len = snprintf(temp->str, temp->size - temp->len,
10483 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10484 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10485 tm->tm_hour, tm->tm_min, tm->tm_sec);
10486
10487 smp->data.str = *temp;
10488 smp->type = SMP_T_STR;
10489 return 1;
10490}
10491
Thierry FOURNIERad903512014-04-11 17:51:01 +020010492/* Match language range with language tag. RFC2616 14.4:
10493 *
10494 * A language-range matches a language-tag if it exactly equals
10495 * the tag, or if it exactly equals a prefix of the tag such
10496 * that the first tag character following the prefix is "-".
10497 *
10498 * Return 1 if the strings match, else return 0.
10499 */
10500static inline int language_range_match(const char *range, int range_len,
10501 const char *tag, int tag_len)
10502{
10503 const char *end = range + range_len;
10504 const char *tend = tag + tag_len;
10505 while (range < end) {
10506 if (*range == '-' && tag == tend)
10507 return 1;
10508 if (*range != *tag || tag == tend)
10509 return 0;
10510 range++;
10511 tag++;
10512 }
10513 /* Return true only if the last char of the tag is matched. */
10514 return tag == tend;
10515}
10516
10517/* Arguments: The list of expected value, the number of parts returned and the separator */
10518static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
10519{
10520 const char *al = smp->data.str.str;
10521 const char *end = al + smp->data.str.len;
10522 const char *token;
10523 int toklen;
10524 int qvalue;
10525 const char *str;
10526 const char *w;
10527 int best_q = 0;
10528
10529 /* Set the constant to the sample, because the output of the
10530 * function will be peek in the constant configuration string.
10531 */
10532 smp->flags |= SMP_F_CONST;
10533 smp->data.str.size = 0;
10534 smp->data.str.str = "";
10535 smp->data.str.len = 0;
10536
10537 /* Parse the accept language */
10538 while (1) {
10539
10540 /* Jump spaces, quit if the end is detected. */
10541 while (al < end && isspace(*al))
10542 al++;
10543 if (al >= end)
10544 break;
10545
10546 /* Start of the fisrt word. */
10547 token = al;
10548
10549 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
10550 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
10551 al++;
10552 if (al == token)
10553 goto expect_comma;
10554
10555 /* Length of the token. */
10556 toklen = al - token;
10557 qvalue = 1000;
10558
10559 /* Check if the token exists in the list. If the token not exists,
10560 * jump to the next token.
10561 */
10562 str = args[0].data.str.str;
10563 w = str;
10564 while (1) {
10565 if (*str == ';' || *str == '\0') {
10566 if (language_range_match(token, toklen, w, str-w))
10567 goto look_for_q;
10568 if (*str == '\0')
10569 goto expect_comma;
10570 w = str + 1;
10571 }
10572 str++;
10573 }
10574 goto expect_comma;
10575
10576look_for_q:
10577
10578 /* Jump spaces, quit if the end is detected. */
10579 while (al < end && isspace(*al))
10580 al++;
10581 if (al >= end)
10582 goto process_value;
10583
10584 /* If ',' is found, process the result */
10585 if (*al == ',')
10586 goto process_value;
10587
10588 /* If the character is different from ';', look
10589 * for the end of the header part in best effort.
10590 */
10591 if (*al != ';')
10592 goto expect_comma;
10593
10594 /* Assumes that the char is ';', now expect "q=". */
10595 al++;
10596
10597 /* Jump spaces, process value if the end is detected. */
10598 while (al < end && isspace(*al))
10599 al++;
10600 if (al >= end)
10601 goto process_value;
10602
10603 /* Expect 'q'. If no 'q', continue in best effort */
10604 if (*al != 'q')
10605 goto process_value;
10606 al++;
10607
10608 /* Jump spaces, process value if the end is detected. */
10609 while (al < end && isspace(*al))
10610 al++;
10611 if (al >= end)
10612 goto process_value;
10613
10614 /* Expect '='. If no '=', continue in best effort */
10615 if (*al != '=')
10616 goto process_value;
10617 al++;
10618
10619 /* Jump spaces, process value if the end is detected. */
10620 while (al < end && isspace(*al))
10621 al++;
10622 if (al >= end)
10623 goto process_value;
10624
10625 /* Parse the q value. */
10626 qvalue = parse_qvalue(al, &al);
10627
10628process_value:
10629
10630 /* If the new q value is the best q value, then store the associated
10631 * language in the response. If qvalue is the biggest value (1000),
10632 * break the process.
10633 */
10634 if (qvalue > best_q) {
10635 smp->data.str.str = (char *)w;
10636 smp->data.str.len = str - w;
10637 if (qvalue >= 1000)
10638 break;
10639 best_q = qvalue;
10640 }
10641
10642expect_comma:
10643
10644 /* Expect comma or end. If the end is detected, quit the loop. */
10645 while (al < end && *al != ',')
10646 al++;
10647 if (al >= end)
10648 break;
10649
10650 /* Comma is found, jump it and restart the analyzer. */
10651 al++;
10652 }
10653
10654 /* Set default value if required. */
10655 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
10656 smp->data.str.str = args[1].data.str.str;
10657 smp->data.str.len = args[1].data.str.len;
10658 }
10659
10660 /* Return true only if a matching language was found. */
10661 return smp->data.str.len != 0;
10662}
10663
Willy Tarreau4a568972010-05-12 08:08:50 +020010664/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010665/* All supported ACL keywords must be declared here. */
10666/************************************************************************/
10667
10668/* Note: must not be declared <const> as its list will be overwritten.
10669 * Please take care of keeping this list alphabetically sorted.
10670 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010671static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010672 { "base", "base", PAT_MATCH_STR },
10673 { "base_beg", "base", PAT_MATCH_BEG },
10674 { "base_dir", "base", PAT_MATCH_DIR },
10675 { "base_dom", "base", PAT_MATCH_DOM },
10676 { "base_end", "base", PAT_MATCH_END },
10677 { "base_len", "base", PAT_MATCH_LEN },
10678 { "base_reg", "base", PAT_MATCH_REG },
10679 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010680
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010681 { "cook", "req.cook", PAT_MATCH_STR },
10682 { "cook_beg", "req.cook", PAT_MATCH_BEG },
10683 { "cook_dir", "req.cook", PAT_MATCH_DIR },
10684 { "cook_dom", "req.cook", PAT_MATCH_DOM },
10685 { "cook_end", "req.cook", PAT_MATCH_END },
10686 { "cook_len", "req.cook", PAT_MATCH_LEN },
10687 { "cook_reg", "req.cook", PAT_MATCH_REG },
10688 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010689
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010690 { "hdr", "req.hdr", PAT_MATCH_STR },
10691 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
10692 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
10693 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
10694 { "hdr_end", "req.hdr", PAT_MATCH_END },
10695 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
10696 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
10697 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010698
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010699 /* these two declarations uses strings with list storage (in place
10700 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
10701 * and delete functions are relative to the list management. The parse
10702 * and match method are related to the corresponding fetch methods. This
10703 * is very particular ACL declaration mode.
10704 */
10705 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
10706 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010707
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010708 { "path", "path", PAT_MATCH_STR },
10709 { "path_beg", "path", PAT_MATCH_BEG },
10710 { "path_dir", "path", PAT_MATCH_DIR },
10711 { "path_dom", "path", PAT_MATCH_DOM },
10712 { "path_end", "path", PAT_MATCH_END },
10713 { "path_len", "path", PAT_MATCH_LEN },
10714 { "path_reg", "path", PAT_MATCH_REG },
10715 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010716
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010717 { "req_ver", "req.ver", PAT_MATCH_STR },
10718 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010719
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010720 { "scook", "res.cook", PAT_MATCH_STR },
10721 { "scook_beg", "res.cook", PAT_MATCH_BEG },
10722 { "scook_dir", "res.cook", PAT_MATCH_DIR },
10723 { "scook_dom", "res.cook", PAT_MATCH_DOM },
10724 { "scook_end", "res.cook", PAT_MATCH_END },
10725 { "scook_len", "res.cook", PAT_MATCH_LEN },
10726 { "scook_reg", "res.cook", PAT_MATCH_REG },
10727 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010728
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010729 { "shdr", "res.hdr", PAT_MATCH_STR },
10730 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
10731 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
10732 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
10733 { "shdr_end", "res.hdr", PAT_MATCH_END },
10734 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
10735 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
10736 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010737
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010738 { "url", "url", PAT_MATCH_STR },
10739 { "url_beg", "url", PAT_MATCH_BEG },
10740 { "url_dir", "url", PAT_MATCH_DIR },
10741 { "url_dom", "url", PAT_MATCH_DOM },
10742 { "url_end", "url", PAT_MATCH_END },
10743 { "url_len", "url", PAT_MATCH_LEN },
10744 { "url_reg", "url", PAT_MATCH_REG },
10745 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010746
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010747 { "urlp", "urlp", PAT_MATCH_STR },
10748 { "urlp_beg", "urlp", PAT_MATCH_BEG },
10749 { "urlp_dir", "urlp", PAT_MATCH_DIR },
10750 { "urlp_dom", "urlp", PAT_MATCH_DOM },
10751 { "urlp_end", "urlp", PAT_MATCH_END },
10752 { "urlp_len", "urlp", PAT_MATCH_LEN },
10753 { "urlp_reg", "urlp", PAT_MATCH_REG },
10754 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010755
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010756 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010757}};
10758
10759/************************************************************************/
10760/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010761/************************************************************************/
10762/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010763static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010764 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010765 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10766 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10767
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010768 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10769 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemand65ad6e12014-01-31 15:08:02 +010010770
William Lallemanda43ba4e2014-01-28 18:14:25 +010010771 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010772 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
10773 { "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 +010010774
Willy Tarreau409bcde2013-01-08 00:31:00 +010010775 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10776 * are only here to match the ACL's name, are request-only and are used
10777 * for ACL compatibility only.
10778 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010779 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10780 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010781 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10782 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10783
10784 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10785 * only here to match the ACL's name, are request-only and are used for
10786 * ACL compatibility only.
10787 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010788 { "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 +010010789 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10790 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10791 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10792
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010793 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010794 { "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 +010010795 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010796 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010797 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010798
10799 /* HTTP protocol on the request path */
10800 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010801 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010802
10803 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010804 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10805 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010806
10807 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010808 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10809 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010810
Willy Tarreau18ed2562013-01-14 15:56:36 +010010811 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010812 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010813 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10814 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10815
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010816 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010817 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010818 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010819 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10820 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10821 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10822
10823 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010824 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010825 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10826 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10827
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010828 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010829 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010830 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010831 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10832 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10833 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10834
Willy Tarreau409bcde2013-01-08 00:31:00 +010010835 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010836 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010837 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10838 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010839 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010840
10841 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010842 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010843 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10844 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10845 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10846
10847 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010848 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010849 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10850 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010851 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10852 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010853 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10854 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010855 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10856 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010857}};
10858
Willy Tarreau8797c062007-05-07 00:55:35 +020010859
Willy Tarreau276fae92013-07-25 14:36:01 +020010860/* Note: must not be declared <const> as its list will be overwritten */
10861static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020010862 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
10863 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020010864 { NULL, NULL, 0, 0, 0 },
10865}};
10866
Willy Tarreau8797c062007-05-07 00:55:35 +020010867__attribute__((constructor))
10868static void __http_protocol_init(void)
10869{
10870 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010871 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010872 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010873}
10874
10875
Willy Tarreau58f10d72006-12-04 02:26:12 +010010876/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010877 * Local variables:
10878 * c-indent-level: 8
10879 * c-basic-offset: 8
10880 * End:
10881 */