blob: 0c6a6233f7b370ca687f7d9c7bffda98549d22b4 [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
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100355/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200356 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100357 * RFC2616 for those chars.
358 */
359
360const char http_is_spht[256] = {
361 [' '] = 1, ['\t'] = 1,
362};
363
364const char http_is_crlf[256] = {
365 ['\r'] = 1, ['\n'] = 1,
366};
367
368const char http_is_lws[256] = {
369 [' '] = 1, ['\t'] = 1,
370 ['\r'] = 1, ['\n'] = 1,
371};
372
373const char http_is_sep[256] = {
374 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
375 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
376 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
377 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
378 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
379};
380
381const char http_is_ctl[256] = {
382 [0 ... 31] = 1,
383 [127] = 1,
384};
385
386/*
387 * A token is any ASCII char that is neither a separator nor a CTL char.
388 * Do not overwrite values in assignment since gcc-2.95 will not handle
389 * them correctly. Instead, define every non-CTL char's status.
390 */
391const char http_is_token[256] = {
392 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
393 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
394 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
395 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
396 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
397 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
398 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
399 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
400 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
401 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
402 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
403 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
404 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
405 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
406 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
407 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
408 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
409 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
410 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
411 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
412 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
413 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
414 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
415 ['|'] = 1, ['}'] = 0, ['~'] = 1,
416};
417
418
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100419/*
420 * An http ver_token is any ASCII which can be found in an HTTP version,
421 * which includes 'H', 'T', 'P', '/', '.' and any digit.
422 */
423const char http_is_ver_token[256] = {
424 ['.'] = 1, ['/'] = 1,
425 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
426 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
427 ['H'] = 1, ['P'] = 1, ['T'] = 1,
428};
429
430
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100431/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100432 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
433 */
434#if defined(DEBUG_FSM)
435static void http_silent_debug(int line, struct session *s)
436{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100437 chunk_printf(&trash,
438 "[%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",
439 line,
440 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
441 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);
442 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100443
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100444 chunk_printf(&trash,
445 " %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",
446 line,
447 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
448 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);
449 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100450}
451#else
452#define http_silent_debug(l,s) do { } while (0)
453#endif
454
455/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100456 * Adds a header and its CRLF at the tail of the message's buffer, just before
457 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100458 * The header is also automatically added to the index <hdr_idx>, and the end
459 * of headers is automatically adjusted. The number of bytes added is returned
460 * on success, otherwise <0 is returned indicating an error.
461 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100462int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100463{
464 int bytes, len;
465
466 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200467 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100468 if (!bytes)
469 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100470 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100471 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
472}
473
474/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100475 * Adds a header and its CRLF at the tail of the message's buffer, just before
476 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100477 * the buffer is only opened and the space reserved, but nothing is copied.
478 * The header is also automatically added to the index <hdr_idx>, and the end
479 * of headers is automatically adjusted. The number of bytes added is returned
480 * on success, otherwise <0 is returned indicating an error.
481 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100482int http_header_add_tail2(struct http_msg *msg,
483 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100484{
485 int bytes;
486
Willy Tarreau9b28e032012-10-12 23:49:43 +0200487 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100488 if (!bytes)
489 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100490 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
492}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493
494/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100495 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
496 * If so, returns the position of the first non-space character relative to
497 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
498 * to return a pointer to the place after the first space. Returns 0 if the
499 * header name does not match. Checks are case-insensitive.
500 */
501int http_header_match2(const char *hdr, const char *end,
502 const char *name, int len)
503{
504 const char *val;
505
506 if (hdr + len >= end)
507 return 0;
508 if (hdr[len] != ':')
509 return 0;
510 if (strncasecmp(hdr, name, len) != 0)
511 return 0;
512 val = hdr + len + 1;
513 while (val < end && HTTP_IS_SPHT(*val))
514 val++;
515 if ((val >= end) && (len + 2 <= end - hdr))
516 return len + 2; /* we may replace starting from second space */
517 return val - hdr;
518}
519
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200520/* Find the first or next occurrence of header <name> in message buffer <sol>
521 * using headers index <idx>, and return it in the <ctx> structure. This
522 * structure holds everything necessary to use the header and find next
523 * occurrence. If its <idx> member is 0, the header is searched from the
524 * beginning. Otherwise, the next occurrence is returned. The function returns
525 * 1 when it finds a value, and 0 when there is no more. It is very similar to
526 * http_find_header2() except that it is designed to work with full-line headers
527 * whose comma is not a delimiter but is part of the syntax. As a special case,
528 * if ctx->val is NULL when searching for a new values of a header, the current
529 * header is rescanned. This allows rescanning after a header deletion.
530 */
531int http_find_full_header2(const char *name, int len,
532 char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 char *eol, *sov;
536 int cur_idx, old_idx;
537
538 cur_idx = ctx->idx;
539 if (cur_idx) {
540 /* We have previously returned a header, let's search another one */
541 sol = ctx->line;
542 eol = sol + idx->v[cur_idx].len;
543 goto next_hdr;
544 }
545
546 /* first request for this header */
547 sol += hdr_idx_first_pos(idx);
548 old_idx = 0;
549 cur_idx = hdr_idx_first_idx(idx);
550 while (cur_idx) {
551 eol = sol + idx->v[cur_idx].len;
552
553 if (len == 0) {
554 /* No argument was passed, we want any header.
555 * To achieve this, we simply build a fake request. */
556 while (sol + len < eol && sol[len] != ':')
557 len++;
558 name = sol;
559 }
560
561 if ((len < eol - sol) &&
562 (sol[len] == ':') &&
563 (strncasecmp(sol, name, len) == 0)) {
564 ctx->del = len;
565 sov = sol + len + 1;
566 while (sov < eol && http_is_lws[(unsigned char)*sov])
567 sov++;
568
569 ctx->line = sol;
570 ctx->prev = old_idx;
571 ctx->idx = cur_idx;
572 ctx->val = sov - sol;
573 ctx->tws = 0;
574 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
575 eol--;
576 ctx->tws++;
577 }
578 ctx->vlen = eol - sov;
579 return 1;
580 }
581 next_hdr:
582 sol = eol + idx->v[cur_idx].cr + 1;
583 old_idx = cur_idx;
584 cur_idx = idx->v[cur_idx].next;
585 }
586 return 0;
587}
588
Willy Tarreau68085d82010-01-18 14:54:04 +0100589/* Find the end of the header value contained between <s> and <e>. See RFC2616,
590 * par 2.2 for more information. Note that it requires a valid header to return
591 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200592 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100593char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594{
595 int quoted, qdpair;
596
597 quoted = qdpair = 0;
598 for (; s < e; s++) {
599 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200600 else if (quoted) {
601 if (*s == '\\') qdpair = 1;
602 else if (*s == '"') quoted = 0;
603 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 else if (*s == '"') quoted = 1;
605 else if (*s == ',') return s;
606 }
607 return s;
608}
609
610/* Find the first or next occurrence of header <name> in message buffer <sol>
611 * using headers index <idx>, and return it in the <ctx> structure. This
612 * structure holds everything necessary to use the header and find next
613 * occurrence. If its <idx> member is 0, the header is searched from the
614 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100615 * 1 when it finds a value, and 0 when there is no more. It is designed to work
616 * with headers defined as comma-separated lists. As a special case, if ctx->val
617 * is NULL when searching for a new values of a header, the current header is
618 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200619 */
620int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200622 struct hdr_ctx *ctx)
623{
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 char *eol, *sov;
625 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 cur_idx = ctx->idx;
628 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200629 /* We have previously returned a value, let's search
630 * another one on the same line.
631 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200632 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200635 eol = sol + idx->v[cur_idx].len;
636
637 if (sov >= eol)
638 /* no more values in this header */
639 goto next_hdr;
640
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 /* values remaining for this header, skip the comma but save it
642 * for later use (eg: for header deletion).
643 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200644 sov++;
645 while (sov < eol && http_is_lws[(unsigned char)*sov])
646 sov++;
647
648 goto return_hdr;
649 }
650
651 /* first request for this header */
652 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100653 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200654 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200655 while (cur_idx) {
656 eol = sol + idx->v[cur_idx].len;
657
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200658 if (len == 0) {
659 /* No argument was passed, we want any header.
660 * To achieve this, we simply build a fake request. */
661 while (sol + len < eol && sol[len] != ':')
662 len++;
663 name = sol;
664 }
665
Willy Tarreau33a7e692007-06-10 19:45:56 +0200666 if ((len < eol - sol) &&
667 (sol[len] == ':') &&
668 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100669 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200670 sov = sol + len + 1;
671 while (sov < eol && http_is_lws[(unsigned char)*sov])
672 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100673
Willy Tarreau33a7e692007-06-10 19:45:56 +0200674 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100675 ctx->prev = old_idx;
676 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200677 ctx->idx = cur_idx;
678 ctx->val = sov - sol;
679
680 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200681 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200682 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200683 eol--;
684 ctx->tws++;
685 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200686 ctx->vlen = eol - sov;
687 return 1;
688 }
689 next_hdr:
690 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100691 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200692 cur_idx = idx->v[cur_idx].next;
693 }
694 return 0;
695}
696
697int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100698 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200699 struct hdr_ctx *ctx)
700{
701 return http_find_header2(name, strlen(name), sol, idx, ctx);
702}
703
Willy Tarreau68085d82010-01-18 14:54:04 +0100704/* Remove one value of a header. This only works on a <ctx> returned by one of
705 * the http_find_header functions. The value is removed, as well as surrounding
706 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100707 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 * message <msg>. The new index is returned. If it is zero, it means there is
709 * no more header, so any processing may stop. The ctx is always left in a form
710 * that can be handled by http_find_header2() to find next occurrence.
711 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100712int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100713{
714 int cur_idx = ctx->idx;
715 char *sol = ctx->line;
716 struct hdr_idx_elem *hdr;
717 int delta, skip_comma;
718
719 if (!cur_idx)
720 return 0;
721
722 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200725 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100726 http_msg_move_end(msg, delta);
727 idx->used--;
728 hdr->len = 0; /* unused entry */
729 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100730 if (idx->tail == ctx->idx)
731 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100732 ctx->idx = ctx->prev; /* walk back to the end of previous header */
733 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
734 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200735 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100736 return ctx->idx;
737 }
738
739 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200740 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
741 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100742 */
743
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200744 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200745 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100747 NULL, 0);
748 hdr->len += delta;
749 http_msg_move_end(msg, delta);
750 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200751 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100752 return ctx->idx;
753}
754
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100755/* This function handles a server error at the stream interface level. The
756 * stream interface is assumed to be already in a closed state. An optional
757 * message is copied into the input buffer, and an HTTP status code stored.
758 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100759 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100761static void http_server_error(struct session *t, struct stream_interface *si,
762 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200764 channel_auto_read(si->ob);
765 channel_abort(si->ob);
766 channel_auto_close(si->ob);
767 channel_erase(si->ob);
768 channel_auto_close(si->ib);
769 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100770 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100771 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200772 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 }
774 if (!(t->flags & SN_ERR_MASK))
775 t->flags |= err;
776 if (!(t->flags & SN_FINST_MASK))
777 t->flags |= finst;
778}
779
Willy Tarreau80587432006-12-24 17:47:20 +0100780/* This function returns the appropriate error location for the given session
781 * and message.
782 */
783
Willy Tarreau783f2582012-09-04 12:19:04 +0200784struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100785{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200786 if (s->be->errmsg[msgnum].str)
787 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100788 else if (s->fe->errmsg[msgnum].str)
789 return &s->fe->errmsg[msgnum];
790 else
791 return &http_err_chunks[msgnum];
792}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793
Willy Tarreau53b6c742006-12-17 13:37:46 +0100794/*
795 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
796 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
797 */
Willy Tarreauc8987b32013-12-06 23:43:17 +0100798static enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100799{
800 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100801 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100802
803 m = ((unsigned)*str - 'A');
804
805 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100806 for (h = http_methods[m]; h->len > 0; h++) {
807 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100808 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100809 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100810 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 };
812 return HTTP_METH_OTHER;
813 }
814 return HTTP_METH_NONE;
815
816}
817
Willy Tarreau21d2af32008-02-14 20:25:24 +0100818/* Parse the URI from the given transaction (which is assumed to be in request
819 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
820 * It is returned otherwise.
821 */
822static char *
823http_get_path(struct http_txn *txn)
824{
825 char *ptr, *end;
826
Willy Tarreau9b28e032012-10-12 23:49:43 +0200827 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100828 end = ptr + txn->req.sl.rq.u_l;
829
830 if (ptr >= end)
831 return NULL;
832
833 /* RFC2616, par. 5.1.2 :
834 * Request-URI = "*" | absuri | abspath | authority
835 */
836
837 if (*ptr == '*')
838 return NULL;
839
840 if (isalpha((unsigned char)*ptr)) {
841 /* this is a scheme as described by RFC3986, par. 3.1 */
842 ptr++;
843 while (ptr < end &&
844 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
845 ptr++;
846 /* skip '://' */
847 if (ptr == end || *ptr++ != ':')
848 return NULL;
849 if (ptr == end || *ptr++ != '/')
850 return NULL;
851 if (ptr == end || *ptr++ != '/')
852 return NULL;
853 }
854 /* skip [user[:passwd]@]host[:[port]] */
855
856 while (ptr < end && *ptr != '/')
857 ptr++;
858
859 if (ptr == end)
860 return NULL;
861
862 /* OK, we got the '/' ! */
863 return ptr;
864}
865
William Lallemand65ad6e12014-01-31 15:08:02 +0100866/* Parse the URI from the given string and look for the "/" beginning the PATH.
867 * If not found, return NULL. It is returned otherwise.
868 */
869static char *
870http_get_path_from_string(char *str)
871{
872 char *ptr = str;
873
874 /* RFC2616, par. 5.1.2 :
875 * Request-URI = "*" | absuri | abspath | authority
876 */
877
878 if (*ptr == '*')
879 return NULL;
880
881 if (isalpha((unsigned char)*ptr)) {
882 /* this is a scheme as described by RFC3986, par. 3.1 */
883 ptr++;
884 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
885 ptr++;
886 /* skip '://' */
887 if (*ptr == '\0' || *ptr++ != ':')
888 return NULL;
889 if (*ptr == '\0' || *ptr++ != '/')
890 return NULL;
891 if (*ptr == '\0' || *ptr++ != '/')
892 return NULL;
893 }
894 /* skip [user[:passwd]@]host[:[port]] */
895
896 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
897 ptr++;
898
899 if (*ptr == '\0' || *ptr == ' ')
900 return NULL;
901
902 /* OK, we got the '/' ! */
903 return ptr;
904}
905
Willy Tarreau71241ab2012-12-27 11:30:54 +0100906/* Returns a 302 for a redirectable request that reaches a server working in
907 * in redirect mode. This may only be called just after the stream interface
908 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
909 * follow normal proxy processing. NOTE: this function is designed to support
910 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100911 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100912void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100913{
914 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100915 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100916 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200917 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918
919 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100920 trash.len = strlen(HTTP_302);
921 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100922
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100923 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100924
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100926 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100927 return;
928
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100929 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100930 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100931 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
932 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100933 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200935 /* 3: add the request URI. Since it was already forwarded, we need
936 * to temporarily rewind the buffer.
937 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100938 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200939 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200940
Willy Tarreauefb453c2008-10-26 20:49:47 +0100941 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200942 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 +0200943
Willy Tarreau9b28e032012-10-12 23:49:43 +0200944 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200945
Willy Tarreauefb453c2008-10-26 20:49:47 +0100946 if (!path)
947 return;
948
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100949 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950 return;
951
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100952 memcpy(trash.str + trash.len, path, len);
953 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100954
955 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100956 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
957 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100958 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100959 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
960 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100961 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100962
963 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200964 si_shutr(si);
965 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100966 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->state = SI_ST_CLO;
968
969 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200970 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100971
972 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100973 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500974 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100975}
976
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100977/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978 * that the server side is closed. Note that err_type is actually a
979 * bitmask, where almost only aborts may be cumulated with other
980 * values. We consider that aborted operations are more important
981 * than timeouts or errors due to the fact that nobody else in the
982 * logs might explain incomplete retries. All others should avoid
983 * being cumulated. It should normally not be possible to have multiple
984 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100985 * Note that connection errors appearing on the second request of a keep-alive
986 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100988void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100989{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100990 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100991
992 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100993 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200994 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100995 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100996 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100997 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
998 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001000 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001001 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001002 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001003 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001004 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001005 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001006 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001007 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1008 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001009 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001010 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001011 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001012 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001013 else if (err_type & SI_ET_CONN_RES)
1014 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001015 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1016 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001017 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001019 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001020}
1021
Willy Tarreau42250582007-04-01 01:30:43 +02001022extern const char sess_term_cond[8];
1023extern const char sess_fin_state[8];
1024extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001025struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001026struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001027struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001028
Willy Tarreau117f59e2007-03-04 18:17:17 +01001029/*
1030 * Capture headers from message starting at <som> according to header list
1031 * <cap_hdr>, and fill the <idx> structure appropriately.
1032 */
1033void capture_headers(char *som, struct hdr_idx *idx,
1034 char **cap, struct cap_hdr *cap_hdr)
1035{
1036 char *eol, *sol, *col, *sov;
1037 int cur_idx;
1038 struct cap_hdr *h;
1039 int len;
1040
1041 sol = som + hdr_idx_first_pos(idx);
1042 cur_idx = hdr_idx_first_idx(idx);
1043
1044 while (cur_idx) {
1045 eol = sol + idx->v[cur_idx].len;
1046
1047 col = sol;
1048 while (col < eol && *col != ':')
1049 col++;
1050
1051 sov = col + 1;
1052 while (sov < eol && http_is_lws[(unsigned char)*sov])
1053 sov++;
1054
1055 for (h = cap_hdr; h; h = h->next) {
1056 if ((h->namelen == col - sol) &&
1057 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1058 if (cap[h->index] == NULL)
1059 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001060 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001061
1062 if (cap[h->index] == NULL) {
1063 Alert("HTTP capture : out of memory.\n");
1064 continue;
1065 }
1066
1067 len = eol - sov;
1068 if (len > h->len)
1069 len = h->len;
1070
1071 memcpy(cap[h->index], sov, len);
1072 cap[h->index][len]=0;
1073 }
1074 }
1075 sol = eol + idx->v[cur_idx].cr + 1;
1076 cur_idx = idx->v[cur_idx].next;
1077 }
1078}
1079
1080
Willy Tarreau42250582007-04-01 01:30:43 +02001081/* either we find an LF at <ptr> or we jump to <bad>.
1082 */
1083#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1084
1085/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1086 * otherwise to <http_msg_ood> with <state> set to <st>.
1087 */
1088#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1089 ptr++; \
1090 if (likely(ptr < end)) \
1091 goto good; \
1092 else { \
1093 state = (st); \
1094 goto http_msg_ood; \
1095 } \
1096 } while (0)
1097
1098
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001100 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001101 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1102 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1103 * will give undefined results.
1104 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1105 * and that msg->sol points to the beginning of the response.
1106 * If a complete line is found (which implies that at least one CR or LF is
1107 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1108 * returned indicating an incomplete line (which does not mean that parts have
1109 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1110 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1111 * upon next call.
1112 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001113 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001114 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1115 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001116 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001117 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001118const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001119 enum ht_state state, const char *ptr, const char *end,
1120 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001121{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001122 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001123
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001126 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001127 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1129
1130 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001131 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001132 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1133 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001134 state = HTTP_MSG_ERROR;
1135 break;
1136
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001140 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001141 goto http_msg_rpcode;
1142 }
1143 if (likely(HTTP_IS_SPHT(*ptr)))
1144 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1145 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001146 state = HTTP_MSG_ERROR;
1147 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001148
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 if (likely(!HTTP_IS_LWS(*ptr)))
1152 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1153
1154 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001155 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001156 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1157 }
1158
1159 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001160 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 http_msg_rsp_reason:
1162 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001163 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 msg->sl.st.r_l = 0;
1165 goto http_msg_rpline_eol;
1166
Willy Tarreau8973c702007-01-21 23:58:29 +01001167 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001168 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001169 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001170 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001171 goto http_msg_rpreason;
1172 }
1173 if (likely(HTTP_IS_SPHT(*ptr)))
1174 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1175 /* so it's a CR/LF, so there is no reason phrase */
1176 goto http_msg_rsp_reason;
1177
Willy Tarreau8973c702007-01-21 23:58:29 +01001178 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001179 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 if (likely(!HTTP_IS_CRLF(*ptr)))
1181 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001182 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 http_msg_rpline_eol:
1184 /* We have seen the end of line. Note that we do not
1185 * necessarily have the \n yet, but at least we know that we
1186 * have EITHER \r OR \n, otherwise the response would not be
1187 * complete. We can then record the response length and return
1188 * to the caller which will be able to register it.
1189 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001190 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 return ptr;
1192
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001194#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1196 exit(1);
1197#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001198 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 }
1200
1201 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001202 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 if (ret_state)
1204 *ret_state = state;
1205 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001206 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001207 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208}
1209
Willy Tarreau8973c702007-01-21 23:58:29 +01001210/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001211 * This function parses a request line between <ptr> and <end>, starting with
1212 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1213 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1214 * will give undefined results.
1215 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1216 * and that msg->sol points to the beginning of the request.
1217 * If a complete line is found (which implies that at least one CR or LF is
1218 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1219 * returned indicating an incomplete line (which does not mean that parts have
1220 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1221 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1222 * upon next call.
1223 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001224 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1226 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001227 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001229const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001230 enum ht_state state, const char *ptr, const char *end,
1231 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001232{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001233 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001234
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001237 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 if (likely(HTTP_IS_TOKEN(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001242 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1244 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001245
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001246 if (likely(HTTP_IS_CRLF(*ptr))) {
1247 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001248 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001250 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001252 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001254 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 msg->sl.rq.v_l = 0;
1256 goto http_msg_rqline_eol;
1257 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001258 state = HTTP_MSG_ERROR;
1259 break;
1260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001262 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001264 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 goto http_msg_rquri;
1266 }
1267 if (likely(HTTP_IS_SPHT(*ptr)))
1268 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1269 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1270 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001273 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001274 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001278 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1280 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001281
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001282 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001283 /* non-ASCII chars are forbidden unless option
1284 * accept-invalid-http-request is enabled in the frontend.
1285 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001286 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001287 if (msg->err_pos < -1)
1288 goto invalid_char;
1289 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001290 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001291 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1292 }
1293
1294 if (likely(HTTP_IS_CRLF(*ptr))) {
1295 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1296 goto http_msg_req09_uri_e;
1297 }
1298
1299 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001300 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001301 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001302 state = HTTP_MSG_ERROR;
1303 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001306 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001308 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 goto http_msg_rqver;
1310 }
1311 if (likely(HTTP_IS_SPHT(*ptr)))
1312 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1313 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1314 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001318 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001320
1321 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001322 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001323 http_msg_rqline_eol:
1324 /* We have seen the end of line. Note that we do not
1325 * necessarily have the \n yet, but at least we know that we
1326 * have EITHER \r OR \n, otherwise the request would not be
1327 * complete. We can then record the request length and return
1328 * to the caller which will be able to register it.
1329 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001330 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001331 return ptr;
1332 }
1333
1334 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001335 state = HTTP_MSG_ERROR;
1336 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001339#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1341 exit(1);
1342#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001343 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001347 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 if (ret_state)
1349 *ret_state = state;
1350 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001351 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001354
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001355/*
1356 * Returns the data from Authorization header. Function may be called more
1357 * than once so data is stored in txn->auth_data. When no header is found
1358 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001359 * searching again for something we are unable to find anyway. However, if
1360 * the result if valid, the cache is not reused because we would risk to
1361 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001362 */
1363
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001364char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001365
1366int
1367get_http_auth(struct session *s)
1368{
1369
1370 struct http_txn *txn = &s->txn;
1371 struct chunk auth_method;
1372 struct hdr_ctx ctx;
1373 char *h, *p;
1374 int len;
1375
1376#ifdef DEBUG_AUTH
1377 printf("Auth for session %p: %d\n", s, txn->auth.method);
1378#endif
1379
1380 if (txn->auth.method == HTTP_AUTH_WRONG)
1381 return 0;
1382
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001383 txn->auth.method = HTTP_AUTH_WRONG;
1384
1385 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001386
1387 if (txn->flags & TX_USE_PX_CONN) {
1388 h = "Proxy-Authorization";
1389 len = strlen(h);
1390 } else {
1391 h = "Authorization";
1392 len = strlen(h);
1393 }
1394
Willy Tarreau9b28e032012-10-12 23:49:43 +02001395 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001396 return 0;
1397
1398 h = ctx.line + ctx.val;
1399
1400 p = memchr(h, ' ', ctx.vlen);
1401 if (!p || p == h)
1402 return 0;
1403
1404 chunk_initlen(&auth_method, h, 0, p-h);
1405 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1406
1407 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1408
1409 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001410 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411
1412 if (len < 0)
1413 return 0;
1414
1415
1416 get_http_auth_buff[len] = '\0';
1417
1418 p = strchr(get_http_auth_buff, ':');
1419
1420 if (!p)
1421 return 0;
1422
1423 txn->auth.user = get_http_auth_buff;
1424 *p = '\0';
1425 txn->auth.pass = p+1;
1426
1427 txn->auth.method = HTTP_AUTH_BASIC;
1428 return 1;
1429 }
1430
1431 return 0;
1432}
1433
Willy Tarreau58f10d72006-12-04 02:26:12 +01001434
Willy Tarreau8973c702007-01-21 23:58:29 +01001435/*
1436 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001437 * depending on the initial msg->msg_state. The caller is responsible for
1438 * ensuring that the message does not wrap. The function can be preempted
1439 * everywhere when data are missing and recalled at the exact same location
1440 * with no information loss. The message may even be realigned between two
1441 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001442 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001443 * fields. Note that msg->sol will be initialized after completing the first
1444 * state, so that none of the msg pointers has to be initialized prior to the
1445 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001447void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448{
Willy Tarreau3770f232013-12-07 00:01:53 +01001449 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001451 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001453 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001454 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001455 ptr = buf->p + msg->next;
1456 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 if (unlikely(ptr >= end))
1459 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001462 /*
1463 * First, states that are specific to the response only.
1464 * We check them first so that request and headers are
1465 * closer to each other (accessed more often).
1466 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001467 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001468 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001470 /* we have a start of message, but we have to check
1471 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001472 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001473 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001474 if (unlikely(ptr != buf->p)) {
1475 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001476 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001477 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001478 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 }
Willy Tarreau26927362012-05-18 23:22:52 +02001480 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001481 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 hdr_idx_init(idx);
1483 state = HTTP_MSG_RPVER;
1484 goto http_msg_rpver;
1485 }
1486
1487 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1488 goto http_msg_invalid;
1489
1490 if (unlikely(*ptr == '\n'))
1491 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1492 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1493 /* stop here */
1494
Willy Tarreau8973c702007-01-21 23:58:29 +01001495 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001496 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 EXPECT_LF_HERE(ptr, http_msg_invalid);
1498 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1499 /* stop here */
1500
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001502 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001503 case HTTP_MSG_RPVER_SP:
1504 case HTTP_MSG_RPCODE:
1505 case HTTP_MSG_RPCODE_SP:
1506 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001507 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001508 state, ptr, end,
1509 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 if (unlikely(!ptr))
1511 return;
1512
1513 /* we have a full response and we know that we have either a CR
1514 * or an LF at <ptr>.
1515 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1517
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001518 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001519 if (likely(*ptr == '\r'))
1520 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1521 goto http_msg_rpline_end;
1522
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001524 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 /* msg->sol must point to the first of CR or LF. */
1526 EXPECT_LF_HERE(ptr, http_msg_invalid);
1527 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1528 /* stop here */
1529
1530 /*
1531 * Second, states that are specific to the request only
1532 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001534 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001536 /* we have a start of message, but we have to check
1537 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001538 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001539 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001540 if (likely(ptr != buf->p)) {
1541 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001542 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001543 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 }
Willy Tarreau26927362012-05-18 23:22:52 +02001546 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001547 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001548 state = HTTP_MSG_RQMETH;
1549 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001551
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1553 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001554
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 if (unlikely(*ptr == '\n'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1557 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001558 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001561 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 EXPECT_LF_HERE(ptr, http_msg_invalid);
1563 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001564 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001567 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 case HTTP_MSG_RQMETH_SP:
1569 case HTTP_MSG_RQURI:
1570 case HTTP_MSG_RQURI_SP:
1571 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001572 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001573 state, ptr, end,
1574 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 if (unlikely(!ptr))
1576 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001577
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 /* we have a full request and we know that we have either a CR
1579 * or an LF at <ptr>.
1580 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001582
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001583 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 if (likely(*ptr == '\r'))
1585 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001589 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 /* check for HTTP/0.9 request : no version information available.
1591 * msg->sol must point to the first of CR or LF.
1592 */
1593 if (unlikely(msg->sl.rq.v_l == 0))
1594 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 EXPECT_LF_HERE(ptr, http_msg_invalid);
1597 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001598 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 /*
1601 * Common states below
1602 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001605 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 if (likely(!HTTP_IS_CRLF(*ptr))) {
1607 goto http_msg_hdr_name;
1608 }
1609
1610 if (likely(*ptr == '\r'))
1611 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1612 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001615 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 /* assumes msg->sol points to the first char */
1617 if (likely(HTTP_IS_TOKEN(*ptr)))
1618 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001619
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001620 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001623 if (likely(msg->err_pos < -1) || *ptr == '\n')
1624 goto http_msg_invalid;
1625
1626 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001627 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001628
1629 /* and we still accept this non-token character */
1630 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001633 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001634 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 if (likely(HTTP_IS_SPHT(*ptr)))
1636 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001639 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 if (likely(!HTTP_IS_CRLF(*ptr))) {
1642 goto http_msg_hdr_val;
1643 }
1644
1645 if (likely(*ptr == '\r'))
1646 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1647 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001650 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 EXPECT_LF_HERE(ptr, http_msg_invalid);
1652 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 if (likely(HTTP_IS_SPHT(*ptr))) {
1657 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001658 for (; buf->p + msg->sov < ptr; msg->sov++)
1659 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 goto http_msg_hdr_l1_sp;
1661 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001662 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001663 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 goto http_msg_complete_header;
1665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001667 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001668 /* assumes msg->sol points to the first char, and msg->sov
1669 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 */
1671 if (likely(!HTTP_IS_CRLF(*ptr)))
1672 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001673
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001674 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 /* Note: we could also copy eol into ->eoh so that we have the
1676 * real header end in case it ends with lots of LWS, but is this
1677 * really needed ?
1678 */
1679 if (likely(*ptr == '\r'))
1680 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1681 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001682
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001684 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 EXPECT_LF_HERE(ptr, http_msg_invalid);
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001689 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1691 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001692 for (; buf->p + msg->eol < ptr; msg->eol++)
1693 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 goto http_msg_hdr_val;
1695 }
1696 http_msg_complete_header:
1697 /*
1698 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001699 * Assumes msg->sol points to the first char, msg->sov points
1700 * to the first character of the value and msg->eol to the
1701 * first CR or LF so we know how the line ends. We insert last
1702 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001704 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 idx, idx->tail) < 0))
1706 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001707
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(!HTTP_IS_CRLF(*ptr))) {
1710 goto http_msg_hdr_name;
1711 }
1712
1713 if (likely(*ptr == '\r'))
1714 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1715 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001718 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 /* Assumes msg->sol points to the first of either CR or LF */
1720 EXPECT_LF_HERE(ptr, http_msg_invalid);
1721 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001722 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001723 msg->eoh = msg->sol;
1724 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001725 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001727
1728 case HTTP_MSG_ERROR:
1729 /* this may only happen if we call http_msg_analyser() twice with an error */
1730 break;
1731
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001733#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1735 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001736#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001737 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 }
1739 http_msg_ood:
1740 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001741 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001742 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 http_msg_invalid:
1746 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001747 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001748 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 return;
1750}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001751
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001752/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1753 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1754 * nothing is done and 1 is returned.
1755 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001756static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001757{
1758 int delta;
1759 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001760 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001761
1762 if (msg->sl.rq.v_l != 0)
1763 return 1;
1764
Willy Tarreau9b28e032012-10-12 23:49:43 +02001765 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001766 delta = 0;
1767
1768 if (msg->sl.rq.u_l == 0) {
1769 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001770 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001771 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001772 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001773 }
1774 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001775 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001776 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001777 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001778 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001779 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001780 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001781 NULL, NULL);
1782 if (unlikely(!cur_end))
1783 return 0;
1784
1785 /* we have a full HTTP/1.0 request now and we know that
1786 * we have either a CR or an LF at <ptr>.
1787 */
1788 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1789 return 1;
1790}
1791
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001792/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001793 * and "keep-alive" values. If we already know that some headers may safely
1794 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001795 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1796 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001797 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001798 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1799 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1800 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001801 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001802 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001803void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001804{
Willy Tarreau5b154472009-12-21 20:11:07 +01001805 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001806 const char *hdr_val = "Connection";
1807 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001808
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001809 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001810 return;
1811
Willy Tarreau88d349d2010-01-25 12:15:43 +01001812 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1813 hdr_val = "Proxy-Connection";
1814 hdr_len = 16;
1815 }
1816
Willy Tarreau5b154472009-12-21 20:11:07 +01001817 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001818 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001819 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001820 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1821 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001822 if (to_del & 2)
1823 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001824 else
1825 txn->flags |= TX_CON_KAL_SET;
1826 }
1827 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1828 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001829 if (to_del & 1)
1830 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001831 else
1832 txn->flags |= TX_CON_CLO_SET;
1833 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001834 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1835 txn->flags |= TX_HDR_CONN_UPG;
1836 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001837 }
1838
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001839 txn->flags |= TX_HDR_CONN_PRS;
1840 return;
1841}
Willy Tarreau5b154472009-12-21 20:11:07 +01001842
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001843/* Apply desired changes on the Connection: header. Values may be removed and/or
1844 * added depending on the <wanted> flags, which are exclusively composed of
1845 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1846 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1847 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001848void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849{
1850 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001851 const char *hdr_val = "Connection";
1852 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853
1854 ctx.idx = 0;
1855
Willy Tarreau88d349d2010-01-25 12:15:43 +01001856
1857 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1858 hdr_val = "Proxy-Connection";
1859 hdr_len = 16;
1860 }
1861
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001862 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001863 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001864 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1865 if (wanted & TX_CON_KAL_SET)
1866 txn->flags |= TX_CON_KAL_SET;
1867 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001868 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001869 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1871 if (wanted & TX_CON_CLO_SET)
1872 txn->flags |= TX_CON_CLO_SET;
1873 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001874 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001875 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001876 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001877
1878 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1879 return;
1880
1881 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1882 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001883 hdr_val = "Connection: close";
1884 hdr_len = 17;
1885 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1886 hdr_val = "Proxy-Connection: close";
1887 hdr_len = 23;
1888 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001889 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001890 }
1891
1892 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1893 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001894 hdr_val = "Connection: keep-alive";
1895 hdr_len = 22;
1896 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1897 hdr_val = "Proxy-Connection: keep-alive";
1898 hdr_len = 28;
1899 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001900 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001901 }
1902 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001903}
1904
Willy Tarreaua458b672012-03-05 11:17:50 +01001905/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001907 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001908 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001910 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001911static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001912{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001913 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001914 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001915 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 const char *end = buf->data + buf->size;
1917 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 unsigned int chunk = 0;
1919
1920 /* The chunk size is in the following form, though we are only
1921 * interested in the size and CRLF :
1922 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1923 */
1924 while (1) {
1925 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001926 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001927 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001929 if (c < 0) /* not a hex digit anymore */
1930 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001931 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001932 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001933 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001934 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001935 chunk = (chunk << 4) + c;
1936 }
1937
Willy Tarreaud98cf932009-12-27 22:54:55 +01001938 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001939 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001940 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941
1942 while (http_is_spht[(unsigned char)*ptr]) {
1943 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001945 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001946 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001947 }
1948
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949 /* Up to there, we know that at least one byte is present at *ptr. Check
1950 * for the end of chunk size.
1951 */
1952 while (1) {
1953 if (likely(HTTP_IS_CRLF(*ptr))) {
1954 /* we now have a CR or an LF at ptr */
1955 if (likely(*ptr == '\r')) {
1956 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001957 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001958 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959 return 0;
1960 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001961
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001963 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001965 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001966 /* done */
1967 break;
1968 }
1969 else if (*ptr == ';') {
1970 /* chunk extension, ends at next CRLF */
1971 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001972 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001973 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001974 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975
1976 while (!HTTP_IS_CRLF(*ptr)) {
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 }
1982 /* we have a CRLF now, loop above */
1983 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001986 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001987 }
1988
Willy Tarreaud98cf932009-12-27 22:54:55 +01001989 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001990 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001991 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001992 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001993 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001994 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001995 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001996 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001997 msg->chunk_len = chunk;
1998 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001999 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002000 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002001 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002003 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002004}
2005
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002006/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002007 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 * the trailers is found, it is automatically scheduled to be forwarded,
2009 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2010 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01002011 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01002012 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01002013 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01002014 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2015 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02002016 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01002017 * matches the length of trailers already parsed and not forwarded. It is also
2018 * important to note that this function is designed to be able to parse wrapped
2019 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002021static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002022{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002023 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002024
Willy Tarreaua458b672012-03-05 11:17:50 +01002025 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002026 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002027 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002028 const char *ptr = b_ptr(buf, msg->next);
2029 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002030 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031
2032 /* scan current line and stop at LF or CRLF */
2033 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002034 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036
2037 if (*ptr == '\n') {
2038 if (!p1)
2039 p1 = ptr;
2040 p2 = ptr;
2041 break;
2042 }
2043
2044 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002045 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002046 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002048 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002049 p1 = ptr;
2050 }
2051
2052 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 if (ptr >= buf->data + buf->size)
2054 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002055 }
2056
2057 /* after LF; point to beginning of next line */
2058 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002059 if (p2 >= buf->data + buf->size)
2060 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002063 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002065
2066 /* schedule this line for forwarding */
2067 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 if (msg->sov >= buf->size)
2069 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002072 /* LF/CRLF at beginning of line => end of trailers at p2.
2073 * Everything was scheduled for forwarding, there's nothing
2074 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002075 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002076 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002077 msg->msg_state = HTTP_MSG_DONE;
2078 return 1;
2079 }
2080 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002081 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 }
2083}
2084
Willy Tarreau54d23df2012-10-25 19:04:45 +02002085/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002087 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002088 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2090 * not enough data are available, the function does not change anything and
2091 * returns zero. If a parse error is encountered, the function returns < 0 and
2092 * does not change anything. Note: this function is designed to parse wrapped
2093 * CRLF at the end of the buffer.
2094 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002095static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002097 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002098 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099 int bytes;
2100
2101 /* NB: we'll check data availabilty at the end. It's not a
2102 * problem because whatever we match first will be checked
2103 * against the correct length.
2104 */
2105 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002106 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107 if (*ptr == '\r') {
2108 bytes++;
2109 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002110 if (ptr >= buf->data + buf->size)
2111 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 }
2113
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002114 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 return 0;
2116
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002117 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002118 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121
2122 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002123 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002125 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2126 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002127 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2129 return 1;
2130}
Willy Tarreau5b154472009-12-21 20:11:07 +01002131
William Lallemand82fe75c2012-10-23 10:25:10 +02002132
2133/*
2134 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002135 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002136int select_compression_request_header(struct session *s, struct buffer *req)
2137{
2138 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002139 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002140 struct hdr_ctx ctx;
2141 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002142 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002143
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002144 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2145 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002146 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2147 */
2148 ctx.idx = 0;
2149 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2150 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002151 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2152 (ctx.vlen < 31 ||
2153 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2154 ctx.line[ctx.val + 30] < '6' ||
2155 (ctx.line[ctx.val + 30] == '6' &&
2156 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2157 s->comp_algo = NULL;
2158 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002159 }
2160
William Lallemand82fe75c2012-10-23 10:25:10 +02002161 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002162 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002163 ctx.idx = 0;
2164 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002165 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002166 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2167 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002168
2169 /* remove all occurrences of the header when "compression offload" is set */
2170
2171 if ((s->be->comp && s->be->comp->offload) ||
2172 (s->fe->comp && s->fe->comp->offload)) {
2173 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2174 ctx.idx = 0;
2175 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2176 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2177 }
2178 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 return 1;
2180 }
2181 }
2182 }
2183 }
2184
2185 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002186 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2187 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002188 if (comp_algo->add_data == identity_add_data) {
2189 s->comp_algo = comp_algo;
2190 return 1;
2191 }
2192 }
2193 }
2194
2195 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002196 return 0;
2197}
2198
2199/*
2200 * Selects a comression algorithm depending of the server response.
2201 */
2202int select_compression_response_header(struct session *s, struct buffer *res)
2203{
2204 struct http_txn *txn = &s->txn;
2205 struct http_msg *msg = &txn->rsp;
2206 struct hdr_ctx ctx;
2207 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002208
2209 /* no common compression algorithm was found in request header */
2210 if (s->comp_algo == NULL)
2211 goto fail;
2212
2213 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002214 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002215 goto fail;
2216
William Lallemandd3002612012-11-26 14:34:47 +01002217 /* 200 only */
2218 if (txn->status != 200)
2219 goto fail;
2220
William Lallemand82fe75c2012-10-23 10:25:10 +02002221 /* Content-Length is null */
2222 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2223 goto fail;
2224
Willy Tarreau667c2a32013-04-09 08:13:58 +02002225 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2226 if (msg->flags & HTTP_MSGF_TE_CHNK)
2227 goto fail;
2228
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002230 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002231 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2232 goto fail;
2233
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002234 /* no compression when Cache-Control: no-transform is present in the message */
2235 ctx.idx = 0;
2236 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2237 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2238 goto fail;
2239 }
2240
William Lallemand82fe75c2012-10-23 10:25:10 +02002241 comp_type = NULL;
2242
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002243 /* we don't want to compress multipart content-types, nor content-types that are
2244 * not listed in the "compression type" directive if any. If no content-type was
2245 * found but configuration requires one, we don't compress either. Backend has
2246 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002247 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002248 ctx.idx = 0;
2249 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2250 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2251 goto fail;
2252
2253 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2254 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002255 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002256 if (ctx.vlen >= comp_type->name_len &&
2257 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002258 /* this Content-Type should be compressed */
2259 break;
2260 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002261 /* this Content-Type should not be compressed */
2262 if (comp_type == NULL)
2263 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002265 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002266 else { /* no content-type header */
2267 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2268 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002269 }
2270
William Lallemandd85f9172012-11-09 17:05:39 +01002271 /* limit compression rate */
2272 if (global.comp_rate_lim > 0)
2273 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2274 goto fail;
2275
William Lallemand072a2bf2012-11-20 17:01:01 +01002276 /* limit cpu usage */
2277 if (idle_pct < compress_min_idle)
2278 goto fail;
2279
William Lallemand4c49fae2012-11-07 15:00:23 +01002280 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002281 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002282 goto fail;
2283
William Lallemandec3e3892012-11-12 17:02:18 +01002284 s->flags |= SN_COMP_READY;
2285
William Lallemand82fe75c2012-10-23 10:25:10 +02002286 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002287 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002288 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2289 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2290
2291 /* add Transfer-Encoding header */
2292 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2293 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2294
2295 /*
2296 * Add Content-Encoding header when it's not identity encoding.
2297 * RFC 2616 : Identity encoding: This content-coding is used only in the
2298 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2299 * header.
2300 */
2301 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002302 trash.len = 18;
2303 memcpy(trash.str, "Content-Encoding: ", trash.len);
2304 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2305 trash.len += s->comp_algo->name_len;
2306 trash.str[trash.len] = '\0';
2307 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002308 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002309 return 1;
2310
2311fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002312 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002313 return 0;
2314}
2315
2316
Willy Tarreaud787e662009-07-07 10:14:51 +02002317/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2318 * processing can continue on next analysers, or zero if it either needs more
2319 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2320 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2321 * when it has nothing left to do, and may remove any analyser when it wants to
2322 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002323 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002324int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002325{
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 /*
2327 * We will parse the partial (or complete) lines.
2328 * We will check the request syntax, and also join multi-line
2329 * headers. An index of all the lines will be elaborated while
2330 * parsing.
2331 *
2332 * For the parsing, we use a 28 states FSM.
2333 *
2334 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002335 * req->buf->p = beginning of request
2336 * req->buf->p + msg->eoh = end of processed headers / start of current one
2337 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002338 * msg->eol = end of current header or line (LF or CRLF)
2339 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002340 *
2341 * At end of parsing, we may perform a capture of the error (if any), and
2342 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2343 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2344 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002348 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 struct http_txn *txn = &s->txn;
2350 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002351 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002352
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002353 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 +01002354 now_ms, __FUNCTION__,
2355 s,
2356 req,
2357 req->rex, req->wex,
2358 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002359 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002360 req->analysers);
2361
Willy Tarreau52a0c602009-08-16 22:45:38 +02002362 /* we're speaking HTTP here, so let's speak HTTP to the client */
2363 s->srv_error = http_return_srv_error;
2364
Willy Tarreau83e3af02009-12-28 17:39:57 +01002365 /* There's a protected area at the end of the buffer for rewriting
2366 * purposes. We don't want to start to parse the request if the
2367 * protected area is affected, because we may have to move processed
2368 * data later, which is much more complicated.
2369 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002370 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002371 if (txn->flags & TX_NOT_FIRST) {
2372 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002373 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002374 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002375 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002376 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002377 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002378 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002379 return 0;
2380 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002381 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2382 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2383 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002384 }
2385
Willy Tarreau065e8332010-01-08 00:30:20 +01002386 /* Note that we have the same problem with the response ; we
2387 * may want to send a redirect, error or anything which requires
2388 * some spare space. So we'll ensure that we have at least
2389 * maxrewrite bytes available in the response buffer before
2390 * processing that one. This will only affect pipelined
2391 * keep-alive requests.
2392 */
2393 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002394 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002395 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2396 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2397 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002398 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002399 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002400 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002401 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002402 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002403 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002404 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002405 return 0;
2406 }
2407 }
2408
Willy Tarreau9b28e032012-10-12 23:49:43 +02002409 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002410 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002411 }
2412
Willy Tarreau59234e92008-11-30 23:51:27 +01002413 /* 1: we might have to print this header in debug mode */
2414 if (unlikely((global.mode & MODE_DEBUG) &&
2415 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002416 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002418
Willy Tarreau9b28e032012-10-12 23:49:43 +02002419 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002420 /* this is a bit complex : in case of error on the request line,
2421 * we know that rq.l is still zero, so we display only the part
2422 * up to the end of the line (truncated by debug_hdr).
2423 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002424 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 sol += hdr_idx_first_pos(&txn->hdr_idx);
2428 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002429
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 while (cur_idx) {
2431 eol = sol + txn->hdr_idx.v[cur_idx].len;
2432 debug_hdr("clihdr", s, sol, eol);
2433 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2434 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002435 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002436 }
2437
Willy Tarreau58f10d72006-12-04 02:26:12 +01002438
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 /*
2440 * Now we quickly check if we have found a full valid request.
2441 * If not so, we check the FD and buffer states before leaving.
2442 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002443 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002444 * requests are checked first. When waiting for a second request
2445 * on a keep-alive session, if we encounter and error, close, t/o,
2446 * we note the error in the session flags but don't set any state.
2447 * Since the error will be noted there, it will not be counted by
2448 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002449 * Last, we may increase some tracked counters' http request errors on
2450 * the cases that are deliberately the client's fault. For instance,
2451 * a timeout or connection reset is not counted as an error. However
2452 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002453 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002454
Willy Tarreau655dce92009-11-08 13:10:58 +01002455 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002456 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002457 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002458 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002459 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002460 session_inc_http_req_ctr(s);
2461 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002462 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002463 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002464 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002465
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 /* 1: Since we are in header mode, if there's no space
2467 * left for headers, we won't be able to free more
2468 * later, so the session will never terminate. We
2469 * must terminate it now.
2470 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002471 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 /* FIXME: check if URI is set and return Status
2473 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002474 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002475 session_inc_http_req_ctr(s);
2476 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002477 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002478 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002479 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 goto return_bad_req;
2481 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002482
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002484 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002485 if (!(s->flags & SN_ERR_MASK))
2486 s->flags |= SN_ERR_CLICL;
2487
Willy Tarreaufcffa692010-01-10 14:21:19 +01002488 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002489 goto failed_keep_alive;
2490
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002492 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002493 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002494 session_inc_http_err_ctr(s);
2495 }
2496
Willy Tarreaudc979f22012-12-04 10:39:01 +01002497 txn->status = 400;
2498 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 msg->msg_state = HTTP_MSG_ERROR;
2500 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002501
Willy Tarreauda7ff642010-06-23 11:44:09 +02002502 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002503 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002504 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002505 if (s->listener->counters)
2506 s->listener->counters->failed_req++;
2507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 if (!(s->flags & SN_FINST_MASK))
2509 s->flags |= SN_FINST_R;
2510 return 0;
2511 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002514 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002515 if (!(s->flags & SN_ERR_MASK))
2516 s->flags |= SN_ERR_CLITO;
2517
Willy Tarreaufcffa692010-01-10 14:21:19 +01002518 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002519 goto failed_keep_alive;
2520
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002522 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002523 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002524 session_inc_http_err_ctr(s);
2525 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002527 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 msg->msg_state = HTTP_MSG_ERROR;
2529 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002530
Willy Tarreauda7ff642010-06-23 11:44:09 +02002531 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002532 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002533 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002534 if (s->listener->counters)
2535 s->listener->counters->failed_req++;
2536
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 if (!(s->flags & SN_FINST_MASK))
2538 s->flags |= SN_FINST_R;
2539 return 0;
2540 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002541
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002543 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002544 if (!(s->flags & SN_ERR_MASK))
2545 s->flags |= SN_ERR_CLICL;
2546
Willy Tarreaufcffa692010-01-10 14:21:19 +01002547 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002548 goto failed_keep_alive;
2549
Willy Tarreau4076a152009-04-02 15:18:36 +02002550 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002551 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002553 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 msg->msg_state = HTTP_MSG_ERROR;
2555 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002556
Willy Tarreauda7ff642010-06-23 11:44:09 +02002557 session_inc_http_err_ctr(s);
2558 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002559 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002560 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002561 if (s->listener->counters)
2562 s->listener->counters->failed_req++;
2563
Willy Tarreau59234e92008-11-30 23:51:27 +01002564 if (!(s->flags & SN_FINST_MASK))
2565 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002566 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002567 }
2568
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002569 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002570 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2571 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002572#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002573 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 +01002574 /* We need more data, we have to re-enable quick-ack in case we
2575 * previously disabled it, otherwise we might cause the client
2576 * to delay next data.
2577 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002578 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002579 }
2580#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002581
Willy Tarreaufcffa692010-01-10 14:21:19 +01002582 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2583 /* If the client starts to talk, let's fall back to
2584 * request timeout processing.
2585 */
2586 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002587 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002588 }
2589
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002591 if (!tick_isset(req->analyse_exp)) {
2592 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2593 (txn->flags & TX_WAIT_NEXT_RQ) &&
2594 tick_isset(s->be->timeout.httpka))
2595 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2596 else
2597 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2598 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 /* we're not ready yet */
2601 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002602
2603 failed_keep_alive:
2604 /* Here we process low-level errors for keep-alive requests. In
2605 * short, if the request is not the first one and it experiences
2606 * a timeout, read error or shutdown, we just silently close so
2607 * that the client can try again.
2608 */
2609 txn->status = 0;
2610 msg->msg_state = HTTP_MSG_RQBEFORE;
2611 req->analysers = 0;
2612 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002613 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002614 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002615 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002616 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002618
Willy Tarreaud787e662009-07-07 10:14:51 +02002619 /* OK now we have a complete HTTP request with indexed headers. Let's
2620 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002621 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002622 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002623 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002624 * byte after the last LF. msg->sov points to the first byte of data.
2625 * msg->eol cannot be trusted because it may have been left uninitialized
2626 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002627 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002628
Willy Tarreauda7ff642010-06-23 11:44:09 +02002629 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002630 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2631
Willy Tarreaub16a5742010-01-10 14:46:16 +01002632 if (txn->flags & TX_WAIT_NEXT_RQ) {
2633 /* kill the pending keep-alive timeout */
2634 txn->flags &= ~TX_WAIT_NEXT_RQ;
2635 req->analyse_exp = TICK_ETERNITY;
2636 }
2637
2638
Willy Tarreaud787e662009-07-07 10:14:51 +02002639 /* Maybe we found in invalid header name while we were configured not
2640 * to block on that, so we have to capture it now.
2641 */
2642 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002643 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002644
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 /*
2646 * 1: identify the method
2647 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002648 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002649
2650 /* we can make use of server redirect on GET and HEAD */
2651 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2652 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 /*
2655 * 2: check if the URI matches the monitor_uri.
2656 * We have to do this for every request which gets in, because
2657 * the monitor-uri is defined by the frontend.
2658 */
2659 if (unlikely((s->fe->monitor_uri_len != 0) &&
2660 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002661 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 s->fe->monitor_uri,
2663 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002664 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002666 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002668
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002670 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002673 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002674 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002675
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 ret = acl_pass(ret);
2677 if (cond->pol == ACL_COND_UNLESS)
2678 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 if (ret) {
2681 /* we fail this request, let's return 503 service unavail */
2682 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002683 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002684 if (!(s->flags & SN_ERR_MASK))
2685 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002686 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002687 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002688 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* nothing to fail, let's reply normaly */
2691 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002692 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002693 if (!(s->flags & SN_ERR_MASK))
2694 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 goto return_prx_cond;
2696 }
2697
2698 /*
2699 * 3: Maybe we have to copy the original REQURI for the logs ?
2700 * Note: we cannot log anymore if the request has been
2701 * classified as invalid.
2702 */
2703 if (unlikely(s->logs.logwait & LW_REQ)) {
2704 /* we have a complete HTTP request that we must log */
2705 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2706 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002707
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 if (urilen >= REQURI_LEN)
2709 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002710 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002711 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002712
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002713 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002714 s->do_log(s);
2715 } else {
2716 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002717 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 }
Willy Tarreau06619262006-12-17 08:37:22 +01002719
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002721 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002722 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002723
Willy Tarreau5b154472009-12-21 20:11:07 +01002724 /* ... and check if the request is HTTP/1.1 or above */
2725 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002726 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2727 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2728 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002729 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002730
2731 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002732 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 +01002733
Willy Tarreau88d349d2010-01-25 12:15:43 +01002734 /* if the frontend has "option http-use-proxy-header", we'll check if
2735 * we have what looks like a proxied connection instead of a connection,
2736 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2737 * Note that this is *not* RFC-compliant, however browsers and proxies
2738 * happen to do that despite being non-standard :-(
2739 * We consider that a request not beginning with either '/' or '*' is
2740 * a proxied connection, which covers both "scheme://location" and
2741 * CONNECT ip:port.
2742 */
2743 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002744 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002745 txn->flags |= TX_USE_PX_CONN;
2746
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002747 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002748 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002749
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002751 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002752 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002754
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002755 /* 6: determine the transfer-length.
2756 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2757 * the presence of a message-body in a REQUEST and its transfer length
2758 * must be determined that way (in order of precedence) :
2759 * 1. The presence of a message-body in a request is signaled by the
2760 * inclusion of a Content-Length or Transfer-Encoding header field
2761 * in the request's header fields. When a request message contains
2762 * both a message-body of non-zero length and a method that does
2763 * not define any semantics for that request message-body, then an
2764 * origin server SHOULD either ignore the message-body or respond
2765 * with an appropriate error message (e.g., 413). A proxy or
2766 * gateway, when presented the same request, SHOULD either forward
2767 * the request inbound with the message- body or ignore the
2768 * message-body when determining a response.
2769 *
2770 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2771 * and the "chunked" transfer-coding (Section 6.2) is used, the
2772 * transfer-length is defined by the use of this transfer-coding.
2773 * If a Transfer-Encoding header field is present and the "chunked"
2774 * transfer-coding is not present, the transfer-length is defined
2775 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002776 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002777 * 3. If a Content-Length header field is present, its decimal value in
2778 * OCTETs represents both the entity-length and the transfer-length.
2779 * If a message is received with both a Transfer-Encoding header
2780 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002781 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002782 * 4. By the server closing the connection. (Closing the connection
2783 * cannot be used to indicate the end of a request body, since that
2784 * would leave no possibility for the server to send back a response.)
2785 *
2786 * Whenever a transfer-coding is applied to a message-body, the set of
2787 * transfer-codings MUST include "chunked", unless the message indicates
2788 * it is terminated by closing the connection. When the "chunked"
2789 * transfer-coding is used, it MUST be the last transfer-coding applied
2790 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002791 */
2792
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002793 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002794 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002795 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002796 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002797 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002798 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002799 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2800 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002801 /* bad transfer-encoding (chunked followed by something else) */
2802 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002803 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002804 break;
2805 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002806 }
2807
Willy Tarreau32b47f42009-10-18 20:55:02 +02002808 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002809 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002810 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002811 signed long long cl;
2812
Willy Tarreauad14f752011-09-02 20:33:27 +02002813 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002814 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002815 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002816 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002817
Willy Tarreauad14f752011-09-02 20:33:27 +02002818 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002819 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002820 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002821 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002822
Willy Tarreauad14f752011-09-02 20:33:27 +02002823 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002824 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002825 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002826 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002827
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002828 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002829 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002830 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002831 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002832
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002833 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002834 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002835 }
2836
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002837 /* bodyless requests have a known length */
2838 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002839 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002840
Willy Tarreaud787e662009-07-07 10:14:51 +02002841 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002842 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002843 req->analyse_exp = TICK_ETERNITY;
2844 return 1;
2845
2846 return_bad_req:
2847 /* We centralize bad requests processing here */
2848 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2849 /* we detected a parsing error. We want to archive this request
2850 * in the dedicated proxy area for later troubleshooting.
2851 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002852 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002853 }
2854
2855 txn->req.msg_state = HTTP_MSG_ERROR;
2856 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002857 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002858
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002859 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002860 if (s->listener->counters)
2861 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002862
2863 return_prx_cond:
2864 if (!(s->flags & SN_ERR_MASK))
2865 s->flags |= SN_ERR_PRXCOND;
2866 if (!(s->flags & SN_FINST_MASK))
2867 s->flags |= SN_FINST_R;
2868
2869 req->analysers = 0;
2870 req->analyse_exp = TICK_ETERNITY;
2871 return 0;
2872}
2873
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002874
Willy Tarreau347a35d2013-11-22 17:51:09 +01002875/* This function prepares an applet to handle the stats. It can deal with the
2876 * "100-continue" expectation, check that admin rules are met for POST requests,
2877 * and program a response message if something was unexpected. It cannot fail
2878 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002879 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002880 * s->target which is supposed to already point to the stats applet. The caller
2881 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002882 */
2883int http_handle_stats(struct session *s, struct channel *req)
2884{
2885 struct stats_admin_rule *stats_admin_rule;
2886 struct stream_interface *si = s->rep->prod;
2887 struct http_txn *txn = &s->txn;
2888 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002889 struct uri_auth *uri_auth = s->be->uri_auth;
2890 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002892
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002893 appctx = si_appctx(si);
2894 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2895 appctx->st1 = appctx->st2 = 0;
2896 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2897 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002898
2899 uri = msg->chn->buf->p + msg->sl.rq.u;
2900 lookup = uri + uri_auth->uri_len;
2901
2902 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2903 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002904 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002905 break;
2906 }
2907 }
2908
2909 if (uri_auth->refresh) {
2910 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2911 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002912 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002913 break;
2914 }
2915 }
2916 }
2917
2918 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2919 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002920 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002921 break;
2922 }
2923 }
2924
2925 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2926 if (memcmp(h, ";st=", 4) == 0) {
2927 int i;
2928 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002929 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002930 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2931 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002932 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002933 break;
2934 }
2935 }
2936 break;
2937 }
2938 }
2939
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002940 appctx->ctx.stats.scope_str = 0;
2941 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002942 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2943 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2944 int itx = 0;
2945 const char *h2;
2946 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2947 const char *err;
2948
2949 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2950 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002951 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002952 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2953 itx++;
2954 h++;
2955 }
2956
2957 if (itx > STAT_SCOPE_TXT_MAXLEN)
2958 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002959 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002960
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002961 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002962 memcpy(scope_txt, h2, itx);
2963 scope_txt[itx] = '\0';
2964 err = invalid_char(scope_txt);
2965 if (err) {
2966 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002967 appctx->ctx.stats.scope_str = 0;
2968 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002969 }
2970 break;
2971 }
2972 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002973
2974 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002975 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002976 int ret = 1;
2977
2978 if (stats_admin_rule->cond) {
2979 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2980 ret = acl_pass(ret);
2981 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2982 ret = !ret;
2983 }
2984
2985 if (ret) {
2986 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002987 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002988 break;
2989 }
2990 }
2991
2992 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002993 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002994 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002995 if (msg->msg_state < HTTP_MSG_100_SENT) {
2996 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2997 * send an HTTP/1.1 100 Continue intermediate response.
2998 */
2999 if (msg->flags & HTTP_MSGF_VER_11) {
3000 struct hdr_ctx ctx;
3001 ctx.idx = 0;
3002 /* Expect is allowed in 1.1, look for it */
3003 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3004 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3005 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3006 }
3007 }
3008 msg->msg_state = HTTP_MSG_100_SENT;
3009 s->logs.tv_request = now; /* update the request timer to reflect full request */
3010 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003011 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003012 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003013 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003014 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3015 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003016 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003017 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003018 else {
3019 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003020 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003021 }
3022
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003023 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003024 return 1;
3025}
3026
Lukas Tribus67db8df2013-06-23 17:37:13 +02003027/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3028 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3029 */
3030static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3031{
3032#ifdef IP_TOS
3033 if (from.ss_family == AF_INET)
3034 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3035#endif
3036#ifdef IPV6_TCLASS
3037 if (from.ss_family == AF_INET6) {
3038 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3039 /* v4-mapped addresses need IP_TOS */
3040 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3041 else
3042 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3043 }
3044#endif
3045}
3046
Willy Tarreau20b0de52012-12-24 15:45:22 +01003047/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003048 * transaction <txn>. Returns the first rule that prevents further processing
3049 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3050 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3051 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003052 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003053static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003054http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003055{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003056 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003057 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003058 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003059
Willy Tarreauff011f22011-01-06 17:51:27 +01003060 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003061 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003062 continue;
3063
Willy Tarreau96257ec2012-12-27 10:46:37 +01003064 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003065 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003066 int ret;
3067
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003068 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003069 ret = acl_pass(ret);
3070
Willy Tarreauff011f22011-01-06 17:51:27 +01003071 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003073
3074 if (!ret) /* condition not matched */
3075 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003076 }
3077
Willy Tarreau20b0de52012-12-24 15:45:22 +01003078
Willy Tarreau96257ec2012-12-27 10:46:37 +01003079 switch (rule->action) {
3080 case HTTP_REQ_ACT_ALLOW:
3081 return NULL; /* "allow" rules are OK */
3082
3083 case HTTP_REQ_ACT_DENY:
3084 txn->flags |= TX_CLDENY;
3085 return rule;
3086
Willy Tarreauccbcc372012-12-27 12:37:57 +01003087 case HTTP_REQ_ACT_TARPIT:
3088 txn->flags |= TX_CLTARPIT;
3089 return rule;
3090
Willy Tarreau96257ec2012-12-27 10:46:37 +01003091 case HTTP_REQ_ACT_AUTH:
3092 return rule;
3093
Willy Tarreau81499eb2012-12-27 12:19:02 +01003094 case HTTP_REQ_ACT_REDIR:
3095 return rule;
3096
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003097 case HTTP_REQ_ACT_SET_NICE:
3098 s->task->nice = rule->arg.nice;
3099 break;
3100
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003101 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003102 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003103 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003104 break;
3105
Willy Tarreau51347ed2013-06-11 19:34:13 +02003106 case HTTP_REQ_ACT_SET_MARK:
3107#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003108 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003109 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003110#endif
3111 break;
3112
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003113 case HTTP_REQ_ACT_SET_LOGL:
3114 s->logs.level = rule->arg.loglevel;
3115 break;
3116
Willy Tarreau96257ec2012-12-27 10:46:37 +01003117 case HTTP_REQ_ACT_SET_HDR:
3118 ctx.idx = 0;
3119 /* remove all occurrences of the header */
3120 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3121 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3122 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003123 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003124 /* now fall through to header addition */
3125
3126 case HTTP_REQ_ACT_ADD_HDR:
3127 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3128 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3129 trash.len = rule->arg.hdr_add.name_len;
3130 trash.str[trash.len++] = ':';
3131 trash.str[trash.len++] = ' ';
3132 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3133 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3134 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003135 }
3136 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003137
3138 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003139 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003140}
3141
Willy Tarreau71241ab2012-12-27 11:30:54 +01003142
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003143/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3144 * transaction <txn>. Returns the first rule that prevents further processing
3145 * of the response (deny, ...) or NULL if it executed all rules or stopped
3146 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3147 * rule.
3148 */
3149static struct http_res_rule *
3150http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3151{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003152 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003153 struct http_res_rule *rule;
3154 struct hdr_ctx ctx;
3155
3156 list_for_each_entry(rule, rules, list) {
3157 if (rule->action >= HTTP_RES_ACT_MAX)
3158 continue;
3159
3160 /* check optional condition */
3161 if (rule->cond) {
3162 int ret;
3163
3164 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3165 ret = acl_pass(ret);
3166
3167 if (rule->cond->pol == ACL_COND_UNLESS)
3168 ret = !ret;
3169
3170 if (!ret) /* condition not matched */
3171 continue;
3172 }
3173
3174
3175 switch (rule->action) {
3176 case HTTP_RES_ACT_ALLOW:
3177 return NULL; /* "allow" rules are OK */
3178
3179 case HTTP_RES_ACT_DENY:
3180 txn->flags |= TX_SVDENY;
3181 return rule;
3182
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003183 case HTTP_RES_ACT_SET_NICE:
3184 s->task->nice = rule->arg.nice;
3185 break;
3186
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003187 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003188 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003189 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003190 break;
3191
Willy Tarreau51347ed2013-06-11 19:34:13 +02003192 case HTTP_RES_ACT_SET_MARK:
3193#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003194 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003195 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003196#endif
3197 break;
3198
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003199 case HTTP_RES_ACT_SET_LOGL:
3200 s->logs.level = rule->arg.loglevel;
3201 break;
3202
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003203 case HTTP_RES_ACT_SET_HDR:
3204 ctx.idx = 0;
3205 /* remove all occurrences of the header */
3206 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3207 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3208 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3209 }
3210 /* now fall through to header addition */
3211
3212 case HTTP_RES_ACT_ADD_HDR:
3213 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3214 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3215 trash.len = rule->arg.hdr_add.name_len;
3216 trash.str[trash.len++] = ':';
3217 trash.str[trash.len++] = ' ';
3218 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3219 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3220 break;
3221 }
3222 }
3223
3224 /* we reached the end of the rules, nothing to report */
3225 return NULL;
3226}
3227
3228
Willy Tarreau71241ab2012-12-27 11:30:54 +01003229/* Perform an HTTP redirect based on the information in <rule>. The function
3230 * returns non-zero on success, or zero in case of a, irrecoverable error such
3231 * as too large a request to build a valid response.
3232 */
3233static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3234{
3235 struct http_msg *msg = &txn->req;
3236 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003237 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003238
3239 /* build redirect message */
3240 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003241 case 308:
3242 msg_fmt = HTTP_308;
3243 break;
3244 case 307:
3245 msg_fmt = HTTP_307;
3246 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003247 case 303:
3248 msg_fmt = HTTP_303;
3249 break;
3250 case 301:
3251 msg_fmt = HTTP_301;
3252 break;
3253 case 302:
3254 default:
3255 msg_fmt = HTTP_302;
3256 break;
3257 }
3258
3259 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3260 return 0;
3261
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003262 location = trash.str + trash.len;
3263
Willy Tarreau71241ab2012-12-27 11:30:54 +01003264 switch(rule->type) {
3265 case REDIRECT_TYPE_SCHEME: {
3266 const char *path;
3267 const char *host;
3268 struct hdr_ctx ctx;
3269 int pathlen;
3270 int hostlen;
3271
3272 host = "";
3273 hostlen = 0;
3274 ctx.idx = 0;
3275 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3276 host = ctx.line + ctx.val;
3277 hostlen = ctx.vlen;
3278 }
3279
3280 path = http_get_path(txn);
3281 /* build message using path */
3282 if (path) {
3283 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3284 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3285 int qs = 0;
3286 while (qs < pathlen) {
3287 if (path[qs] == '?') {
3288 pathlen = qs;
3289 break;
3290 }
3291 qs++;
3292 }
3293 }
3294 } else {
3295 path = "/";
3296 pathlen = 1;
3297 }
3298
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003299 if (rule->rdr_str) { /* this is an old "redirect" rule */
3300 /* check if we can add scheme + "://" + host + path */
3301 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3302 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003303
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003304 /* add scheme */
3305 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3306 trash.len += rule->rdr_len;
3307 }
3308 else {
3309 /* add scheme with executing log format */
3310 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003311
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003312 /* check if we can add scheme + "://" + host + path */
3313 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3314 return 0;
3315 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003316 /* add "://" */
3317 memcpy(trash.str + trash.len, "://", 3);
3318 trash.len += 3;
3319
3320 /* add host */
3321 memcpy(trash.str + trash.len, host, hostlen);
3322 trash.len += hostlen;
3323
3324 /* add path */
3325 memcpy(trash.str + trash.len, path, pathlen);
3326 trash.len += pathlen;
3327
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003328 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003329 if (trash.len && trash.str[trash.len - 1] != '/' &&
3330 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3331 if (trash.len > trash.size - 5)
3332 return 0;
3333 trash.str[trash.len] = '/';
3334 trash.len++;
3335 }
3336
3337 break;
3338 }
3339 case REDIRECT_TYPE_PREFIX: {
3340 const char *path;
3341 int pathlen;
3342
3343 path = http_get_path(txn);
3344 /* build message using path */
3345 if (path) {
3346 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3347 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3348 int qs = 0;
3349 while (qs < pathlen) {
3350 if (path[qs] == '?') {
3351 pathlen = qs;
3352 break;
3353 }
3354 qs++;
3355 }
3356 }
3357 } else {
3358 path = "/";
3359 pathlen = 1;
3360 }
3361
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003362 if (rule->rdr_str) { /* this is an old "redirect" rule */
3363 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3364 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003365
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003366 /* add prefix. Note that if prefix == "/", we don't want to
3367 * add anything, otherwise it makes it hard for the user to
3368 * configure a self-redirection.
3369 */
3370 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3371 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3372 trash.len += rule->rdr_len;
3373 }
3374 }
3375 else {
3376 /* add prefix with executing log format */
3377 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3378
3379 /* Check length */
3380 if (trash.len + pathlen > trash.size - 4)
3381 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003382 }
3383
3384 /* add path */
3385 memcpy(trash.str + trash.len, path, pathlen);
3386 trash.len += pathlen;
3387
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003388 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003389 if (trash.len && trash.str[trash.len - 1] != '/' &&
3390 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3391 if (trash.len > trash.size - 5)
3392 return 0;
3393 trash.str[trash.len] = '/';
3394 trash.len++;
3395 }
3396
3397 break;
3398 }
3399 case REDIRECT_TYPE_LOCATION:
3400 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003401 if (rule->rdr_str) { /* this is an old "redirect" rule */
3402 if (trash.len + rule->rdr_len > trash.size - 4)
3403 return 0;
3404
3405 /* add location */
3406 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3407 trash.len += rule->rdr_len;
3408 }
3409 else {
3410 /* add location with executing log format */
3411 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003412
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003413 /* Check left length */
3414 if (trash.len > trash.size - 4)
3415 return 0;
3416 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003417 break;
3418 }
3419
3420 if (rule->cookie_len) {
3421 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3422 trash.len += 14;
3423 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3424 trash.len += rule->cookie_len;
3425 memcpy(trash.str + trash.len, "\r\n", 2);
3426 trash.len += 2;
3427 }
3428
3429 /* add end of headers and the keep-alive/close status.
3430 * We may choose to set keep-alive if the Location begins
3431 * with a slash, because the client will come back to the
3432 * same server.
3433 */
3434 txn->status = rule->code;
3435 /* let's log the request time */
3436 s->logs.tv_request = now;
3437
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003438 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003439 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3440 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3441 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3442 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3443 /* keep-alive possible */
3444 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3445 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3446 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3447 trash.len += 30;
3448 } else {
3449 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3450 trash.len += 24;
3451 }
3452 }
3453 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3454 trash.len += 4;
3455 bo_inject(txn->rsp.chn, trash.str, trash.len);
3456 /* "eat" the request */
3457 bi_fast_delete(txn->req.chn->buf, msg->sov);
3458 msg->sov = 0;
3459 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3460 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3461 txn->req.msg_state = HTTP_MSG_CLOSED;
3462 txn->rsp.msg_state = HTTP_MSG_DONE;
3463 } else {
3464 /* keep-alive not possible */
3465 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3466 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3467 trash.len += 29;
3468 } else {
3469 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3470 trash.len += 23;
3471 }
3472 stream_int_retnclose(txn->req.chn->prod, &trash);
3473 txn->req.chn->analysers = 0;
3474 }
3475
3476 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003477 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003478 if (!(s->flags & SN_FINST_MASK))
3479 s->flags |= SN_FINST_R;
3480
3481 return 1;
3482}
3483
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003484/* This stream analyser runs all HTTP request processing which is common to
3485 * frontends and backends, which means blocking ACLs, filters, connection-close,
3486 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003487 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003488 * either needs more data or wants to immediately abort the request (eg: deny,
3489 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003490 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003491int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003492{
Willy Tarreaud787e662009-07-07 10:14:51 +02003493 struct http_txn *txn = &s->txn;
3494 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003495 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003496 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003497 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003498 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003499
Willy Tarreau655dce92009-11-08 13:10:58 +01003500 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003501 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003502 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003503 return 0;
3504 }
3505
Willy Tarreau3a816292009-07-07 10:55:49 +02003506 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003507 req->analyse_exp = TICK_ETERNITY;
3508
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003509 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 +02003510 now_ms, __FUNCTION__,
3511 s,
3512 req,
3513 req->rex, req->wex,
3514 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003515 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003516 req->analysers);
3517
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003518 /* first check whether we have some ACLs set to block this request */
3519 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003520 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003521
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003522 ret = acl_pass(ret);
3523 if (cond->pol == ACL_COND_UNLESS)
3524 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003525
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003526 if (ret) {
3527 txn->status = 403;
3528 /* let's log the request time */
3529 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003530 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003531 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003532 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003533 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003534 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003535
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003536 /* just in case we have some per-backend tracking */
3537 session_inc_be_http_req_ctr(s);
3538
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003539 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003540 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003541
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003542 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003543 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003544 if (stats_check_uri(s->rep->prod, txn, px)) {
3545 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003546 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3547 txn->status = 500;
3548 s->logs.tv_request = now;
3549 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003550
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003551 if (!(s->flags & SN_ERR_MASK))
3552 s->flags |= SN_ERR_RESOURCE;
3553 goto return_prx_cond;
3554 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003555 /* parse the whole stats request and extract the relevant information */
3556 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003557 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 +01003558 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003559 }
3560
Willy Tarreau3b44e722013-11-16 10:28:23 +01003561 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3562 * blocked by an http-request rule.
3563 */
3564 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003565 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003566 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003567 }
Willy Tarreau06619262006-12-17 08:37:22 +01003568
Willy Tarreau3b44e722013-11-16 10:28:23 +01003569 /* return a 403 if either rule has blocked */
3570 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003571 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003572 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003573 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003574 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003575 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003576 s->fe->fe_counters.denied_req++;
3577 if (s->fe != s->be)
3578 s->be->be_counters.denied_req++;
3579 if (s->listener->counters)
3580 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003581 goto return_prx_cond;
3582 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003583
3584 /* When a connection is tarpitted, we use the tarpit timeout,
3585 * which may be the same as the connect timeout if unspecified.
3586 * If unset, then set it to zero because we really want it to
3587 * eventually expire. We build the tarpit as an analyser.
3588 */
3589 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003590 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003591 /* wipe the request out so that we can drop the connection early
3592 * if the client closes first.
3593 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003594 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003595 req->analysers = 0; /* remove switching rules etc... */
3596 req->analysers |= AN_REQ_HTTP_TARPIT;
3597 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3598 if (!req->analyse_exp)
3599 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003600 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003601 s->fe->fe_counters.denied_req++;
3602 if (s->fe != s->be)
3603 s->be->be_counters.denied_req++;
3604 if (s->listener->counters)
3605 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003606 return 1;
3607 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003608 }
Willy Tarreau06619262006-12-17 08:37:22 +01003609
Willy Tarreau70dffda2014-01-30 03:07:23 +01003610 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003611 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003612 * Option httpclose by itself sets tunnel mode where headers are mangled.
3613 * However, if another mode is set, it will affect it (eg: server-close/
3614 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3615 * if FE and BE have the same settings (common). The method consists in
3616 * checking if options changed between the two calls (implying that either
3617 * one is non-null, or one of them is non-null and we are there for the first
3618 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003619 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003620
Willy Tarreau416ce612014-01-31 15:45:34 +01003621 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003622 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003623 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003624
Willy Tarreau70dffda2014-01-30 03:07:23 +01003625 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3626 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3627 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3628 tmp = TX_CON_WANT_TUN;
3629
3630 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3631 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3632 tmp = TX_CON_WANT_TUN;
3633 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003634
3635 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003636 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3637 /* option httpclose + server_close => forceclose */
3638 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3639 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3640 tmp = TX_CON_WANT_CLO;
3641 else
3642 tmp = TX_CON_WANT_SCL;
3643 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003644
3645 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3646 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003647 tmp = TX_CON_WANT_CLO;
3648
Willy Tarreau5b154472009-12-21 20:11:07 +01003649 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3650 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003651
Willy Tarreau416ce612014-01-31 15:45:34 +01003652 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3653 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003654 /* parse the Connection header and possibly clean it */
3655 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003656 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003657 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3658 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003659 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003660 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003661 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003662 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003663 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003664
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003665 /* check if client or config asks for explicit close in KAL/SCL */
3666 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3667 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3668 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003669 (!(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 +01003670 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003671 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003672 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3673 }
Willy Tarreau78599912009-10-17 20:12:21 +02003674
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003675 /* we can be blocked here because the request needs to be authenticated,
3676 * either to pass or to access stats.
3677 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003678 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003679 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003680
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003681 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003682 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003683
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003684 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003685 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003686 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003687 /* on 401 we still count one error, because normal browsing
3688 * won't significantly increase the counter but brute force
3689 * attempts will.
3690 */
3691 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003692 goto return_prx_cond;
3693 }
3694
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003695 /* add request headers from the rule sets in the same order */
3696 list_for_each_entry(wl, &px->req_add, list) {
3697 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003698 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003699 ret = acl_pass(ret);
3700 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3701 ret = !ret;
3702 if (!ret)
3703 continue;
3704 }
3705
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003706 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003707 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003708 }
3709
3710 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3711 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3712 goto return_bad_req;
3713 req->analyse_exp = TICK_ETERNITY;
3714 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003715 }
3716
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003717 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003718 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003719 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3720 s->fe->fe_counters.intercepted_req++;
3721
3722 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3723 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3724 if (!(s->flags & SN_FINST_MASK))
3725 s->flags |= SN_FINST_R;
3726
3727 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003728 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003729 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003730 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003731
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003732 /* check whether we have some ACLs set to redirect this request */
3733 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003734 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003735 int ret;
3736
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003737 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003738 ret = acl_pass(ret);
3739 if (rule->cond->pol == ACL_COND_UNLESS)
3740 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003741 if (!ret)
3742 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003743 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003744 if (!http_apply_redirect_rule(rule, s, txn))
3745 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003746
Willy Tarreau71241ab2012-12-27 11:30:54 +01003747 req->analyse_exp = TICK_ETERNITY;
3748 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003749 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003750
Willy Tarreau2be39392010-01-03 17:24:51 +01003751 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3752 * If this happens, then the data will not come immediately, so we must
3753 * send all what we have without waiting. Note that due to the small gain
3754 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003755 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003756 * itself once used.
3757 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003758 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003759
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003760 /* that's OK for us now, let's move on to next analysers */
3761 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003762
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003763 return_bad_req:
3764 /* We centralize bad requests processing here */
3765 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3766 /* we detected a parsing error. We want to archive this request
3767 * in the dedicated proxy area for later troubleshooting.
3768 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003769 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003770 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003771
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003772 txn->req.msg_state = HTTP_MSG_ERROR;
3773 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003774 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003775
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003776 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003777 if (s->listener->counters)
3778 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003779
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003780 return_prx_cond:
3781 if (!(s->flags & SN_ERR_MASK))
3782 s->flags |= SN_ERR_PRXCOND;
3783 if (!(s->flags & SN_FINST_MASK))
3784 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003785
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003786 req->analysers = 0;
3787 req->analyse_exp = TICK_ETERNITY;
3788 return 0;
3789}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003790
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003791/* This function performs all the processing enabled for the current request.
3792 * It returns 1 if the processing can continue on next analysers, or zero if it
3793 * needs more data, encounters an error, or wants to immediately abort the
3794 * request. It relies on buffers flags, and updates s->req->analysers.
3795 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003796int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003797{
3798 struct http_txn *txn = &s->txn;
3799 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003800 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003801
Willy Tarreau655dce92009-11-08 13:10:58 +01003802 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003803 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003804 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003805 return 0;
3806 }
3807
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003808 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 +02003809 now_ms, __FUNCTION__,
3810 s,
3811 req,
3812 req->rex, req->wex,
3813 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003814 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003815 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003816
William Lallemand82fe75c2012-10-23 10:25:10 +02003817 if (s->fe->comp || s->be->comp)
3818 select_compression_request_header(s, req->buf);
3819
Willy Tarreau59234e92008-11-30 23:51:27 +01003820 /*
3821 * Right now, we know that we have processed the entire headers
3822 * and that unwanted requests have been filtered out. We can do
3823 * whatever we want with the remaining request. Also, now we
3824 * may have separate values for ->fe, ->be.
3825 */
Willy Tarreau06619262006-12-17 08:37:22 +01003826
Willy Tarreau59234e92008-11-30 23:51:27 +01003827 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003828 * If HTTP PROXY is set we simply get remote server address parsing
3829 * incoming request. Note that this requires that a connection is
3830 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003831 */
3832 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003833 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003834 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003835
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003836 /* Note that for now we don't reuse existing proxy connections */
3837 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003838 txn->req.msg_state = HTTP_MSG_ERROR;
3839 txn->status = 500;
3840 req->analysers = 0;
3841 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3842
3843 if (!(s->flags & SN_ERR_MASK))
3844 s->flags |= SN_ERR_RESOURCE;
3845 if (!(s->flags & SN_FINST_MASK))
3846 s->flags |= SN_FINST_R;
3847
3848 return 0;
3849 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003850
3851 path = http_get_path(txn);
3852 url2sa(req->buf->p + msg->sl.rq.u,
3853 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3854 &conn->addr.to);
3855 /* if the path was found, we have to remove everything between
3856 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3857 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3858 * u_l characters by a single "/".
3859 */
3860 if (path) {
3861 char *cur_ptr = req->buf->p;
3862 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3863 int delta;
3864
3865 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3866 http_msg_move_end(&txn->req, delta);
3867 cur_end += delta;
3868 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3869 goto return_bad_req;
3870 }
3871 else {
3872 char *cur_ptr = req->buf->p;
3873 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3874 int delta;
3875
3876 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3877 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3878 http_msg_move_end(&txn->req, delta);
3879 cur_end += delta;
3880 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3881 goto return_bad_req;
3882 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003883 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003884
Willy Tarreau59234e92008-11-30 23:51:27 +01003885 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003886 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003887 * Note that doing so might move headers in the request, but
3888 * the fields will stay coherent and the URI will not move.
3889 * This should only be performed in the backend.
3890 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003891 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003892 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3893 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003894
Willy Tarreau59234e92008-11-30 23:51:27 +01003895 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003896 * 8: the appsession cookie was looked up very early in 1.2,
3897 * so let's do the same now.
3898 */
3899
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003900 /* It needs to look into the URI unless persistence must be ignored */
3901 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003902 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 +01003903 }
3904
William Lallemanda73203e2012-03-12 12:48:57 +01003905 /* add unique-id if "header-unique-id" is specified */
3906
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003907 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3908 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3909 goto return_bad_req;
3910 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003911 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003912 }
William Lallemanda73203e2012-03-12 12:48:57 +01003913
3914 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003915 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3916 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003917 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003918 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003919 goto return_bad_req;
3920 }
3921
Cyril Bontéb21570a2009-11-29 20:04:48 +01003922 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003923 * 9: add X-Forwarded-For if either the frontend or the backend
3924 * asks for it.
3925 */
3926 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003927 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003928 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003929 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3930 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003931 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003932 /* The header is set to be added only if none is present
3933 * and we found it, so don't do anything.
3934 */
3935 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003936 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003937 /* Add an X-Forwarded-For header unless the source IP is
3938 * in the 'except' network range.
3939 */
3940 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003941 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003942 != s->fe->except_net.s_addr) &&
3943 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003944 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003945 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003946 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003947 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003948 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003949
3950 /* Note: we rely on the backend to get the header name to be used for
3951 * x-forwarded-for, because the header is really meant for the backends.
3952 * However, if the backend did not specify any option, we have to rely
3953 * on the frontend's header name.
3954 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003955 if (s->be->fwdfor_hdr_len) {
3956 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003957 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003958 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003959 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003960 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003961 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003962 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003963
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003964 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003965 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003966 }
3967 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003968 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003969 /* FIXME: for the sake of completeness, we should also support
3970 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003971 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003972 int len;
3973 char pn[INET6_ADDRSTRLEN];
3974 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003975 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003976 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003977
Willy Tarreau59234e92008-11-30 23:51:27 +01003978 /* Note: we rely on the backend to get the header name to be used for
3979 * x-forwarded-for, because the header is really meant for the backends.
3980 * However, if the backend did not specify any option, we have to rely
3981 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003982 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003983 if (s->be->fwdfor_hdr_len) {
3984 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003985 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 } else {
3987 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003989 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003990 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003992 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003993 goto return_bad_req;
3994 }
3995 }
3996
3997 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003998 * 10: add X-Original-To if either the frontend or the backend
3999 * asks for it.
4000 */
4001 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4002
4003 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004004 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004005 /* Add an X-Original-To header unless the destination IP is
4006 * in the 'except' network range.
4007 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004008 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004009
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004010 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004011 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004012 (((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 +02004013 != s->fe->except_to.s_addr) &&
4014 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004015 (((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 +02004016 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004017 int len;
4018 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004019 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004020
4021 /* Note: we rely on the backend to get the header name to be used for
4022 * x-original-to, because the header is really meant for the backends.
4023 * However, if the backend did not specify any option, we have to rely
4024 * on the frontend's header name.
4025 */
4026 if (s->be->orgto_hdr_len) {
4027 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004028 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004029 } else {
4030 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004031 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004032 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004033 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004034
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004035 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004036 goto return_bad_req;
4037 }
4038 }
4039 }
4040
Willy Tarreau50fc7772012-11-11 22:19:57 +01004041 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4042 * If an "Upgrade" token is found, the header is left untouched in order not to have
4043 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4044 * "Upgrade" is present in the Connection header.
4045 */
4046 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4047 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004048 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4049 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004050 unsigned int want_flags = 0;
4051
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004052 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004053 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004054 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4055 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004056 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004057 want_flags |= TX_CON_CLO_SET;
4058 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004059 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004060 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4061 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004062 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004063 want_flags |= TX_CON_KAL_SET;
4064 }
4065
4066 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004067 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004068 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004069
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004070
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 /* If we have no server assigned yet and we're balancing on url_param
4072 * with a POST request, we may be interested in checking the body for
4073 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004074 */
4075 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4076 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004077 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004078 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004079 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004080 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004081 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004082
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004083 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004084 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004085#ifdef TCP_QUICKACK
4086 /* We expect some data from the client. Unless we know for sure
4087 * we already have a full request, we have to re-enable quick-ack
4088 * in case we previously disabled it, otherwise we might cause
4089 * the client to delay further data.
4090 */
4091 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004092 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004093 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004094 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004095 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004096#endif
4097 }
Willy Tarreau03945942009-12-22 16:50:27 +01004098
Willy Tarreau59234e92008-11-30 23:51:27 +01004099 /*************************************************************
4100 * OK, that's finished for the headers. We have done what we *
4101 * could. Let's switch to the DATA state. *
4102 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004103 req->analyse_exp = TICK_ETERNITY;
4104 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004105
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004106 /* if the server closes the connection, we want to immediately react
4107 * and close the socket to save packets and syscalls.
4108 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004109 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4110 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004111
Willy Tarreau59234e92008-11-30 23:51:27 +01004112 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004113 /* OK let's go on with the BODY now */
4114 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004115
Willy Tarreau59234e92008-11-30 23:51:27 +01004116 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004117 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004118 /* we detected a parsing error. We want to archive this request
4119 * in the dedicated proxy area for later troubleshooting.
4120 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004121 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004122 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004123
Willy Tarreau59234e92008-11-30 23:51:27 +01004124 txn->req.msg_state = HTTP_MSG_ERROR;
4125 txn->status = 400;
4126 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004127 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004128
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004129 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004130 if (s->listener->counters)
4131 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004132
Willy Tarreau59234e92008-11-30 23:51:27 +01004133 if (!(s->flags & SN_ERR_MASK))
4134 s->flags |= SN_ERR_PRXCOND;
4135 if (!(s->flags & SN_FINST_MASK))
4136 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004137 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004138}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004139
Willy Tarreau60b85b02008-11-30 23:28:40 +01004140/* This function is an analyser which processes the HTTP tarpit. It always
4141 * returns zero, at the beginning because it prevents any other processing
4142 * from occurring, and at the end because it terminates the request.
4143 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004144int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004145{
4146 struct http_txn *txn = &s->txn;
4147
4148 /* This connection is being tarpitted. The CLIENT side has
4149 * already set the connect expiration date to the right
4150 * timeout. We just have to check that the client is still
4151 * there and that the timeout has not expired.
4152 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004153 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004154 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004155 !tick_is_expired(req->analyse_exp, now_ms))
4156 return 0;
4157
4158 /* We will set the queue timer to the time spent, just for
4159 * logging purposes. We fake a 500 server error, so that the
4160 * attacker will not suspect his connection has been tarpitted.
4161 * It will not cause trouble to the logs because we can exclude
4162 * the tarpitted connections by filtering on the 'PT' status flags.
4163 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004164 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4165
4166 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004167 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004168 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004169
4170 req->analysers = 0;
4171 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004172
Willy Tarreau60b85b02008-11-30 23:28:40 +01004173 if (!(s->flags & SN_ERR_MASK))
4174 s->flags |= SN_ERR_PRXCOND;
4175 if (!(s->flags & SN_FINST_MASK))
4176 s->flags |= SN_FINST_T;
4177 return 0;
4178}
4179
Willy Tarreaud34af782008-11-30 23:36:37 +01004180/* This function is an analyser which processes the HTTP request body. It looks
4181 * for parameters to be used for the load balancing algorithm (url_param). It
4182 * must only be called after the standard HTTP request processing has occurred,
4183 * because it expects the request to be parsed. It returns zero if it needs to
4184 * read more data, or 1 once it has completed its analysis.
4185 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004186int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004187{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004188 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004189 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004190 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004191
4192 /* We have to parse the HTTP request body to find any required data.
4193 * "balance url_param check_post" should have been the only way to get
4194 * into this. We were brought here after HTTP header analysis, so all
4195 * related structures are ready.
4196 */
4197
Willy Tarreau522d6c02009-12-06 18:49:18 +01004198 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4199 goto missing_data;
4200
4201 if (msg->msg_state < HTTP_MSG_100_SENT) {
4202 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4203 * send an HTTP/1.1 100 Continue intermediate response.
4204 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004205 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004206 struct hdr_ctx ctx;
4207 ctx.idx = 0;
4208 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004209 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004210 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004211 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004212 }
4213 }
4214 msg->msg_state = HTTP_MSG_100_SENT;
4215 }
4216
4217 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004218 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004219 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004220 * is still null. We must save the body in msg->next because it
4221 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004222 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004223 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004224
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004225 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004226 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4227 else
4228 msg->msg_state = HTTP_MSG_DATA;
4229 }
4230
4231 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004232 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004233 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004234 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004235 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004236 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004237
Willy Tarreau115acb92009-12-26 13:56:06 +01004238 if (!ret)
4239 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004240 else if (ret < 0) {
4241 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004242 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004243 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004244 }
4245
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004247 * We have the first data byte is in msg->sov. We're waiting for at
4248 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004249 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004250
Willy Tarreau124d9912011-03-01 20:30:48 +01004251 if (msg->body_len < limit)
4252 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004253
Willy Tarreau9b28e032012-10-12 23:49:43 +02004254 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004255 goto http_end;
4256
4257 missing_data:
4258 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004259 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004260 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004261 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004262 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004263
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004264 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004265 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004266 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004267
4268 if (!(s->flags & SN_ERR_MASK))
4269 s->flags |= SN_ERR_CLITO;
4270 if (!(s->flags & SN_FINST_MASK))
4271 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004272 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004273 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004274
4275 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004276 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004277 /* Not enough data. We'll re-use the http-request
4278 * timeout here. Ideally, we should set the timeout
4279 * relative to the accept() date. We just set the
4280 * request timeout once at the beginning of the
4281 * request.
4282 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004283 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004284 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004285 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004286 return 0;
4287 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004288
4289 http_end:
4290 /* The situation will not evolve, so let's give up on the analysis. */
4291 s->logs.tv_request = now; /* update the request timer to reflect full request */
4292 req->analysers &= ~an_bit;
4293 req->analyse_exp = TICK_ETERNITY;
4294 return 1;
4295
4296 return_bad_req: /* let's centralize all bad requests */
4297 txn->req.msg_state = HTTP_MSG_ERROR;
4298 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004299 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004300
Willy Tarreau79ebac62010-06-07 13:47:49 +02004301 if (!(s->flags & SN_ERR_MASK))
4302 s->flags |= SN_ERR_PRXCOND;
4303 if (!(s->flags & SN_FINST_MASK))
4304 s->flags |= SN_FINST_R;
4305
Willy Tarreau522d6c02009-12-06 18:49:18 +01004306 return_err_msg:
4307 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004308 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004309 if (s->listener->counters)
4310 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004311 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004312}
4313
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004314/* send a server's name with an outgoing request over an established connection.
4315 * Note: this function is designed to be called once the request has been scheduled
4316 * for being forwarded. This is the reason why it rewinds the buffer before
4317 * proceeding.
4318 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004319int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004320
4321 struct hdr_ctx ctx;
4322
Mark Lamourinec2247f02012-01-04 13:02:01 -05004323 char *hdr_name = be->server_id_hdr_name;
4324 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004325 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004326 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004327 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004328
William Lallemandd9e90662012-01-30 17:27:17 +01004329 ctx.idx = 0;
4330
Willy Tarreau9b28e032012-10-12 23:49:43 +02004331 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004332 if (old_o) {
4333 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004334 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004335 }
4336
Willy Tarreau9b28e032012-10-12 23:49:43 +02004337 old_i = chn->buf->i;
4338 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 -05004339 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004340 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004341 }
4342
4343 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004344 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004345 memcpy(hdr_val, hdr_name, hdr_name_len);
4346 hdr_val += hdr_name_len;
4347 *hdr_val++ = ':';
4348 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004349 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4350 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004351
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004352 if (old_o) {
4353 /* If this was a forwarded request, we must readjust the amount of
4354 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004355 * variations. Note that if the request was already scheduled for
4356 * forwarding, it had its req->sol pointing to the body, which
4357 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004358 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004359 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004360 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004361 }
4362
Mark Lamourinec2247f02012-01-04 13:02:01 -05004363 return 0;
4364}
4365
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366/* Terminate current transaction and prepare a new one. This is very tricky
4367 * right now but it works.
4368 */
4369void http_end_txn_clean_session(struct session *s)
4370{
Willy Tarreau068621e2013-12-23 15:11:25 +01004371 int prev_status = s->txn.status;
4372
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373 /* FIXME: We need a more portable way of releasing a backend's and a
4374 * server's connections. We need a safer way to reinitialize buffer
4375 * flags. We also need a more accurate method for computing per-request
4376 * data.
4377 */
4378 http_silent_debug(__LINE__, s);
4379
Willy Tarreau4213a112013-12-15 10:25:42 +01004380 /* unless we're doing keep-alive, we want to quickly close the connection
4381 * to the server.
4382 */
4383 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4384 !si_conn_ready(s->req->cons)) {
4385 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4386 si_shutr(s->req->cons);
4387 si_shutw(s->req->cons);
4388 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389
4390 http_silent_debug(__LINE__, s);
4391
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004392 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004393 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004394 if (unlikely(s->srv_conn))
4395 sess_change_server(s, NULL);
4396 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397
4398 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4399 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004400 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401
4402 if (s->txn.status) {
4403 int n;
4404
4405 n = s->txn.status / 100;
4406 if (n < 1 || n > 5)
4407 n = 0;
4408
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004409 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004410 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004411 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004412 s->fe->fe_counters.p.http.comp_rsp++;
4413 }
Willy Tarreau24657792010-02-26 10:30:28 +01004414 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004415 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004416 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004417 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004418 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004419 s->be->be_counters.p.http.comp_rsp++;
4420 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004421 }
4422
4423 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004424 s->logs.bytes_in -= s->req->buf->i;
4425 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004426
4427 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004428 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 !(s->flags & SN_MONITOR) &&
4430 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4431 s->do_log(s);
4432 }
4433
4434 s->logs.accept_date = date; /* user-visible date for logging */
4435 s->logs.tv_accept = now; /* corrected date for internal use */
4436 tv_zero(&s->logs.tv_request);
4437 s->logs.t_queue = -1;
4438 s->logs.t_connect = -1;
4439 s->logs.t_data = -1;
4440 s->logs.t_close = 0;
4441 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4442 s->logs.srv_queue_size = 0; /* we will get this number soon */
4443
Willy Tarreau9b28e032012-10-12 23:49:43 +02004444 s->logs.bytes_in = s->req->total = s->req->buf->i;
4445 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446
4447 if (s->pend_pos)
4448 pendconn_free(s->pend_pos);
4449
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004450 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 if (s->flags & SN_CURR_SESS) {
4452 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004453 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004454 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004455 if (may_dequeue_tasks(objt_server(s->target), s->be))
4456 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004457 }
4458
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004459 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004460
Willy Tarreau4213a112013-12-15 10:25:42 +01004461 /* only release our endpoint if we don't intend to reuse the
4462 * connection.
4463 */
4464 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4465 !si_conn_ready(s->req->cons)) {
4466 si_release_endpoint(s->req->cons);
4467 }
4468
Willy Tarreau610ecce2010-01-04 21:15:02 +01004469 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004470 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004471 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004472 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004473 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004474 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4475 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 +02004476 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 +01004477 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004478
Willy Tarreau610ecce2010-01-04 21:15:02 +01004479 s->txn.meth = 0;
4480 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004481 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004482
4483 if (prev_status == 401 || prev_status == 407) {
4484 /* In HTTP keep-alive mode, if we receive a 401, we still have
4485 * a chance of being able to send the visitor again to the same
4486 * server over the same connection. This is required by some
4487 * broken protocols such as NTLM, and anyway whenever there is
4488 * an opportunity for sending the challenge to the proper place,
4489 * it's better to do it (at least it helps with debugging).
4490 */
4491 s->txn.flags |= TX_PREFER_LAST;
4492 }
4493
Willy Tarreauee55dc02010-06-01 10:56:34 +02004494 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495 s->req->cons->flags |= SI_FL_INDEP_STR;
4496
Willy Tarreau96e31212011-05-30 18:10:30 +02004497 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004498 s->req->flags |= CF_NEVER_WAIT;
4499 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004500 }
4501
Willy Tarreau610ecce2010-01-04 21:15:02 +01004502 /* if the request buffer is not empty, it means we're
4503 * about to process another request, so send pending
4504 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004505 * Just don't do this if the buffer is close to be full,
4506 * because the request will wait for it to flush a little
4507 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004509 if (s->req->buf->i) {
4510 if (s->rep->buf->o &&
4511 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4512 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004513 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004514 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004515
4516 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004517 channel_auto_read(s->req);
4518 channel_auto_close(s->req);
4519 channel_auto_read(s->rep);
4520 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004521
Willy Tarreau27375622013-12-17 00:00:28 +01004522 /* we're in keep-alive with an idle connection, monitor it */
4523 si_idle_conn(s->req->cons);
4524
Willy Tarreau342b11c2010-11-24 16:22:09 +01004525 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004526 s->rep->analysers = 0;
4527
4528 http_silent_debug(__LINE__, s);
4529}
4530
4531
4532/* This function updates the request state machine according to the response
4533 * state machine and buffer flags. It returns 1 if it changes anything (flag
4534 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4535 * it is only used to find when a request/response couple is complete. Both
4536 * this function and its equivalent should loop until both return zero. It
4537 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4538 */
4539int http_sync_req_state(struct session *s)
4540{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004541 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004543 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544 unsigned int old_state = txn->req.msg_state;
4545
4546 http_silent_debug(__LINE__, s);
4547 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4548 return 0;
4549
4550 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004551 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004552 * We can shut the read side unless we want to abort_on_close,
4553 * or we have a POST request. The issue with POST requests is
4554 * that some browsers still send a CRLF after the request, and
4555 * this CRLF must be read so that it does not remain in the kernel
4556 * buffers, otherwise a close could cause an RST on some systems
4557 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004558 * Note that if we're using keep-alive on the client side, we'd
4559 * rather poll now and keep the polling enabled for the whole
4560 * session's life than enabling/disabling it between each
4561 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004562 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004563 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4564 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4565 !(s->be->options & PR_O_ABRT_CLOSE) &&
4566 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004567 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568
Willy Tarreau40f151a2012-12-20 12:10:09 +01004569 /* if the server closes the connection, we want to immediately react
4570 * and close the socket to save packets and syscalls.
4571 */
4572 chn->cons->flags |= SI_FL_NOHALF;
4573
Willy Tarreau610ecce2010-01-04 21:15:02 +01004574 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4575 goto wait_other_side;
4576
4577 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4578 /* The server has not finished to respond, so we
4579 * don't want to move in order not to upset it.
4580 */
4581 goto wait_other_side;
4582 }
4583
4584 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4585 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004586 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004588 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 goto wait_other_side;
4590 }
4591
4592 /* When we get here, it means that both the request and the
4593 * response have finished receiving. Depending on the connection
4594 * mode, we'll have to wait for the last bytes to leave in either
4595 * direction, and sometimes for a close to be effective.
4596 */
4597
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004598 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4599 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004600 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4601 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004602 }
4603 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4604 /* Option forceclose is set, or either side wants to close,
4605 * let's enforce it now that we're not expecting any new
4606 * data to come. The caller knows the session is complete
4607 * once both states are CLOSED.
4608 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004609 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4610 channel_shutr_now(chn);
4611 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004612 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004613 }
4614 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004615 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4616 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004617 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004618 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4619 channel_auto_read(chn);
4620 txn->req.msg_state = HTTP_MSG_TUNNEL;
4621 chn->flags |= CF_NEVER_WAIT;
4622 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004623 }
4624
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004625 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004627 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004628
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004629 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004630 txn->req.msg_state = HTTP_MSG_CLOSING;
4631 goto http_msg_closing;
4632 }
4633 else {
4634 txn->req.msg_state = HTTP_MSG_CLOSED;
4635 goto http_msg_closed;
4636 }
4637 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004638 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 }
4640
4641 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4642 http_msg_closing:
4643 /* nothing else to forward, just waiting for the output buffer
4644 * to be empty and for the shutw_now to take effect.
4645 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004646 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004647 txn->req.msg_state = HTTP_MSG_CLOSED;
4648 goto http_msg_closed;
4649 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004650 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 txn->req.msg_state = HTTP_MSG_ERROR;
4652 goto wait_other_side;
4653 }
4654 }
4655
4656 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4657 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004658 /* see above in MSG_DONE why we only do this in these states */
4659 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4660 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4661 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004662 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004663 goto wait_other_side;
4664 }
4665
4666 wait_other_side:
4667 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004668 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669}
4670
4671
4672/* This function updates the response state machine according to the request
4673 * state machine and buffer flags. It returns 1 if it changes anything (flag
4674 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4675 * it is only used to find when a request/response couple is complete. Both
4676 * this function and its equivalent should loop until both return zero. It
4677 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4678 */
4679int http_sync_res_state(struct session *s)
4680{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004681 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004682 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004683 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684 unsigned int old_state = txn->rsp.msg_state;
4685
4686 http_silent_debug(__LINE__, s);
4687 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4688 return 0;
4689
4690 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4691 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004692 * still monitor the server connection for a possible close
4693 * while the request is being uploaded, so we don't disable
4694 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004695 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004696 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697
4698 if (txn->req.msg_state == HTTP_MSG_ERROR)
4699 goto wait_other_side;
4700
4701 if (txn->req.msg_state < HTTP_MSG_DONE) {
4702 /* The client seems to still be sending data, probably
4703 * because we got an error response during an upload.
4704 * We have the choice of either breaking the connection
4705 * or letting it pass through. Let's do the later.
4706 */
4707 goto wait_other_side;
4708 }
4709
4710 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4711 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004712 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004713 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004714 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004715 goto wait_other_side;
4716 }
4717
4718 /* When we get here, it means that both the request and the
4719 * response have finished receiving. Depending on the connection
4720 * mode, we'll have to wait for the last bytes to leave in either
4721 * direction, and sometimes for a close to be effective.
4722 */
4723
4724 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4725 /* Server-close mode : shut read and wait for the request
4726 * side to close its output buffer. The caller will detect
4727 * when we're in DONE and the other is in CLOSED and will
4728 * catch that for the final cleanup.
4729 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004730 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4731 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004733 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4734 /* Option forceclose is set, or either side wants to close,
4735 * let's enforce it now that we're not expecting any new
4736 * data to come. The caller knows the session is complete
4737 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004739 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4740 channel_shutr_now(chn);
4741 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004742 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743 }
4744 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004745 /* The last possible modes are keep-alive and tunnel. Tunnel will
4746 * need to forward remaining data. Keep-alive will need to monitor
4747 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004749 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004750 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004751 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4752 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004753 }
4754
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004755 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004756 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004757 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004758 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4759 goto http_msg_closing;
4760 }
4761 else {
4762 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4763 goto http_msg_closed;
4764 }
4765 }
4766 goto wait_other_side;
4767 }
4768
4769 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4770 http_msg_closing:
4771 /* nothing else to forward, just waiting for the output buffer
4772 * to be empty and for the shutw_now to take effect.
4773 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004774 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004775 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4776 goto http_msg_closed;
4777 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004778 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004779 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004780 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004781 if (objt_server(s->target))
4782 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004783 goto wait_other_side;
4784 }
4785 }
4786
4787 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4788 http_msg_closed:
4789 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004790 bi_erase(chn);
4791 channel_auto_close(chn);
4792 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793 goto wait_other_side;
4794 }
4795
4796 wait_other_side:
4797 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004798 /* We force the response to leave immediately if we're waiting for the
4799 * other side, since there is no pending shutdown to push it out.
4800 */
4801 if (!channel_is_empty(chn))
4802 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004803 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804}
4805
4806
4807/* Resync the request and response state machines. Return 1 if either state
4808 * changes.
4809 */
4810int http_resync_states(struct session *s)
4811{
4812 struct http_txn *txn = &s->txn;
4813 int old_req_state = txn->req.msg_state;
4814 int old_res_state = txn->rsp.msg_state;
4815
4816 http_silent_debug(__LINE__, s);
4817 http_sync_req_state(s);
4818 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004819 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004820 if (!http_sync_res_state(s))
4821 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004822 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 if (!http_sync_req_state(s))
4824 break;
4825 }
4826 http_silent_debug(__LINE__, s);
4827 /* OK, both state machines agree on a compatible state.
4828 * There are a few cases we're interested in :
4829 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4830 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4831 * directions, so let's simply disable both analysers.
4832 * - HTTP_MSG_CLOSED on the response only means we must abort the
4833 * request.
4834 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4835 * with server-close mode means we've completed one request and we
4836 * must re-initialize the server connection.
4837 */
4838
4839 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4840 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4841 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4842 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4843 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004844 channel_auto_close(s->req);
4845 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004846 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004847 channel_auto_close(s->rep);
4848 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004849 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004850 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4851 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004852 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004853 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004854 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004855 channel_auto_close(s->rep);
4856 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004857 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004858 channel_abort(s->req);
4859 channel_auto_close(s->req);
4860 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004861 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004862 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004863 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4864 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004865 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004866 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4867 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4868 /* server-close/keep-alive: terminate this transaction,
4869 * possibly killing the server connection and reinitialize
4870 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004871 */
4872 http_end_txn_clean_session(s);
4873 }
4874
4875 http_silent_debug(__LINE__, s);
4876 return txn->req.msg_state != old_req_state ||
4877 txn->rsp.msg_state != old_res_state;
4878}
4879
Willy Tarreaud98cf932009-12-27 22:54:55 +01004880/* This function is an analyser which forwards request body (including chunk
4881 * sizes if any). It is called as soon as we must forward, even if we forward
4882 * zero byte. The only situation where it must not be called is when we're in
4883 * tunnel mode and we want to forward till the close. It's used both to forward
4884 * remaining data and to resync after end of body. It expects the msg_state to
4885 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4886 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004887 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004888 * bytes of pending data + the headers if not already done (between sol and sov).
4889 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004890 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004891int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004892{
4893 struct http_txn *txn = &s->txn;
4894 struct http_msg *msg = &s->txn.req;
4895
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004896 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4897 return 0;
4898
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004899 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004900 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004901 /* Output closed while we were sending data. We must abort and
4902 * wake the other side up.
4903 */
4904 msg->msg_state = HTTP_MSG_ERROR;
4905 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004906 return 1;
4907 }
4908
Willy Tarreau80a92c02014-03-12 10:41:13 +01004909 /* Some post-connect processing might want us to refrain from starting to
4910 * forward data. Currently, the only reason for this is "balance url_param"
4911 * whichs need to parse/process the request after we've enabled forwarding.
4912 */
4913 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
4914 if (!(s->rep->flags & CF_READ_ATTACHED)) {
4915 channel_auto_connect(req);
4916 goto missing_data;
4917 }
4918 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4919 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004920
4921 /* Note that we don't have to send 100-continue back because we don't
4922 * need the data to complete our job, and it's up to the server to
4923 * decide whether to return 100, 417 or anything else in return of
4924 * an "Expect: 100-continue" header.
4925 */
4926
4927 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004928 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004929 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004930 * is still null. We must save the body in msg->next because it
4931 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004932 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004933 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004934
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004935 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004936 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004937 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004938 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004939 }
4940
Willy Tarreau80a92c02014-03-12 10:41:13 +01004941 /* in most states, we should abort in case of early close */
4942 channel_auto_close(req);
4943
Willy Tarreaud98cf932009-12-27 22:54:55 +01004944 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004945 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004946
Willy Tarreau610ecce2010-01-04 21:15:02 +01004947 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004948 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004949 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004950 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004951 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004952 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004953 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004954 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004955 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004956
Willy Tarreaucaabe412010-01-03 23:08:28 +01004957 if (msg->msg_state == HTTP_MSG_DATA) {
4958 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004959 if (req->to_forward) {
4960 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004961 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004962 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01004963
4964 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004965 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004966 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004968 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004969 }
4970 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004971 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004972 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004973 * TRAILERS state.
4974 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004975 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004976
Willy Tarreau54d23df2012-10-25 19:04:45 +02004977 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004978 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004979 else if (ret < 0) {
4980 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004981 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004982 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004983 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004984 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004985 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004986 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004987 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004988 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004989 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004990
4991 if (ret == 0)
4992 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004993 else if (ret < 0) {
4994 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004995 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004996 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004997 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004998 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004999 /* we're in MSG_CHUNK_SIZE now */
5000 }
5001 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005002 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005003
5004 if (ret == 0)
5005 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005006 else if (ret < 0) {
5007 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005008 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005009 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005010 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005011 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005012 /* we're in HTTP_MSG_DONE now */
5013 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005015 int old_state = msg->msg_state;
5016
Willy Tarreau610ecce2010-01-04 21:15:02 +01005017 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005018 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005019 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5020 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005021 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 if (http_resync_states(s)) {
5023 /* some state changes occurred, maybe the analyser
5024 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005025 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005026 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005027 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005028 /* request errors are most likely due to
5029 * the server aborting the transfer.
5030 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005031 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005032 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005033 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005034 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005035 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005036 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005038 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005039
5040 /* If "option abortonclose" is set on the backend, we
5041 * want to monitor the client's connection and forward
5042 * any shutdown notification to the server, which will
5043 * decide whether to close or to go on processing the
5044 * request.
5045 */
5046 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005047 channel_auto_read(req);
5048 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005049 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005050 else if (s->txn.meth == HTTP_METH_POST) {
5051 /* POST requests may require to read extra CRLF
5052 * sent by broken browsers and which could cause
5053 * an RST to be sent upon close on some systems
5054 * (eg: Linux).
5055 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005056 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005057 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005058
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005060 }
5061 }
5062
Willy Tarreaud98cf932009-12-27 22:54:55 +01005063 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005064 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005065 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005066 if (!(s->flags & SN_ERR_MASK))
5067 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005068 if (!(s->flags & SN_FINST_MASK)) {
5069 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5070 s->flags |= SN_FINST_H;
5071 else
5072 s->flags |= SN_FINST_D;
5073 }
5074
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005075 s->fe->fe_counters.cli_aborts++;
5076 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005077 if (objt_server(s->target))
5078 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005079
5080 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005081 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005082
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005083 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005084 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005085 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005086
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005087 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005088 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005089 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005090 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005091 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005092
Willy Tarreau5c620922011-05-11 19:56:11 +02005093 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005094 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005095 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005096 * modes are already handled by the stream sock layer. We must not do
5097 * this in content-length mode because it could present the MSG_MORE
5098 * flag with the last block of forwarded data, which would cause an
5099 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005100 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005101 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005102 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005103
Willy Tarreau610ecce2010-01-04 21:15:02 +01005104 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005105 return 0;
5106
Willy Tarreaud98cf932009-12-27 22:54:55 +01005107 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005108 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005109 if (s->listener->counters)
5110 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005111 return_bad_req_stats_ok:
5112 txn->req.msg_state = HTTP_MSG_ERROR;
5113 if (txn->status) {
5114 /* Note: we don't send any error if some data were already sent */
5115 stream_int_retnclose(req->prod, NULL);
5116 } else {
5117 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005118 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005119 }
5120 req->analysers = 0;
5121 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005122
5123 if (!(s->flags & SN_ERR_MASK))
5124 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005125 if (!(s->flags & SN_FINST_MASK)) {
5126 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5127 s->flags |= SN_FINST_H;
5128 else
5129 s->flags |= SN_FINST_D;
5130 }
5131 return 0;
5132
5133 aborted_xfer:
5134 txn->req.msg_state = HTTP_MSG_ERROR;
5135 if (txn->status) {
5136 /* Note: we don't send any error if some data were already sent */
5137 stream_int_retnclose(req->prod, NULL);
5138 } else {
5139 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005140 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005141 }
5142 req->analysers = 0;
5143 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5144
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005145 s->fe->fe_counters.srv_aborts++;
5146 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005147 if (objt_server(s->target))
5148 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005149
5150 if (!(s->flags & SN_ERR_MASK))
5151 s->flags |= SN_ERR_SRVCL;
5152 if (!(s->flags & SN_FINST_MASK)) {
5153 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5154 s->flags |= SN_FINST_H;
5155 else
5156 s->flags |= SN_FINST_D;
5157 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005158 return 0;
5159}
5160
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005161/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5162 * processing can continue on next analysers, or zero if it either needs more
5163 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5164 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5165 * when it has nothing left to do, and may remove any analyser when it wants to
5166 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005167 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005168int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005169{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005170 struct http_txn *txn = &s->txn;
5171 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005172 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005173 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005174 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005175 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005176
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005177 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 +02005178 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005179 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005180 rep,
5181 rep->rex, rep->wex,
5182 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005183 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005184 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005185
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005186 /*
5187 * Now parse the partial (or complete) lines.
5188 * We will check the response syntax, and also join multi-line
5189 * headers. An index of all the lines will be elaborated while
5190 * parsing.
5191 *
5192 * For the parsing, we use a 28 states FSM.
5193 *
5194 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005195 * rep->buf->p = beginning of response
5196 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5197 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005198 * msg->eol = end of current header or line (LF or CRLF)
5199 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005200 */
5201
Willy Tarreau83e3af02009-12-28 17:39:57 +01005202 /* There's a protected area at the end of the buffer for rewriting
5203 * purposes. We don't want to start to parse the request if the
5204 * protected area is affected, because we may have to move processed
5205 * data later, which is much more complicated.
5206 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005207 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005208 if (unlikely(!channel_reserved(rep))) {
5209 /* some data has still not left the buffer, wake us once that's done */
5210 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5211 goto abort_response;
5212 channel_dont_close(rep);
5213 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005214 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005215 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005216 }
5217
Willy Tarreau379357a2013-06-08 12:55:46 +02005218 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5219 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5220 buffer_slow_realign(rep->buf);
5221
Willy Tarreau9b28e032012-10-12 23:49:43 +02005222 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005223 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005224 }
5225
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005226 /* 1: we might have to print this header in debug mode */
5227 if (unlikely((global.mode & MODE_DEBUG) &&
5228 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005229 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005230 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005231
Willy Tarreau9b28e032012-10-12 23:49:43 +02005232 sol = rep->buf->p;
5233 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005234 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005235
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005236 sol += hdr_idx_first_pos(&txn->hdr_idx);
5237 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005238
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005239 while (cur_idx) {
5240 eol = sol + txn->hdr_idx.v[cur_idx].len;
5241 debug_hdr("srvhdr", s, sol, eol);
5242 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5243 cur_idx = txn->hdr_idx.v[cur_idx].next;
5244 }
5245 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005246
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 /*
5248 * Now we quickly check if we have found a full valid response.
5249 * If not so, we check the FD and buffer states before leaving.
5250 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005251 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005252 * responses are checked first.
5253 *
5254 * Depending on whether the client is still there or not, we
5255 * may send an error response back or not. Note that normally
5256 * we should only check for HTTP status there, and check I/O
5257 * errors somewhere else.
5258 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005259
Willy Tarreau655dce92009-11-08 13:10:58 +01005260 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005261 /* Invalid response */
5262 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5263 /* we detected a parsing error. We want to archive this response
5264 * in the dedicated proxy area for later troubleshooting.
5265 */
5266 hdr_response_bad:
5267 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005268 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005269
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005270 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005271 if (objt_server(s->target)) {
5272 objt_server(s->target)->counters.failed_resp++;
5273 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005274 }
Willy Tarreau64648412010-03-05 10:41:54 +01005275 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005276 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005277 rep->analysers = 0;
5278 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005279 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005280 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005281 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005282
5283 if (!(s->flags & SN_ERR_MASK))
5284 s->flags |= SN_ERR_PRXCOND;
5285 if (!(s->flags & SN_FINST_MASK))
5286 s->flags |= SN_FINST_H;
5287
5288 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005289 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005290
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005291 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005292 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005293 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005294 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005295 goto hdr_response_bad;
5296 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005297
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005298 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005299 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005300 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005301 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005302 else if (txn->flags & TX_NOT_FIRST)
5303 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005304
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005305 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005306 if (objt_server(s->target)) {
5307 objt_server(s->target)->counters.failed_resp++;
5308 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005309 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005310
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005311 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005312 rep->analysers = 0;
5313 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005314 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005315 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005316 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005317
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005318 if (!(s->flags & SN_ERR_MASK))
5319 s->flags |= SN_ERR_SRVCL;
5320 if (!(s->flags & SN_FINST_MASK))
5321 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005322 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005323 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005324
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005325 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005326 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005327 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005328 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005329 else if (txn->flags & TX_NOT_FIRST)
5330 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005331
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005332 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005333 if (objt_server(s->target)) {
5334 objt_server(s->target)->counters.failed_resp++;
5335 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005336 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005337
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005338 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005339 rep->analysers = 0;
5340 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005341 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005342 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005343 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005344
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005345 if (!(s->flags & SN_ERR_MASK))
5346 s->flags |= SN_ERR_SRVTO;
5347 if (!(s->flags & SN_FINST_MASK))
5348 s->flags |= SN_FINST_H;
5349 return 0;
5350 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005351
Willy Tarreauf003d372012-11-26 13:35:37 +01005352 /* client abort with an abortonclose */
5353 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5354 s->fe->fe_counters.cli_aborts++;
5355 s->be->be_counters.cli_aborts++;
5356 if (objt_server(s->target))
5357 objt_server(s->target)->counters.cli_aborts++;
5358
5359 rep->analysers = 0;
5360 channel_auto_close(rep);
5361
5362 txn->status = 400;
5363 bi_erase(rep);
5364 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5365
5366 if (!(s->flags & SN_ERR_MASK))
5367 s->flags |= SN_ERR_CLICL;
5368 if (!(s->flags & SN_FINST_MASK))
5369 s->flags |= SN_FINST_H;
5370
5371 /* process_session() will take care of the error */
5372 return 0;
5373 }
5374
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005375 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005376 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005377 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005378 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005379 else if (txn->flags & TX_NOT_FIRST)
5380 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005381
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005382 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005383 if (objt_server(s->target)) {
5384 objt_server(s->target)->counters.failed_resp++;
5385 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005386 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005387
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005388 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389 rep->analysers = 0;
5390 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005391 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005392 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005393 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005394
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005395 if (!(s->flags & SN_ERR_MASK))
5396 s->flags |= SN_ERR_SRVCL;
5397 if (!(s->flags & SN_FINST_MASK))
5398 s->flags |= SN_FINST_H;
5399 return 0;
5400 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005401
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005402 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005403 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005404 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005405 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005406 else if (txn->flags & TX_NOT_FIRST)
5407 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005408
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005409 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005410 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005411 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005412
5413 if (!(s->flags & SN_ERR_MASK))
5414 s->flags |= SN_ERR_CLICL;
5415 if (!(s->flags & SN_FINST_MASK))
5416 s->flags |= SN_FINST_H;
5417
5418 /* process_session() will take care of the error */
5419 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005420 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005421
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005422 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005423 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005424 return 0;
5425 }
5426
5427 /* More interesting part now : we know that we have a complete
5428 * response which at least looks like HTTP. We have an indicator
5429 * of each header's length, so we can parse them quickly.
5430 */
5431
5432 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005433 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005434
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005435 /*
5436 * 1: get the status code
5437 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005438 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005439 if (n < 1 || n > 5)
5440 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005441 /* when the client triggers a 4xx from the server, it's most often due
5442 * to a missing object or permission. These events should be tracked
5443 * because if they happen often, it may indicate a brute force or a
5444 * vulnerability scan.
5445 */
5446 if (n == 4)
5447 session_inc_http_err_ctr(s);
5448
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005449 if (objt_server(s->target))
5450 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005451
Willy Tarreau5b154472009-12-21 20:11:07 +01005452 /* check if the response is HTTP/1.1 or above */
5453 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005454 ((rep->buf->p[5] > '1') ||
5455 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005456 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005457
5458 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005459 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 +01005460
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005461 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005462 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005463
Willy Tarreau9b28e032012-10-12 23:49:43 +02005464 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005465
Willy Tarreau39650402010-03-15 19:44:39 +01005466 /* Adjust server's health based on status code. Note: status codes 501
5467 * and 505 are triggered on demand by client request, so we must not
5468 * count them as server failures.
5469 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005470 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005471 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005472 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005473 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005474 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005475 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005476
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005477 /*
5478 * 2: check for cacheability.
5479 */
5480
5481 switch (txn->status) {
5482 case 200:
5483 case 203:
5484 case 206:
5485 case 300:
5486 case 301:
5487 case 410:
5488 /* RFC2616 @13.4:
5489 * "A response received with a status code of
5490 * 200, 203, 206, 300, 301 or 410 MAY be stored
5491 * by a cache (...) unless a cache-control
5492 * directive prohibits caching."
5493 *
5494 * RFC2616 @9.5: POST method :
5495 * "Responses to this method are not cacheable,
5496 * unless the response includes appropriate
5497 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005498 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005499 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005500 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005501 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5502 break;
5503 default:
5504 break;
5505 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005506
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005507 /*
5508 * 3: we may need to capture headers
5509 */
5510 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005511 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005512 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005513 txn->rsp.cap, s->fe->rsp_cap);
5514
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005515 /* 4: determine the transfer-length.
5516 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5517 * the presence of a message-body in a RESPONSE and its transfer length
5518 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005519 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005520 * All responses to the HEAD request method MUST NOT include a
5521 * message-body, even though the presence of entity-header fields
5522 * might lead one to believe they do. All 1xx (informational), 204
5523 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5524 * message-body. All other responses do include a message-body,
5525 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005526 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005527 * 1. Any response which "MUST NOT" include a message-body (such as the
5528 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5529 * always terminated by the first empty line after the header fields,
5530 * regardless of the entity-header fields present in the message.
5531 *
5532 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5533 * the "chunked" transfer-coding (Section 6.2) is used, the
5534 * transfer-length is defined by the use of this transfer-coding.
5535 * If a Transfer-Encoding header field is present and the "chunked"
5536 * transfer-coding is not present, the transfer-length is defined by
5537 * the sender closing the connection.
5538 *
5539 * 3. If a Content-Length header field is present, its decimal value in
5540 * OCTETs represents both the entity-length and the transfer-length.
5541 * If a message is received with both a Transfer-Encoding header
5542 * field and a Content-Length header field, the latter MUST be ignored.
5543 *
5544 * 4. If the message uses the media type "multipart/byteranges", and
5545 * the transfer-length is not otherwise specified, then this self-
5546 * delimiting media type defines the transfer-length. This media
5547 * type MUST NOT be used unless the sender knows that the recipient
5548 * can parse it; the presence in a request of a Range header with
5549 * multiple byte-range specifiers from a 1.1 client implies that the
5550 * client can parse multipart/byteranges responses.
5551 *
5552 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005553 */
5554
5555 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005556 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005557 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005558 * FIXME: should we parse anyway and return an error on chunked encoding ?
5559 */
5560 if (txn->meth == HTTP_METH_HEAD ||
5561 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005562 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005563 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005564 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005565 goto skip_content_length;
5566 }
5567
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005568 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005569 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005570 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005571 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005572 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005573 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5574 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005575 /* bad transfer-encoding (chunked followed by something else) */
5576 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005577 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005578 break;
5579 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005580 }
5581
5582 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5583 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005584 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005585 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005586 signed long long cl;
5587
Willy Tarreauad14f752011-09-02 20:33:27 +02005588 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005589 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005590 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005591 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005592
Willy Tarreauad14f752011-09-02 20:33:27 +02005593 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005594 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005595 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005596 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005597
Willy Tarreauad14f752011-09-02 20:33:27 +02005598 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005599 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005600 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005601 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005602
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005603 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005604 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005605 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005606 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005607
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005608 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005609 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005610 }
5611
William Lallemand82fe75c2012-10-23 10:25:10 +02005612 if (s->fe->comp || s->be->comp)
5613 select_compression_response_header(s, rep->buf);
5614
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005615 /* FIXME: we should also implement the multipart/byterange method.
5616 * For now on, we resort to close mode in this case (unknown length).
5617 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005618skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005619
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005620 /* end of job, return OK */
5621 rep->analysers &= ~an_bit;
5622 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005623 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005624 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005625
5626 abort_keep_alive:
5627 /* A keep-alive request to the server failed on a network error.
5628 * The client is required to retry. We need to close without returning
5629 * any other information so that the client retries.
5630 */
5631 txn->status = 0;
5632 rep->analysers = 0;
5633 s->req->analysers = 0;
5634 channel_auto_close(rep);
5635 s->logs.logwait = 0;
5636 s->logs.level = 0;
5637 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5638 bi_erase(rep);
5639 stream_int_retnclose(rep->cons, NULL);
5640 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005641}
5642
5643/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005644 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5645 * and updates t->rep->analysers. It might make sense to explode it into several
5646 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005647 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005648int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005649{
5650 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005651 struct http_msg *msg = &txn->rsp;
5652 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005653 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005654 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005655
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005656 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 +02005657 now_ms, __FUNCTION__,
5658 t,
5659 rep,
5660 rep->rex, rep->wex,
5661 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005662 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005663 rep->analysers);
5664
Willy Tarreau655dce92009-11-08 13:10:58 +01005665 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666 return 0;
5667
5668 rep->analysers &= ~an_bit;
5669 rep->analyse_exp = TICK_ETERNITY;
5670
Willy Tarreau5b154472009-12-21 20:11:07 +01005671 /* Now we have to check if we need to modify the Connection header.
5672 * This is more difficult on the response than it is on the request,
5673 * because we can have two different HTTP versions and we don't know
5674 * how the client will interprete a response. For instance, let's say
5675 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5676 * HTTP/1.1 response without any header. Maybe it will bound itself to
5677 * HTTP/1.0 because it only knows about it, and will consider the lack
5678 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5679 * the lack of header as a keep-alive. Thus we will use two flags
5680 * indicating how a request MAY be understood by the client. In case
5681 * of multiple possibilities, we'll fix the header to be explicit. If
5682 * ambiguous cases such as both close and keepalive are seen, then we
5683 * will fall back to explicit close. Note that we won't take risks with
5684 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005685 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005686 */
5687
Willy Tarreaudc008c52010-02-01 16:20:08 +01005688 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5689 txn->status == 101)) {
5690 /* Either we've established an explicit tunnel, or we're
5691 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005692 * to understand the next protocols. We have to switch to tunnel
5693 * mode, so that we transfer the request and responses then let
5694 * this protocol pass unmodified. When we later implement specific
5695 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005696 * header which contains information about that protocol for
5697 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005698 */
5699 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5700 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005701 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5702 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005703 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5704 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005705 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005706
Willy Tarreau70dffda2014-01-30 03:07:23 +01005707 /* this situation happens when combining pretend-keepalive with httpclose. */
5708 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5709 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5710 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5711 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5712
Willy Tarreau60466522010-01-18 19:08:45 +01005713 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005714 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005715 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5716 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005717
Willy Tarreau60466522010-01-18 19:08:45 +01005718 /* now adjust header transformations depending on current state */
5719 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5720 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5721 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005722 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005723 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005724 }
Willy Tarreau60466522010-01-18 19:08:45 +01005725 else { /* SCL / KAL */
5726 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005727 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005728 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005729 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005730
Willy Tarreau60466522010-01-18 19:08:45 +01005731 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005732 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005733
Willy Tarreau60466522010-01-18 19:08:45 +01005734 /* Some keep-alive responses are converted to Server-close if
5735 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005736 */
Willy Tarreau60466522010-01-18 19:08:45 +01005737 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5738 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005739 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005740 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005741 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005742 }
5743
Willy Tarreau7959a552013-09-23 16:44:27 +02005744 /* we want to have the response time before we start processing it */
5745 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5746
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005747 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005748 /*
5749 * 3: we will have to evaluate the filters.
5750 * As opposed to version 1.2, now they will be evaluated in the
5751 * filters order and not in the header order. This means that
5752 * each filter has to be validated among all headers.
5753 *
5754 * Filters are tried with ->be first, then with ->fe if it is
5755 * different from ->be.
5756 */
5757
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005758 cur_proxy = t->be;
5759 while (1) {
5760 struct proxy *rule_set = cur_proxy;
5761
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005762 /* evaluate http-response rules */
5763 if (!http_res_last_rule)
5764 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5765
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005766 /* try headers filters */
5767 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005768 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005769 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005770 if (objt_server(t->target)) {
5771 objt_server(t->target)->counters.failed_resp++;
5772 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005773 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005774 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005775 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005776 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005777 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005778 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005779 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005780 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005781 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005782 if (!(t->flags & SN_ERR_MASK))
5783 t->flags |= SN_ERR_PRXCOND;
5784 if (!(t->flags & SN_FINST_MASK))
5785 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005786 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005787 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005788 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005789
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005790 /* has the response been denied ? */
5791 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005792 if (objt_server(t->target))
5793 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005794
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005795 t->be->be_counters.denied_resp++;
5796 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005797 if (t->listener->counters)
5798 t->listener->counters->denied_resp++;
5799
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005800 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005801 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005802
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005803 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005804 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005805 if (txn->status < 200)
5806 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005807 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005808 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005809 ret = acl_pass(ret);
5810 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5811 ret = !ret;
5812 if (!ret)
5813 continue;
5814 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005815 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005816 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005817 }
5818
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005819 /* check whether we're already working on the frontend */
5820 if (cur_proxy == t->fe)
5821 break;
5822 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005823 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005824
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005825 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005826 * We may be facing a 100-continue response, in which case this
5827 * is not the right response, and we're waiting for the next one.
5828 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005829 * next one.
5830 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005831 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005832 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005833 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005834 msg->msg_state = HTTP_MSG_RPBEFORE;
5835 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005836 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005837 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5838 return 1;
5839 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005840 else if (unlikely(txn->status < 200))
5841 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005842
5843 /* we don't have any 1xx status code now */
5844
5845 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005846 * 4: check for server cookie.
5847 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005848 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5849 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005850 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005851
Willy Tarreaubaaee002006-06-26 02:48:02 +02005852
Willy Tarreaua15645d2007-03-18 16:22:39 +01005853 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005854 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005855 */
Willy Tarreau67402132012-05-31 20:40:20 +02005856 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005857 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005858
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005859 /*
5860 * 6: add server cookie in the response if needed
5861 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005862 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005863 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005864 (!(t->flags & SN_DIRECT) ||
5865 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5866 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5867 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5868 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005869 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005870 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005871 /* the server is known, it's not the one the client requested, or the
5872 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005873 * insert a set-cookie here, except if we want to insert only on POST
5874 * requests and this one isn't. Note that servers which don't have cookies
5875 * (eg: some backup servers) will return a full cookie removal request.
5876 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005877 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005878 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005879 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5880 t->be->cookie_name);
5881 }
5882 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005883 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005884
5885 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5886 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005887 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5888 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5889 trash.len += 5;
5890
Willy Tarreauef4f3912010-10-07 21:00:29 +02005891 if (t->be->cookie_maxlife) {
5892 /* emit first_date, which is either the original one or
5893 * the current date.
5894 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005895 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005896 s30tob64(txn->cookie_first_date ?
5897 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005898 (date.tv_sec+3) >> 2, trash.str + trash.len);
5899 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005900 }
5901 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005902 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005903 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005904
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005905 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005906 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005907
Willy Tarreau4992dd22012-05-31 21:02:17 +02005908 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005909 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005910
5911 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005912 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005913
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005914 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005915 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005916
Willy Tarreauf1348312010-10-07 15:54:11 +02005917 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005918 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005919 /* the server did not change, only the date was updated */
5920 txn->flags |= TX_SCK_UPDATED;
5921 else
5922 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005923
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005924 /* Here, we will tell an eventual cache on the client side that we don't
5925 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5926 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5927 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5928 */
Willy Tarreau67402132012-05-31 20:40:20 +02005929 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005930
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005931 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5932
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005933 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005934 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005936 }
5937 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005938
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005939 /*
5940 * 7: check if result will be cacheable with a cookie.
5941 * We'll block the response if security checks have caught
5942 * nasty things such as a cacheable cookie.
5943 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005944 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5945 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005946 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005947
5948 /* we're in presence of a cacheable response containing
5949 * a set-cookie header. We'll block it as requested by
5950 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005951 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005952 if (objt_server(t->target))
5953 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005954
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005955 t->be->be_counters.denied_resp++;
5956 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005957 if (t->listener->counters)
5958 t->listener->counters->denied_resp++;
5959
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005960 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005961 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005962 send_log(t->be, LOG_ALERT,
5963 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005964 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005965 goto return_srv_prx_502;
5966 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005967
5968 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005969 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005970 * If an "Upgrade" token is found, the header is left untouched in order
5971 * not to have to deal with some client bugs : some of them fail an upgrade
5972 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005973 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005974 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5975 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005976 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5977 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005978 unsigned int want_flags = 0;
5979
5980 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5981 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5982 /* we want a keep-alive response here. Keep-alive header
5983 * required if either side is not 1.1.
5984 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005985 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005986 want_flags |= TX_CON_KAL_SET;
5987 }
5988 else {
5989 /* we want a close response here. Close header required if
5990 * the server is 1.1, regardless of the client.
5991 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005992 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005993 want_flags |= TX_CON_CLO_SET;
5994 }
5995
5996 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005997 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005998 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005999
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006000 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006001 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006002 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006003 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006004
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006005 /*************************************************************
6006 * OK, that's finished for the headers. We have done what we *
6007 * could. Let's switch to the DATA state. *
6008 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006009
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006010 /* if the user wants to log as soon as possible, without counting
6011 * bytes from the server, then this is the right moment. We have
6012 * to temporarily assign bytes_out to log what we currently have.
6013 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006014 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006015 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6016 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006017 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006018 t->logs.bytes_out = 0;
6019 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006020
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006021 /* Note: we must not try to cheat by jumping directly to DATA,
6022 * otherwise we would not let the client side wake up.
6023 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006024
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006025 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006026 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006027 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006028}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006029
Willy Tarreaud98cf932009-12-27 22:54:55 +01006030/* This function is an analyser which forwards response body (including chunk
6031 * sizes if any). It is called as soon as we must forward, even if we forward
6032 * zero byte. The only situation where it must not be called is when we're in
6033 * tunnel mode and we want to forward till the close. It's used both to forward
6034 * remaining data and to resync after end of body. It expects the msg_state to
6035 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6036 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006037 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006038 * bytes of pending data + the headers if not already done (between sol and sov).
6039 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006040 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006041int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006042{
6043 struct http_txn *txn = &s->txn;
6044 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02006045 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02006046 static struct buffer *tmpbuf = NULL;
6047 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006048 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006049 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006050
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006051 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6052 return 0;
6053
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006054 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006055 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006056 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006057 /* Output closed while we were sending data. We must abort and
6058 * wake the other side up.
6059 */
6060 msg->msg_state = HTTP_MSG_ERROR;
6061 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006062 return 1;
6063 }
6064
Willy Tarreau4fe41902010-06-07 22:27:41 +02006065 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006066 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006067
William Lallemand82fe75c2012-10-23 10:25:10 +02006068 /* this is the first time we need the compression buffer */
6069 if (s->comp_algo != NULL && tmpbuf == NULL) {
6070 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6071 goto aborted_xfer; /* no memory */
6072 }
6073
Willy Tarreaud98cf932009-12-27 22:54:55 +01006074 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006075 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006076 * rep->buf.p still points to the beginning of the message and msg->sol
6077 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006078 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006079 channel_forward(res, msg->sov);
6080 msg->next = 0;
6081 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006082
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006083 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006084 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006085 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006086 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006087 }
6088
William Lallemand82fe75c2012-10-23 10:25:10 +02006089 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006090 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006091 if (ret < 0) {
6092 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006093 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006094 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006095 compressing = 1;
6096 }
6097
Willy Tarreaud98cf932009-12-27 22:54:55 +01006098 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006099 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006100 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006101 if (s->comp_algo == NULL) {
6102 bytes = msg->sov - msg->sol;
6103 if (msg->chunk_len || bytes) {
6104 msg->sol = msg->sov;
6105 msg->next -= bytes; /* will be forwarded */
6106 msg->chunk_len += bytes;
6107 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6108 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006109 }
6110
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006111 switch (msg->msg_state - HTTP_MSG_DATA) {
6112 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006113 if (compressing) {
6114 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6115 if (ret < 0)
6116 goto aborted_xfer;
6117 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006118
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006119 if (res->to_forward || msg->chunk_len) {
6120 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006121 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006122 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006123
6124 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006125 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006126 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006127 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006128 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006129 if (compressing && consumed_data) {
6130 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6131 compressing = 0;
6132 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006133 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006134 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006135 /* fall through for HTTP_MSG_CHUNK_CRLF */
6136
6137 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6138 /* we want the CRLF after the data */
6139
6140 ret = http_skip_chunk_crlf(msg);
6141 if (ret == 0)
6142 goto missing_data;
6143 else if (ret < 0) {
6144 if (msg->err_pos >= 0)
6145 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6146 goto return_bad_res;
6147 }
6148 /* skipping data in buffer for compression */
6149 if (compressing) {
6150 b_adv(res->buf, msg->next);
6151 msg->next = 0;
6152 msg->sov = 0;
6153 msg->sol = 0;
6154 }
6155 /* we're in MSG_CHUNK_SIZE now, fall through */
6156
6157 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006158 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006159 * set ->sov and ->next to point to the body and switch to DATA or
6160 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006161 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006162
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006163 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006164 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006165 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006166 else if (ret < 0) {
6167 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006168 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006169 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006170 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006171 if (compressing) {
6172 if (likely(msg->chunk_len > 0)) {
6173 /* skipping data if we are in compression mode */
6174 b_adv(res->buf, msg->next);
6175 msg->next = 0;
6176 msg->sov = 0;
6177 msg->sol = 0;
6178 } else {
6179 if (consumed_data) {
6180 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6181 compressing = 0;
6182 }
6183 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006184 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006185 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006186 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006187
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006188 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6189 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006190 if (ret == 0)
6191 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006192 else if (ret < 0) {
6193 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006194 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006195 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006196 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006197 if (s->comp_algo != NULL) {
6198 /* forwarding trailers */
6199 channel_forward(res, msg->next);
6200 msg->next = 0;
6201 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006202 /* we're in HTTP_MSG_DONE now, but we might still have
6203 * some data pending, so let's loop over once.
6204 */
6205 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006206
6207 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006208 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006209
6210 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006211 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006212 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6213 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006214 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006215 if (http_resync_states(s)) {
6216 http_silent_debug(__LINE__, s);
6217 /* some state changes occurred, maybe the analyser
6218 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006219 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006220 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006221 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006222 /* response errors are most likely due to
6223 * the client aborting the transfer.
6224 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006225 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006226 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006227 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006228 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006229 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006230 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006231 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006232 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006233 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006234 }
6235 }
6236
Willy Tarreaud98cf932009-12-27 22:54:55 +01006237 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006238 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006239 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6240 compressing = 0;
6241 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006242
6243 if (res->flags & CF_SHUTW)
6244 goto aborted_xfer;
6245
6246 /* stop waiting for data if the input is closed before the end. If the
6247 * client side was already closed, it means that the client has aborted,
6248 * so we don't want to count this as a server abort. Otherwise it's a
6249 * server abort.
6250 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006251 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006252 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6253 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006254 if (!(s->flags & SN_ERR_MASK))
6255 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006256 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006257 if (objt_server(s->target))
6258 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006259 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006260 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006261
Willy Tarreau40dba092010-03-04 18:14:51 +01006262 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006263 if (!s->req->analysers)
6264 goto return_bad_res;
6265
Willy Tarreauea953162012-05-18 23:41:28 +02006266 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006267 if (s->comp_algo == NULL) {
6268 bytes = msg->sov - msg->sol;
6269 if (msg->chunk_len || bytes) {
6270 msg->sol = msg->sov;
6271 msg->next -= bytes; /* will be forwarded */
6272 msg->chunk_len += bytes;
6273 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6274 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006275 }
6276
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006277 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006278 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006279 * Similarly, with keep-alive on the client side, we don't want to forward a
6280 * close.
6281 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006282 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006283 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6284 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006285 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006286
Willy Tarreau5c620922011-05-11 19:56:11 +02006287 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006288 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006289 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006290 * modes are already handled by the stream sock layer. We must not do
6291 * this in content-length mode because it could present the MSG_MORE
6292 * flag with the last block of forwarded data, which would cause an
6293 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006294 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006295 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006296 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006297
Willy Tarreaud98cf932009-12-27 22:54:55 +01006298 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006299 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006300 return 0;
6301
Willy Tarreau40dba092010-03-04 18:14:51 +01006302 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006303 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006304 if (objt_server(s->target))
6305 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006306
6307 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006308 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006309 /* don't send any error message as we're in the body */
6310 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006311 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006312 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006313 if (objt_server(s->target))
6314 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006315
6316 if (!(s->flags & SN_ERR_MASK))
6317 s->flags |= SN_ERR_PRXCOND;
6318 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006319 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006320 return 0;
6321
6322 aborted_xfer:
6323 txn->rsp.msg_state = HTTP_MSG_ERROR;
6324 /* don't send any error message as we're in the body */
6325 stream_int_retnclose(res->cons, NULL);
6326 res->analysers = 0;
6327 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6328
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006329 s->fe->fe_counters.cli_aborts++;
6330 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006331 if (objt_server(s->target))
6332 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006333
6334 if (!(s->flags & SN_ERR_MASK))
6335 s->flags |= SN_ERR_CLICL;
6336 if (!(s->flags & SN_FINST_MASK))
6337 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006338 return 0;
6339}
6340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006341/* Iterate the same filter through all request headers.
6342 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006343 * Since it can manage the switch to another backend, it updates the per-proxy
6344 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006346int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006347{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 char term;
6349 char *cur_ptr, *cur_end, *cur_next;
6350 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006351 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006353 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006355 last_hdr = 0;
6356
Willy Tarreau9b28e032012-10-12 23:49:43 +02006357 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006358 old_idx = 0;
6359
6360 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006361 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006362 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006363 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006364 (exp->action == ACT_ALLOW ||
6365 exp->action == ACT_DENY ||
6366 exp->action == ACT_TARPIT))
6367 return 0;
6368
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006369 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 if (!cur_idx)
6371 break;
6372
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006373 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006374 cur_ptr = cur_next;
6375 cur_end = cur_ptr + cur_hdr->len;
6376 cur_next = cur_end + cur_hdr->cr + 1;
6377
6378 /* Now we have one header between cur_ptr and cur_end,
6379 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006380 */
6381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382 /* The annoying part is that pattern matching needs
6383 * that we modify the contents to null-terminate all
6384 * strings before testing them.
6385 */
6386
6387 term = *cur_end;
6388 *cur_end = '\0';
6389
6390 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6391 switch (exp->action) {
6392 case ACT_SETBE:
6393 /* It is not possible to jump a second time.
6394 * FIXME: should we return an HTTP/500 here so that
6395 * the admin knows there's a problem ?
6396 */
6397 if (t->be != t->fe)
6398 break;
6399
6400 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006401 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 last_hdr = 1;
6403 break;
6404
6405 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006406 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006407 last_hdr = 1;
6408 break;
6409
6410 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006411 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006412 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006413 break;
6414
6415 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006416 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006417 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006418 break;
6419
6420 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006421 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6422 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006423 /* FIXME: if the user adds a newline in the replacement, the
6424 * index will not be recalculated for now, and the new line
6425 * will not be counted as a new header.
6426 */
6427
6428 cur_end += delta;
6429 cur_next += delta;
6430 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006431 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 break;
6433
6434 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006435 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006436 cur_next += delta;
6437
Willy Tarreaufa355d42009-11-29 18:12:29 +01006438 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006439 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6440 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 cur_hdr->len = 0;
6442 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006443 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006444 break;
6445
6446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006448 if (cur_end)
6449 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 /* keep the link from this header to next one in case of later
6452 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006453 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 old_idx = cur_idx;
6455 }
6456 return 0;
6457}
6458
6459
6460/* Apply the filter to the request line.
6461 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6462 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006463 * Since it can manage the switch to another backend, it updates the per-proxy
6464 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006466int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467{
6468 char term;
6469 char *cur_ptr, *cur_end;
6470 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006471 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006472 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473
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 else if (exp->action == ACT_REMOVE)
6482 return 0;
6483
6484 done = 0;
6485
Willy Tarreau9b28e032012-10-12 23:49:43 +02006486 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006487 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488
6489 /* Now we have the request line between cur_ptr and cur_end */
6490
6491 /* The annoying part is that pattern matching needs
6492 * that we modify the contents to null-terminate all
6493 * strings before testing them.
6494 */
6495
6496 term = *cur_end;
6497 *cur_end = '\0';
6498
6499 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6500 switch (exp->action) {
6501 case ACT_SETBE:
6502 /* It is not possible to jump a second time.
6503 * FIXME: should we return an HTTP/500 here so that
6504 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006505 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 if (t->be != t->fe)
6507 break;
6508
6509 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006510 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 done = 1;
6512 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006515 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006516 done = 1;
6517 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006520 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006521 done = 1;
6522 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006525 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 done = 1;
6527 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 case ACT_REPLACE:
6530 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006531 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6532 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 /* FIXME: if the user adds a newline in the replacement, the
6534 * index will not be recalculated for now, and the new line
6535 * will not be counted as a new header.
6536 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006537
Willy Tarreaufa355d42009-11-29 18:12:29 +01006538 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006540 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 HTTP_MSG_RQMETH,
6542 cur_ptr, cur_end + 1,
6543 NULL, NULL);
6544 if (unlikely(!cur_end))
6545 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006547 /* we have a full request and we know that we have either a CR
6548 * or an LF at <ptr>.
6549 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006550 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6551 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552 /* there is no point trying this regex on headers */
6553 return 1;
6554 }
6555 }
6556 *cur_end = term; /* restore the string terminator */
6557 return done;
6558}
Willy Tarreau97de6242006-12-27 17:18:38 +01006559
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560
Willy Tarreau58f10d72006-12-04 02:26:12 +01006561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006563 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006565 * unparsable request. Since it can manage the switch to another backend, it
6566 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006568int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006570 struct http_txn *txn = &s->txn;
6571 struct hdr_exp *exp;
6572
6573 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006574 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006576 /*
6577 * The interleaving of transformations and verdicts
6578 * makes it difficult to decide to continue or stop
6579 * the evaluation.
6580 */
6581
Willy Tarreau6c123b12010-01-28 20:22:06 +01006582 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6583 break;
6584
Willy Tarreau3d300592007-03-18 18:34:41 +01006585 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006586 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006587 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006588 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006589
6590 /* if this filter had a condition, evaluate it now and skip to
6591 * next filter if the condition does not match.
6592 */
6593 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006594 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006595 ret = acl_pass(ret);
6596 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6597 ret = !ret;
6598
6599 if (!ret)
6600 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006601 }
6602
6603 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006604 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006605 if (unlikely(ret < 0))
6606 return -1;
6607
6608 if (likely(ret == 0)) {
6609 /* The filter did not match the request, it can be
6610 * iterated through all headers.
6611 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006612 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006614 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006615 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616}
6617
6618
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619
Willy Tarreau58f10d72006-12-04 02:26:12 +01006620/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006621 * Try to retrieve the server associated to the appsession.
6622 * If the server is found, it's assigned to the session.
6623 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006624void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006625 struct http_txn *txn = &t->txn;
6626 appsess *asession = NULL;
6627 char *sessid_temp = NULL;
6628
Cyril Bontéb21570a2009-11-29 20:04:48 +01006629 if (len > t->be->appsession_len) {
6630 len = t->be->appsession_len;
6631 }
6632
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006633 if (t->be->options2 & PR_O2_AS_REQL) {
6634 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006635 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006636 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006637 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006638 }
6639
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006640 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006641 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6642 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6643 return;
6644 }
6645
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006646 memcpy(txn->sessid, buf, len);
6647 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006648 }
6649
6650 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6651 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6652 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6653 return;
6654 }
6655
Cyril Bontéb21570a2009-11-29 20:04:48 +01006656 memcpy(sessid_temp, buf, len);
6657 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006658
6659 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6660 /* free previously allocated memory */
6661 pool_free2(apools.sessid, sessid_temp);
6662
6663 if (asession != NULL) {
6664 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6665 if (!(t->be->options2 & PR_O2_AS_REQL))
6666 asession->request_count++;
6667
6668 if (asession->serverid != NULL) {
6669 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006670
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006671 while (srv) {
6672 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006673 if ((srv->state & SRV_RUNNING) ||
6674 (t->be->options & PR_O_PERSIST) ||
6675 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006676 /* we found the server and it's usable */
6677 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006678 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006679 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006680 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006681
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006682 break;
6683 } else {
6684 txn->flags &= ~TX_CK_MASK;
6685 txn->flags |= TX_CK_DOWN;
6686 }
6687 }
6688 srv = srv->next;
6689 }
6690 }
6691 }
6692}
6693
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006694/* Find the end of a cookie value contained between <s> and <e>. It works the
6695 * same way as with headers above except that the semi-colon also ends a token.
6696 * See RFC2965 for more information. Note that it requires a valid header to
6697 * return a valid result.
6698 */
6699char *find_cookie_value_end(char *s, const char *e)
6700{
6701 int quoted, qdpair;
6702
6703 quoted = qdpair = 0;
6704 for (; s < e; s++) {
6705 if (qdpair) qdpair = 0;
6706 else if (quoted) {
6707 if (*s == '\\') qdpair = 1;
6708 else if (*s == '"') quoted = 0;
6709 }
6710 else if (*s == '"') quoted = 1;
6711 else if (*s == ',' || *s == ';') return s;
6712 }
6713 return s;
6714}
6715
6716/* Delete a value in a header between delimiters <from> and <next> in buffer
6717 * <buf>. The number of characters displaced is returned, and the pointer to
6718 * the first delimiter is updated if required. The function tries as much as
6719 * possible to respect the following principles :
6720 * - replace <from> delimiter by the <next> one unless <from> points to a
6721 * colon, in which case <next> is simply removed
6722 * - set exactly one space character after the new first delimiter, unless
6723 * there are not enough characters in the block being moved to do so.
6724 * - remove unneeded spaces before the previous delimiter and after the new
6725 * one.
6726 *
6727 * It is the caller's responsibility to ensure that :
6728 * - <from> points to a valid delimiter or the colon ;
6729 * - <next> points to a valid delimiter or the final CR/LF ;
6730 * - there are non-space chars before <from> ;
6731 * - there is a CR/LF at or after <next>.
6732 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006733int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006734{
6735 char *prev = *from;
6736
6737 if (*prev == ':') {
6738 /* We're removing the first value, preserve the colon and add a
6739 * space if possible.
6740 */
6741 if (!http_is_crlf[(unsigned char)*next])
6742 next++;
6743 prev++;
6744 if (prev < next)
6745 *prev++ = ' ';
6746
6747 while (http_is_spht[(unsigned char)*next])
6748 next++;
6749 } else {
6750 /* Remove useless spaces before the old delimiter. */
6751 while (http_is_spht[(unsigned char)*(prev-1)])
6752 prev--;
6753 *from = prev;
6754
6755 /* copy the delimiter and if possible a space if we're
6756 * not at the end of the line.
6757 */
6758 if (!http_is_crlf[(unsigned char)*next]) {
6759 *prev++ = *next++;
6760 if (prev + 1 < next)
6761 *prev++ = ' ';
6762 while (http_is_spht[(unsigned char)*next])
6763 next++;
6764 }
6765 }
6766 return buffer_replace2(buf, prev, next, NULL, 0);
6767}
6768
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006769/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006770 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 * desirable to call it only when needed. This code is quite complex because
6772 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6773 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006774 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006775void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006776{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006777 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006778 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006779 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6781 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006782
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006783 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006784 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006785 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006786
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006787 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006788 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006789 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006790
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006791 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006792 hdr_beg = hdr_next;
6793 hdr_end = hdr_beg + cur_hdr->len;
6794 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006795
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006796 /* We have one full header between hdr_beg and hdr_end, and the
6797 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006798 * "Cookie:" headers.
6799 */
6800
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006801 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006802 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006803 old_idx = cur_idx;
6804 continue;
6805 }
6806
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006807 del_from = NULL; /* nothing to be deleted */
6808 preserve_hdr = 0; /* assume we may kill the whole header */
6809
Willy Tarreau58f10d72006-12-04 02:26:12 +01006810 /* Now look for cookies. Conforming to RFC2109, we have to support
6811 * attributes whose name begin with a '$', and associate them with
6812 * the right cookie, if we want to delete this cookie.
6813 * So there are 3 cases for each cookie read :
6814 * 1) it's a special attribute, beginning with a '$' : ignore it.
6815 * 2) it's a server id cookie that we *MAY* want to delete : save
6816 * some pointers on it (last semi-colon, beginning of cookie...)
6817 * 3) it's an application cookie : we *MAY* have to delete a previous
6818 * "special" cookie.
6819 * At the end of loop, if a "special" cookie remains, we may have to
6820 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006821 * *MUST* delete it.
6822 *
6823 * Note: RFC2965 is unclear about the processing of spaces around
6824 * the equal sign in the ATTR=VALUE form. A careful inspection of
6825 * the RFC explicitly allows spaces before it, and not within the
6826 * tokens (attrs or values). An inspection of RFC2109 allows that
6827 * too but section 10.1.3 lets one think that spaces may be allowed
6828 * after the equal sign too, resulting in some (rare) buggy
6829 * implementations trying to do that. So let's do what servers do.
6830 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6831 * allowed quoted strings in values, with any possible character
6832 * after a backslash, including control chars and delimitors, which
6833 * causes parsing to become ambiguous. Browsers also allow spaces
6834 * within values even without quotes.
6835 *
6836 * We have to keep multiple pointers in order to support cookie
6837 * removal at the beginning, middle or end of header without
6838 * corrupting the header. All of these headers are valid :
6839 *
6840 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6841 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6842 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6843 * | | | | | | | | |
6844 * | | | | | | | | hdr_end <--+
6845 * | | | | | | | +--> next
6846 * | | | | | | +----> val_end
6847 * | | | | | +-----------> val_beg
6848 * | | | | +--------------> equal
6849 * | | | +----------------> att_end
6850 * | | +---------------------> att_beg
6851 * | +--------------------------> prev
6852 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006853 */
6854
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006855 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6856 /* Iterate through all cookies on this line */
6857
6858 /* find att_beg */
6859 att_beg = prev + 1;
6860 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6861 att_beg++;
6862
6863 /* find att_end : this is the first character after the last non
6864 * space before the equal. It may be equal to hdr_end.
6865 */
6866 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006867
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006868 while (equal < hdr_end) {
6869 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006870 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 if (http_is_spht[(unsigned char)*equal++])
6872 continue;
6873 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006874 }
6875
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006876 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6877 * is between <att_beg> and <equal>, both may be identical.
6878 */
6879
6880 /* look for end of cookie if there is an equal sign */
6881 if (equal < hdr_end && *equal == '=') {
6882 /* look for the beginning of the value */
6883 val_beg = equal + 1;
6884 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6885 val_beg++;
6886
6887 /* find the end of the value, respecting quotes */
6888 next = find_cookie_value_end(val_beg, hdr_end);
6889
6890 /* make val_end point to the first white space or delimitor after the value */
6891 val_end = next;
6892 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6893 val_end--;
6894 } else {
6895 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006896 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 /* We have nothing to do with attributes beginning with '$'. However,
6899 * they will automatically be removed if a header before them is removed,
6900 * since they're supposed to be linked together.
6901 */
6902 if (*att_beg == '$')
6903 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006904
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 /* Ignore cookies with no equal sign */
6906 if (equal == next) {
6907 /* This is not our cookie, so we must preserve it. But if we already
6908 * scheduled another cookie for removal, we cannot remove the
6909 * complete header, but we can remove the previous block itself.
6910 */
6911 preserve_hdr = 1;
6912 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006913 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 val_end += delta;
6915 next += delta;
6916 hdr_end += delta;
6917 hdr_next += delta;
6918 cur_hdr->len += delta;
6919 http_msg_move_end(&txn->req, delta);
6920 prev = del_from;
6921 del_from = NULL;
6922 }
6923 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006924 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* if there are spaces around the equal sign, we need to
6927 * strip them otherwise we'll get trouble for cookie captures,
6928 * or even for rewrites. Since this happens extremely rarely,
6929 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006931 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6932 int stripped_before = 0;
6933 int stripped_after = 0;
6934
6935 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006936 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 equal += stripped_before;
6938 val_beg += stripped_before;
6939 }
6940
6941 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006942 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006943 val_beg += stripped_after;
6944 stripped_before += stripped_after;
6945 }
6946
6947 val_end += stripped_before;
6948 next += stripped_before;
6949 hdr_end += stripped_before;
6950 hdr_next += stripped_before;
6951 cur_hdr->len += stripped_before;
6952 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006953 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006954 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006955
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 /* First, let's see if we want to capture this cookie. We check
6957 * that we don't already have a client side cookie, because we
6958 * can only capture one. Also as an optimisation, we ignore
6959 * cookies shorter than the declared name.
6960 */
6961 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6962 (val_end - att_beg >= t->fe->capture_namelen) &&
6963 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6964 int log_len = val_end - att_beg;
6965
6966 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6967 Alert("HTTP logging : out of memory.\n");
6968 } else {
6969 if (log_len > t->fe->capture_len)
6970 log_len = t->fe->capture_len;
6971 memcpy(txn->cli_cookie, att_beg, log_len);
6972 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006974 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975
Willy Tarreaubca99692010-10-06 19:25:55 +02006976 /* Persistence cookies in passive, rewrite or insert mode have the
6977 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006978 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006979 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006981 * For cookies in prefix mode, the form is :
6982 *
6983 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006984 */
6985 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6986 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6987 struct server *srv = t->be->srv;
6988 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006990 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6991 * have the server ID between val_beg and delim, and the original cookie between
6992 * delim+1 and val_end. Otherwise, delim==val_end :
6993 *
6994 * Cookie: NAME=SRV; # in all but prefix modes
6995 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6996 * | || || | |+-> next
6997 * | || || | +--> val_end
6998 * | || || +---------> delim
6999 * | || |+------------> val_beg
7000 * | || +-------------> att_end = equal
7001 * | |+-----------------> att_beg
7002 * | +------------------> prev
7003 * +-------------------------> hdr_beg
7004 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007005
Willy Tarreau67402132012-05-31 20:40:20 +02007006 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007007 for (delim = val_beg; delim < val_end; delim++)
7008 if (*delim == COOKIE_DELIM)
7009 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007010 } else {
7011 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007012 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007013 /* Now check if the cookie contains a date field, which would
7014 * appear after a vertical bar ('|') just after the server name
7015 * and before the delimiter.
7016 */
7017 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7018 if (vbar1) {
7019 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007020 * right is the last seen date. It is a base64 encoded
7021 * 30-bit value representing the UNIX date since the
7022 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007023 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007024 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007025 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007026 if (val_end - vbar1 >= 5) {
7027 val = b64tos30(vbar1);
7028 if (val > 0)
7029 txn->cookie_last_date = val << 2;
7030 }
7031 /* look for a second vertical bar */
7032 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7033 if (vbar1 && (val_end - vbar1 > 5)) {
7034 val = b64tos30(vbar1 + 1);
7035 if (val > 0)
7036 txn->cookie_first_date = val << 2;
7037 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007038 }
7039 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007040
Willy Tarreauf64d1412010-10-07 20:06:11 +02007041 /* if the cookie has an expiration date and the proxy wants to check
7042 * it, then we do that now. We first check if the cookie is too old,
7043 * then only if it has expired. We detect strict overflow because the
7044 * time resolution here is not great (4 seconds). Cookies with dates
7045 * in the future are ignored if their offset is beyond one day. This
7046 * allows an admin to fix timezone issues without expiring everyone
7047 * and at the same time avoids keeping unwanted side effects for too
7048 * long.
7049 */
7050 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007051 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7052 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007053 txn->flags &= ~TX_CK_MASK;
7054 txn->flags |= TX_CK_OLD;
7055 delim = val_beg; // let's pretend we have not found the cookie
7056 txn->cookie_first_date = 0;
7057 txn->cookie_last_date = 0;
7058 }
7059 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007060 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7061 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007062 txn->flags &= ~TX_CK_MASK;
7063 txn->flags |= TX_CK_EXPIRED;
7064 delim = val_beg; // let's pretend we have not found the cookie
7065 txn->cookie_first_date = 0;
7066 txn->cookie_last_date = 0;
7067 }
7068
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007069 /* Here, we'll look for the first running server which supports the cookie.
7070 * This allows to share a same cookie between several servers, for example
7071 * to dedicate backup servers to specific servers only.
7072 * However, to prevent clients from sticking to cookie-less backup server
7073 * when they have incidentely learned an empty cookie, we simply ignore
7074 * empty cookies and mark them as invalid.
7075 * The same behaviour is applied when persistence must be ignored.
7076 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007077 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007078 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007080 while (srv) {
7081 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7082 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7083 if ((srv->state & SRV_RUNNING) ||
7084 (t->be->options & PR_O_PERSIST) ||
7085 (t->flags & SN_FORCE_PRST)) {
7086 /* we found the server and we can use it */
7087 txn->flags &= ~TX_CK_MASK;
7088 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7089 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007090 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007091 break;
7092 } else {
7093 /* we found a server, but it's down,
7094 * mark it as such and go on in case
7095 * another one is available.
7096 */
7097 txn->flags &= ~TX_CK_MASK;
7098 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 srv = srv->next;
7102 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103
Willy Tarreauf64d1412010-10-07 20:06:11 +02007104 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007105 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007106 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007107 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7108 txn->flags |= TX_CK_UNUSED;
7109 else
7110 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007111 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007113 /* depending on the cookie mode, we may have to either :
7114 * - delete the complete cookie if we're in insert+indirect mode, so that
7115 * the server never sees it ;
7116 * - remove the server id from the cookie value, and tag the cookie as an
7117 * application cookie so that it does not get accidentely removed later,
7118 * if we're in cookie prefix mode
7119 */
Willy Tarreau67402132012-05-31 20:40:20 +02007120 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007121 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122
Willy Tarreau9b28e032012-10-12 23:49:43 +02007123 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 val_end += delta;
7125 next += delta;
7126 hdr_end += delta;
7127 hdr_next += delta;
7128 cur_hdr->len += delta;
7129 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007131 del_from = NULL;
7132 preserve_hdr = 1; /* we want to keep this cookie */
7133 }
7134 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007135 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007136 del_from = prev;
7137 }
7138 } else {
7139 /* This is not our cookie, so we must preserve it. But if we already
7140 * scheduled another cookie for removal, we cannot remove the
7141 * complete header, but we can remove the previous block itself.
7142 */
7143 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007144
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007145 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007146 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007147 if (att_beg >= del_from)
7148 att_beg += delta;
7149 if (att_end >= del_from)
7150 att_end += delta;
7151 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007152 val_end += delta;
7153 next += delta;
7154 hdr_end += delta;
7155 hdr_next += delta;
7156 cur_hdr->len += delta;
7157 http_msg_move_end(&txn->req, delta);
7158 prev = del_from;
7159 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007161 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007163 /* Look for the appsession cookie unless persistence must be ignored */
7164 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7165 int cmp_len, value_len;
7166 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007167
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007168 if (t->be->options2 & PR_O2_AS_PFX) {
7169 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7170 value_begin = att_beg + t->be->appsession_name_len;
7171 value_len = val_end - att_beg - t->be->appsession_name_len;
7172 } else {
7173 cmp_len = att_end - att_beg;
7174 value_begin = val_beg;
7175 value_len = val_end - val_beg;
7176 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007177
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007178 /* let's see if the cookie is our appcookie */
7179 if (cmp_len == t->be->appsession_name_len &&
7180 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7181 manage_client_side_appsession(t, value_begin, value_len);
7182 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007183 }
7184
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007185 /* continue with next cookie on this header line */
7186 att_beg = next;
7187 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007188
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007189 /* There are no more cookies on this line.
7190 * We may still have one (or several) marked for deletion at the
7191 * end of the line. We must do this now in two ways :
7192 * - if some cookies must be preserved, we only delete from the
7193 * mark to the end of line ;
7194 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007195 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007196 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007197 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007198 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007199 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007200 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007201 cur_hdr->len += delta;
7202 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007203 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007204
7205 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007206 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7207 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007208 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007209 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007210 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007211 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007212 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007213 }
7214
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007215 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007216 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007217 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007218}
7219
7220
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221/* Iterate the same filter through all response headers contained in <rtr>.
7222 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7223 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007224int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225{
7226 char term;
7227 char *cur_ptr, *cur_end, *cur_next;
7228 int cur_idx, old_idx, last_hdr;
7229 struct http_txn *txn = &t->txn;
7230 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007231 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232
7233 last_hdr = 0;
7234
Willy Tarreau9b28e032012-10-12 23:49:43 +02007235 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 old_idx = 0;
7237
7238 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007239 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007241 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 (exp->action == ACT_ALLOW ||
7243 exp->action == ACT_DENY))
7244 return 0;
7245
7246 cur_idx = txn->hdr_idx.v[old_idx].next;
7247 if (!cur_idx)
7248 break;
7249
7250 cur_hdr = &txn->hdr_idx.v[cur_idx];
7251 cur_ptr = cur_next;
7252 cur_end = cur_ptr + cur_hdr->len;
7253 cur_next = cur_end + cur_hdr->cr + 1;
7254
7255 /* Now we have one header between cur_ptr and cur_end,
7256 * and the next header starts at cur_next.
7257 */
7258
7259 /* The annoying part is that pattern matching needs
7260 * that we modify the contents to null-terminate all
7261 * strings before testing them.
7262 */
7263
7264 term = *cur_end;
7265 *cur_end = '\0';
7266
7267 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7268 switch (exp->action) {
7269 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007270 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 last_hdr = 1;
7272 break;
7273
7274 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007275 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 last_hdr = 1;
7277 break;
7278
7279 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007280 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7281 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007282 /* FIXME: if the user adds a newline in the replacement, the
7283 * index will not be recalculated for now, and the new line
7284 * will not be counted as a new header.
7285 */
7286
7287 cur_end += delta;
7288 cur_next += delta;
7289 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007290 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 break;
7292
7293 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007294 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295 cur_next += delta;
7296
Willy Tarreaufa355d42009-11-29 18:12:29 +01007297 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7299 txn->hdr_idx.used--;
7300 cur_hdr->len = 0;
7301 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007302 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 break;
7304
7305 }
7306 }
7307 if (cur_end)
7308 *cur_end = term; /* restore the string terminator */
7309
7310 /* keep the link from this header to next one in case of later
7311 * removal of next header.
7312 */
7313 old_idx = cur_idx;
7314 }
7315 return 0;
7316}
7317
7318
7319/* Apply the filter to the status line in the response buffer <rtr>.
7320 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7321 * or -1 if a replacement resulted in an invalid status line.
7322 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007323int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324{
7325 char term;
7326 char *cur_ptr, *cur_end;
7327 int done;
7328 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007329 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330
7331
Willy Tarreau3d300592007-03-18 18:34:41 +01007332 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007334 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335 (exp->action == ACT_ALLOW ||
7336 exp->action == ACT_DENY))
7337 return 0;
7338 else if (exp->action == ACT_REMOVE)
7339 return 0;
7340
7341 done = 0;
7342
Willy Tarreau9b28e032012-10-12 23:49:43 +02007343 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007344 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345
7346 /* Now we have the status line between cur_ptr and cur_end */
7347
7348 /* The annoying part is that pattern matching needs
7349 * that we modify the contents to null-terminate all
7350 * strings before testing them.
7351 */
7352
7353 term = *cur_end;
7354 *cur_end = '\0';
7355
7356 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7357 switch (exp->action) {
7358 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007359 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360 done = 1;
7361 break;
7362
7363 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007364 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365 done = 1;
7366 break;
7367
7368 case ACT_REPLACE:
7369 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007370 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7371 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 /* FIXME: if the user adds a newline in the replacement, the
7373 * index will not be recalculated for now, and the new line
7374 * will not be counted as a new header.
7375 */
7376
Willy Tarreaufa355d42009-11-29 18:12:29 +01007377 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007378 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007379 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007380 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007381 cur_ptr, cur_end + 1,
7382 NULL, NULL);
7383 if (unlikely(!cur_end))
7384 return -1;
7385
7386 /* we have a full respnse and we know that we have either a CR
7387 * or an LF at <ptr>.
7388 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007389 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007390 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007391 /* there is no point trying this regex on headers */
7392 return 1;
7393 }
7394 }
7395 *cur_end = term; /* restore the string terminator */
7396 return done;
7397}
7398
7399
7400
7401/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007402 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7404 * unparsable response.
7405 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007406int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007408 struct http_txn *txn = &s->txn;
7409 struct hdr_exp *exp;
7410
7411 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412 int ret;
7413
7414 /*
7415 * The interleaving of transformations and verdicts
7416 * makes it difficult to decide to continue or stop
7417 * the evaluation.
7418 */
7419
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007420 if (txn->flags & TX_SVDENY)
7421 break;
7422
Willy Tarreau3d300592007-03-18 18:34:41 +01007423 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007424 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7425 exp->action == ACT_PASS)) {
7426 exp = exp->next;
7427 continue;
7428 }
7429
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007430 /* if this filter had a condition, evaluate it now and skip to
7431 * next filter if the condition does not match.
7432 */
7433 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007434 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007435 ret = acl_pass(ret);
7436 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7437 ret = !ret;
7438 if (!ret)
7439 continue;
7440 }
7441
Willy Tarreaua15645d2007-03-18 16:22:39 +01007442 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007443 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444 if (unlikely(ret < 0))
7445 return -1;
7446
7447 if (likely(ret == 0)) {
7448 /* The filter did not match the response, it can be
7449 * iterated through all headers.
7450 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007451 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453 }
7454 return 0;
7455}
7456
7457
Willy Tarreaua15645d2007-03-18 16:22:39 +01007458/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007459 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007460 * desirable to call it only when needed. This function is also used when we
7461 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007462 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007463void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007464{
7465 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007466 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007467 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007468 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007469 char *hdr_beg, *hdr_end, *hdr_next;
7470 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007471
Willy Tarreaua15645d2007-03-18 16:22:39 +01007472 /* Iterate through the headers.
7473 * we start with the start line.
7474 */
7475 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007476 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477
7478 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7479 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007480 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007481
7482 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 hdr_beg = hdr_next;
7484 hdr_end = hdr_beg + cur_hdr->len;
7485 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007486
Willy Tarreau24581ba2010-08-31 22:39:35 +02007487 /* We have one full header between hdr_beg and hdr_end, and the
7488 * next header starts at hdr_next. We're only interested in
7489 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490 */
7491
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 is_cookie2 = 0;
7493 prev = hdr_beg + 10;
7494 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007495 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7497 if (!val) {
7498 old_idx = cur_idx;
7499 continue;
7500 }
7501 is_cookie2 = 1;
7502 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007503 }
7504
Willy Tarreau24581ba2010-08-31 22:39:35 +02007505 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7506 * <prev> points to the colon.
7507 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007508 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509
Willy Tarreau24581ba2010-08-31 22:39:35 +02007510 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7511 * check-cache is enabled) and we are not interested in checking
7512 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007513 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007514 if (t->be->cookie_name == NULL &&
7515 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007516 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007517 return;
7518
Willy Tarreau24581ba2010-08-31 22:39:35 +02007519 /* OK so now we know we have to process this response cookie.
7520 * The format of the Set-Cookie header is slightly different
7521 * from the format of the Cookie header in that it does not
7522 * support the comma as a cookie delimiter (thus the header
7523 * cannot be folded) because the Expires attribute described in
7524 * the original Netscape's spec may contain an unquoted date
7525 * with a comma inside. We have to live with this because
7526 * many browsers don't support Max-Age and some browsers don't
7527 * support quoted strings. However the Set-Cookie2 header is
7528 * clean.
7529 *
7530 * We have to keep multiple pointers in order to support cookie
7531 * removal at the beginning, middle or end of header without
7532 * corrupting the header (in case of set-cookie2). A special
7533 * pointer, <scav> points to the beginning of the set-cookie-av
7534 * fields after the first semi-colon. The <next> pointer points
7535 * either to the end of line (set-cookie) or next unquoted comma
7536 * (set-cookie2). All of these headers are valid :
7537 *
7538 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7539 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7540 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7541 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7542 * | | | | | | | | | |
7543 * | | | | | | | | +-> next hdr_end <--+
7544 * | | | | | | | +------------> scav
7545 * | | | | | | +--------------> val_end
7546 * | | | | | +--------------------> val_beg
7547 * | | | | +----------------------> equal
7548 * | | | +------------------------> att_end
7549 * | | +----------------------------> att_beg
7550 * | +------------------------------> prev
7551 * +-----------------------------------------> hdr_beg
7552 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007553
Willy Tarreau24581ba2010-08-31 22:39:35 +02007554 for (; prev < hdr_end; prev = next) {
7555 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 /* find att_beg */
7558 att_beg = prev + 1;
7559 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7560 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007561
Willy Tarreau24581ba2010-08-31 22:39:35 +02007562 /* find att_end : this is the first character after the last non
7563 * space before the equal. It may be equal to hdr_end.
7564 */
7565 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566
Willy Tarreau24581ba2010-08-31 22:39:35 +02007567 while (equal < hdr_end) {
7568 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7569 break;
7570 if (http_is_spht[(unsigned char)*equal++])
7571 continue;
7572 att_end = equal;
7573 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007574
Willy Tarreau24581ba2010-08-31 22:39:35 +02007575 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7576 * is between <att_beg> and <equal>, both may be identical.
7577 */
7578
7579 /* look for end of cookie if there is an equal sign */
7580 if (equal < hdr_end && *equal == '=') {
7581 /* look for the beginning of the value */
7582 val_beg = equal + 1;
7583 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7584 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007585
Willy Tarreau24581ba2010-08-31 22:39:35 +02007586 /* find the end of the value, respecting quotes */
7587 next = find_cookie_value_end(val_beg, hdr_end);
7588
7589 /* make val_end point to the first white space or delimitor after the value */
7590 val_end = next;
7591 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7592 val_end--;
7593 } else {
7594 /* <equal> points to next comma, semi-colon or EOL */
7595 val_beg = val_end = next = equal;
7596 }
7597
7598 if (next < hdr_end) {
7599 /* Set-Cookie2 supports multiple cookies, and <next> points to
7600 * a colon or semi-colon before the end. So skip all attr-value
7601 * pairs and look for the next comma. For Set-Cookie, since
7602 * commas are permitted in values, skip to the end.
7603 */
7604 if (is_cookie2)
7605 next = find_hdr_value_end(next, hdr_end);
7606 else
7607 next = hdr_end;
7608 }
7609
7610 /* Now everything is as on the diagram above */
7611
7612 /* Ignore cookies with no equal sign */
7613 if (equal == val_end)
7614 continue;
7615
7616 /* If there are spaces around the equal sign, we need to
7617 * strip them otherwise we'll get trouble for cookie captures,
7618 * or even for rewrites. Since this happens extremely rarely,
7619 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007621 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7622 int stripped_before = 0;
7623 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007624
Willy Tarreau24581ba2010-08-31 22:39:35 +02007625 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007626 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007627 equal += stripped_before;
7628 val_beg += stripped_before;
7629 }
7630
7631 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007632 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007633 val_beg += stripped_after;
7634 stripped_before += stripped_after;
7635 }
7636
7637 val_end += stripped_before;
7638 next += stripped_before;
7639 hdr_end += stripped_before;
7640 hdr_next += stripped_before;
7641 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007642 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007643 }
7644
7645 /* First, let's see if we want to capture this cookie. We check
7646 * that we don't already have a server side cookie, because we
7647 * can only capture one. Also as an optimisation, we ignore
7648 * cookies shorter than the declared name.
7649 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007650 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007651 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007652 (val_end - att_beg >= t->fe->capture_namelen) &&
7653 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7654 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007655 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656 Alert("HTTP logging : out of memory.\n");
7657 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007658 else {
7659 if (log_len > t->fe->capture_len)
7660 log_len = t->fe->capture_len;
7661 memcpy(txn->srv_cookie, att_beg, log_len);
7662 txn->srv_cookie[log_len] = 0;
7663 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 }
7665
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007666 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007667 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007668 if (!(t->flags & SN_IGNORE_PRST) &&
7669 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7670 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007671 /* assume passive cookie by default */
7672 txn->flags &= ~TX_SCK_MASK;
7673 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674
7675 /* If the cookie is in insert mode on a known server, we'll delete
7676 * this occurrence because we'll insert another one later.
7677 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007678 * a direct access.
7679 */
Willy Tarreau67402132012-05-31 20:40:20 +02007680 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007681 /* The "preserve" flag was set, we don't want to touch the
7682 * server's cookie.
7683 */
7684 }
Willy Tarreau67402132012-05-31 20:40:20 +02007685 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7686 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007687 /* this cookie must be deleted */
7688 if (*prev == ':' && next == hdr_end) {
7689 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007690 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007691 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7692 txn->hdr_idx.used--;
7693 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007694 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007695 hdr_next += delta;
7696 http_msg_move_end(&txn->rsp, delta);
7697 /* note: while both invalid now, <next> and <hdr_end>
7698 * are still equal, so the for() will stop as expected.
7699 */
7700 } else {
7701 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007702 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007703 next = prev;
7704 hdr_end += delta;
7705 hdr_next += delta;
7706 cur_hdr->len += delta;
7707 http_msg_move_end(&txn->rsp, delta);
7708 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007709 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007710 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007711 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712 }
Willy Tarreau67402132012-05-31 20:40:20 +02007713 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007714 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007715 * with this server since we know it.
7716 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007718 next += delta;
7719 hdr_end += delta;
7720 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007721 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007722 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723
Willy Tarreauf1348312010-10-07 15:54:11 +02007724 txn->flags &= ~TX_SCK_MASK;
7725 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007726 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007727 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007728 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007729 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007730 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007731 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007732 next += delta;
7733 hdr_end += delta;
7734 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007735 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007736 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007737
Willy Tarreau827aee92011-03-10 16:55:02 +01007738 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007739 txn->flags &= ~TX_SCK_MASK;
7740 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007741 }
7742 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007743 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7744 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007745 int cmp_len, value_len;
7746 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007747
Cyril Bontéb21570a2009-11-29 20:04:48 +01007748 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007749 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7750 value_begin = att_beg + t->be->appsession_name_len;
7751 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007752 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007753 cmp_len = att_end - att_beg;
7754 value_begin = val_beg;
7755 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007756 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007757
Cyril Bonté17530c32010-04-06 21:11:10 +02007758 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007759 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7760 /* free a possibly previously allocated memory */
7761 pool_free2(apools.sessid, txn->sessid);
7762
Cyril Bontéb21570a2009-11-29 20:04:48 +01007763 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007764 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007765 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7766 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7767 return;
7768 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007769 memcpy(txn->sessid, value_begin, value_len);
7770 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007771 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007772 }
7773 /* that's done for this cookie, check the next one on the same
7774 * line when next != hdr_end (only if is_cookie2).
7775 */
7776 }
7777 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007778 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007779 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007780
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007781 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007782 appsess *asession = NULL;
7783 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007784 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007785 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007786 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007787 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7788 Alert("Not enough Memory process_srv():asession:calloc().\n");
7789 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7790 return;
7791 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007792 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7793
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007794 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7795 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7796 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007797 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007798 return;
7799 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007800 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007801 asession->sessid[t->be->appsession_len] = 0;
7802
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007803 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007804 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007805 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007806 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007807 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007808 return;
7809 }
7810 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007811 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007812
7813 asession->request_count = 0;
7814 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7815 }
7816
7817 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7818 asession->request_count++;
7819 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820}
7821
7822
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823/*
7824 * Check if response is cacheable or not. Updates t->flags.
7825 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007826void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007827{
7828 struct http_txn *txn = &t->txn;
7829 char *p1, *p2;
7830
7831 char *cur_ptr, *cur_end, *cur_next;
7832 int cur_idx;
7833
Willy Tarreau5df51872007-11-25 16:20:08 +01007834 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 return;
7836
7837 /* Iterate through the headers.
7838 * we start with the start line.
7839 */
7840 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007841 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007842
7843 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7844 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007845 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846
7847 cur_hdr = &txn->hdr_idx.v[cur_idx];
7848 cur_ptr = cur_next;
7849 cur_end = cur_ptr + cur_hdr->len;
7850 cur_next = cur_end + cur_hdr->cr + 1;
7851
7852 /* We have one full header between cur_ptr and cur_end, and the
7853 * next header starts at cur_next. We're only interested in
7854 * "Cookie:" headers.
7855 */
7856
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007857 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7858 if (val) {
7859 if ((cur_end - (cur_ptr + val) >= 8) &&
7860 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7861 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7862 return;
7863 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864 }
7865
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007866 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7867 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868 continue;
7869
7870 /* OK, right now we know we have a cache-control header at cur_ptr */
7871
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007872 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873
7874 if (p1 >= cur_end) /* no more info */
7875 continue;
7876
7877 /* p1 is at the beginning of the value */
7878 p2 = p1;
7879
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007880 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881 p2++;
7882
7883 /* we have a complete value between p1 and p2 */
7884 if (p2 < cur_end && *p2 == '=') {
7885 /* we have something of the form no-cache="set-cookie" */
7886 if ((cur_end - p1 >= 21) &&
7887 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7888 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007889 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 continue;
7891 }
7892
7893 /* OK, so we know that either p2 points to the end of string or to a comma */
7894 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007895 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7897 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7898 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007899 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900 return;
7901 }
7902
7903 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007904 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905 continue;
7906 }
7907 }
7908}
7909
7910
Willy Tarreau58f10d72006-12-04 02:26:12 +01007911/*
7912 * Try to retrieve a known appsession in the URI, then the associated server.
7913 * If the server is found, it's assigned to the session.
7914 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007915void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007916{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007917 char *end_params, *first_param, *cur_param, *next_param;
7918 char separator;
7919 int value_len;
7920
7921 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007923 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007924 (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 +01007925 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007926 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007927
Cyril Bontéb21570a2009-11-29 20:04:48 +01007928 first_param = NULL;
7929 switch (mode) {
7930 case PR_O2_AS_M_PP:
7931 first_param = memchr(begin, ';', len);
7932 break;
7933 case PR_O2_AS_M_QS:
7934 first_param = memchr(begin, '?', len);
7935 break;
7936 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937
Cyril Bontéb21570a2009-11-29 20:04:48 +01007938 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007939 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007940 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941
Cyril Bontéb21570a2009-11-29 20:04:48 +01007942 switch (mode) {
7943 case PR_O2_AS_M_PP:
7944 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7945 end_params = (char *) begin + len;
7946 }
7947 separator = ';';
7948 break;
7949 case PR_O2_AS_M_QS:
7950 end_params = (char *) begin + len;
7951 separator = '&';
7952 break;
7953 default:
7954 /* unknown mode, shouldn't happen */
7955 return;
7956 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007957
Cyril Bontéb21570a2009-11-29 20:04:48 +01007958 cur_param = next_param = end_params;
7959 while (cur_param > first_param) {
7960 cur_param--;
7961 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7962 /* let's see if this is the appsession parameter */
7963 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7964 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7965 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7966 /* Cool... it's the right one */
7967 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7968 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7969 if (value_len > 0) {
7970 manage_client_side_appsession(t, cur_param, value_len);
7971 }
7972 break;
7973 }
7974 next_param = cur_param;
7975 }
7976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007977#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007978 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007979 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007980#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007981}
7982
Willy Tarreaub2513902006-12-17 14:52:38 +01007983/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007984 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007985 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007986 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007987 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007988 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007989 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007990 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007991 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007992int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007993{
7994 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007995 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007996 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007997
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007998 if (!uri_auth)
7999 return 0;
8000
Cyril Bonté70be45d2010-10-12 00:14:35 +02008001 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008002 return 0;
8003
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008004 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008005 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008006 return 0;
8007
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008008 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008009 return 0;
8010
Willy Tarreaub2513902006-12-17 14:52:38 +01008011 return 1;
8012}
8013
Willy Tarreau4076a152009-04-02 15:18:36 +02008014/*
8015 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008016 * By default it tries to report the error position as msg->err_pos. However if
8017 * this one is not set, it will then report msg->next, which is the last known
8018 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008019 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008020 */
8021void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008022 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008023 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008024{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008025 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008026 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008027
Willy Tarreau9b28e032012-10-12 23:49:43 +02008028 es->len = MIN(chn->buf->i, sizeof(es->buf));
8029 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008030 len1 = MIN(len1, es->len);
8031 len2 = es->len - len1; /* remaining data if buffer wraps */
8032
Willy Tarreau9b28e032012-10-12 23:49:43 +02008033 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008034 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008035 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008036
Willy Tarreau4076a152009-04-02 15:18:36 +02008037 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008038 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008039 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008040 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008041
Willy Tarreau4076a152009-04-02 15:18:36 +02008042 es->when = date; // user-visible date
8043 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008044 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008045 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008046 if (objt_conn(s->req->prod->end))
8047 es->src = __objt_conn(s->req->prod->end)->addr.from;
8048 else
8049 memset(&es->src, 0, sizeof(es->src));
8050
Willy Tarreau078272e2010-12-12 12:46:33 +01008051 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008052 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008053 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008054 es->s_flags = s->flags;
8055 es->t_flags = s->txn.flags;
8056 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008057 es->b_out = chn->buf->o;
8058 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008059 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008060 es->m_clen = msg->chunk_len;
8061 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008062}
Willy Tarreaub2513902006-12-17 14:52:38 +01008063
Willy Tarreau294c4732011-12-16 21:35:50 +01008064/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8065 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8066 * performed over the whole headers. Otherwise it must contain a valid header
8067 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8068 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8069 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8070 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008071 * -1. The value fetch stops at commas, so this function is suited for use with
8072 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008073 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008074 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008075unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008076 struct hdr_idx *idx, int occ,
8077 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008078{
Willy Tarreau294c4732011-12-16 21:35:50 +01008079 struct hdr_ctx local_ctx;
8080 char *ptr_hist[MAX_HDR_HISTORY];
8081 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008082 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008083 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008084
Willy Tarreau294c4732011-12-16 21:35:50 +01008085 if (!ctx) {
8086 local_ctx.idx = 0;
8087 ctx = &local_ctx;
8088 }
8089
Willy Tarreaubce70882009-09-07 11:51:47 +02008090 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008091 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008092 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008093 occ--;
8094 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008095 *vptr = ctx->line + ctx->val;
8096 *vlen = ctx->vlen;
8097 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008098 }
8099 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008100 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008101 }
8102
8103 /* negative occurrence, we scan all the list then walk back */
8104 if (-occ > MAX_HDR_HISTORY)
8105 return 0;
8106
Willy Tarreau294c4732011-12-16 21:35:50 +01008107 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008108 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008109 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8110 len_hist[hist_ptr] = ctx->vlen;
8111 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008112 hist_ptr = 0;
8113 found++;
8114 }
8115 if (-occ > found)
8116 return 0;
8117 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008118 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8119 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8120 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008121 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008122 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008123 if (hist_ptr >= MAX_HDR_HISTORY)
8124 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008125 *vptr = ptr_hist[hist_ptr];
8126 *vlen = len_hist[hist_ptr];
8127 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008128}
8129
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008130/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8131 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8132 * performed over the whole headers. Otherwise it must contain a valid header
8133 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8134 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8135 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8136 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8137 * -1. This function differs from http_get_hdr() in that it only returns full
8138 * line header values and does not stop at commas.
8139 * The return value is 0 if nothing was found, or non-zero otherwise.
8140 */
8141unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8142 struct hdr_idx *idx, int occ,
8143 struct hdr_ctx *ctx, char **vptr, int *vlen)
8144{
8145 struct hdr_ctx local_ctx;
8146 char *ptr_hist[MAX_HDR_HISTORY];
8147 int len_hist[MAX_HDR_HISTORY];
8148 unsigned int hist_ptr;
8149 int found;
8150
8151 if (!ctx) {
8152 local_ctx.idx = 0;
8153 ctx = &local_ctx;
8154 }
8155
8156 if (occ >= 0) {
8157 /* search from the beginning */
8158 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8159 occ--;
8160 if (occ <= 0) {
8161 *vptr = ctx->line + ctx->val;
8162 *vlen = ctx->vlen;
8163 return 1;
8164 }
8165 }
8166 return 0;
8167 }
8168
8169 /* negative occurrence, we scan all the list then walk back */
8170 if (-occ > MAX_HDR_HISTORY)
8171 return 0;
8172
8173 found = hist_ptr = 0;
8174 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8175 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8176 len_hist[hist_ptr] = ctx->vlen;
8177 if (++hist_ptr >= MAX_HDR_HISTORY)
8178 hist_ptr = 0;
8179 found++;
8180 }
8181 if (-occ > found)
8182 return 0;
8183 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8184 * find occurrence -occ, so we have to check [hist_ptr+occ].
8185 */
8186 hist_ptr += occ;
8187 if (hist_ptr >= MAX_HDR_HISTORY)
8188 hist_ptr -= MAX_HDR_HISTORY;
8189 *vptr = ptr_hist[hist_ptr];
8190 *vlen = len_hist[hist_ptr];
8191 return 1;
8192}
8193
Willy Tarreaubaaee002006-06-26 02:48:02 +02008194/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008195 * Print a debug line with a header. Always stop at the first CR or LF char,
8196 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8197 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008198 */
8199void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8200{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008201 int max;
8202 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008203 dir,
8204 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8205 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 +02008206
8207 for (max = 0; start + max < end; max++)
8208 if (start[max] == '\r' || start[max] == '\n')
8209 break;
8210
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008211 UBOUND(max, trash.size - trash.len - 3);
8212 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8213 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008214 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008215}
8216
Willy Tarreau0937bc42009-12-22 15:03:09 +01008217/*
8218 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8219 * the required fields are properly allocated and that we only need to (re)init
8220 * them. This should be used before processing any new request.
8221 */
8222void http_init_txn(struct session *s)
8223{
8224 struct http_txn *txn = &s->txn;
8225 struct proxy *fe = s->fe;
8226
8227 txn->flags = 0;
8228 txn->status = -1;
8229
Willy Tarreauf64d1412010-10-07 20:06:11 +02008230 txn->cookie_first_date = 0;
8231 txn->cookie_last_date = 0;
8232
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008233 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008234 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008235 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008236 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008237 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008238 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008239 txn->req.chunk_len = 0LL;
8240 txn->req.body_len = 0LL;
8241 txn->rsp.chunk_len = 0LL;
8242 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008243 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8244 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008245 txn->req.chn = s->req;
8246 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008247
8248 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008249
8250 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8251 if (fe->options2 & PR_O2_REQBUG_OK)
8252 txn->req.err_pos = -1; /* let buggy requests pass */
8253
Willy Tarreau46023632010-01-07 22:51:47 +01008254 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008255 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8256
Willy Tarreau46023632010-01-07 22:51:47 +01008257 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008258 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8259
8260 if (txn->hdr_idx.v)
8261 hdr_idx_init(&txn->hdr_idx);
8262}
8263
8264/* to be used at the end of a transaction */
8265void http_end_txn(struct session *s)
8266{
8267 struct http_txn *txn = &s->txn;
8268
Willy Tarreau75195602014-03-11 15:48:55 +01008269 /* release any possible compression context */
8270 if (s->flags & SN_COMP_READY)
8271 s->comp_algo->end(&s->comp_ctx);
8272 s->comp_algo = NULL;
8273 s->flags &= ~SN_COMP_READY;
8274
Willy Tarreau0937bc42009-12-22 15:03:09 +01008275 /* these ones will have been dynamically allocated */
8276 pool_free2(pool2_requri, txn->uri);
8277 pool_free2(pool2_capture, txn->cli_cookie);
8278 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008279 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008280 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008281
William Lallemanda73203e2012-03-12 12:48:57 +01008282 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008283 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008284 txn->uri = NULL;
8285 txn->srv_cookie = NULL;
8286 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008287
8288 if (txn->req.cap) {
8289 struct cap_hdr *h;
8290 for (h = s->fe->req_cap; h; h = h->next)
8291 pool_free2(h->pool, txn->req.cap[h->index]);
8292 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8293 }
8294
8295 if (txn->rsp.cap) {
8296 struct cap_hdr *h;
8297 for (h = s->fe->rsp_cap; h; h = h->next)
8298 pool_free2(h->pool, txn->rsp.cap[h->index]);
8299 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8300 }
8301
Willy Tarreau0937bc42009-12-22 15:03:09 +01008302}
8303
8304/* to be used at the end of a transaction to prepare a new one */
8305void http_reset_txn(struct session *s)
8306{
8307 http_end_txn(s);
8308 http_init_txn(s);
8309
8310 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008311 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008312 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008313 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008314 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008315 /* re-init store persistence */
8316 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008317 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008318
Willy Tarreau0937bc42009-12-22 15:03:09 +01008319 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008320
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008321 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008322
Willy Tarreau739cfba2010-01-25 23:11:14 +01008323 /* We must trim any excess data from the response buffer, because we
8324 * may have blocked an invalid response from a server that we don't
8325 * want to accidentely forward once we disable the analysers, nor do
8326 * we want those data to come along with next response. A typical
8327 * example of such data would be from a buggy server responding to
8328 * a HEAD with some data, or sending more than the advertised
8329 * content-length.
8330 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008331 if (unlikely(s->rep->buf->i))
8332 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008333
Willy Tarreau0937bc42009-12-22 15:03:09 +01008334 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008335 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008336
Willy Tarreaud04e8582010-05-31 12:31:35 +02008337 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008338 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008339
8340 s->req->rex = TICK_ETERNITY;
8341 s->req->wex = TICK_ETERNITY;
8342 s->req->analyse_exp = TICK_ETERNITY;
8343 s->rep->rex = TICK_ETERNITY;
8344 s->rep->wex = TICK_ETERNITY;
8345 s->rep->analyse_exp = TICK_ETERNITY;
8346}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008347
Willy Tarreauff011f22011-01-06 17:51:27 +01008348void free_http_req_rules(struct list *r) {
8349 struct http_req_rule *tr, *pr;
8350
8351 list_for_each_entry_safe(pr, tr, r, list) {
8352 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008353 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008354 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008355
8356 free(pr);
8357 }
8358}
8359
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008360/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008361struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8362{
8363 struct http_req_rule *rule;
8364 int cur_arg;
8365
8366 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8367 if (!rule) {
8368 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008369 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008370 }
8371
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008372 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008373 rule->action = HTTP_REQ_ACT_ALLOW;
8374 cur_arg = 1;
8375 } else if (!strcmp(args[0], "deny")) {
8376 rule->action = HTTP_REQ_ACT_DENY;
8377 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008378 } else if (!strcmp(args[0], "tarpit")) {
8379 rule->action = HTTP_REQ_ACT_TARPIT;
8380 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008381 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008382 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008383 cur_arg = 1;
8384
8385 while(*args[cur_arg]) {
8386 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008387 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008388 cur_arg+=2;
8389 continue;
8390 } else
8391 break;
8392 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008393 } else if (!strcmp(args[0], "set-nice")) {
8394 rule->action = HTTP_REQ_ACT_SET_NICE;
8395 cur_arg = 1;
8396
8397 if (!*args[cur_arg] ||
8398 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8399 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8400 file, linenum, args[0]);
8401 goto out_err;
8402 }
8403 rule->arg.nice = atoi(args[cur_arg]);
8404 if (rule->arg.nice < -1024)
8405 rule->arg.nice = -1024;
8406 else if (rule->arg.nice > 1024)
8407 rule->arg.nice = 1024;
8408 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008409 } else if (!strcmp(args[0], "set-tos")) {
8410#ifdef IP_TOS
8411 char *err;
8412 rule->action = HTTP_REQ_ACT_SET_TOS;
8413 cur_arg = 1;
8414
8415 if (!*args[cur_arg] ||
8416 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8417 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8418 file, linenum, args[0]);
8419 goto out_err;
8420 }
8421
8422 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8423 if (err && *err != '\0') {
8424 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8425 file, linenum, err, args[0]);
8426 goto out_err;
8427 }
8428 cur_arg++;
8429#else
8430 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8431 goto out_err;
8432#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008433 } else if (!strcmp(args[0], "set-mark")) {
8434#ifdef SO_MARK
8435 char *err;
8436 rule->action = HTTP_REQ_ACT_SET_MARK;
8437 cur_arg = 1;
8438
8439 if (!*args[cur_arg] ||
8440 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8441 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8442 file, linenum, args[0]);
8443 goto out_err;
8444 }
8445
8446 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8447 if (err && *err != '\0') {
8448 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8449 file, linenum, err, args[0]);
8450 goto out_err;
8451 }
8452 cur_arg++;
8453 global.last_checks |= LSTCHK_NETADM;
8454#else
8455 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8456 goto out_err;
8457#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008458 } else if (!strcmp(args[0], "set-log-level")) {
8459 rule->action = HTTP_REQ_ACT_SET_LOGL;
8460 cur_arg = 1;
8461
8462 if (!*args[cur_arg] ||
8463 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8464 bad_log_level:
8465 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8466 file, linenum, args[0]);
8467 goto out_err;
8468 }
8469 if (strcmp(args[cur_arg], "silent") == 0)
8470 rule->arg.loglevel = -1;
8471 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8472 goto bad_log_level;
8473 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008474 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8475 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8476 cur_arg = 1;
8477
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008478 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8479 (*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 +01008480 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8481 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008482 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008483 }
8484
8485 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8486 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8487 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008488
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008489 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008490 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Willy Tarreau434c57c2013-01-08 01:10:24 +01008491 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008492 free(proxy->conf.lfs_file);
8493 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8494 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008495 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008496 } else if (strcmp(args[0], "redirect") == 0) {
8497 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008498 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008499
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008500 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008501 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8502 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8503 goto out_err;
8504 }
8505
8506 /* this redirect rule might already contain a parsed condition which
8507 * we'll pass to the http-request rule.
8508 */
8509 rule->action = HTTP_REQ_ACT_REDIR;
8510 rule->arg.redir = redir;
8511 rule->cond = redir->cond;
8512 redir->cond = NULL;
8513 cur_arg = 2;
8514 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008515 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008516 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 +01008517 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008518 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008519 }
8520
8521 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8522 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008523 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008524
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008525 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8526 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8527 file, linenum, args[0], errmsg);
8528 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008529 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008530 }
8531 rule->cond = cond;
8532 }
8533 else if (*args[cur_arg]) {
8534 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8535 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8536 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008537 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008538 }
8539
8540 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008541 out_err:
8542 free(rule);
8543 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008544}
8545
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008546/* parse an "http-respose" rule */
8547struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8548{
8549 struct http_res_rule *rule;
8550 int cur_arg;
8551
8552 rule = calloc(1, sizeof(*rule));
8553 if (!rule) {
8554 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8555 goto out_err;
8556 }
8557
8558 if (!strcmp(args[0], "allow")) {
8559 rule->action = HTTP_RES_ACT_ALLOW;
8560 cur_arg = 1;
8561 } else if (!strcmp(args[0], "deny")) {
8562 rule->action = HTTP_RES_ACT_DENY;
8563 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008564 } else if (!strcmp(args[0], "set-nice")) {
8565 rule->action = HTTP_RES_ACT_SET_NICE;
8566 cur_arg = 1;
8567
8568 if (!*args[cur_arg] ||
8569 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8570 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8571 file, linenum, args[0]);
8572 goto out_err;
8573 }
8574 rule->arg.nice = atoi(args[cur_arg]);
8575 if (rule->arg.nice < -1024)
8576 rule->arg.nice = -1024;
8577 else if (rule->arg.nice > 1024)
8578 rule->arg.nice = 1024;
8579 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008580 } else if (!strcmp(args[0], "set-tos")) {
8581#ifdef IP_TOS
8582 char *err;
8583 rule->action = HTTP_RES_ACT_SET_TOS;
8584 cur_arg = 1;
8585
8586 if (!*args[cur_arg] ||
8587 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8588 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8589 file, linenum, args[0]);
8590 goto out_err;
8591 }
8592
8593 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8594 if (err && *err != '\0') {
8595 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8596 file, linenum, err, args[0]);
8597 goto out_err;
8598 }
8599 cur_arg++;
8600#else
8601 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8602 goto out_err;
8603#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008604 } else if (!strcmp(args[0], "set-mark")) {
8605#ifdef SO_MARK
8606 char *err;
8607 rule->action = HTTP_RES_ACT_SET_MARK;
8608 cur_arg = 1;
8609
8610 if (!*args[cur_arg] ||
8611 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8612 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8613 file, linenum, args[0]);
8614 goto out_err;
8615 }
8616
8617 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8618 if (err && *err != '\0') {
8619 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8620 file, linenum, err, args[0]);
8621 goto out_err;
8622 }
8623 cur_arg++;
8624 global.last_checks |= LSTCHK_NETADM;
8625#else
8626 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8627 goto out_err;
8628#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008629 } else if (!strcmp(args[0], "set-log-level")) {
8630 rule->action = HTTP_RES_ACT_SET_LOGL;
8631 cur_arg = 1;
8632
8633 if (!*args[cur_arg] ||
8634 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8635 bad_log_level:
8636 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8637 file, linenum, args[0]);
8638 goto out_err;
8639 }
8640 if (strcmp(args[cur_arg], "silent") == 0)
8641 rule->arg.loglevel = -1;
8642 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8643 goto bad_log_level;
8644 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008645 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8646 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8647 cur_arg = 1;
8648
8649 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8650 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8651 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8652 file, linenum, args[0]);
8653 goto out_err;
8654 }
8655
8656 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8657 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8658 LIST_INIT(&rule->arg.hdr_add.fmt);
8659
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008660 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008661 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008662 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008663 free(proxy->conf.lfs_file);
8664 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8665 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008666 cur_arg += 2;
8667 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008668 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 +02008669 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8670 goto out_err;
8671 }
8672
8673 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8674 struct acl_cond *cond;
8675 char *errmsg = NULL;
8676
8677 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8678 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8679 file, linenum, args[0], errmsg);
8680 free(errmsg);
8681 goto out_err;
8682 }
8683 rule->cond = cond;
8684 }
8685 else if (*args[cur_arg]) {
8686 Alert("parsing [%s:%d]: 'http-response %s' expects"
8687 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8688 file, linenum, args[0], args[cur_arg]);
8689 goto out_err;
8690 }
8691
8692 return rule;
8693 out_err:
8694 free(rule);
8695 return NULL;
8696}
8697
Willy Tarreau4baae242012-12-27 12:00:31 +01008698/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008699 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8700 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008701 */
8702struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008703 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008704{
8705 struct redirect_rule *rule;
8706 int cur_arg;
8707 int type = REDIRECT_TYPE_NONE;
8708 int code = 302;
8709 const char *destination = NULL;
8710 const char *cookie = NULL;
8711 int cookie_set = 0;
8712 unsigned int flags = REDIRECT_FLAG_NONE;
8713 struct acl_cond *cond = NULL;
8714
8715 cur_arg = 0;
8716 while (*(args[cur_arg])) {
8717 if (strcmp(args[cur_arg], "location") == 0) {
8718 if (!*args[cur_arg + 1])
8719 goto missing_arg;
8720
8721 type = REDIRECT_TYPE_LOCATION;
8722 cur_arg++;
8723 destination = args[cur_arg];
8724 }
8725 else if (strcmp(args[cur_arg], "prefix") == 0) {
8726 if (!*args[cur_arg + 1])
8727 goto missing_arg;
8728
8729 type = REDIRECT_TYPE_PREFIX;
8730 cur_arg++;
8731 destination = args[cur_arg];
8732 }
8733 else if (strcmp(args[cur_arg], "scheme") == 0) {
8734 if (!*args[cur_arg + 1])
8735 goto missing_arg;
8736
8737 type = REDIRECT_TYPE_SCHEME;
8738 cur_arg++;
8739 destination = args[cur_arg];
8740 }
8741 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8742 if (!*args[cur_arg + 1])
8743 goto missing_arg;
8744
8745 cur_arg++;
8746 cookie = args[cur_arg];
8747 cookie_set = 1;
8748 }
8749 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8750 if (!*args[cur_arg + 1])
8751 goto missing_arg;
8752
8753 cur_arg++;
8754 cookie = args[cur_arg];
8755 cookie_set = 0;
8756 }
8757 else if (strcmp(args[cur_arg], "code") == 0) {
8758 if (!*args[cur_arg + 1])
8759 goto missing_arg;
8760
8761 cur_arg++;
8762 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008763 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008764 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008765 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008766 args[cur_arg - 1], args[cur_arg]);
8767 return NULL;
8768 }
8769 }
8770 else if (!strcmp(args[cur_arg],"drop-query")) {
8771 flags |= REDIRECT_FLAG_DROP_QS;
8772 }
8773 else if (!strcmp(args[cur_arg],"append-slash")) {
8774 flags |= REDIRECT_FLAG_APPEND_SLASH;
8775 }
8776 else if (strcmp(args[cur_arg], "if") == 0 ||
8777 strcmp(args[cur_arg], "unless") == 0) {
8778 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8779 if (!cond) {
8780 memprintf(errmsg, "error in condition: %s", *errmsg);
8781 return NULL;
8782 }
8783 break;
8784 }
8785 else {
8786 memprintf(errmsg,
8787 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8788 args[cur_arg]);
8789 return NULL;
8790 }
8791 cur_arg++;
8792 }
8793
8794 if (type == REDIRECT_TYPE_NONE) {
8795 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8796 return NULL;
8797 }
8798
8799 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8800 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008801 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008802
8803 if (!use_fmt) {
8804 /* old-style static redirect rule */
8805 rule->rdr_str = strdup(destination);
8806 rule->rdr_len = strlen(destination);
8807 }
8808 else {
8809 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008810
8811 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8812 * if prefix == "/", we don't want to add anything, otherwise it
8813 * makes it hard for the user to configure a self-redirection.
8814 */
8815 proxy->conf.args.ctx = ARGC_RDR;
8816 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008817 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008818 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008819 free(curproxy->conf.lfs_file);
8820 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8821 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008822 }
8823 }
8824
Willy Tarreau4baae242012-12-27 12:00:31 +01008825 if (cookie) {
8826 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8827 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8828 */
8829 rule->cookie_len = strlen(cookie);
8830 if (cookie_set) {
8831 rule->cookie_str = malloc(rule->cookie_len + 10);
8832 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8833 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8834 rule->cookie_len += 9;
8835 } else {
8836 rule->cookie_str = malloc(rule->cookie_len + 21);
8837 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8838 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8839 rule->cookie_len += 20;
8840 }
8841 }
8842 rule->type = type;
8843 rule->code = code;
8844 rule->flags = flags;
8845 LIST_INIT(&rule->list);
8846 return rule;
8847
8848 missing_arg:
8849 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8850 return NULL;
8851}
8852
Willy Tarreau8797c062007-05-07 00:55:35 +02008853/************************************************************************/
8854/* The code below is dedicated to ACL parsing and matching */
8855/************************************************************************/
8856
8857
Willy Tarreau14174bc2012-04-16 14:34:04 +02008858/* This function ensures that the prerequisites for an L7 fetch are ready,
8859 * which means that a request or response is ready. If some data is missing,
8860 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008861 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8862 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008863 *
8864 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008865 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8866 * decide whether or not an HTTP message is present ;
8867 * 0 if the requested data cannot be fetched or if it is certain that
8868 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008869 * 1 if an HTTP message is ready
8870 */
8871static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008872smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008873 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008874{
8875 struct http_txn *txn = l7;
8876 struct http_msg *msg = &txn->req;
8877
8878 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8879 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8880 */
8881
8882 if (unlikely(!s || !txn))
8883 return 0;
8884
8885 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008886 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008887
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008888 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008889 if (unlikely(!s->req))
8890 return 0;
8891
Willy Tarreauaae75e32013-03-29 12:31:49 +01008892 /* If the buffer does not leave enough free space at the end,
8893 * we must first realign it.
8894 */
8895 if (s->req->buf->p > s->req->buf->data &&
8896 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8897 buffer_slow_realign(s->req->buf);
8898
Willy Tarreau14174bc2012-04-16 14:34:04 +02008899 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008900 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008901 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008902
8903 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008904 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008905 http_msg_analyzer(msg, &txn->hdr_idx);
8906
8907 /* Still no valid request ? */
8908 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008909 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008910 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008911 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008912 }
8913 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008914 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008915 return 0;
8916 }
8917
8918 /* OK we just got a valid HTTP request. We have some minor
8919 * preparation to perform so that further checks can rely
8920 * on HTTP tests.
8921 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008922
8923 /* If the request was parsed but was too large, we must absolutely
8924 * return an error so that it is not processed. At the moment this
8925 * cannot happen, but if the parsers are to change in the future,
8926 * we want this check to be maintained.
8927 */
8928 if (unlikely(s->req->buf->i + s->req->buf->p >
8929 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8930 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008931 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008932 return 1;
8933 }
8934
Willy Tarreau9b28e032012-10-12 23:49:43 +02008935 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008936 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8937 s->flags |= SN_REDIRECTABLE;
8938
Willy Tarreau506d0502013-07-06 13:29:24 +02008939 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8940 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008941 }
8942
Willy Tarreau506d0502013-07-06 13:29:24 +02008943 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008944 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008945 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008946
8947 /* otherwise everything's ready for the request */
8948 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008949 else {
8950 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008951 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8952 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008953 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008954 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008955 }
8956
8957 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008958 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008959 return 1;
8960}
Willy Tarreau8797c062007-05-07 00:55:35 +02008961
Willy Tarreauc0239e02012-04-16 14:42:55 +02008962#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008963 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 +02008964
Willy Tarreau24e32d82012-04-23 23:55:44 +02008965#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008966 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 +02008967
Willy Tarreau8797c062007-05-07 00:55:35 +02008968
8969/* 1. Check on METHOD
8970 * We use the pre-parsed method if it is known, and store its number as an
8971 * integer. If it is unknown, we use the pointer and the length.
8972 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008973static int pat_parse_meth(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008974{
8975 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008976 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008977
Willy Tarreauae8b7962007-06-09 23:10:04 +02008978 len = strlen(*text);
8979 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008980
8981 pattern->val.i = meth;
8982 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008983 if (usage == PAT_U_COMPILE) {
8984 pattern->ptr.str = strdup(*text);
8985 if (!pattern->ptr.str) {
8986 memprintf(err, "out of memory while loading pattern");
8987 return 0;
8988 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008989 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008990 else {
8991 trash = get_trash_chunk();
8992 if (trash->size < len) {
8993 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8994 len, trash->size);
8995 return 0;
8996 }
8997 pattern->ptr.str = trash->str;
8998 }
8999 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02009000 pattern->len = len;
9001 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01009002 else
9003 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02009004 return 1;
9005}
9006
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009007/* This function fetches the method of current HTTP request and stores
9008 * it in the global pattern struct as a chunk. There are two possibilities :
9009 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9010 * in <len> and <ptr> is NULL ;
9011 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9012 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009013 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009014 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009015static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009016smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009017 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009018{
9019 int meth;
9020 struct http_txn *txn = l7;
9021
Willy Tarreau24e32d82012-04-23 23:55:44 +02009022 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009023
Willy Tarreau8797c062007-05-07 00:55:35 +02009024 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02009025 smp->type = SMP_T_UINT;
9026 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009027 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009028 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9029 /* ensure the indexes are not affected */
9030 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009031 smp->type = SMP_T_CSTR;
9032 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009033 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009034 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009035 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009036 return 1;
9037}
9038
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009039/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01009040static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02009041{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009042 int icase;
9043
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009044
Willy Tarreauf853c462012-04-23 18:53:56 +02009045 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009046 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009047 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009048 return PAT_MATCH;
9049 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009050 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009051
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009052 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9053 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009054 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009055
9056 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009057 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009058 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009059
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009060 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009061 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9062 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009063 return PAT_NOMATCH;
9064 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009065}
9066
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009067static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009068smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009069 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009070{
9071 struct http_txn *txn = l7;
9072 char *ptr;
9073 int len;
9074
Willy Tarreauc0239e02012-04-16 14:42:55 +02009075 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009076
Willy Tarreau8797c062007-05-07 00:55:35 +02009077 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009078 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009079
9080 while ((len-- > 0) && (*ptr++ != '/'));
9081 if (len <= 0)
9082 return 0;
9083
Willy Tarreauf853c462012-04-23 18:53:56 +02009084 smp->type = SMP_T_CSTR;
9085 smp->data.str.str = ptr;
9086 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009087
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009088 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009089 return 1;
9090}
9091
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009092static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009093smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009094 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009095{
9096 struct http_txn *txn = l7;
9097 char *ptr;
9098 int len;
9099
Willy Tarreauc0239e02012-04-16 14:42:55 +02009100 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009101
Willy Tarreauf26b2522012-12-14 08:33:14 +01009102 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9103 return 0;
9104
Willy Tarreau8797c062007-05-07 00:55:35 +02009105 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009106 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009107
9108 while ((len-- > 0) && (*ptr++ != '/'));
9109 if (len <= 0)
9110 return 0;
9111
Willy Tarreauf853c462012-04-23 18:53:56 +02009112 smp->type = SMP_T_CSTR;
9113 smp->data.str.str = ptr;
9114 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009115
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009116 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009117 return 1;
9118}
9119
9120/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009121static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009122smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009123 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009124{
9125 struct http_txn *txn = l7;
9126 char *ptr;
9127 int len;
9128
Willy Tarreauc0239e02012-04-16 14:42:55 +02009129 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009130
Willy Tarreauf26b2522012-12-14 08:33:14 +01009131 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9132 return 0;
9133
Willy Tarreau8797c062007-05-07 00:55:35 +02009134 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009135 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009136
Willy Tarreauf853c462012-04-23 18:53:56 +02009137 smp->type = SMP_T_UINT;
9138 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009139 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009140 return 1;
9141}
9142
9143/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009144static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009145smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009146 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009147{
9148 struct http_txn *txn = l7;
9149
Willy Tarreauc0239e02012-04-16 14:42:55 +02009150 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009151
Willy Tarreauf853c462012-04-23 18:53:56 +02009152 smp->type = SMP_T_CSTR;
9153 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009154 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009155 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009156 return 1;
9157}
9158
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009159static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009160smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009161 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009162{
9163 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009164 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009165
Willy Tarreauc0239e02012-04-16 14:42:55 +02009166 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009167
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009168 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9169 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009170 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009171
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009172 smp->type = SMP_T_IPV4;
9173 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009174 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009175 return 1;
9176}
9177
9178static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009179smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009180 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009181{
9182 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009183 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009184
Willy Tarreauc0239e02012-04-16 14:42:55 +02009185 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009186
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009187 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9188 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9189 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009190
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009191 smp->type = SMP_T_UINT;
9192 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009193 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009194 return 1;
9195}
9196
Willy Tarreau185b5c42012-04-26 15:11:51 +02009197/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9198 * Accepts an optional argument of type string containing the header field name,
9199 * and an optional argument of type signed or unsigned integer to request an
9200 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009201 * headers are considered from the first one. It does not stop on commas and
9202 * returns full lines instead (useful for User-Agent or Date for example).
9203 */
9204static int
9205smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009206 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009207{
9208 struct http_txn *txn = l7;
9209 struct hdr_idx *idx = &txn->hdr_idx;
9210 struct hdr_ctx *ctx = smp->ctx.a[0];
9211 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9212 int occ = 0;
9213 const char *name_str = NULL;
9214 int name_len = 0;
9215
9216 if (!ctx) {
9217 /* first call */
9218 ctx = &static_hdr_ctx;
9219 ctx->idx = 0;
9220 smp->ctx.a[0] = ctx;
9221 }
9222
9223 if (args) {
9224 if (args[0].type != ARGT_STR)
9225 return 0;
9226 name_str = args[0].data.str.str;
9227 name_len = args[0].data.str.len;
9228
9229 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9230 occ = args[1].data.uint;
9231 }
9232
9233 CHECK_HTTP_MESSAGE_FIRST();
9234
9235 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9236 /* search for header from the beginning */
9237 ctx->idx = 0;
9238
9239 if (!occ && !(opt & SMP_OPT_ITERATE))
9240 /* no explicit occurrence and single fetch => last header by default */
9241 occ = -1;
9242
9243 if (!occ)
9244 /* prepare to report multiple occurrences for ACL fetches */
9245 smp->flags |= SMP_F_NOT_LAST;
9246
9247 smp->type = SMP_T_CSTR;
9248 smp->flags |= SMP_F_VOL_HDR;
9249 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9250 return 1;
9251
9252 smp->flags &= ~SMP_F_NOT_LAST;
9253 return 0;
9254}
9255
9256/* 6. Check on HTTP header count. The number of occurrences is returned.
9257 * Accepts exactly 1 argument of type string. It does not stop on commas and
9258 * returns full lines instead (useful for User-Agent or Date for example).
9259 */
9260static int
9261smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009262 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009263{
9264 struct http_txn *txn = l7;
9265 struct hdr_idx *idx = &txn->hdr_idx;
9266 struct hdr_ctx ctx;
9267 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9268 int cnt;
9269
9270 if (!args || args->type != ARGT_STR)
9271 return 0;
9272
9273 CHECK_HTTP_MESSAGE_FIRST();
9274
9275 ctx.idx = 0;
9276 cnt = 0;
9277 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9278 cnt++;
9279
9280 smp->type = SMP_T_UINT;
9281 smp->data.uint = cnt;
9282 smp->flags = SMP_F_VOL_HDR;
9283 return 1;
9284}
9285
9286/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9287 * Accepts an optional argument of type string containing the header field name,
9288 * and an optional argument of type signed or unsigned integer to request an
9289 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009290 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009291 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009292static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009293smp_fetch_hdr(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 Tarreau33a7e692007-06-10 19:45:56 +02009295{
9296 struct http_txn *txn = l7;
9297 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009298 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009299 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009300 int occ = 0;
9301 const char *name_str = NULL;
9302 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009303
Willy Tarreaua890d072013-04-02 12:01:06 +02009304 if (!ctx) {
9305 /* first call */
9306 ctx = &static_hdr_ctx;
9307 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009308 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009309 }
9310
Willy Tarreau185b5c42012-04-26 15:11:51 +02009311 if (args) {
9312 if (args[0].type != ARGT_STR)
9313 return 0;
9314 name_str = args[0].data.str.str;
9315 name_len = args[0].data.str.len;
9316
9317 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9318 occ = args[1].data.uint;
9319 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009320
Willy Tarreaue333ec92012-04-16 16:26:40 +02009321 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009322
Willy Tarreau185b5c42012-04-26 15:11:51 +02009323 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009324 /* search for header from the beginning */
9325 ctx->idx = 0;
9326
Willy Tarreau185b5c42012-04-26 15:11:51 +02009327 if (!occ && !(opt & SMP_OPT_ITERATE))
9328 /* no explicit occurrence and single fetch => last header by default */
9329 occ = -1;
9330
9331 if (!occ)
9332 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009333 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009334
Willy Tarreau185b5c42012-04-26 15:11:51 +02009335 smp->type = SMP_T_CSTR;
9336 smp->flags |= SMP_F_VOL_HDR;
9337 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 +02009338 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009339
Willy Tarreau37406352012-04-23 16:16:37 +02009340 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009341 return 0;
9342}
9343
Willy Tarreauc11416f2007-06-17 16:58:38 +02009344/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009345 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009346 */
9347static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009348smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009349 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009350{
9351 struct http_txn *txn = l7;
9352 struct hdr_idx *idx = &txn->hdr_idx;
9353 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009354 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009355 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009356
Willy Tarreau24e32d82012-04-23 23:55:44 +02009357 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009358 return 0;
9359
Willy Tarreaue333ec92012-04-16 16:26:40 +02009360 CHECK_HTTP_MESSAGE_FIRST();
9361
Willy Tarreau33a7e692007-06-10 19:45:56 +02009362 ctx.idx = 0;
9363 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009364 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 +02009365 cnt++;
9366
Willy Tarreauf853c462012-04-23 18:53:56 +02009367 smp->type = SMP_T_UINT;
9368 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009369 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009370 return 1;
9371}
9372
Willy Tarreau185b5c42012-04-26 15:11:51 +02009373/* Fetch an HTTP header's integer value. The integer value is returned. It
9374 * takes a mandatory argument of type string and an optional one of type int
9375 * to designate a specific occurrence. It returns an unsigned integer, which
9376 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009377 */
9378static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009379smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009380 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009381{
Willy Tarreauef38c392013-07-22 16:29:32 +02009382 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009383
Willy Tarreauf853c462012-04-23 18:53:56 +02009384 if (ret > 0) {
9385 smp->type = SMP_T_UINT;
9386 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9387 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009388
Willy Tarreaud53e2422012-04-16 17:21:11 +02009389 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009390}
9391
Cyril Bonté69fa9922012-10-25 00:01:06 +02009392/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9393 * and an optional one of type int to designate a specific occurrence.
9394 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009395 */
9396static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009397smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009398 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009399{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009400 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009401
Willy Tarreauef38c392013-07-22 16:29:32 +02009402 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009403 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9404 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009405 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009406 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009407 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009408 if (smp->data.str.len < temp->size - 1) {
9409 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9410 temp->str[smp->data.str.len] = '\0';
9411 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9412 smp->type = SMP_T_IPV6;
9413 break;
9414 }
9415 }
9416 }
9417
Willy Tarreaud53e2422012-04-16 17:21:11 +02009418 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009419 if (!(smp->flags & SMP_F_NOT_LAST))
9420 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009421 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009422 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009423}
9424
Willy Tarreau737b0c12007-06-10 21:28:46 +02009425/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9426 * the first '/' after the possible hostname, and ends before the possible '?'.
9427 */
9428static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009429smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009430 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009431{
9432 struct http_txn *txn = l7;
9433 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009434
Willy Tarreauc0239e02012-04-16 14:42:55 +02009435 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009436
Willy Tarreau9b28e032012-10-12 23:49:43 +02009437 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009438 ptr = http_get_path(txn);
9439 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009440 return 0;
9441
9442 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009443 smp->type = SMP_T_CSTR;
9444 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009445
9446 while (ptr < end && *ptr != '?')
9447 ptr++;
9448
Willy Tarreauf853c462012-04-23 18:53:56 +02009449 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009450 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009451 return 1;
9452}
9453
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009454/* This produces a concatenation of the first occurrence of the Host header
9455 * followed by the path component if it begins with a slash ('/'). This means
9456 * that '*' will not be added, resulting in exactly the first Host entry.
9457 * If no Host header is found, then the path is returned as-is. The returned
9458 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009459 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009460 */
9461static int
9462smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009463 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009464{
9465 struct http_txn *txn = l7;
9466 char *ptr, *end, *beg;
9467 struct hdr_ctx ctx;
9468
9469 CHECK_HTTP_MESSAGE_FIRST();
9470
9471 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009472 if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009473 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009474 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009475
9476 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009477 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009478 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009479 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009480 smp->data.str.len = ctx.vlen;
9481
9482 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009483 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009484 beg = http_get_path(txn);
9485 if (!beg)
9486 beg = end;
9487
9488 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9489
9490 if (beg < ptr && *beg == '/') {
9491 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9492 smp->data.str.len += ptr - beg;
9493 }
9494
9495 smp->flags = SMP_F_VOL_1ST;
9496 return 1;
9497}
9498
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009499/* This produces a 32-bit hash of the concatenation of the first occurrence of
9500 * the Host header followed by the path component if it begins with a slash ('/').
9501 * This means that '*' will not be added, resulting in exactly the first Host
9502 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009503 * is hashed using the path hash followed by a full avalanche hash and provides a
9504 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009505 * high-traffic sites without having to store whole paths.
9506 */
9507static int
9508smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009509 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009510{
9511 struct http_txn *txn = l7;
9512 struct hdr_ctx ctx;
9513 unsigned int hash = 0;
9514 char *ptr, *beg, *end;
9515 int len;
9516
9517 CHECK_HTTP_MESSAGE_FIRST();
9518
9519 ctx.idx = 0;
9520 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9521 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9522 ptr = ctx.line + ctx.val;
9523 len = ctx.vlen;
9524 while (len--)
9525 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9526 }
9527
9528 /* now retrieve the path */
9529 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9530 beg = http_get_path(txn);
9531 if (!beg)
9532 beg = end;
9533
9534 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9535
9536 if (beg < ptr && *beg == '/') {
9537 while (beg < ptr)
9538 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9539 }
9540 hash = full_hash(hash);
9541
9542 smp->type = SMP_T_UINT;
9543 smp->data.uint = hash;
9544 smp->flags = SMP_F_VOL_1ST;
9545 return 1;
9546}
9547
Willy Tarreau4a550602012-12-09 14:53:32 +01009548/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009549 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9550 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9551 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009552 * that in environments where IPv6 is insignificant, truncating the output to
9553 * 8 bytes would still work.
9554 */
9555static int
9556smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009557 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009558{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009559 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009560 struct connection *cli_conn = objt_conn(l4->si[0].end);
9561
9562 if (!cli_conn)
9563 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009564
Willy Tarreauef38c392013-07-22 16:29:32 +02009565 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009566 return 0;
9567
Willy Tarreau47ca5452012-12-23 20:22:19 +01009568 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009569 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9570 temp->len += sizeof(smp->data.uint);
9571
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009572 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009573 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009574 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009575 temp->len += 4;
9576 break;
9577 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009578 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009579 temp->len += 16;
9580 break;
9581 default:
9582 return 0;
9583 }
9584
9585 smp->data.str = *temp;
9586 smp->type = SMP_T_BIN;
9587 return 1;
9588}
9589
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009590static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009591smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009592 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009593{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009594 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9595 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9596 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009597
Willy Tarreau24e32d82012-04-23 23:55:44 +02009598 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009599
Willy Tarreauf853c462012-04-23 18:53:56 +02009600 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009601 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009602 return 1;
9603}
9604
Willy Tarreau7f18e522010-10-22 20:04:13 +02009605/* return a valid test if the current request is the first one on the connection */
9606static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009607smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009608 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009609{
9610 if (!s)
9611 return 0;
9612
Willy Tarreauf853c462012-04-23 18:53:56 +02009613 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009614 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009615 return 1;
9616}
9617
Willy Tarreau34db1082012-04-19 17:16:54 +02009618/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009619static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009620smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009621 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009622{
9623
Willy Tarreau24e32d82012-04-23 23:55:44 +02009624 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009625 return 0;
9626
Willy Tarreauc0239e02012-04-16 14:42:55 +02009627 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009628
Willy Tarreauc0239e02012-04-16 14:42:55 +02009629 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009630 return 0;
9631
Willy Tarreauf853c462012-04-23 18:53:56 +02009632 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009633 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009634 return 1;
9635}
Willy Tarreau8797c062007-05-07 00:55:35 +02009636
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009637/* Accepts exactly 1 argument of type userlist */
9638static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009639smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009640 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009641{
9642
9643 if (!args || args->type != ARGT_USR)
9644 return 0;
9645
9646 CHECK_HTTP_MESSAGE_FIRST();
9647
9648 if (!get_http_auth(l4))
9649 return 0;
9650
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009651 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009652 smp->ctx.a[0] = args->data.usr; /* user list */
9653 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9654 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9655
9656 /* if the user does not belong to the userlist or has a wrong password,
9657 * report that it unconditionally does not match. Otherwise we return
9658 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009659 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009660 */
9661 smp->type = SMP_T_BOOL;
9662 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9663 if (smp->data.uint)
9664 smp->type = SMP_T_UINT;
9665
9666 return 1;
9667}
9668
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009669/* Try to find the next occurrence of a cookie name in a cookie header value.
9670 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9671 * the cookie value is returned into *value and *value_l, and the function
9672 * returns a pointer to the next pointer to search from if the value was found.
9673 * Otherwise if the cookie was not found, NULL is returned and neither value
9674 * nor value_l are touched. The input <hdr> string should first point to the
9675 * header's value, and the <hdr_end> pointer must point to the first character
9676 * not part of the value. <list> must be non-zero if value may represent a list
9677 * of values (cookie headers). This makes it faster to abort parsing when no
9678 * list is expected.
9679 */
9680static char *
9681extract_cookie_value(char *hdr, const char *hdr_end,
9682 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009683 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009684{
9685 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9686 char *next;
9687
9688 /* we search at least a cookie name followed by an equal, and more
9689 * generally something like this :
9690 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9691 */
9692 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9693 /* Iterate through all cookies on this line */
9694
9695 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9696 att_beg++;
9697
9698 /* find att_end : this is the first character after the last non
9699 * space before the equal. It may be equal to hdr_end.
9700 */
9701 equal = att_end = att_beg;
9702
9703 while (equal < hdr_end) {
9704 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9705 break;
9706 if (http_is_spht[(unsigned char)*equal++])
9707 continue;
9708 att_end = equal;
9709 }
9710
9711 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9712 * is between <att_beg> and <equal>, both may be identical.
9713 */
9714
9715 /* look for end of cookie if there is an equal sign */
9716 if (equal < hdr_end && *equal == '=') {
9717 /* look for the beginning of the value */
9718 val_beg = equal + 1;
9719 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9720 val_beg++;
9721
9722 /* find the end of the value, respecting quotes */
9723 next = find_cookie_value_end(val_beg, hdr_end);
9724
9725 /* make val_end point to the first white space or delimitor after the value */
9726 val_end = next;
9727 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9728 val_end--;
9729 } else {
9730 val_beg = val_end = next = equal;
9731 }
9732
9733 /* We have nothing to do with attributes beginning with '$'. However,
9734 * they will automatically be removed if a header before them is removed,
9735 * since they're supposed to be linked together.
9736 */
9737 if (*att_beg == '$')
9738 continue;
9739
9740 /* Ignore cookies with no equal sign */
9741 if (equal == next)
9742 continue;
9743
9744 /* Now we have the cookie name between att_beg and att_end, and
9745 * its value between val_beg and val_end.
9746 */
9747
9748 if (att_end - att_beg == cookie_name_l &&
9749 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9750 /* let's return this value and indicate where to go on from */
9751 *value = val_beg;
9752 *value_l = val_end - val_beg;
9753 return next + 1;
9754 }
9755
9756 /* Set-Cookie headers only have the name in the first attr=value part */
9757 if (!list)
9758 break;
9759 }
9760
9761 return NULL;
9762}
9763
William Lallemanda43ba4e2014-01-28 18:14:25 +01009764/* Fetch a captured HTTP request header. The index is the position of
9765 * the "capture" option in the configuration file
9766 */
9767static int
9768smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9769 const struct arg *args, struct sample *smp, const char *kw)
9770{
9771 struct proxy *fe = l4->fe;
9772 struct http_txn *txn = l7;
9773 int idx;
9774
9775 if (!args || args->type != ARGT_UINT)
9776 return 0;
9777
9778 idx = args->data.uint;
9779
9780 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9781 return 0;
9782
9783 smp->type = SMP_T_CSTR;
9784 smp->data.str.str = txn->req.cap[idx];
9785 smp->data.str.len = strlen(txn->req.cap[idx]);
9786
9787 return 1;
9788}
9789
9790/* Fetch a captured HTTP response header. The index is the position of
9791 * the "capture" option in the configuration file
9792 */
9793static int
9794smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9795 const struct arg *args, struct sample *smp, const char *kw)
9796{
9797 struct proxy *fe = l4->fe;
9798 struct http_txn *txn = l7;
9799 int idx;
9800
9801 if (!args || args->type != ARGT_UINT)
9802 return 0;
9803
9804 idx = args->data.uint;
9805
9806 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9807 return 0;
9808
9809 smp->type = SMP_T_CSTR;
9810 smp->data.str.str = txn->rsp.cap[idx];
9811 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9812
9813 return 1;
9814}
9815
William Lallemand65ad6e12014-01-31 15:08:02 +01009816/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9817static int
9818smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9819 const struct arg *args, struct sample *smp, const char *kw)
9820{
9821 struct chunk *temp;
9822 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009823 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009824
9825 if (!txn->uri)
9826 return 0;
9827
William Lallemand96a77852014-02-05 00:30:02 +01009828 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009829
William Lallemand96a77852014-02-05 00:30:02 +01009830 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9831 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009832
William Lallemand96a77852014-02-05 00:30:02 +01009833 temp = get_trash_chunk();
9834 temp->str = txn->uri;
9835 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009836 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009837 smp->type = SMP_T_CSTR;
William Lallemand65ad6e12014-01-31 15:08:02 +01009838
9839 return 1;
9840
9841}
9842
9843/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9844static int
9845smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9846 const struct arg *args, struct sample *smp, const char *kw)
9847{
9848 struct chunk *temp;
9849 struct http_txn *txn = l7;
9850 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009851
9852 if (!txn->uri)
9853 return 0;
William Lallemand96a77852014-02-05 00:30:02 +01009854
William Lallemand65ad6e12014-01-31 15:08:02 +01009855 ptr = txn->uri;
9856
9857 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9858 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +01009859
William Lallemand65ad6e12014-01-31 15:08:02 +01009860 if (!*ptr)
9861 return 0;
9862
9863 ptr++; /* skip the space */
9864
9865 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +01009866 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +01009867 if (!ptr)
9868 return 0;
9869 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
9870 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009871
9872 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009873 smp->data.str.len = ptr - temp->str;
9874 smp->type = SMP_T_CSTR;
William Lallemand65ad6e12014-01-31 15:08:02 +01009875
9876 return 1;
9877}
9878
9879
Willy Tarreaue333ec92012-04-16 16:26:40 +02009880/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009881 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009882 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009883 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009884 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009885 * Accepts exactly 1 argument of type string. If the input options indicate
9886 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009887 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009888 */
9889static int
Willy Tarreau51539362012-05-08 12:46:28 +02009890smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009891 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009892{
9893 struct http_txn *txn = l7;
9894 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009895 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009896 const struct http_msg *msg;
9897 const char *hdr_name;
9898 int hdr_name_len;
9899 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009900 int occ = 0;
9901 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009902
Willy Tarreau24e32d82012-04-23 23:55:44 +02009903 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009904 return 0;
9905
Willy Tarreaua890d072013-04-02 12:01:06 +02009906 if (!ctx) {
9907 /* first call */
9908 ctx = &static_hdr_ctx;
9909 ctx->idx = 0;
9910 smp->ctx.a[2] = ctx;
9911 }
9912
Willy Tarreaue333ec92012-04-16 16:26:40 +02009913 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009914
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009915 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009916 msg = &txn->req;
9917 hdr_name = "Cookie";
9918 hdr_name_len = 6;
9919 } else {
9920 msg = &txn->rsp;
9921 hdr_name = "Set-Cookie";
9922 hdr_name_len = 10;
9923 }
9924
Willy Tarreau28376d62012-04-26 21:26:10 +02009925 if (!occ && !(opt & SMP_OPT_ITERATE))
9926 /* no explicit occurrence and single fetch => last cookie by default */
9927 occ = -1;
9928
9929 /* OK so basically here, either we want only one value and it's the
9930 * last one, or we want to iterate over all of them and we fetch the
9931 * next one.
9932 */
9933
Willy Tarreau9b28e032012-10-12 23:49:43 +02009934 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009935 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009936 /* search for the header from the beginning, we must first initialize
9937 * the search parameters.
9938 */
Willy Tarreau37406352012-04-23 16:16:37 +02009939 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009940 ctx->idx = 0;
9941 }
9942
Willy Tarreau28376d62012-04-26 21:26:10 +02009943 smp->flags |= SMP_F_VOL_HDR;
9944
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009945 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009946 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9947 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009948 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9949 goto out;
9950
Willy Tarreau24e32d82012-04-23 23:55:44 +02009951 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009952 continue;
9953
Willy Tarreau37406352012-04-23 16:16:37 +02009954 smp->ctx.a[0] = ctx->line + ctx->val;
9955 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009956 }
9957
Willy Tarreauf853c462012-04-23 18:53:56 +02009958 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009959 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009960 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009961 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009962 &smp->data.str.str,
9963 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009964 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009965 found = 1;
9966 if (occ >= 0) {
9967 /* one value was returned into smp->data.str.{str,len} */
9968 smp->flags |= SMP_F_NOT_LAST;
9969 return 1;
9970 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009971 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009972 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009973 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009974 /* all cookie headers and values were scanned. If we're looking for the
9975 * last occurrence, we may return it now.
9976 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009977 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009978 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009979 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009980}
9981
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009982/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009983 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +01009984 * multiple cookies may be parsed on the same line. The returned sample is of
9985 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009986 */
9987static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009988smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009989 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009990{
9991 struct http_txn *txn = l7;
9992 struct hdr_idx *idx = &txn->hdr_idx;
9993 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009994 const struct http_msg *msg;
9995 const char *hdr_name;
9996 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009997 int cnt;
9998 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009999 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010000
Willy Tarreau24e32d82012-04-23 23:55:44 +020010001 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010002 return 0;
10003
Willy Tarreaue333ec92012-04-16 16:26:40 +020010004 CHECK_HTTP_MESSAGE_FIRST();
10005
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010006 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010007 msg = &txn->req;
10008 hdr_name = "Cookie";
10009 hdr_name_len = 6;
10010 } else {
10011 msg = &txn->rsp;
10012 hdr_name = "Set-Cookie";
10013 hdr_name_len = 10;
10014 }
10015
Willy Tarreau9b28e032012-10-12 23:49:43 +020010016 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010017 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010018 ctx.idx = 0;
10019 cnt = 0;
10020
10021 while (1) {
10022 /* Note: val_beg == NULL every time we need to fetch a new header */
10023 if (!val_beg) {
10024 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10025 break;
10026
Willy Tarreau24e32d82012-04-23 23:55:44 +020010027 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010028 continue;
10029
10030 val_beg = ctx.line + ctx.val;
10031 val_end = val_beg + ctx.vlen;
10032 }
10033
Willy Tarreauf853c462012-04-23 18:53:56 +020010034 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010035 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010036 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010037 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010038 &smp->data.str.str,
10039 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010040 cnt++;
10041 }
10042 }
10043
Willy Tarreaub169eba2013-12-16 15:14:43 +010010044 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010045 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010046 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010047 return 1;
10048}
10049
Willy Tarreau51539362012-05-08 12:46:28 +020010050/* Fetch an cookie's integer value. The integer value is returned. It
10051 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10052 */
10053static int
10054smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010055 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010056{
Willy Tarreauef38c392013-07-22 16:29:32 +020010057 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010058
10059 if (ret > 0) {
10060 smp->type = SMP_T_UINT;
10061 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10062 }
10063
10064 return ret;
10065}
10066
Willy Tarreau8797c062007-05-07 00:55:35 +020010067/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010068/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010069/************************************************************************/
10070
David Cournapeau16023ee2010-12-23 20:55:41 +090010071/*
10072 * Given a path string and its length, find the position of beginning of the
10073 * query string. Returns NULL if no query string is found in the path.
10074 *
10075 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10076 *
10077 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10078 */
bedis4c75cca2012-10-05 08:38:24 +020010079static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010080{
10081 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010082
bedis4c75cca2012-10-05 08:38:24 +020010083 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010084 return p ? p + 1 : NULL;
10085}
10086
bedis4c75cca2012-10-05 08:38:24 +020010087static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010088{
bedis4c75cca2012-10-05 08:38:24 +020010089 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010090}
10091
10092/*
10093 * Given a url parameter, find the starting position of the first occurence,
10094 * or NULL if the parameter is not found.
10095 *
10096 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10097 * the function will return query_string+8.
10098 */
10099static char*
10100find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010101 char* url_param_name, size_t url_param_name_l,
10102 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010103{
10104 char *pos, *last;
10105
10106 pos = query_string;
10107 last = query_string + query_string_l - url_param_name_l - 1;
10108
10109 while (pos <= last) {
10110 if (pos[url_param_name_l] == '=') {
10111 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10112 return pos;
10113 pos += url_param_name_l + 1;
10114 }
bedis4c75cca2012-10-05 08:38:24 +020010115 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010116 pos++;
10117 pos++;
10118 }
10119 return NULL;
10120}
10121
10122/*
10123 * Given a url parameter name, returns its value and size into *value and
10124 * *value_l respectively, and returns non-zero. If the parameter is not found,
10125 * zero is returned and value/value_l are not touched.
10126 */
10127static int
10128find_url_param_value(char* path, size_t path_l,
10129 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010130 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010131{
10132 char *query_string, *qs_end;
10133 char *arg_start;
10134 char *value_start, *value_end;
10135
bedis4c75cca2012-10-05 08:38:24 +020010136 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010137 if (!query_string)
10138 return 0;
10139
10140 qs_end = path + path_l;
10141 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010142 url_param_name, url_param_name_l,
10143 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010144 if (!arg_start)
10145 return 0;
10146
10147 value_start = arg_start + url_param_name_l + 1;
10148 value_end = value_start;
10149
bedis4c75cca2012-10-05 08:38:24 +020010150 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010151 value_end++;
10152
10153 *value = value_start;
10154 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010155 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010156}
10157
10158static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010159smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010160 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010161{
bedis4c75cca2012-10-05 08:38:24 +020010162 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010163 struct http_txn *txn = l7;
10164 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010165
bedis4c75cca2012-10-05 08:38:24 +020010166 if (!args || args[0].type != ARGT_STR ||
10167 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010168 return 0;
10169
10170 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010171
bedis4c75cca2012-10-05 08:38:24 +020010172 if (args[1].type)
10173 delim = *args[1].data.str.str;
10174
Willy Tarreau9b28e032012-10-12 23:49:43 +020010175 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010176 args->data.str.str, args->data.str.len,
10177 &smp->data.str.str, &smp->data.str.len,
10178 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010179 return 0;
10180
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010181 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010182 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010183 return 1;
10184}
10185
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010186/* Return the signed integer value for the specified url parameter (see url_param
10187 * above).
10188 */
10189static int
10190smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010191 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010192{
Willy Tarreauef38c392013-07-22 16:29:32 +020010193 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010194
10195 if (ret > 0) {
10196 smp->type = SMP_T_UINT;
10197 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10198 }
10199
10200 return ret;
10201}
10202
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010203/* This produces a 32-bit hash of the concatenation of the first occurrence of
10204 * the Host header followed by the path component if it begins with a slash ('/').
10205 * This means that '*' will not be added, resulting in exactly the first Host
10206 * entry. If no Host header is found, then the path is used. The resulting value
10207 * is hashed using the url hash followed by a full avalanche hash and provides a
10208 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10209 * high-traffic sites without having to store whole paths.
10210 * this differs from the base32 functions in that it includes the url parameters
10211 * as well as the path
10212 */
10213static int
10214smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010215 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010216{
10217 struct http_txn *txn = l7;
10218 struct hdr_ctx ctx;
10219 unsigned int hash = 0;
10220 char *ptr, *beg, *end;
10221 int len;
10222
10223 CHECK_HTTP_MESSAGE_FIRST();
10224
10225 ctx.idx = 0;
10226 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10227 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10228 ptr = ctx.line + ctx.val;
10229 len = ctx.vlen;
10230 while (len--)
10231 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10232 }
10233
10234 /* now retrieve the path */
10235 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10236 beg = http_get_path(txn);
10237 if (!beg)
10238 beg = end;
10239
10240 for (ptr = beg; ptr < end ; ptr++);
10241
10242 if (beg < ptr && *beg == '/') {
10243 while (beg < ptr)
10244 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10245 }
10246 hash = full_hash(hash);
10247
10248 smp->type = SMP_T_UINT;
10249 smp->data.uint = hash;
10250 smp->flags = SMP_F_VOL_1ST;
10251 return 1;
10252}
10253
10254/* This concatenates the source address with the 32-bit hash of the Host and
10255 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10256 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10257 * on the source address length. The URL hash is stored before the address so
10258 * that in environments where IPv6 is insignificant, truncating the output to
10259 * 8 bytes would still work.
10260 */
10261static int
10262smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010263 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010264{
10265 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010266 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010267
Willy Tarreaue155ec22013-11-18 18:33:22 +010010268 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010269 return 0;
10270
10271 temp = get_trash_chunk();
10272 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10273 temp->len += sizeof(smp->data.uint);
10274
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010275 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010276 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010277 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010278 temp->len += 4;
10279 break;
10280 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010281 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010282 temp->len += 16;
10283 break;
10284 default:
10285 return 0;
10286 }
10287
10288 smp->data.str = *temp;
10289 smp->type = SMP_T_BIN;
10290 return 1;
10291}
10292
Willy Tarreau185b5c42012-04-26 15:11:51 +020010293/* This function is used to validate the arguments passed to any "hdr" fetch
10294 * keyword. These keywords support an optional positive or negative occurrence
10295 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10296 * is assumed that the types are already the correct ones. Returns 0 on error,
10297 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10298 * error message in case of error, that the caller is responsible for freeing.
10299 * The initial location must either be freeable or NULL.
10300 */
10301static int val_hdr(struct arg *arg, char **err_msg)
10302{
10303 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010304 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010305 return 0;
10306 }
10307 return 1;
10308}
10309
Willy Tarreau276fae92013-07-25 14:36:01 +020010310/* takes an UINT value on input supposed to represent the time since EPOCH,
10311 * adds an optional offset found in args[0] and emits a string representing
10312 * the date in RFC-1123/5322 format.
10313 */
10314static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10315{
10316 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10317 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10318 struct chunk *temp;
10319 struct tm *tm;
10320 time_t curr_date = smp->data.uint;
10321
10322 /* add offset */
10323 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10324 curr_date += args[0].data.sint;
10325
10326 tm = gmtime(&curr_date);
10327
10328 temp = get_trash_chunk();
10329 temp->len = snprintf(temp->str, temp->size - temp->len,
10330 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10331 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10332 tm->tm_hour, tm->tm_min, tm->tm_sec);
10333
10334 smp->data.str = *temp;
10335 smp->type = SMP_T_STR;
10336 return 1;
10337}
10338
Willy Tarreau4a568972010-05-12 08:08:50 +020010339/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010340/* All supported ACL keywords must be declared here. */
10341/************************************************************************/
10342
10343/* Note: must not be declared <const> as its list will be overwritten.
10344 * Please take care of keeping this list alphabetically sorted.
10345 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010346static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010347 { "base", "base", pat_parse_str, pat_match_str },
10348 { "base_beg", "base", pat_parse_str, pat_match_beg },
10349 { "base_dir", "base", pat_parse_str, pat_match_dir },
10350 { "base_dom", "base", pat_parse_str, pat_match_dom },
10351 { "base_end", "base", pat_parse_str, pat_match_end },
10352 { "base_len", "base", pat_parse_int, pat_match_len },
10353 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10354 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010355
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010356 { "cook", "req.cook", pat_parse_str, pat_match_str },
10357 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10358 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10359 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10360 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10361 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10362 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10363 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010364
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010365 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10366 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10367 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10368 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10369 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10370 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10371 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10372 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010373
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010374 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010375
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010376 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010377
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010378 { "path", "path", pat_parse_str, pat_match_str },
10379 { "path_beg", "path", pat_parse_str, pat_match_beg },
10380 { "path_dir", "path", pat_parse_str, pat_match_dir },
10381 { "path_dom", "path", pat_parse_str, pat_match_dom },
10382 { "path_end", "path", pat_parse_str, pat_match_end },
10383 { "path_len", "path", pat_parse_int, pat_match_len },
10384 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10385 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010386
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010387 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10388 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010389
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010390 { "scook", "res.cook", pat_parse_str, pat_match_str },
10391 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10392 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10393 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10394 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10395 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10396 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10397 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010398
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010399 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10400 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10401 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10402 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10403 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10404 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10405 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10406 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010407
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010408 { "url", "url", pat_parse_str, pat_match_str },
10409 { "url_beg", "url", pat_parse_str, pat_match_beg },
10410 { "url_dir", "url", pat_parse_str, pat_match_dir },
10411 { "url_dom", "url", pat_parse_str, pat_match_dom },
10412 { "url_end", "url", pat_parse_str, pat_match_end },
10413 { "url_len", "url", pat_parse_int, pat_match_len },
10414 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10415 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010416
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010417 { "urlp", "urlp", pat_parse_str, pat_match_str },
10418 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10419 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10420 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10421 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10422 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10423 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10424 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010425
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010426 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010427}};
10428
10429/************************************************************************/
10430/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010431/************************************************************************/
10432/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010433static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010434 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10435 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10436 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10437
William Lallemand65ad6e12014-01-31 15:08:02 +010010438 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10439 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10440
William Lallemanda43ba4e2014-01-28 18:14:25 +010010441 /* capture are allocated and are permanent in the session */
10442 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10443 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRSHP },
10444
Willy Tarreau409bcde2013-01-08 00:31:00 +010010445 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10446 * are only here to match the ACL's name, are request-only and are used
10447 * for ACL compatibility only.
10448 */
10449 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10450 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10451 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10452 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10453
10454 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10455 * only here to match the ACL's name, are request-only and are used for
10456 * ACL compatibility only.
10457 */
10458 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10459 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10460 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10461 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10462
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010463 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10464 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010465 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10466 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10467 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010468
10469 /* HTTP protocol on the request path */
10470 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010471 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010472
10473 /* HTTP version on the request path */
10474 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010475 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010476
10477 /* HTTP version on the response path */
10478 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010479 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10480
Willy Tarreau18ed2562013-01-14 15:56:36 +010010481 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10482 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10483 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10484 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10485
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010486 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10487 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010488 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10489 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10490 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10491 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10492
10493 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10494 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10495 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10496 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10497
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010498 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10499 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010500 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10501 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10502 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10503 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10504
Willy Tarreau409bcde2013-01-08 00:31:00 +010010505 /* scook is valid only on the response and is used for ACL compatibility */
10506 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10507 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10508 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10509 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10510
10511 /* shdr is valid only on the response and is used for ACL compatibility */
10512 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10513 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10514 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10515 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10516
10517 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10518 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010519 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10520 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010521 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10522 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10523 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10524 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10525 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10526 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010527}};
10528
Willy Tarreau8797c062007-05-07 00:55:35 +020010529
Willy Tarreau276fae92013-07-25 14:36:01 +020010530/* Note: must not be declared <const> as its list will be overwritten */
10531static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10532 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10533 { NULL, NULL, 0, 0, 0 },
10534}};
10535
Willy Tarreau8797c062007-05-07 00:55:35 +020010536__attribute__((constructor))
10537static void __http_protocol_init(void)
10538{
10539 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010540 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010541 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010542}
10543
10544
Willy Tarreau58f10d72006-12-04 02:26:12 +010010545/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010546 * Local variables:
10547 * c-indent-level: 8
10548 * c-basic-offset: 8
10549 * End:
10550 */