blob: 1a8a6d92ff86a0d71b62025e2c5f489a86cb7826 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100238fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240#else
241#error "Check if your OS uses bitfields for fd_sets"
242#endif
243
Willy Tarreau80587432006-12-24 17:47:20 +0100244void init_proto_http()
245{
Willy Tarreau42250582007-04-01 01:30:43 +0200246 int i;
247 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100248 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200249
Willy Tarreau80587432006-12-24 17:47:20 +0100250 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
251 if (!http_err_msgs[msg]) {
252 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
253 abort();
254 }
255
256 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
257 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
258 }
Willy Tarreau42250582007-04-01 01:30:43 +0200259
260 /* initialize the log header encoding map : '{|}"#' should be encoded with
261 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
262 * URL encoding only requires '"', '#' to be encoded as well as non-
263 * printable characters above.
264 */
265 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
266 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200268 for (i = 0; i < 32; i++) {
269 FD_SET(i, hdr_encode_map);
270 FD_SET(i, url_encode_map);
271 }
272 for (i = 127; i < 256; i++) {
273 FD_SET(i, hdr_encode_map);
274 FD_SET(i, url_encode_map);
275 }
276
277 tmp = "\"#{|}";
278 while (*tmp) {
279 FD_SET(*tmp, hdr_encode_map);
280 tmp++;
281 }
282
283 tmp = "\"#";
284 while (*tmp) {
285 FD_SET(*tmp, url_encode_map);
286 tmp++;
287 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200288
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100289 /* initialize the http header encoding map. The draft httpbis define the
290 * header content as:
291 *
292 * HTTP-message = start-line
293 * *( header-field CRLF )
294 * CRLF
295 * [ message-body ]
296 * header-field = field-name ":" OWS field-value OWS
297 * field-value = *( field-content / obs-fold )
298 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
299 * obs-fold = CRLF 1*( SP / HTAB )
300 * field-vchar = VCHAR / obs-text
301 * VCHAR = %x21-7E
302 * obs-text = %x80-FF
303 *
304 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
305 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
306 * "obs-fold" is volontary forgotten because haproxy remove this.
307 */
308 memset(http_encode_map, 0, sizeof(http_encode_map));
309 for (i = 0x00; i <= 0x08; i++)
310 FD_SET(i, http_encode_map);
311 for (i = 0x0a; i <= 0x1f; i++)
312 FD_SET(i, http_encode_map);
313 FD_SET(0x7f, http_encode_map);
314
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200315 /* memory allocations */
316 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100317 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100318}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319
Willy Tarreau53b6c742006-12-17 13:37:46 +0100320/*
321 * We have 26 list of methods (1 per first letter), each of which can have
322 * up to 3 entries (2 valid, 1 null).
323 */
324struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100325 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100326 int len;
327 const char text[8];
328};
329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100330const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331 ['C' - 'A'] = {
332 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
333 },
334 ['D' - 'A'] = {
335 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
336 },
337 ['G' - 'A'] = {
338 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
339 },
340 ['H' - 'A'] = {
341 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
342 },
343 ['P' - 'A'] = {
344 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
345 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
346 },
347 ['T' - 'A'] = {
348 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
349 },
350 /* rest is empty like this :
351 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
352 */
353};
354
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100355const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
356 [HTTP_METH_NONE] = { "", 0 },
357 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
358 [HTTP_METH_GET] = { "GET", 3 },
359 [HTTP_METH_HEAD] = { "HEAD", 4 },
360 [HTTP_METH_POST] = { "POST", 4 },
361 [HTTP_METH_PUT] = { "PUT", 3 },
362 [HTTP_METH_DELETE] = { "DELETE", 6 },
363 [HTTP_METH_TRACE] = { "TRACE", 5 },
364 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
365};
366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100367/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100369 * RFC2616 for those chars.
370 */
371
372const char http_is_spht[256] = {
373 [' '] = 1, ['\t'] = 1,
374};
375
376const char http_is_crlf[256] = {
377 ['\r'] = 1, ['\n'] = 1,
378};
379
380const char http_is_lws[256] = {
381 [' '] = 1, ['\t'] = 1,
382 ['\r'] = 1, ['\n'] = 1,
383};
384
385const char http_is_sep[256] = {
386 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
387 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
388 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
389 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
390 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
391};
392
393const char http_is_ctl[256] = {
394 [0 ... 31] = 1,
395 [127] = 1,
396};
397
398/*
399 * A token is any ASCII char that is neither a separator nor a CTL char.
400 * Do not overwrite values in assignment since gcc-2.95 will not handle
401 * them correctly. Instead, define every non-CTL char's status.
402 */
403const char http_is_token[256] = {
404 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
405 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
406 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
407 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
408 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
409 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
410 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
411 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
412 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
413 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
414 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
415 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
416 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
417 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
418 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
419 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
420 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
421 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
422 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
423 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
424 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
425 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
426 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
427 ['|'] = 1, ['}'] = 0, ['~'] = 1,
428};
429
430
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100431/*
432 * An http ver_token is any ASCII which can be found in an HTTP version,
433 * which includes 'H', 'T', 'P', '/', '.' and any digit.
434 */
435const char http_is_ver_token[256] = {
436 ['.'] = 1, ['/'] = 1,
437 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
438 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
439 ['H'] = 1, ['P'] = 1, ['T'] = 1,
440};
441
442
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100444 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
445 */
446#if defined(DEBUG_FSM)
447static void http_silent_debug(int line, struct session *s)
448{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100449 chunk_printf(&trash,
450 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
451 line,
452 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
453 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
454 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100455
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100456 chunk_printf(&trash,
457 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
458 line,
459 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
460 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
461 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100462}
463#else
464#define http_silent_debug(l,s) do { } while (0)
465#endif
466
467/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100468 * Adds a header and its CRLF at the tail of the message's buffer, just before
469 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100470 * The header is also automatically added to the index <hdr_idx>, and the end
471 * of headers is automatically adjusted. The number of bytes added is returned
472 * on success, otherwise <0 is returned indicating an error.
473 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100474int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475{
476 int bytes, len;
477
478 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200479 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100480 if (!bytes)
481 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100482 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
484}
485
486/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100487 * Adds a header and its CRLF at the tail of the message's buffer, just before
488 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 * the buffer is only opened and the space reserved, but nothing is copied.
490 * The header is also automatically added to the index <hdr_idx>, and the end
491 * of headers is automatically adjusted. The number of bytes added is returned
492 * on success, otherwise <0 is returned indicating an error.
493 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100494int http_header_add_tail2(struct http_msg *msg,
495 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100496{
497 int bytes;
498
Willy Tarreau9b28e032012-10-12 23:49:43 +0200499 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100500 if (!bytes)
501 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100502 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100503 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
504}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505
506/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100507 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
508 * If so, returns the position of the first non-space character relative to
509 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
510 * to return a pointer to the place after the first space. Returns 0 if the
511 * header name does not match. Checks are case-insensitive.
512 */
513int http_header_match2(const char *hdr, const char *end,
514 const char *name, int len)
515{
516 const char *val;
517
518 if (hdr + len >= end)
519 return 0;
520 if (hdr[len] != ':')
521 return 0;
522 if (strncasecmp(hdr, name, len) != 0)
523 return 0;
524 val = hdr + len + 1;
525 while (val < end && HTTP_IS_SPHT(*val))
526 val++;
527 if ((val >= end) && (len + 2 <= end - hdr))
528 return len + 2; /* we may replace starting from second space */
529 return val - hdr;
530}
531
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200532/* Find the first or next occurrence of header <name> in message buffer <sol>
533 * using headers index <idx>, and return it in the <ctx> structure. This
534 * structure holds everything necessary to use the header and find next
535 * occurrence. If its <idx> member is 0, the header is searched from the
536 * beginning. Otherwise, the next occurrence is returned. The function returns
537 * 1 when it finds a value, and 0 when there is no more. It is very similar to
538 * http_find_header2() except that it is designed to work with full-line headers
539 * whose comma is not a delimiter but is part of the syntax. As a special case,
540 * if ctx->val is NULL when searching for a new values of a header, the current
541 * header is rescanned. This allows rescanning after a header deletion.
542 */
543int http_find_full_header2(const char *name, int len,
544 char *sol, struct hdr_idx *idx,
545 struct hdr_ctx *ctx)
546{
547 char *eol, *sov;
548 int cur_idx, old_idx;
549
550 cur_idx = ctx->idx;
551 if (cur_idx) {
552 /* We have previously returned a header, let's search another one */
553 sol = ctx->line;
554 eol = sol + idx->v[cur_idx].len;
555 goto next_hdr;
556 }
557
558 /* first request for this header */
559 sol += hdr_idx_first_pos(idx);
560 old_idx = 0;
561 cur_idx = hdr_idx_first_idx(idx);
562 while (cur_idx) {
563 eol = sol + idx->v[cur_idx].len;
564
565 if (len == 0) {
566 /* No argument was passed, we want any header.
567 * To achieve this, we simply build a fake request. */
568 while (sol + len < eol && sol[len] != ':')
569 len++;
570 name = sol;
571 }
572
573 if ((len < eol - sol) &&
574 (sol[len] == ':') &&
575 (strncasecmp(sol, name, len) == 0)) {
576 ctx->del = len;
577 sov = sol + len + 1;
578 while (sov < eol && http_is_lws[(unsigned char)*sov])
579 sov++;
580
581 ctx->line = sol;
582 ctx->prev = old_idx;
583 ctx->idx = cur_idx;
584 ctx->val = sov - sol;
585 ctx->tws = 0;
586 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
587 eol--;
588 ctx->tws++;
589 }
590 ctx->vlen = eol - sov;
591 return 1;
592 }
593 next_hdr:
594 sol = eol + idx->v[cur_idx].cr + 1;
595 old_idx = cur_idx;
596 cur_idx = idx->v[cur_idx].next;
597 }
598 return 0;
599}
600
Willy Tarreau68085d82010-01-18 14:54:04 +0100601/* Find the end of the header value contained between <s> and <e>. See RFC2616,
602 * par 2.2 for more information. Note that it requires a valid header to return
603 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100605char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200606{
607 int quoted, qdpair;
608
609 quoted = qdpair = 0;
610 for (; s < e; s++) {
611 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200612 else if (quoted) {
613 if (*s == '\\') qdpair = 1;
614 else if (*s == '"') quoted = 0;
615 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 else if (*s == '"') quoted = 1;
617 else if (*s == ',') return s;
618 }
619 return s;
620}
621
622/* Find the first or next occurrence of header <name> in message buffer <sol>
623 * using headers index <idx>, and return it in the <ctx> structure. This
624 * structure holds everything necessary to use the header and find next
625 * occurrence. If its <idx> member is 0, the header is searched from the
626 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 * 1 when it finds a value, and 0 when there is no more. It is designed to work
628 * with headers defined as comma-separated lists. As a special case, if ctx->val
629 * is NULL when searching for a new values of a header, the current header is
630 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200631 */
632int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200634 struct hdr_ctx *ctx)
635{
Willy Tarreau68085d82010-01-18 14:54:04 +0100636 char *eol, *sov;
637 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 cur_idx = ctx->idx;
640 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200641 /* We have previously returned a value, let's search
642 * another one on the same line.
643 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200644 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200645 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100646 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200647 eol = sol + idx->v[cur_idx].len;
648
649 if (sov >= eol)
650 /* no more values in this header */
651 goto next_hdr;
652
Willy Tarreau68085d82010-01-18 14:54:04 +0100653 /* values remaining for this header, skip the comma but save it
654 * for later use (eg: for header deletion).
655 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 sov++;
657 while (sov < eol && http_is_lws[(unsigned char)*sov])
658 sov++;
659
660 goto return_hdr;
661 }
662
663 /* first request for this header */
664 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100665 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200666 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 while (cur_idx) {
668 eol = sol + idx->v[cur_idx].len;
669
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200670 if (len == 0) {
671 /* No argument was passed, we want any header.
672 * To achieve this, we simply build a fake request. */
673 while (sol + len < eol && sol[len] != ':')
674 len++;
675 name = sol;
676 }
677
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 if ((len < eol - sol) &&
679 (sol[len] == ':') &&
680 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100681 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200682 sov = sol + len + 1;
683 while (sov < eol && http_is_lws[(unsigned char)*sov])
684 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100685
Willy Tarreau33a7e692007-06-10 19:45:56 +0200686 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100687 ctx->prev = old_idx;
688 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200689 ctx->idx = cur_idx;
690 ctx->val = sov - sol;
691
692 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200693 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200694 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 eol--;
696 ctx->tws++;
697 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200698 ctx->vlen = eol - sov;
699 return 1;
700 }
701 next_hdr:
702 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100703 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200704 cur_idx = idx->v[cur_idx].next;
705 }
706 return 0;
707}
708
709int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100710 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200711 struct hdr_ctx *ctx)
712{
713 return http_find_header2(name, strlen(name), sol, idx, ctx);
714}
715
Willy Tarreau68085d82010-01-18 14:54:04 +0100716/* Remove one value of a header. This only works on a <ctx> returned by one of
717 * the http_find_header functions. The value is removed, as well as surrounding
718 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100719 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100720 * message <msg>. The new index is returned. If it is zero, it means there is
721 * no more header, so any processing may stop. The ctx is always left in a form
722 * that can be handled by http_find_header2() to find next occurrence.
723 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100724int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100725{
726 int cur_idx = ctx->idx;
727 char *sol = ctx->line;
728 struct hdr_idx_elem *hdr;
729 int delta, skip_comma;
730
731 if (!cur_idx)
732 return 0;
733
734 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200735 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100736 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200737 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100738 http_msg_move_end(msg, delta);
739 idx->used--;
740 hdr->len = 0; /* unused entry */
741 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100742 if (idx->tail == ctx->idx)
743 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100744 ctx->idx = ctx->prev; /* walk back to the end of previous header */
745 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
746 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 return ctx->idx;
749 }
750
751 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
753 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100754 */
755
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200756 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200757 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200758 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 NULL, 0);
760 hdr->len += delta;
761 http_msg_move_end(msg, delta);
762 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200763 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100764 return ctx->idx;
765}
766
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100767/* This function handles a server error at the stream interface level. The
768 * stream interface is assumed to be already in a closed state. An optional
769 * message is copied into the input buffer, and an HTTP status code stored.
770 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100771 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100773static void http_server_error(struct session *t, struct stream_interface *si,
774 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200776 channel_auto_read(si->ob);
777 channel_abort(si->ob);
778 channel_auto_close(si->ob);
779 channel_erase(si->ob);
780 channel_auto_close(si->ib);
781 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100782 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100783 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200784 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 }
786 if (!(t->flags & SN_ERR_MASK))
787 t->flags |= err;
788 if (!(t->flags & SN_FINST_MASK))
789 t->flags |= finst;
790}
791
Willy Tarreau80587432006-12-24 17:47:20 +0100792/* This function returns the appropriate error location for the given session
793 * and message.
794 */
795
Willy Tarreau783f2582012-09-04 12:19:04 +0200796struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100797{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 if (s->be->errmsg[msgnum].str)
799 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100800 else if (s->fe->errmsg[msgnum].str)
801 return &s->fe->errmsg[msgnum];
802 else
803 return &http_err_chunks[msgnum];
804}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805
Willy Tarreau53b6c742006-12-17 13:37:46 +0100806/*
807 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
808 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
809 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100810enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811{
812 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100813 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100814
815 m = ((unsigned)*str - 'A');
816
817 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100818 for (h = http_methods[m]; h->len > 0; h++) {
819 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100820 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100821 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100822 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100823 };
824 return HTTP_METH_OTHER;
825 }
826 return HTTP_METH_NONE;
827
828}
829
Willy Tarreau21d2af32008-02-14 20:25:24 +0100830/* Parse the URI from the given transaction (which is assumed to be in request
831 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
832 * It is returned otherwise.
833 */
834static char *
835http_get_path(struct http_txn *txn)
836{
837 char *ptr, *end;
838
Willy Tarreau9b28e032012-10-12 23:49:43 +0200839 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100840 end = ptr + txn->req.sl.rq.u_l;
841
842 if (ptr >= end)
843 return NULL;
844
845 /* RFC2616, par. 5.1.2 :
846 * Request-URI = "*" | absuri | abspath | authority
847 */
848
849 if (*ptr == '*')
850 return NULL;
851
852 if (isalpha((unsigned char)*ptr)) {
853 /* this is a scheme as described by RFC3986, par. 3.1 */
854 ptr++;
855 while (ptr < end &&
856 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
857 ptr++;
858 /* skip '://' */
859 if (ptr == end || *ptr++ != ':')
860 return NULL;
861 if (ptr == end || *ptr++ != '/')
862 return NULL;
863 if (ptr == end || *ptr++ != '/')
864 return NULL;
865 }
866 /* skip [user[:passwd]@]host[:[port]] */
867
868 while (ptr < end && *ptr != '/')
869 ptr++;
870
871 if (ptr == end)
872 return NULL;
873
874 /* OK, we got the '/' ! */
875 return ptr;
876}
877
William Lallemand65ad6e12014-01-31 15:08:02 +0100878/* Parse the URI from the given string and look for the "/" beginning the PATH.
879 * If not found, return NULL. It is returned otherwise.
880 */
881static char *
882http_get_path_from_string(char *str)
883{
884 char *ptr = str;
885
886 /* RFC2616, par. 5.1.2 :
887 * Request-URI = "*" | absuri | abspath | authority
888 */
889
890 if (*ptr == '*')
891 return NULL;
892
893 if (isalpha((unsigned char)*ptr)) {
894 /* this is a scheme as described by RFC3986, par. 3.1 */
895 ptr++;
896 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
897 ptr++;
898 /* skip '://' */
899 if (*ptr == '\0' || *ptr++ != ':')
900 return NULL;
901 if (*ptr == '\0' || *ptr++ != '/')
902 return NULL;
903 if (*ptr == '\0' || *ptr++ != '/')
904 return NULL;
905 }
906 /* skip [user[:passwd]@]host[:[port]] */
907
908 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
909 ptr++;
910
911 if (*ptr == '\0' || *ptr == ' ')
912 return NULL;
913
914 /* OK, we got the '/' ! */
915 return ptr;
916}
917
Willy Tarreau71241ab2012-12-27 11:30:54 +0100918/* Returns a 302 for a redirectable request that reaches a server working in
919 * in redirect mode. This may only be called just after the stream interface
920 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
921 * follow normal proxy processing. NOTE: this function is designed to support
922 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100924void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925{
926 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100927 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200929 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930
931 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 trash.len = strlen(HTTP_302);
933 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100935 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100936
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100938 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 return;
940
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100941 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100942 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100943 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
944 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100945 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100946
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200947 /* 3: add the request URI. Since it was already forwarded, we need
948 * to temporarily rewind the buffer.
949 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200951 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200952
Willy Tarreauefb453c2008-10-26 20:49:47 +0100953 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200954 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200955
Willy Tarreau9b28e032012-10-12 23:49:43 +0200956 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200957
Willy Tarreauefb453c2008-10-26 20:49:47 +0100958 if (!path)
959 return;
960
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100961 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100962 return;
963
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100964 memcpy(trash.str + trash.len, path, len);
965 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100966
967 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100968 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
969 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100970 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100971 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
972 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100973 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100974
975 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200976 si_shutr(si);
977 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 si->state = SI_ST_CLO;
980
981 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200982 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100985 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500986 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987}
988
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 * that the server side is closed. Note that err_type is actually a
991 * bitmask, where almost only aborts may be cumulated with other
992 * values. We consider that aborted operations are more important
993 * than timeouts or errors due to the fact that nobody else in the
994 * logs might explain incomplete retries. All others should avoid
995 * being cumulated. It should normally not be possible to have multiple
996 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100997 * Note that connection errors appearing on the second request of a keep-alive
998 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001000void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001001{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001002 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003
1004 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001005 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001006 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001008 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001009 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1010 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001012 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001013 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001014 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001015 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001016 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001017 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001019 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1020 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001022 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001023 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001024 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001025 else if (err_type & SI_ET_CONN_RES)
1026 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001027 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1028 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001029 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001030 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001031 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032}
1033
Willy Tarreau42250582007-04-01 01:30:43 +02001034extern const char sess_term_cond[8];
1035extern const char sess_fin_state[8];
1036extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001037struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001038struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001039struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040
Willy Tarreau117f59e2007-03-04 18:17:17 +01001041/*
1042 * Capture headers from message starting at <som> according to header list
1043 * <cap_hdr>, and fill the <idx> structure appropriately.
1044 */
1045void capture_headers(char *som, struct hdr_idx *idx,
1046 char **cap, struct cap_hdr *cap_hdr)
1047{
1048 char *eol, *sol, *col, *sov;
1049 int cur_idx;
1050 struct cap_hdr *h;
1051 int len;
1052
1053 sol = som + hdr_idx_first_pos(idx);
1054 cur_idx = hdr_idx_first_idx(idx);
1055
1056 while (cur_idx) {
1057 eol = sol + idx->v[cur_idx].len;
1058
1059 col = sol;
1060 while (col < eol && *col != ':')
1061 col++;
1062
1063 sov = col + 1;
1064 while (sov < eol && http_is_lws[(unsigned char)*sov])
1065 sov++;
1066
1067 for (h = cap_hdr; h; h = h->next) {
1068 if ((h->namelen == col - sol) &&
1069 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1070 if (cap[h->index] == NULL)
1071 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001072 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001073
1074 if (cap[h->index] == NULL) {
1075 Alert("HTTP capture : out of memory.\n");
1076 continue;
1077 }
1078
1079 len = eol - sov;
1080 if (len > h->len)
1081 len = h->len;
1082
1083 memcpy(cap[h->index], sov, len);
1084 cap[h->index][len]=0;
1085 }
1086 }
1087 sol = eol + idx->v[cur_idx].cr + 1;
1088 cur_idx = idx->v[cur_idx].next;
1089 }
1090}
1091
1092
Willy Tarreau42250582007-04-01 01:30:43 +02001093/* either we find an LF at <ptr> or we jump to <bad>.
1094 */
1095#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1096
1097/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1098 * otherwise to <http_msg_ood> with <state> set to <st>.
1099 */
1100#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1101 ptr++; \
1102 if (likely(ptr < end)) \
1103 goto good; \
1104 else { \
1105 state = (st); \
1106 goto http_msg_ood; \
1107 } \
1108 } while (0)
1109
1110
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001112 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1114 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1115 * will give undefined results.
1116 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1117 * and that msg->sol points to the beginning of the response.
1118 * If a complete line is found (which implies that at least one CR or LF is
1119 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1120 * returned indicating an incomplete line (which does not mean that parts have
1121 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1122 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1123 * upon next call.
1124 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001125 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1127 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001128 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001130const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001131 enum ht_state state, const char *ptr, const char *end,
1132 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001133{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001134 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001135
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001139 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1141
1142 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001143 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001144 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1145 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001146 state = HTTP_MSG_ERROR;
1147 break;
1148
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001152 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001153 goto http_msg_rpcode;
1154 }
1155 if (likely(HTTP_IS_SPHT(*ptr)))
1156 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1157 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001158 state = HTTP_MSG_ERROR;
1159 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001160
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001162 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001163 if (likely(!HTTP_IS_LWS(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1165
1166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001167 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1169 }
1170
1171 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001172 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 http_msg_rsp_reason:
1174 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001176 msg->sl.st.r_l = 0;
1177 goto http_msg_rpline_eol;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001182 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 goto http_msg_rpreason;
1184 }
1185 if (likely(HTTP_IS_SPHT(*ptr)))
1186 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1187 /* so it's a CR/LF, so there is no reason phrase */
1188 goto http_msg_rsp_reason;
1189
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 if (likely(!HTTP_IS_CRLF(*ptr)))
1193 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001194 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 http_msg_rpline_eol:
1196 /* We have seen the end of line. Note that we do not
1197 * necessarily have the \n yet, but at least we know that we
1198 * have EITHER \r OR \n, otherwise the response would not be
1199 * complete. We can then record the response length and return
1200 * to the caller which will be able to register it.
1201 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001202 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 return ptr;
1204
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001206#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001207 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1208 exit(1);
1209#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001210 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001211 }
1212
1213 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001214 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001215 if (ret_state)
1216 *ret_state = state;
1217 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001218 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001219 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001220}
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 * This function parses a request line between <ptr> and <end>, starting with
1224 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1225 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1226 * will give undefined results.
1227 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1228 * and that msg->sol points to the beginning of the request.
1229 * If a complete line is found (which implies that at least one CR or LF is
1230 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1231 * returned indicating an incomplete line (which does not mean that parts have
1232 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1233 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1234 * upon next call.
1235 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001236 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1238 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001239 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001241const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001242 enum ht_state state, const char *ptr, const char *end,
1243 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001245 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001249 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 if (likely(HTTP_IS_TOKEN(*ptr)))
1251 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001254 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 if (likely(HTTP_IS_CRLF(*ptr))) {
1259 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001260 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001262 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001264 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001266 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 msg->sl.rq.v_l = 0;
1268 goto http_msg_rqline_eol;
1269 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001270 state = HTTP_MSG_ERROR;
1271 break;
1272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001276 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 goto http_msg_rquri;
1278 }
1279 if (likely(HTTP_IS_SPHT(*ptr)))
1280 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1281 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1282 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001285 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001286 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001290 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1292 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001293
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001294 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001295 /* non-ASCII chars are forbidden unless option
1296 * accept-invalid-http-request is enabled in the frontend.
1297 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001298 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001299 if (msg->err_pos < -1)
1300 goto invalid_char;
1301 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1304 }
1305
1306 if (likely(HTTP_IS_CRLF(*ptr))) {
1307 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1308 goto http_msg_req09_uri_e;
1309 }
1310
1311 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001312 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001313 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001314 state = HTTP_MSG_ERROR;
1315 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001320 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 goto http_msg_rqver;
1322 }
1323 if (likely(HTTP_IS_SPHT(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1325 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1326 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001330 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332
1333 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001334 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001335 http_msg_rqline_eol:
1336 /* We have seen the end of line. Note that we do not
1337 * necessarily have the \n yet, but at least we know that we
1338 * have EITHER \r OR \n, otherwise the request would not be
1339 * complete. We can then record the request length and return
1340 * to the caller which will be able to register it.
1341 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001342 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001343 return ptr;
1344 }
1345
1346 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001347 state = HTTP_MSG_ERROR;
1348 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001351#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1353 exit(1);
1354#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001355 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001359 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 if (ret_state)
1361 *ret_state = state;
1362 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001363 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001367/*
1368 * Returns the data from Authorization header. Function may be called more
1369 * than once so data is stored in txn->auth_data. When no header is found
1370 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001371 * searching again for something we are unable to find anyway. However, if
1372 * the result if valid, the cache is not reused because we would risk to
1373 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001374 */
1375
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001376/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1377 * set according to global.tune.bufsize.
1378 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001379char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001380
1381int
1382get_http_auth(struct session *s)
1383{
1384
1385 struct http_txn *txn = &s->txn;
1386 struct chunk auth_method;
1387 struct hdr_ctx ctx;
1388 char *h, *p;
1389 int len;
1390
1391#ifdef DEBUG_AUTH
1392 printf("Auth for session %p: %d\n", s, txn->auth.method);
1393#endif
1394
1395 if (txn->auth.method == HTTP_AUTH_WRONG)
1396 return 0;
1397
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001398 txn->auth.method = HTTP_AUTH_WRONG;
1399
1400 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001401
1402 if (txn->flags & TX_USE_PX_CONN) {
1403 h = "Proxy-Authorization";
1404 len = strlen(h);
1405 } else {
1406 h = "Authorization";
1407 len = strlen(h);
1408 }
1409
Willy Tarreau9b28e032012-10-12 23:49:43 +02001410 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411 return 0;
1412
1413 h = ctx.line + ctx.val;
1414
1415 p = memchr(h, ' ', ctx.vlen);
1416 if (!p || p == h)
1417 return 0;
1418
1419 chunk_initlen(&auth_method, h, 0, p-h);
1420 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1421
1422 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1423
1424 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001425 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001426
1427 if (len < 0)
1428 return 0;
1429
1430
1431 get_http_auth_buff[len] = '\0';
1432
1433 p = strchr(get_http_auth_buff, ':');
1434
1435 if (!p)
1436 return 0;
1437
1438 txn->auth.user = get_http_auth_buff;
1439 *p = '\0';
1440 txn->auth.pass = p+1;
1441
1442 txn->auth.method = HTTP_AUTH_BASIC;
1443 return 1;
1444 }
1445
1446 return 0;
1447}
1448
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau8973c702007-01-21 23:58:29 +01001450/*
1451 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001452 * depending on the initial msg->msg_state. The caller is responsible for
1453 * ensuring that the message does not wrap. The function can be preempted
1454 * everywhere when data are missing and recalled at the exact same location
1455 * with no information loss. The message may even be realigned between two
1456 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001457 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001458 * fields. Note that msg->sol will be initialized after completing the first
1459 * state, so that none of the msg pointers has to be initialized prior to the
1460 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001462void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463{
Willy Tarreau3770f232013-12-07 00:01:53 +01001464 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001467
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001469 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 ptr = buf->p + msg->next;
1471 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (unlikely(ptr >= end))
1474 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 /*
1478 * First, states that are specific to the response only.
1479 * We check them first so that request and headers are
1480 * closer to each other (accessed more often).
1481 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001485 /* we have a start of message, but we have to check
1486 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001487 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001488 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001489 if (unlikely(ptr != buf->p)) {
1490 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001491 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001492 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001493 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 }
Willy Tarreau26927362012-05-18 23:22:52 +02001495 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001496 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 hdr_idx_init(idx);
1498 state = HTTP_MSG_RPVER;
1499 goto http_msg_rpver;
1500 }
1501
1502 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1503 goto http_msg_invalid;
1504
1505 if (unlikely(*ptr == '\n'))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1507 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1508 /* stop here */
1509
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001511 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001512 EXPECT_LF_HERE(ptr, http_msg_invalid);
1513 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1514 /* stop here */
1515
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001517 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 case HTTP_MSG_RPVER_SP:
1519 case HTTP_MSG_RPCODE:
1520 case HTTP_MSG_RPCODE_SP:
1521 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001522 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001523 state, ptr, end,
1524 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 if (unlikely(!ptr))
1526 return;
1527
1528 /* we have a full response and we know that we have either a CR
1529 * or an LF at <ptr>.
1530 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001531 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1532
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001533 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 if (likely(*ptr == '\r'))
1535 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1536 goto http_msg_rpline_end;
1537
Willy Tarreau8973c702007-01-21 23:58:29 +01001538 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001539 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001540 /* msg->sol must point to the first of CR or LF. */
1541 EXPECT_LF_HERE(ptr, http_msg_invalid);
1542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1543 /* stop here */
1544
1545 /*
1546 * Second, states that are specific to the request only
1547 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001549 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001551 /* we have a start of message, but we have to check
1552 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001553 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001554 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001555 if (likely(ptr != buf->p)) {
1556 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001557 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001558 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 }
Willy Tarreau26927362012-05-18 23:22:52 +02001561 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001562 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 state = HTTP_MSG_RQMETH;
1564 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1568 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 if (unlikely(*ptr == '\n'))
1571 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1572 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001573 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001579 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001582 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 case HTTP_MSG_RQMETH_SP:
1584 case HTTP_MSG_RQURI:
1585 case HTTP_MSG_RQURI_SP:
1586 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001587 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001588 state, ptr, end,
1589 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 if (unlikely(!ptr))
1591 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 /* we have a full request and we know that we have either a CR
1594 * or an LF at <ptr>.
1595 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001598 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 if (likely(*ptr == '\r'))
1600 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 /* check for HTTP/0.9 request : no version information available.
1606 * msg->sol must point to the first of CR or LF.
1607 */
1608 if (unlikely(msg->sl.rq.v_l == 0))
1609 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8973c702007-01-21 23:58:29 +01001615 /*
1616 * Common states below
1617 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001620 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 if (likely(!HTTP_IS_CRLF(*ptr))) {
1622 goto http_msg_hdr_name;
1623 }
1624
1625 if (likely(*ptr == '\r'))
1626 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1627 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001630 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 /* assumes msg->sol points to the first char */
1632 if (likely(HTTP_IS_TOKEN(*ptr)))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001634
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001635 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001638 if (likely(msg->err_pos < -1) || *ptr == '\n')
1639 goto http_msg_invalid;
1640
1641 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001642 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001643
1644 /* and we still accept this non-token character */
1645 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001648 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001649 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 if (likely(HTTP_IS_SPHT(*ptr)))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001654 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 if (likely(!HTTP_IS_CRLF(*ptr))) {
1657 goto http_msg_hdr_val;
1658 }
1659
1660 if (likely(*ptr == '\r'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1662 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001665 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 EXPECT_LF_HERE(ptr, http_msg_invalid);
1667 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 if (likely(HTTP_IS_SPHT(*ptr))) {
1672 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 for (; buf->p + msg->sov < ptr; msg->sov++)
1674 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 goto http_msg_hdr_l1_sp;
1676 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001677 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001678 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 goto http_msg_complete_header;
1680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001682 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001683 /* assumes msg->sol points to the first char, and msg->sov
1684 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 */
1686 if (likely(!HTTP_IS_CRLF(*ptr)))
1687 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001689 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 /* Note: we could also copy eol into ->eoh so that we have the
1691 * real header end in case it ends with lots of LWS, but is this
1692 * really needed ?
1693 */
1694 if (likely(*ptr == '\r'))
1695 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1696 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001699 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 EXPECT_LF_HERE(ptr, http_msg_invalid);
1701 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001704 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1706 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 for (; buf->p + msg->eol < ptr; msg->eol++)
1708 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 goto http_msg_hdr_val;
1710 }
1711 http_msg_complete_header:
1712 /*
1713 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001714 * Assumes msg->sol points to the first char, msg->sov points
1715 * to the first character of the value and msg->eol to the
1716 * first CR or LF so we know how the line ends. We insert last
1717 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001719 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 idx, idx->tail) < 0))
1721 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001722
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001723 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(!HTTP_IS_CRLF(*ptr))) {
1725 goto http_msg_hdr_name;
1726 }
1727
1728 if (likely(*ptr == '\r'))
1729 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1730 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001731
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001733 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 /* Assumes msg->sol points to the first of either CR or LF */
1735 EXPECT_LF_HERE(ptr, http_msg_invalid);
1736 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001737 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001738 msg->eoh = msg->sol;
1739 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001740 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001742
1743 case HTTP_MSG_ERROR:
1744 /* this may only happen if we call http_msg_analyser() twice with an error */
1745 break;
1746
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001748#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1750 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001751#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001752 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 }
1754 http_msg_ood:
1755 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001756 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001757 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 http_msg_invalid:
1761 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001762 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001763 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 return;
1765}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001766
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001767/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1768 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1769 * nothing is done and 1 is returned.
1770 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001771static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001772{
1773 int delta;
1774 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001775 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776
1777 if (msg->sl.rq.v_l != 0)
1778 return 1;
1779
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001780 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1781 if (txn->meth != HTTP_METH_GET)
1782 return 0;
1783
Willy Tarreau9b28e032012-10-12 23:49:43 +02001784 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001785 delta = 0;
1786
1787 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001788 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1789 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001790 }
1791 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001792 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001793 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001794 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001795 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001796 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001797 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001798 NULL, NULL);
1799 if (unlikely(!cur_end))
1800 return 0;
1801
1802 /* we have a full HTTP/1.0 request now and we know that
1803 * we have either a CR or an LF at <ptr>.
1804 */
1805 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1806 return 1;
1807}
1808
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001809/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001810 * and "keep-alive" values. If we already know that some headers may safely
1811 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001812 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1813 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001814 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001815 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1816 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1817 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001818 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001819 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001820void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001821{
Willy Tarreau5b154472009-12-21 20:11:07 +01001822 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001823 const char *hdr_val = "Connection";
1824 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001825
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001826 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 return;
1828
Willy Tarreau88d349d2010-01-25 12:15:43 +01001829 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1830 hdr_val = "Proxy-Connection";
1831 hdr_len = 16;
1832 }
1833
Willy Tarreau5b154472009-12-21 20:11:07 +01001834 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001835 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001836 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001837 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1838 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 if (to_del & 2)
1840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else
1842 txn->flags |= TX_CON_KAL_SET;
1843 }
1844 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1845 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001846 if (to_del & 1)
1847 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001848 else
1849 txn->flags |= TX_CON_CLO_SET;
1850 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001851 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1852 txn->flags |= TX_HDR_CONN_UPG;
1853 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001854 }
1855
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001856 txn->flags |= TX_HDR_CONN_PRS;
1857 return;
1858}
Willy Tarreau5b154472009-12-21 20:11:07 +01001859
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001860/* Apply desired changes on the Connection: header. Values may be removed and/or
1861 * added depending on the <wanted> flags, which are exclusively composed of
1862 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1863 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1864 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001865void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001866{
1867 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001868 const char *hdr_val = "Connection";
1869 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870
1871 ctx.idx = 0;
1872
Willy Tarreau88d349d2010-01-25 12:15:43 +01001873
1874 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1875 hdr_val = "Proxy-Connection";
1876 hdr_len = 16;
1877 }
1878
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001879 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001880 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001881 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1882 if (wanted & TX_CON_KAL_SET)
1883 txn->flags |= TX_CON_KAL_SET;
1884 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001885 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1888 if (wanted & TX_CON_CLO_SET)
1889 txn->flags |= TX_CON_CLO_SET;
1890 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001891 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001892 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001893 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001894
1895 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1896 return;
1897
1898 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1899 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001900 hdr_val = "Connection: close";
1901 hdr_len = 17;
1902 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1903 hdr_val = "Proxy-Connection: close";
1904 hdr_len = 23;
1905 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001906 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001907 }
1908
1909 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1910 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001911 hdr_val = "Connection: keep-alive";
1912 hdr_len = 22;
1913 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1914 hdr_val = "Proxy-Connection: keep-alive";
1915 hdr_len = 28;
1916 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001917 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001918 }
1919 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001920}
1921
Willy Tarreaua458b672012-03-05 11:17:50 +01001922/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001923 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001924 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001925 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001926 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001927 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001928static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001929{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001930 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001931 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001932 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001933 const char *end = buf->data + buf->size;
1934 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001935 unsigned int chunk = 0;
1936
1937 /* The chunk size is in the following form, though we are only
1938 * interested in the size and CRLF :
1939 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1940 */
1941 while (1) {
1942 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001943 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001944 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001945 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001946 if (c < 0) /* not a hex digit anymore */
1947 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001948 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001949 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001950 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001951 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001952 chunk = (chunk << 4) + c;
1953 }
1954
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001956 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001957 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958
1959 while (http_is_spht[(unsigned char)*ptr]) {
1960 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001961 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001962 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001963 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001964 }
1965
Willy Tarreaud98cf932009-12-27 22:54:55 +01001966 /* Up to there, we know that at least one byte is present at *ptr. Check
1967 * for the end of chunk size.
1968 */
1969 while (1) {
1970 if (likely(HTTP_IS_CRLF(*ptr))) {
1971 /* we now have a CR or an LF at ptr */
1972 if (likely(*ptr == '\r')) {
1973 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001974 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001975 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 return 0;
1977 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001978
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001980 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001982 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001983 /* done */
1984 break;
1985 }
1986 else if (*ptr == ';') {
1987 /* chunk extension, ends at next CRLF */
1988 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001989 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001990 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001991 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992
1993 while (!HTTP_IS_CRLF(*ptr)) {
1994 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001995 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001996 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 return 0;
1998 }
1999 /* we have a CRLF now, loop above */
2000 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002001 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002003 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 }
2005
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01002007 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002009 */
Willy Tarreau0161d622013-04-02 01:26:55 +02002010 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002011 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01002012 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002013 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002014 msg->chunk_len = chunk;
2015 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002016 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002017 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002018 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002019 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002020 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002021}
2022
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002023/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002024 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 * the trailers is found, it is automatically scheduled to be forwarded,
2026 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2027 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01002028 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01002029 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01002030 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01002031 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2032 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02002033 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01002034 * matches the length of trailers already parsed and not forwarded. It is also
2035 * important to note that this function is designed to be able to parse wrapped
2036 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002038static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002039{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002040 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002041
Willy Tarreaua458b672012-03-05 11:17:50 +01002042 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002043 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002044 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002045 const char *ptr = b_ptr(buf, msg->next);
2046 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002047 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048
2049 /* scan current line and stop at LF or CRLF */
2050 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002051 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002052 return 0;
2053
2054 if (*ptr == '\n') {
2055 if (!p1)
2056 p1 = ptr;
2057 p2 = ptr;
2058 break;
2059 }
2060
2061 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002062 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002063 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002064 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002065 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 p1 = ptr;
2067 }
2068
2069 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 if (ptr >= buf->data + buf->size)
2071 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072 }
2073
2074 /* after LF; point to beginning of next line */
2075 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002076 if (p2 >= buf->data + buf->size)
2077 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002078
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002079 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002080 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002081 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002082
2083 /* schedule this line for forwarding */
2084 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002085 if (msg->sov >= buf->size)
2086 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002087
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002088 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002089 /* LF/CRLF at beginning of line => end of trailers at p2.
2090 * Everything was scheduled for forwarding, there's nothing
2091 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002092 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002093 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002094 msg->msg_state = HTTP_MSG_DONE;
2095 return 1;
2096 }
2097 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099 }
2100}
2101
Willy Tarreau54d23df2012-10-25 19:04:45 +02002102/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002104 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002105 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2107 * not enough data are available, the function does not change anything and
2108 * returns zero. If a parse error is encountered, the function returns < 0 and
2109 * does not change anything. Note: this function is designed to parse wrapped
2110 * CRLF at the end of the buffer.
2111 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002112static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002114 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002115 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 int bytes;
2117
2118 /* NB: we'll check data availabilty at the end. It's not a
2119 * problem because whatever we match first will be checked
2120 * against the correct length.
2121 */
2122 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002123 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 if (*ptr == '\r') {
2125 bytes++;
2126 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002127 if (ptr >= buf->data + buf->size)
2128 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002129 }
2130
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002131 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002132 return 0;
2133
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002134 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002135 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002137 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002138
2139 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002140 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002142 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2143 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002144 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2146 return 1;
2147}
Willy Tarreau5b154472009-12-21 20:11:07 +01002148
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002149/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2150 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2151 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2152 * Unparsable qvalues return 1000 as "q=1.000".
2153 */
2154int parse_qvalue(const char *qvalue)
2155{
2156 int q = 1000;
2157
2158 if (!isdigit(*qvalue))
2159 goto out;
2160 q = (*qvalue++ - '0') * 1000;
2161
2162 if (*qvalue++ != '.')
2163 goto out;
2164
2165 if (!isdigit(*qvalue))
2166 goto out;
2167 q += (*qvalue++ - '0') * 100;
2168
2169 if (!isdigit(*qvalue))
2170 goto out;
2171 q += (*qvalue++ - '0') * 10;
2172
2173 if (!isdigit(*qvalue))
2174 goto out;
2175 q += (*qvalue++ - '0') * 1;
2176 out:
2177 if (q > 1000)
2178 q = 1000;
2179 return q;
2180}
William Lallemand82fe75c2012-10-23 10:25:10 +02002181
2182/*
2183 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002184 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002185int select_compression_request_header(struct session *s, struct buffer *req)
2186{
2187 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002188 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002189 struct hdr_ctx ctx;
2190 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002191 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002192
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002193 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2194 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002195 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2196 */
2197 ctx.idx = 0;
2198 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2199 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002200 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2201 (ctx.vlen < 31 ||
2202 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2203 ctx.line[ctx.val + 30] < '6' ||
2204 (ctx.line[ctx.val + 30] == '6' &&
2205 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2206 s->comp_algo = NULL;
2207 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002208 }
2209
William Lallemand82fe75c2012-10-23 10:25:10 +02002210 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002211 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002212 int best_q = 0;
2213
William Lallemand82fe75c2012-10-23 10:25:10 +02002214 ctx.idx = 0;
2215 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002216 const char *qval;
2217 int q;
2218 int toklen;
2219
2220 /* try to isolate the token from the optional q-value */
2221 toklen = 0;
2222 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2223 toklen++;
2224
2225 qval = ctx.line + ctx.val + toklen;
2226 while (1) {
2227 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2228 qval++;
2229
2230 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2231 qval = NULL;
2232 break;
2233 }
2234 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002235
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002236 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2237 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002238
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002239 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2240 qval = NULL;
2241 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002242 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002243 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2244 break;
2245
2246 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2247 qval++;
2248 }
2249
2250 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
2251 q = qval ? parse_qvalue(qval + 2) : 1000;
2252
2253 if (q <= best_q)
2254 continue;
2255
2256 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2257 if (*(ctx.line + ctx.val) == '*' ||
2258 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2259 s->comp_algo = comp_algo;
2260 best_q = q;
2261 break;
2262 }
2263 }
2264 }
2265 }
2266
2267 /* remove all occurrences of the header when "compression offload" is set */
2268 if (s->comp_algo) {
2269 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2270 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2271 ctx.idx = 0;
2272 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2273 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002274 }
2275 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002276 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002277 }
2278
2279 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002280 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2281 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002282 if (comp_algo->add_data == identity_add_data) {
2283 s->comp_algo = comp_algo;
2284 return 1;
2285 }
2286 }
2287 }
2288
2289 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002290 return 0;
2291}
2292
2293/*
2294 * Selects a comression algorithm depending of the server response.
2295 */
2296int select_compression_response_header(struct session *s, struct buffer *res)
2297{
2298 struct http_txn *txn = &s->txn;
2299 struct http_msg *msg = &txn->rsp;
2300 struct hdr_ctx ctx;
2301 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002302
2303 /* no common compression algorithm was found in request header */
2304 if (s->comp_algo == NULL)
2305 goto fail;
2306
2307 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002308 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002309 goto fail;
2310
William Lallemandd3002612012-11-26 14:34:47 +01002311 /* 200 only */
2312 if (txn->status != 200)
2313 goto fail;
2314
William Lallemand82fe75c2012-10-23 10:25:10 +02002315 /* Content-Length is null */
2316 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2317 goto fail;
2318
Willy Tarreau667c2a32013-04-09 08:13:58 +02002319 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2320 if (msg->flags & HTTP_MSGF_TE_CHNK)
2321 goto fail;
2322
William Lallemand82fe75c2012-10-23 10:25:10 +02002323 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002324 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002325 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2326 goto fail;
2327
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002328 /* no compression when Cache-Control: no-transform is present in the message */
2329 ctx.idx = 0;
2330 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2331 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2332 goto fail;
2333 }
2334
William Lallemand82fe75c2012-10-23 10:25:10 +02002335 comp_type = NULL;
2336
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002337 /* we don't want to compress multipart content-types, nor content-types that are
2338 * not listed in the "compression type" directive if any. If no content-type was
2339 * found but configuration requires one, we don't compress either. Backend has
2340 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002341 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002342 ctx.idx = 0;
2343 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2344 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2345 goto fail;
2346
2347 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2348 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002349 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002350 if (ctx.vlen >= comp_type->name_len &&
2351 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002352 /* this Content-Type should be compressed */
2353 break;
2354 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002355 /* this Content-Type should not be compressed */
2356 if (comp_type == NULL)
2357 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002358 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002359 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002360 else { /* no content-type header */
2361 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2362 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002363 }
2364
William Lallemandd85f9172012-11-09 17:05:39 +01002365 /* limit compression rate */
2366 if (global.comp_rate_lim > 0)
2367 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2368 goto fail;
2369
William Lallemand072a2bf2012-11-20 17:01:01 +01002370 /* limit cpu usage */
2371 if (idle_pct < compress_min_idle)
2372 goto fail;
2373
William Lallemand4c49fae2012-11-07 15:00:23 +01002374 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002375 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002376 goto fail;
2377
William Lallemandec3e3892012-11-12 17:02:18 +01002378 s->flags |= SN_COMP_READY;
2379
William Lallemand82fe75c2012-10-23 10:25:10 +02002380 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002381 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002382 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2383 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2384
2385 /* add Transfer-Encoding header */
2386 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2387 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2388
2389 /*
2390 * Add Content-Encoding header when it's not identity encoding.
2391 * RFC 2616 : Identity encoding: This content-coding is used only in the
2392 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2393 * header.
2394 */
2395 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002396 trash.len = 18;
2397 memcpy(trash.str, "Content-Encoding: ", trash.len);
2398 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2399 trash.len += s->comp_algo->name_len;
2400 trash.str[trash.len] = '\0';
2401 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002403 return 1;
2404
2405fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002406 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002407 return 0;
2408}
2409
2410
Willy Tarreaud787e662009-07-07 10:14:51 +02002411/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2412 * processing can continue on next analysers, or zero if it either needs more
2413 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2414 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2415 * when it has nothing left to do, and may remove any analyser when it wants to
2416 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002417 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002418int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002419{
Willy Tarreau59234e92008-11-30 23:51:27 +01002420 /*
2421 * We will parse the partial (or complete) lines.
2422 * We will check the request syntax, and also join multi-line
2423 * headers. An index of all the lines will be elaborated while
2424 * parsing.
2425 *
2426 * For the parsing, we use a 28 states FSM.
2427 *
2428 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002429 * req->buf->p = beginning of request
2430 * req->buf->p + msg->eoh = end of processed headers / start of current one
2431 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002432 * msg->eol = end of current header or line (LF or CRLF)
2433 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002434 *
2435 * At end of parsing, we may perform a capture of the error (if any), and
2436 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2437 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2438 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002440
Willy Tarreau59234e92008-11-30 23:51:27 +01002441 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002443 struct http_txn *txn = &s->txn;
2444 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002445 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002446
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002447 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 +01002448 now_ms, __FUNCTION__,
2449 s,
2450 req,
2451 req->rex, req->wex,
2452 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002453 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002454 req->analysers);
2455
Willy Tarreau52a0c602009-08-16 22:45:38 +02002456 /* we're speaking HTTP here, so let's speak HTTP to the client */
2457 s->srv_error = http_return_srv_error;
2458
Willy Tarreau83e3af02009-12-28 17:39:57 +01002459 /* There's a protected area at the end of the buffer for rewriting
2460 * purposes. We don't want to start to parse the request if the
2461 * protected area is affected, because we may have to move processed
2462 * data later, which is much more complicated.
2463 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002464 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002465 if (txn->flags & TX_NOT_FIRST) {
2466 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002467 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002468 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002469 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002470 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002471 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002472 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002473 return 0;
2474 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002475 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2476 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2477 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002478 }
2479
Willy Tarreau065e8332010-01-08 00:30:20 +01002480 /* Note that we have the same problem with the response ; we
2481 * may want to send a redirect, error or anything which requires
2482 * some spare space. So we'll ensure that we have at least
2483 * maxrewrite bytes available in the response buffer before
2484 * processing that one. This will only affect pipelined
2485 * keep-alive requests.
2486 */
2487 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002488 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002489 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2490 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2491 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002492 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002493 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002494 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002495 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002496 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002497 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002498 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002499 return 0;
2500 }
2501 }
2502
Willy Tarreau9b28e032012-10-12 23:49:43 +02002503 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002504 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002505 }
2506
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 /* 1: we might have to print this header in debug mode */
2508 if (unlikely((global.mode & MODE_DEBUG) &&
2509 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002510 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002512
Willy Tarreau9b28e032012-10-12 23:49:43 +02002513 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002514 /* this is a bit complex : in case of error on the request line,
2515 * we know that rq.l is still zero, so we display only the part
2516 * up to the end of the line (truncated by debug_hdr).
2517 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002518 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002520
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 sol += hdr_idx_first_pos(&txn->hdr_idx);
2522 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 while (cur_idx) {
2525 eol = sol + txn->hdr_idx.v[cur_idx].len;
2526 debug_hdr("clihdr", s, sol, eol);
2527 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2528 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002529 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 }
2531
Willy Tarreau58f10d72006-12-04 02:26:12 +01002532
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 /*
2534 * Now we quickly check if we have found a full valid request.
2535 * If not so, we check the FD and buffer states before leaving.
2536 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002537 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002538 * requests are checked first. When waiting for a second request
2539 * on a keep-alive session, if we encounter and error, close, t/o,
2540 * we note the error in the session flags but don't set any state.
2541 * Since the error will be noted there, it will not be counted by
2542 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002543 * Last, we may increase some tracked counters' http request errors on
2544 * the cases that are deliberately the client's fault. For instance,
2545 * a timeout or connection reset is not counted as an error. However
2546 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002548
Willy Tarreau655dce92009-11-08 13:10:58 +01002549 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002550 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002552 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002553 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002554 session_inc_http_req_ctr(s);
2555 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002556 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002558 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002559
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 /* 1: Since we are in header mode, if there's no space
2561 * left for headers, we won't be able to free more
2562 * later, so the session will never terminate. We
2563 * must terminate it now.
2564 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002565 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 /* FIXME: check if URI is set and return Status
2567 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002568 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002569 session_inc_http_req_ctr(s);
2570 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002571 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002572 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002573 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002574 goto return_bad_req;
2575 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002576
Willy Tarreau59234e92008-11-30 23:51:27 +01002577 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002578 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002579 if (!(s->flags & SN_ERR_MASK))
2580 s->flags |= SN_ERR_CLICL;
2581
Willy Tarreaufcffa692010-01-10 14:21:19 +01002582 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002583 goto failed_keep_alive;
2584
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002586 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002587 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002588 session_inc_http_err_ctr(s);
2589 }
2590
Willy Tarreaudc979f22012-12-04 10:39:01 +01002591 txn->status = 400;
2592 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 msg->msg_state = HTTP_MSG_ERROR;
2594 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002595
Willy Tarreauda7ff642010-06-23 11:44:09 +02002596 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002597 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002598 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002599 if (s->listener->counters)
2600 s->listener->counters->failed_req++;
2601
Willy Tarreau59234e92008-11-30 23:51:27 +01002602 if (!(s->flags & SN_FINST_MASK))
2603 s->flags |= SN_FINST_R;
2604 return 0;
2605 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002606
Willy Tarreau59234e92008-11-30 23:51:27 +01002607 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002608 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002609 if (!(s->flags & SN_ERR_MASK))
2610 s->flags |= SN_ERR_CLITO;
2611
Willy Tarreaufcffa692010-01-10 14:21:19 +01002612 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002613 goto failed_keep_alive;
2614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002616 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002617 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002618 session_inc_http_err_ctr(s);
2619 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002621 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 msg->msg_state = HTTP_MSG_ERROR;
2623 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002624
Willy Tarreauda7ff642010-06-23 11:44:09 +02002625 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002626 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002627 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002628 if (s->listener->counters)
2629 s->listener->counters->failed_req++;
2630
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 if (!(s->flags & SN_FINST_MASK))
2632 s->flags |= SN_FINST_R;
2633 return 0;
2634 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002635
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002637 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002638 if (!(s->flags & SN_ERR_MASK))
2639 s->flags |= SN_ERR_CLICL;
2640
Willy Tarreaufcffa692010-01-10 14:21:19 +01002641 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002642 goto failed_keep_alive;
2643
Willy Tarreau4076a152009-04-02 15:18:36 +02002644 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002645 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002647 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 msg->msg_state = HTTP_MSG_ERROR;
2649 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002650
Willy Tarreauda7ff642010-06-23 11:44:09 +02002651 session_inc_http_err_ctr(s);
2652 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002653 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002654 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002655 if (s->listener->counters)
2656 s->listener->counters->failed_req++;
2657
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 if (!(s->flags & SN_FINST_MASK))
2659 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002660 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002661 }
2662
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002663 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002664 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2665 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002666#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002667 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 +01002668 /* We need more data, we have to re-enable quick-ack in case we
2669 * previously disabled it, otherwise we might cause the client
2670 * to delay next data.
2671 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002672 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002673 }
2674#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002675
Willy Tarreaufcffa692010-01-10 14:21:19 +01002676 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2677 /* If the client starts to talk, let's fall back to
2678 * request timeout processing.
2679 */
2680 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002681 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002682 }
2683
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002685 if (!tick_isset(req->analyse_exp)) {
2686 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2687 (txn->flags & TX_WAIT_NEXT_RQ) &&
2688 tick_isset(s->be->timeout.httpka))
2689 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2690 else
2691 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2692 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002693
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 /* we're not ready yet */
2695 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002696
2697 failed_keep_alive:
2698 /* Here we process low-level errors for keep-alive requests. In
2699 * short, if the request is not the first one and it experiences
2700 * a timeout, read error or shutdown, we just silently close so
2701 * that the client can try again.
2702 */
2703 txn->status = 0;
2704 msg->msg_state = HTTP_MSG_RQBEFORE;
2705 req->analysers = 0;
2706 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002707 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002708 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002709 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002710 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002712
Willy Tarreaud787e662009-07-07 10:14:51 +02002713 /* OK now we have a complete HTTP request with indexed headers. Let's
2714 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002716 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002717 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002718 * byte after the last LF. msg->sov points to the first byte of data.
2719 * msg->eol cannot be trusted because it may have been left uninitialized
2720 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002721 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002722
Willy Tarreauda7ff642010-06-23 11:44:09 +02002723 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002724 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2725
Willy Tarreaub16a5742010-01-10 14:46:16 +01002726 if (txn->flags & TX_WAIT_NEXT_RQ) {
2727 /* kill the pending keep-alive timeout */
2728 txn->flags &= ~TX_WAIT_NEXT_RQ;
2729 req->analyse_exp = TICK_ETERNITY;
2730 }
2731
2732
Willy Tarreaud787e662009-07-07 10:14:51 +02002733 /* Maybe we found in invalid header name while we were configured not
2734 * to block on that, so we have to capture it now.
2735 */
2736 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002737 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /*
2740 * 1: identify the method
2741 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002742 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002743
2744 /* we can make use of server redirect on GET and HEAD */
2745 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2746 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002747
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 /*
2749 * 2: check if the URI matches the monitor_uri.
2750 * We have to do this for every request which gets in, because
2751 * the monitor-uri is defined by the frontend.
2752 */
2753 if (unlikely((s->fe->monitor_uri_len != 0) &&
2754 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002755 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 s->fe->monitor_uri,
2757 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002758 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002760 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002762
Willy Tarreau59234e92008-11-30 23:51:27 +01002763 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002764 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002765
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002767 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002768 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 ret = acl_pass(ret);
2771 if (cond->pol == ACL_COND_UNLESS)
2772 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002773
Willy Tarreau59234e92008-11-30 23:51:27 +01002774 if (ret) {
2775 /* we fail this request, let's return 503 service unavail */
2776 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002777 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002778 if (!(s->flags & SN_ERR_MASK))
2779 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002780 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002781 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002782 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002783
Willy Tarreau59234e92008-11-30 23:51:27 +01002784 /* nothing to fail, let's reply normaly */
2785 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002786 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002787 if (!(s->flags & SN_ERR_MASK))
2788 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002789 goto return_prx_cond;
2790 }
2791
2792 /*
2793 * 3: Maybe we have to copy the original REQURI for the logs ?
2794 * Note: we cannot log anymore if the request has been
2795 * classified as invalid.
2796 */
2797 if (unlikely(s->logs.logwait & LW_REQ)) {
2798 /* we have a complete HTTP request that we must log */
2799 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2800 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002801
Willy Tarreau59234e92008-11-30 23:51:27 +01002802 if (urilen >= REQURI_LEN)
2803 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002804 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002806
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002807 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 s->do_log(s);
2809 } else {
2810 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002811 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 }
Willy Tarreau06619262006-12-17 08:37:22 +01002813
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002815 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002816 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002817
Willy Tarreau5b154472009-12-21 20:11:07 +01002818 /* ... and check if the request is HTTP/1.1 or above */
2819 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002820 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2821 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2822 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002823 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002824
2825 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002826 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 +01002827
Willy Tarreau88d349d2010-01-25 12:15:43 +01002828 /* if the frontend has "option http-use-proxy-header", we'll check if
2829 * we have what looks like a proxied connection instead of a connection,
2830 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2831 * Note that this is *not* RFC-compliant, however browsers and proxies
2832 * happen to do that despite being non-standard :-(
2833 * We consider that a request not beginning with either '/' or '*' is
2834 * a proxied connection, which covers both "scheme://location" and
2835 * CONNECT ip:port.
2836 */
2837 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002838 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002839 txn->flags |= TX_USE_PX_CONN;
2840
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002841 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002842 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002843
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002845 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002846 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002847 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002848
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002849 /* 6: determine the transfer-length.
2850 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2851 * the presence of a message-body in a REQUEST and its transfer length
2852 * must be determined that way (in order of precedence) :
2853 * 1. The presence of a message-body in a request is signaled by the
2854 * inclusion of a Content-Length or Transfer-Encoding header field
2855 * in the request's header fields. When a request message contains
2856 * both a message-body of non-zero length and a method that does
2857 * not define any semantics for that request message-body, then an
2858 * origin server SHOULD either ignore the message-body or respond
2859 * with an appropriate error message (e.g., 413). A proxy or
2860 * gateway, when presented the same request, SHOULD either forward
2861 * the request inbound with the message- body or ignore the
2862 * message-body when determining a response.
2863 *
2864 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2865 * and the "chunked" transfer-coding (Section 6.2) is used, the
2866 * transfer-length is defined by the use of this transfer-coding.
2867 * If a Transfer-Encoding header field is present and the "chunked"
2868 * transfer-coding is not present, the transfer-length is defined
2869 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002870 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002871 * 3. If a Content-Length header field is present, its decimal value in
2872 * OCTETs represents both the entity-length and the transfer-length.
2873 * If a message is received with both a Transfer-Encoding header
2874 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002875 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002876 * 4. By the server closing the connection. (Closing the connection
2877 * cannot be used to indicate the end of a request body, since that
2878 * would leave no possibility for the server to send back a response.)
2879 *
2880 * Whenever a transfer-coding is applied to a message-body, the set of
2881 * transfer-codings MUST include "chunked", unless the message indicates
2882 * it is terminated by closing the connection. When the "chunked"
2883 * transfer-coding is used, it MUST be the last transfer-coding applied
2884 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002885 */
2886
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002887 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002888 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002889 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002890 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002891 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002892 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002893 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2894 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002895 /* bad transfer-encoding (chunked followed by something else) */
2896 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002897 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002898 break;
2899 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002900 }
2901
Willy Tarreau32b47f42009-10-18 20:55:02 +02002902 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002903 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002904 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002905 signed long long cl;
2906
Willy Tarreauad14f752011-09-02 20:33:27 +02002907 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002908 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002909 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002910 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002911
Willy Tarreauad14f752011-09-02 20:33:27 +02002912 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002913 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002914 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002915 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002916
Willy Tarreauad14f752011-09-02 20:33:27 +02002917 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002918 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002919 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002920 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002921
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002922 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002923 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002924 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002925 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002926
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002927 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002928 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002929 }
2930
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002931 /* bodyless requests have a known length */
2932 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002933 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002934
Willy Tarreaud787e662009-07-07 10:14:51 +02002935 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002936 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002937 req->analyse_exp = TICK_ETERNITY;
2938 return 1;
2939
2940 return_bad_req:
2941 /* We centralize bad requests processing here */
2942 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2943 /* we detected a parsing error. We want to archive this request
2944 * in the dedicated proxy area for later troubleshooting.
2945 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002946 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002947 }
2948
2949 txn->req.msg_state = HTTP_MSG_ERROR;
2950 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002951 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002952
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002953 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002954 if (s->listener->counters)
2955 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002956
2957 return_prx_cond:
2958 if (!(s->flags & SN_ERR_MASK))
2959 s->flags |= SN_ERR_PRXCOND;
2960 if (!(s->flags & SN_FINST_MASK))
2961 s->flags |= SN_FINST_R;
2962
2963 req->analysers = 0;
2964 req->analyse_exp = TICK_ETERNITY;
2965 return 0;
2966}
2967
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002968
Willy Tarreau347a35d2013-11-22 17:51:09 +01002969/* This function prepares an applet to handle the stats. It can deal with the
2970 * "100-continue" expectation, check that admin rules are met for POST requests,
2971 * and program a response message if something was unexpected. It cannot fail
2972 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002973 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002974 * s->target which is supposed to already point to the stats applet. The caller
2975 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002976 */
2977int http_handle_stats(struct session *s, struct channel *req)
2978{
2979 struct stats_admin_rule *stats_admin_rule;
2980 struct stream_interface *si = s->rep->prod;
2981 struct http_txn *txn = &s->txn;
2982 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002983 struct uri_auth *uri_auth = s->be->uri_auth;
2984 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002985 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002986
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002987 appctx = si_appctx(si);
2988 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2989 appctx->st1 = appctx->st2 = 0;
2990 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2991 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002992
2993 uri = msg->chn->buf->p + msg->sl.rq.u;
2994 lookup = uri + uri_auth->uri_len;
2995
2996 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2997 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002998 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002999 break;
3000 }
3001 }
3002
3003 if (uri_auth->refresh) {
3004 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3005 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003006 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003007 break;
3008 }
3009 }
3010 }
3011
3012 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3013 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003014 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003015 break;
3016 }
3017 }
3018
3019 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3020 if (memcmp(h, ";st=", 4) == 0) {
3021 int i;
3022 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003023 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003024 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3025 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003026 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003027 break;
3028 }
3029 }
3030 break;
3031 }
3032 }
3033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003034 appctx->ctx.stats.scope_str = 0;
3035 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003036 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3037 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3038 int itx = 0;
3039 const char *h2;
3040 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3041 const char *err;
3042
3043 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3044 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003045 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003046 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3047 itx++;
3048 h++;
3049 }
3050
3051 if (itx > STAT_SCOPE_TXT_MAXLEN)
3052 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003053 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003054
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003055 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003056 memcpy(scope_txt, h2, itx);
3057 scope_txt[itx] = '\0';
3058 err = invalid_char(scope_txt);
3059 if (err) {
3060 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003061 appctx->ctx.stats.scope_str = 0;
3062 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003063 }
3064 break;
3065 }
3066 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003067
3068 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003069 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003070 int ret = 1;
3071
3072 if (stats_admin_rule->cond) {
3073 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3074 ret = acl_pass(ret);
3075 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3076 ret = !ret;
3077 }
3078
3079 if (ret) {
3080 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003081 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003082 break;
3083 }
3084 }
3085
3086 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003087 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003088 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003089 if (msg->msg_state < HTTP_MSG_100_SENT) {
3090 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3091 * send an HTTP/1.1 100 Continue intermediate response.
3092 */
3093 if (msg->flags & HTTP_MSGF_VER_11) {
3094 struct hdr_ctx ctx;
3095 ctx.idx = 0;
3096 /* Expect is allowed in 1.1, look for it */
3097 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3098 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3099 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3100 }
3101 }
3102 msg->msg_state = HTTP_MSG_100_SENT;
3103 s->logs.tv_request = now; /* update the request timer to reflect full request */
3104 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003106 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003107 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003108 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3109 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003110 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003111 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003112 else {
3113 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003114 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003115 }
3116
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003117 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003118 return 1;
3119}
3120
Lukas Tribus67db8df2013-06-23 17:37:13 +02003121/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3122 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3123 */
3124static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3125{
3126#ifdef IP_TOS
3127 if (from.ss_family == AF_INET)
3128 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3129#endif
3130#ifdef IPV6_TCLASS
3131 if (from.ss_family == AF_INET6) {
3132 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3133 /* v4-mapped addresses need IP_TOS */
3134 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3135 else
3136 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3137 }
3138#endif
3139}
3140
Willy Tarreau20b0de52012-12-24 15:45:22 +01003141/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003142 * transaction <txn>. Returns the first rule that prevents further processing
3143 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3144 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3145 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003146 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003147static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003148http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003149{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003150 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003151 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003152 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003153
Willy Tarreauff011f22011-01-06 17:51:27 +01003154 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003155 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003156 continue;
3157
Willy Tarreau96257ec2012-12-27 10:46:37 +01003158 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003159 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003160 int ret;
3161
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003162 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003163 ret = acl_pass(ret);
3164
Willy Tarreauff011f22011-01-06 17:51:27 +01003165 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003166 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003167
3168 if (!ret) /* condition not matched */
3169 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003170 }
3171
Willy Tarreau20b0de52012-12-24 15:45:22 +01003172
Willy Tarreau96257ec2012-12-27 10:46:37 +01003173 switch (rule->action) {
3174 case HTTP_REQ_ACT_ALLOW:
3175 return NULL; /* "allow" rules are OK */
3176
3177 case HTTP_REQ_ACT_DENY:
3178 txn->flags |= TX_CLDENY;
3179 return rule;
3180
Willy Tarreauccbcc372012-12-27 12:37:57 +01003181 case HTTP_REQ_ACT_TARPIT:
3182 txn->flags |= TX_CLTARPIT;
3183 return rule;
3184
Willy Tarreau96257ec2012-12-27 10:46:37 +01003185 case HTTP_REQ_ACT_AUTH:
3186 return rule;
3187
Willy Tarreau81499eb2012-12-27 12:19:02 +01003188 case HTTP_REQ_ACT_REDIR:
3189 return rule;
3190
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003191 case HTTP_REQ_ACT_SET_NICE:
3192 s->task->nice = rule->arg.nice;
3193 break;
3194
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003195 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003196 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003197 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003198 break;
3199
Willy Tarreau51347ed2013-06-11 19:34:13 +02003200 case HTTP_REQ_ACT_SET_MARK:
3201#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003202 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003203 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003204#endif
3205 break;
3206
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003207 case HTTP_REQ_ACT_SET_LOGL:
3208 s->logs.level = rule->arg.loglevel;
3209 break;
3210
Willy Tarreau96257ec2012-12-27 10:46:37 +01003211 case HTTP_REQ_ACT_SET_HDR:
3212 ctx.idx = 0;
3213 /* remove all occurrences of the header */
3214 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3215 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3216 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003217 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003218 /* now fall through to header addition */
3219
3220 case HTTP_REQ_ACT_ADD_HDR:
3221 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3222 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3223 trash.len = rule->arg.hdr_add.name_len;
3224 trash.str[trash.len++] = ':';
3225 trash.str[trash.len++] = ' ';
3226 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3227 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3228 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003229 }
3230 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003231
3232 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003233 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003234}
3235
Willy Tarreau71241ab2012-12-27 11:30:54 +01003236
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003237/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3238 * transaction <txn>. Returns the first rule that prevents further processing
3239 * of the response (deny, ...) or NULL if it executed all rules or stopped
3240 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3241 * rule.
3242 */
3243static struct http_res_rule *
3244http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3245{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003246 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003247 struct http_res_rule *rule;
3248 struct hdr_ctx ctx;
3249
3250 list_for_each_entry(rule, rules, list) {
3251 if (rule->action >= HTTP_RES_ACT_MAX)
3252 continue;
3253
3254 /* check optional condition */
3255 if (rule->cond) {
3256 int ret;
3257
3258 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3259 ret = acl_pass(ret);
3260
3261 if (rule->cond->pol == ACL_COND_UNLESS)
3262 ret = !ret;
3263
3264 if (!ret) /* condition not matched */
3265 continue;
3266 }
3267
3268
3269 switch (rule->action) {
3270 case HTTP_RES_ACT_ALLOW:
3271 return NULL; /* "allow" rules are OK */
3272
3273 case HTTP_RES_ACT_DENY:
3274 txn->flags |= TX_SVDENY;
3275 return rule;
3276
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003277 case HTTP_RES_ACT_SET_NICE:
3278 s->task->nice = rule->arg.nice;
3279 break;
3280
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003281 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003282 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003283 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003284 break;
3285
Willy Tarreau51347ed2013-06-11 19:34:13 +02003286 case HTTP_RES_ACT_SET_MARK:
3287#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003288 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003289 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003290#endif
3291 break;
3292
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003293 case HTTP_RES_ACT_SET_LOGL:
3294 s->logs.level = rule->arg.loglevel;
3295 break;
3296
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003297 case HTTP_RES_ACT_SET_HDR:
3298 ctx.idx = 0;
3299 /* remove all occurrences of the header */
3300 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3301 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3302 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3303 }
3304 /* now fall through to header addition */
3305
3306 case HTTP_RES_ACT_ADD_HDR:
3307 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3308 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3309 trash.len = rule->arg.hdr_add.name_len;
3310 trash.str[trash.len++] = ':';
3311 trash.str[trash.len++] = ' ';
3312 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3313 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3314 break;
3315 }
3316 }
3317
3318 /* we reached the end of the rules, nothing to report */
3319 return NULL;
3320}
3321
3322
Willy Tarreau71241ab2012-12-27 11:30:54 +01003323/* Perform an HTTP redirect based on the information in <rule>. The function
3324 * returns non-zero on success, or zero in case of a, irrecoverable error such
3325 * as too large a request to build a valid response.
3326 */
3327static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3328{
3329 struct http_msg *msg = &txn->req;
3330 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003331 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003332
3333 /* build redirect message */
3334 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003335 case 308:
3336 msg_fmt = HTTP_308;
3337 break;
3338 case 307:
3339 msg_fmt = HTTP_307;
3340 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003341 case 303:
3342 msg_fmt = HTTP_303;
3343 break;
3344 case 301:
3345 msg_fmt = HTTP_301;
3346 break;
3347 case 302:
3348 default:
3349 msg_fmt = HTTP_302;
3350 break;
3351 }
3352
3353 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3354 return 0;
3355
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003356 location = trash.str + trash.len;
3357
Willy Tarreau71241ab2012-12-27 11:30:54 +01003358 switch(rule->type) {
3359 case REDIRECT_TYPE_SCHEME: {
3360 const char *path;
3361 const char *host;
3362 struct hdr_ctx ctx;
3363 int pathlen;
3364 int hostlen;
3365
3366 host = "";
3367 hostlen = 0;
3368 ctx.idx = 0;
3369 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3370 host = ctx.line + ctx.val;
3371 hostlen = ctx.vlen;
3372 }
3373
3374 path = http_get_path(txn);
3375 /* build message using path */
3376 if (path) {
3377 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3378 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3379 int qs = 0;
3380 while (qs < pathlen) {
3381 if (path[qs] == '?') {
3382 pathlen = qs;
3383 break;
3384 }
3385 qs++;
3386 }
3387 }
3388 } else {
3389 path = "/";
3390 pathlen = 1;
3391 }
3392
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003393 if (rule->rdr_str) { /* this is an old "redirect" rule */
3394 /* check if we can add scheme + "://" + host + path */
3395 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3396 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003397
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003398 /* add scheme */
3399 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3400 trash.len += rule->rdr_len;
3401 }
3402 else {
3403 /* add scheme with executing log format */
3404 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003405
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003406 /* check if we can add scheme + "://" + host + path */
3407 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3408 return 0;
3409 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003410 /* add "://" */
3411 memcpy(trash.str + trash.len, "://", 3);
3412 trash.len += 3;
3413
3414 /* add host */
3415 memcpy(trash.str + trash.len, host, hostlen);
3416 trash.len += hostlen;
3417
3418 /* add path */
3419 memcpy(trash.str + trash.len, path, pathlen);
3420 trash.len += pathlen;
3421
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003422 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003423 if (trash.len && trash.str[trash.len - 1] != '/' &&
3424 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3425 if (trash.len > trash.size - 5)
3426 return 0;
3427 trash.str[trash.len] = '/';
3428 trash.len++;
3429 }
3430
3431 break;
3432 }
3433 case REDIRECT_TYPE_PREFIX: {
3434 const char *path;
3435 int pathlen;
3436
3437 path = http_get_path(txn);
3438 /* build message using path */
3439 if (path) {
3440 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3441 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3442 int qs = 0;
3443 while (qs < pathlen) {
3444 if (path[qs] == '?') {
3445 pathlen = qs;
3446 break;
3447 }
3448 qs++;
3449 }
3450 }
3451 } else {
3452 path = "/";
3453 pathlen = 1;
3454 }
3455
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003456 if (rule->rdr_str) { /* this is an old "redirect" rule */
3457 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3458 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003459
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003460 /* add prefix. Note that if prefix == "/", we don't want to
3461 * add anything, otherwise it makes it hard for the user to
3462 * configure a self-redirection.
3463 */
3464 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3465 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3466 trash.len += rule->rdr_len;
3467 }
3468 }
3469 else {
3470 /* add prefix with executing log format */
3471 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3472
3473 /* Check length */
3474 if (trash.len + pathlen > trash.size - 4)
3475 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003476 }
3477
3478 /* add path */
3479 memcpy(trash.str + trash.len, path, pathlen);
3480 trash.len += pathlen;
3481
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003482 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003483 if (trash.len && trash.str[trash.len - 1] != '/' &&
3484 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3485 if (trash.len > trash.size - 5)
3486 return 0;
3487 trash.str[trash.len] = '/';
3488 trash.len++;
3489 }
3490
3491 break;
3492 }
3493 case REDIRECT_TYPE_LOCATION:
3494 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003495 if (rule->rdr_str) { /* this is an old "redirect" rule */
3496 if (trash.len + rule->rdr_len > trash.size - 4)
3497 return 0;
3498
3499 /* add location */
3500 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3501 trash.len += rule->rdr_len;
3502 }
3503 else {
3504 /* add location with executing log format */
3505 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003506
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003507 /* Check left length */
3508 if (trash.len > trash.size - 4)
3509 return 0;
3510 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003511 break;
3512 }
3513
3514 if (rule->cookie_len) {
3515 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3516 trash.len += 14;
3517 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3518 trash.len += rule->cookie_len;
3519 memcpy(trash.str + trash.len, "\r\n", 2);
3520 trash.len += 2;
3521 }
3522
3523 /* add end of headers and the keep-alive/close status.
3524 * We may choose to set keep-alive if the Location begins
3525 * with a slash, because the client will come back to the
3526 * same server.
3527 */
3528 txn->status = rule->code;
3529 /* let's log the request time */
3530 s->logs.tv_request = now;
3531
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003532 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003533 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3534 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3535 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3536 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3537 /* keep-alive possible */
3538 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3539 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3540 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3541 trash.len += 30;
3542 } else {
3543 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3544 trash.len += 24;
3545 }
3546 }
3547 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3548 trash.len += 4;
3549 bo_inject(txn->rsp.chn, trash.str, trash.len);
3550 /* "eat" the request */
3551 bi_fast_delete(txn->req.chn->buf, msg->sov);
3552 msg->sov = 0;
3553 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3554 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3555 txn->req.msg_state = HTTP_MSG_CLOSED;
3556 txn->rsp.msg_state = HTTP_MSG_DONE;
3557 } else {
3558 /* keep-alive not possible */
3559 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3560 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3561 trash.len += 29;
3562 } else {
3563 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3564 trash.len += 23;
3565 }
3566 stream_int_retnclose(txn->req.chn->prod, &trash);
3567 txn->req.chn->analysers = 0;
3568 }
3569
3570 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003571 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003572 if (!(s->flags & SN_FINST_MASK))
3573 s->flags |= SN_FINST_R;
3574
3575 return 1;
3576}
3577
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003578/* This stream analyser runs all HTTP request processing which is common to
3579 * frontends and backends, which means blocking ACLs, filters, connection-close,
3580 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003581 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003582 * either needs more data or wants to immediately abort the request (eg: deny,
3583 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003584 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003585int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003586{
Willy Tarreaud787e662009-07-07 10:14:51 +02003587 struct http_txn *txn = &s->txn;
3588 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003589 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003590 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003591 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003592 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003593
Willy Tarreau655dce92009-11-08 13:10:58 +01003594 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003595 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003596 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003597 return 0;
3598 }
3599
Willy Tarreau3a816292009-07-07 10:55:49 +02003600 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003601 req->analyse_exp = TICK_ETERNITY;
3602
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003603 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 +02003604 now_ms, __FUNCTION__,
3605 s,
3606 req,
3607 req->rex, req->wex,
3608 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003609 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003610 req->analysers);
3611
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003612 /* first check whether we have some ACLs set to block this request */
3613 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003614 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003615
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003616 ret = acl_pass(ret);
3617 if (cond->pol == ACL_COND_UNLESS)
3618 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003619
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003620 if (ret) {
3621 txn->status = 403;
3622 /* let's log the request time */
3623 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003624 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003625 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003626 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003627 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003628 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003629
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003630 /* just in case we have some per-backend tracking */
3631 session_inc_be_http_req_ctr(s);
3632
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003633 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003634 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003635
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003636 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003637 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003638 if (stats_check_uri(s->rep->prod, txn, px)) {
3639 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003640 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3641 txn->status = 500;
3642 s->logs.tv_request = now;
3643 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003644
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003645 if (!(s->flags & SN_ERR_MASK))
3646 s->flags |= SN_ERR_RESOURCE;
3647 goto return_prx_cond;
3648 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003649 /* parse the whole stats request and extract the relevant information */
3650 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003651 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 +01003652 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003653 }
3654
Willy Tarreau3b44e722013-11-16 10:28:23 +01003655 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3656 * blocked by an http-request rule.
3657 */
3658 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003659 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003660 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003661 }
Willy Tarreau06619262006-12-17 08:37:22 +01003662
Willy Tarreau3b44e722013-11-16 10:28:23 +01003663 /* return a 403 if either rule has blocked */
3664 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003665 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003666 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003667 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003668 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003669 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003670 s->fe->fe_counters.denied_req++;
3671 if (s->fe != s->be)
3672 s->be->be_counters.denied_req++;
3673 if (s->listener->counters)
3674 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003675 goto return_prx_cond;
3676 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003677
3678 /* When a connection is tarpitted, we use the tarpit timeout,
3679 * which may be the same as the connect timeout if unspecified.
3680 * If unset, then set it to zero because we really want it to
3681 * eventually expire. We build the tarpit as an analyser.
3682 */
3683 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003684 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003685 /* wipe the request out so that we can drop the connection early
3686 * if the client closes first.
3687 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003688 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003689 req->analysers = 0; /* remove switching rules etc... */
3690 req->analysers |= AN_REQ_HTTP_TARPIT;
3691 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3692 if (!req->analyse_exp)
3693 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003694 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003695 s->fe->fe_counters.denied_req++;
3696 if (s->fe != s->be)
3697 s->be->be_counters.denied_req++;
3698 if (s->listener->counters)
3699 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003700 return 1;
3701 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003702 }
Willy Tarreau06619262006-12-17 08:37:22 +01003703
Willy Tarreau70dffda2014-01-30 03:07:23 +01003704 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003705 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003706 * Option httpclose by itself sets tunnel mode where headers are mangled.
3707 * However, if another mode is set, it will affect it (eg: server-close/
3708 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3709 * if FE and BE have the same settings (common). The method consists in
3710 * checking if options changed between the two calls (implying that either
3711 * one is non-null, or one of them is non-null and we are there for the first
3712 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003713 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003714
Willy Tarreau416ce612014-01-31 15:45:34 +01003715 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003716 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003717 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003718
Willy Tarreau70dffda2014-01-30 03:07:23 +01003719 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3720 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3721 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3722 tmp = TX_CON_WANT_TUN;
3723
3724 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3725 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3726 tmp = TX_CON_WANT_TUN;
3727 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003728
3729 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003730 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3731 /* option httpclose + server_close => forceclose */
3732 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3733 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3734 tmp = TX_CON_WANT_CLO;
3735 else
3736 tmp = TX_CON_WANT_SCL;
3737 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003738
3739 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3740 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003741 tmp = TX_CON_WANT_CLO;
3742
Willy Tarreau5b154472009-12-21 20:11:07 +01003743 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3744 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003745
Willy Tarreau416ce612014-01-31 15:45:34 +01003746 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3747 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003748 /* parse the Connection header and possibly clean it */
3749 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003750 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003751 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3752 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003753 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003754 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003755 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003756 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003757 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003758
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003759 /* check if client or config asks for explicit close in KAL/SCL */
3760 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3761 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3762 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003763 (!(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 +01003764 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003765 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003766 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3767 }
Willy Tarreau78599912009-10-17 20:12:21 +02003768
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003769 /* we can be blocked here because the request needs to be authenticated,
3770 * either to pass or to access stats.
3771 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003772 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003773 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003774
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003775 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003776 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003777
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003778 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003779 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003780 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003781 /* on 401 we still count one error, because normal browsing
3782 * won't significantly increase the counter but brute force
3783 * attempts will.
3784 */
3785 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003786 goto return_prx_cond;
3787 }
3788
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003789 /* add request headers from the rule sets in the same order */
3790 list_for_each_entry(wl, &px->req_add, list) {
3791 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003792 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003793 ret = acl_pass(ret);
3794 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3795 ret = !ret;
3796 if (!ret)
3797 continue;
3798 }
3799
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003800 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003801 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003802 }
3803
3804 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3805 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3806 goto return_bad_req;
3807 req->analyse_exp = TICK_ETERNITY;
3808 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003809 }
3810
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003811 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003812 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003813 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3814 s->fe->fe_counters.intercepted_req++;
3815
3816 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3817 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3818 if (!(s->flags & SN_FINST_MASK))
3819 s->flags |= SN_FINST_R;
3820
3821 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003822 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003823 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003824 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003825
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003826 /* check whether we have some ACLs set to redirect this request */
3827 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003828 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003829 int ret;
3830
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003831 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003832 ret = acl_pass(ret);
3833 if (rule->cond->pol == ACL_COND_UNLESS)
3834 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003835 if (!ret)
3836 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003837 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003838 if (!http_apply_redirect_rule(rule, s, txn))
3839 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003840
Willy Tarreau71241ab2012-12-27 11:30:54 +01003841 req->analyse_exp = TICK_ETERNITY;
3842 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003843 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003844
Willy Tarreau2be39392010-01-03 17:24:51 +01003845 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3846 * If this happens, then the data will not come immediately, so we must
3847 * send all what we have without waiting. Note that due to the small gain
3848 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003849 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003850 * itself once used.
3851 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003852 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003853
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003854 /* that's OK for us now, let's move on to next analysers */
3855 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003856
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003857 return_bad_req:
3858 /* We centralize bad requests processing here */
3859 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3860 /* we detected a parsing error. We want to archive this request
3861 * in the dedicated proxy area for later troubleshooting.
3862 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003863 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003864 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003865
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003866 txn->req.msg_state = HTTP_MSG_ERROR;
3867 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003868 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003869
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003870 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003871 if (s->listener->counters)
3872 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003873
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003874 return_prx_cond:
3875 if (!(s->flags & SN_ERR_MASK))
3876 s->flags |= SN_ERR_PRXCOND;
3877 if (!(s->flags & SN_FINST_MASK))
3878 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003879
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003880 req->analysers = 0;
3881 req->analyse_exp = TICK_ETERNITY;
3882 return 0;
3883}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003884
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003885/* This function performs all the processing enabled for the current request.
3886 * It returns 1 if the processing can continue on next analysers, or zero if it
3887 * needs more data, encounters an error, or wants to immediately abort the
3888 * request. It relies on buffers flags, and updates s->req->analysers.
3889 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003890int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003891{
3892 struct http_txn *txn = &s->txn;
3893 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003894 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003895
Willy Tarreau655dce92009-11-08 13:10:58 +01003896 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003897 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003898 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003899 return 0;
3900 }
3901
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003902 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 +02003903 now_ms, __FUNCTION__,
3904 s,
3905 req,
3906 req->rex, req->wex,
3907 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003908 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003909 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003910
William Lallemand82fe75c2012-10-23 10:25:10 +02003911 if (s->fe->comp || s->be->comp)
3912 select_compression_request_header(s, req->buf);
3913
Willy Tarreau59234e92008-11-30 23:51:27 +01003914 /*
3915 * Right now, we know that we have processed the entire headers
3916 * and that unwanted requests have been filtered out. We can do
3917 * whatever we want with the remaining request. Also, now we
3918 * may have separate values for ->fe, ->be.
3919 */
Willy Tarreau06619262006-12-17 08:37:22 +01003920
Willy Tarreau59234e92008-11-30 23:51:27 +01003921 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003922 * If HTTP PROXY is set we simply get remote server address parsing
3923 * incoming request. Note that this requires that a connection is
3924 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003925 */
3926 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003927 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003928 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003929
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003930 /* Note that for now we don't reuse existing proxy connections */
3931 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003932 txn->req.msg_state = HTTP_MSG_ERROR;
3933 txn->status = 500;
3934 req->analysers = 0;
3935 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3936
3937 if (!(s->flags & SN_ERR_MASK))
3938 s->flags |= SN_ERR_RESOURCE;
3939 if (!(s->flags & SN_FINST_MASK))
3940 s->flags |= SN_FINST_R;
3941
3942 return 0;
3943 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003944
3945 path = http_get_path(txn);
3946 url2sa(req->buf->p + msg->sl.rq.u,
3947 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003948 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003949 /* if the path was found, we have to remove everything between
3950 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3951 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3952 * u_l characters by a single "/".
3953 */
3954 if (path) {
3955 char *cur_ptr = req->buf->p;
3956 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3957 int delta;
3958
3959 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3960 http_msg_move_end(&txn->req, delta);
3961 cur_end += delta;
3962 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3963 goto return_bad_req;
3964 }
3965 else {
3966 char *cur_ptr = req->buf->p;
3967 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3968 int delta;
3969
3970 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3971 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3972 http_msg_move_end(&txn->req, delta);
3973 cur_end += delta;
3974 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3975 goto return_bad_req;
3976 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003977 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003978
Willy Tarreau59234e92008-11-30 23:51:27 +01003979 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003980 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003981 * Note that doing so might move headers in the request, but
3982 * the fields will stay coherent and the URI will not move.
3983 * This should only be performed in the backend.
3984 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003985 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3987 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003988
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003990 * 8: the appsession cookie was looked up very early in 1.2,
3991 * so let's do the same now.
3992 */
3993
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003994 /* It needs to look into the URI unless persistence must be ignored */
3995 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003996 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 +01003997 }
3998
William Lallemanda73203e2012-03-12 12:48:57 +01003999 /* add unique-id if "header-unique-id" is specified */
4000
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004001 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4002 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4003 goto return_bad_req;
4004 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004005 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004006 }
William Lallemanda73203e2012-03-12 12:48:57 +01004007
4008 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004009 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4010 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004011 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004012 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004013 goto return_bad_req;
4014 }
4015
Cyril Bontéb21570a2009-11-29 20:04:48 +01004016 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004017 * 9: add X-Forwarded-For if either the frontend or the backend
4018 * asks for it.
4019 */
4020 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004021 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004022 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004023 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4024 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004025 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004026 /* The header is set to be added only if none is present
4027 * and we found it, so don't do anything.
4028 */
4029 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004030 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004031 /* Add an X-Forwarded-For header unless the source IP is
4032 * in the 'except' network range.
4033 */
4034 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004035 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004036 != s->fe->except_net.s_addr) &&
4037 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004038 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004039 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004040 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004041 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004042 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004043
4044 /* Note: we rely on the backend to get the header name to be used for
4045 * x-forwarded-for, because the header is really meant for the backends.
4046 * However, if the backend did not specify any option, we have to rely
4047 * on the frontend's header name.
4048 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004049 if (s->be->fwdfor_hdr_len) {
4050 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004051 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004052 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004053 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004054 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004055 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004056 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004057
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004058 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004059 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004060 }
4061 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004062 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004063 /* FIXME: for the sake of completeness, we should also support
4064 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004065 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004066 int len;
4067 char pn[INET6_ADDRSTRLEN];
4068 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004069 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004070 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004071
Willy Tarreau59234e92008-11-30 23:51:27 +01004072 /* Note: we rely on the backend to get the header name to be used for
4073 * x-forwarded-for, because the header is really meant for the backends.
4074 * However, if the backend did not specify any option, we have to rely
4075 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004076 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004077 if (s->be->fwdfor_hdr_len) {
4078 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004079 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004080 } else {
4081 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004082 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004083 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004084 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004085
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004086 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004087 goto return_bad_req;
4088 }
4089 }
4090
4091 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004092 * 10: add X-Original-To if either the frontend or the backend
4093 * asks for it.
4094 */
4095 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4096
4097 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004098 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004099 /* Add an X-Original-To header unless the destination IP is
4100 * in the 'except' network range.
4101 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004102 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004103
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004104 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004105 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004106 (((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 +02004107 != s->fe->except_to.s_addr) &&
4108 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004109 (((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 +02004110 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004111 int len;
4112 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004113 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004114
4115 /* Note: we rely on the backend to get the header name to be used for
4116 * x-original-to, because the header is really meant for the backends.
4117 * However, if the backend did not specify any option, we have to rely
4118 * on the frontend's header name.
4119 */
4120 if (s->be->orgto_hdr_len) {
4121 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004122 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004123 } else {
4124 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004125 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004126 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004127 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004128
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004129 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004130 goto return_bad_req;
4131 }
4132 }
4133 }
4134
Willy Tarreau50fc7772012-11-11 22:19:57 +01004135 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4136 * If an "Upgrade" token is found, the header is left untouched in order not to have
4137 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4138 * "Upgrade" is present in the Connection header.
4139 */
4140 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4141 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004142 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4143 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004144 unsigned int want_flags = 0;
4145
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004146 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004147 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004148 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4149 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004150 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004151 want_flags |= TX_CON_CLO_SET;
4152 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004153 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004154 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4155 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004156 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004157 want_flags |= TX_CON_KAL_SET;
4158 }
4159
4160 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004161 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004162 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004163
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004164
Willy Tarreau522d6c02009-12-06 18:49:18 +01004165 /* If we have no server assigned yet and we're balancing on url_param
4166 * with a POST request, we may be interested in checking the body for
4167 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004168 */
4169 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4170 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004171 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004172 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004173 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004174 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004175 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004176
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004177 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004178 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004179#ifdef TCP_QUICKACK
4180 /* We expect some data from the client. Unless we know for sure
4181 * we already have a full request, we have to re-enable quick-ack
4182 * in case we previously disabled it, otherwise we might cause
4183 * the client to delay further data.
4184 */
4185 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004186 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004187 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004188 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004189 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004190#endif
4191 }
Willy Tarreau03945942009-12-22 16:50:27 +01004192
Willy Tarreau59234e92008-11-30 23:51:27 +01004193 /*************************************************************
4194 * OK, that's finished for the headers. We have done what we *
4195 * could. Let's switch to the DATA state. *
4196 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004197 req->analyse_exp = TICK_ETERNITY;
4198 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004199
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004200 /* if the server closes the connection, we want to immediately react
4201 * and close the socket to save packets and syscalls.
4202 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004203 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4204 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004205
Willy Tarreau59234e92008-11-30 23:51:27 +01004206 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004207 /* OK let's go on with the BODY now */
4208 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004209
Willy Tarreau59234e92008-11-30 23:51:27 +01004210 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004211 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004212 /* we detected a parsing error. We want to archive this request
4213 * in the dedicated proxy area for later troubleshooting.
4214 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004215 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004216 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004217
Willy Tarreau59234e92008-11-30 23:51:27 +01004218 txn->req.msg_state = HTTP_MSG_ERROR;
4219 txn->status = 400;
4220 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004221 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004222
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004223 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004224 if (s->listener->counters)
4225 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004226
Willy Tarreau59234e92008-11-30 23:51:27 +01004227 if (!(s->flags & SN_ERR_MASK))
4228 s->flags |= SN_ERR_PRXCOND;
4229 if (!(s->flags & SN_FINST_MASK))
4230 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004231 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004232}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004233
Willy Tarreau60b85b02008-11-30 23:28:40 +01004234/* This function is an analyser which processes the HTTP tarpit. It always
4235 * returns zero, at the beginning because it prevents any other processing
4236 * from occurring, and at the end because it terminates the request.
4237 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004238int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004239{
4240 struct http_txn *txn = &s->txn;
4241
4242 /* This connection is being tarpitted. The CLIENT side has
4243 * already set the connect expiration date to the right
4244 * timeout. We just have to check that the client is still
4245 * there and that the timeout has not expired.
4246 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004247 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004248 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004249 !tick_is_expired(req->analyse_exp, now_ms))
4250 return 0;
4251
4252 /* We will set the queue timer to the time spent, just for
4253 * logging purposes. We fake a 500 server error, so that the
4254 * attacker will not suspect his connection has been tarpitted.
4255 * It will not cause trouble to the logs because we can exclude
4256 * the tarpitted connections by filtering on the 'PT' status flags.
4257 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004258 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4259
4260 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004261 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004262 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004263
4264 req->analysers = 0;
4265 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004266
Willy Tarreau60b85b02008-11-30 23:28:40 +01004267 if (!(s->flags & SN_ERR_MASK))
4268 s->flags |= SN_ERR_PRXCOND;
4269 if (!(s->flags & SN_FINST_MASK))
4270 s->flags |= SN_FINST_T;
4271 return 0;
4272}
4273
Willy Tarreaud34af782008-11-30 23:36:37 +01004274/* This function is an analyser which processes the HTTP request body. It looks
4275 * for parameters to be used for the load balancing algorithm (url_param). It
4276 * must only be called after the standard HTTP request processing has occurred,
4277 * because it expects the request to be parsed. It returns zero if it needs to
4278 * read more data, or 1 once it has completed its analysis.
4279 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004280int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004281{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004282 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004283 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004284 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004285
4286 /* We have to parse the HTTP request body to find any required data.
4287 * "balance url_param check_post" should have been the only way to get
4288 * into this. We were brought here after HTTP header analysis, so all
4289 * related structures are ready.
4290 */
4291
Willy Tarreau522d6c02009-12-06 18:49:18 +01004292 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4293 goto missing_data;
4294
4295 if (msg->msg_state < HTTP_MSG_100_SENT) {
4296 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4297 * send an HTTP/1.1 100 Continue intermediate response.
4298 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004299 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004300 struct hdr_ctx ctx;
4301 ctx.idx = 0;
4302 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004303 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004304 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004305 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004306 }
4307 }
4308 msg->msg_state = HTTP_MSG_100_SENT;
4309 }
4310
4311 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004312 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004313 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004314 * is still null. We must save the body in msg->next because it
4315 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004316 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004317 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004318
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004319 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004320 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4321 else
4322 msg->msg_state = HTTP_MSG_DATA;
4323 }
4324
4325 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004326 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004327 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004328 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004329 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004330 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004331
Willy Tarreau115acb92009-12-26 13:56:06 +01004332 if (!ret)
4333 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004334 else if (ret < 0) {
4335 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004336 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004337 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004338 }
4339
Willy Tarreaud98cf932009-12-27 22:54:55 +01004340 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004341 * We have the first data byte is in msg->sov. We're waiting for at
4342 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004343 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004344
Willy Tarreau124d9912011-03-01 20:30:48 +01004345 if (msg->body_len < limit)
4346 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004347
Willy Tarreau9b28e032012-10-12 23:49:43 +02004348 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004349 goto http_end;
4350
4351 missing_data:
4352 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004353 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004354 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004355 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004356 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004357
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004358 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004359 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004360 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004361
4362 if (!(s->flags & SN_ERR_MASK))
4363 s->flags |= SN_ERR_CLITO;
4364 if (!(s->flags & SN_FINST_MASK))
4365 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004366 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004367 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004368
4369 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004370 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004371 /* Not enough data. We'll re-use the http-request
4372 * timeout here. Ideally, we should set the timeout
4373 * relative to the accept() date. We just set the
4374 * request timeout once at the beginning of the
4375 * request.
4376 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004377 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004378 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004379 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004380 return 0;
4381 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004382
4383 http_end:
4384 /* The situation will not evolve, so let's give up on the analysis. */
4385 s->logs.tv_request = now; /* update the request timer to reflect full request */
4386 req->analysers &= ~an_bit;
4387 req->analyse_exp = TICK_ETERNITY;
4388 return 1;
4389
4390 return_bad_req: /* let's centralize all bad requests */
4391 txn->req.msg_state = HTTP_MSG_ERROR;
4392 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004393 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004394
Willy Tarreau79ebac62010-06-07 13:47:49 +02004395 if (!(s->flags & SN_ERR_MASK))
4396 s->flags |= SN_ERR_PRXCOND;
4397 if (!(s->flags & SN_FINST_MASK))
4398 s->flags |= SN_FINST_R;
4399
Willy Tarreau522d6c02009-12-06 18:49:18 +01004400 return_err_msg:
4401 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004402 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004403 if (s->listener->counters)
4404 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004405 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004406}
4407
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004408/* send a server's name with an outgoing request over an established connection.
4409 * Note: this function is designed to be called once the request has been scheduled
4410 * for being forwarded. This is the reason why it rewinds the buffer before
4411 * proceeding.
4412 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004413int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004414
4415 struct hdr_ctx ctx;
4416
Mark Lamourinec2247f02012-01-04 13:02:01 -05004417 char *hdr_name = be->server_id_hdr_name;
4418 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004419 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004420 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004421 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004422
William Lallemandd9e90662012-01-30 17:27:17 +01004423 ctx.idx = 0;
4424
Willy Tarreau9b28e032012-10-12 23:49:43 +02004425 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004426 if (old_o) {
4427 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004428 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004429 }
4430
Willy Tarreau9b28e032012-10-12 23:49:43 +02004431 old_i = chn->buf->i;
4432 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 -05004433 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004434 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004435 }
4436
4437 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004438 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004439 memcpy(hdr_val, hdr_name, hdr_name_len);
4440 hdr_val += hdr_name_len;
4441 *hdr_val++ = ':';
4442 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004443 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4444 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004445
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004446 if (old_o) {
4447 /* If this was a forwarded request, we must readjust the amount of
4448 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004449 * variations. Note that if the request was already scheduled for
4450 * forwarding, it had its req->sol pointing to the body, which
4451 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004452 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004453 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004454 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004455 }
4456
Mark Lamourinec2247f02012-01-04 13:02:01 -05004457 return 0;
4458}
4459
Willy Tarreau610ecce2010-01-04 21:15:02 +01004460/* Terminate current transaction and prepare a new one. This is very tricky
4461 * right now but it works.
4462 */
4463void http_end_txn_clean_session(struct session *s)
4464{
Willy Tarreau068621e2013-12-23 15:11:25 +01004465 int prev_status = s->txn.status;
4466
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 /* FIXME: We need a more portable way of releasing a backend's and a
4468 * server's connections. We need a safer way to reinitialize buffer
4469 * flags. We also need a more accurate method for computing per-request
4470 * data.
4471 */
4472 http_silent_debug(__LINE__, s);
4473
Willy Tarreau4213a112013-12-15 10:25:42 +01004474 /* unless we're doing keep-alive, we want to quickly close the connection
4475 * to the server.
4476 */
4477 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4478 !si_conn_ready(s->req->cons)) {
4479 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4480 si_shutr(s->req->cons);
4481 si_shutw(s->req->cons);
4482 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004483
4484 http_silent_debug(__LINE__, s);
4485
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004486 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004487 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004488 if (unlikely(s->srv_conn))
4489 sess_change_server(s, NULL);
4490 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491
4492 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4493 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004494 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495
4496 if (s->txn.status) {
4497 int n;
4498
4499 n = s->txn.status / 100;
4500 if (n < 1 || n > 5)
4501 n = 0;
4502
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004503 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004504 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004505 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004506 s->fe->fe_counters.p.http.comp_rsp++;
4507 }
Willy Tarreau24657792010-02-26 10:30:28 +01004508 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004509 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004510 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004511 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004512 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004513 s->be->be_counters.p.http.comp_rsp++;
4514 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 }
4516
4517 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004518 s->logs.bytes_in -= s->req->buf->i;
4519 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520
4521 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004522 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 !(s->flags & SN_MONITOR) &&
4524 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4525 s->do_log(s);
4526 }
4527
4528 s->logs.accept_date = date; /* user-visible date for logging */
4529 s->logs.tv_accept = now; /* corrected date for internal use */
4530 tv_zero(&s->logs.tv_request);
4531 s->logs.t_queue = -1;
4532 s->logs.t_connect = -1;
4533 s->logs.t_data = -1;
4534 s->logs.t_close = 0;
4535 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4536 s->logs.srv_queue_size = 0; /* we will get this number soon */
4537
Willy Tarreau9b28e032012-10-12 23:49:43 +02004538 s->logs.bytes_in = s->req->total = s->req->buf->i;
4539 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540
4541 if (s->pend_pos)
4542 pendconn_free(s->pend_pos);
4543
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004544 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 if (s->flags & SN_CURR_SESS) {
4546 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004547 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004549 if (may_dequeue_tasks(objt_server(s->target), s->be))
4550 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 }
4552
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004553 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554
Willy Tarreau4213a112013-12-15 10:25:42 +01004555 /* only release our endpoint if we don't intend to reuse the
4556 * connection.
4557 */
4558 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4559 !si_conn_ready(s->req->cons)) {
4560 si_release_endpoint(s->req->cons);
4561 }
4562
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004565 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004566 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004567 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004568 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4569 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 +02004570 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 +01004571 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004572
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 s->txn.meth = 0;
4574 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004575 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004576
4577 if (prev_status == 401 || prev_status == 407) {
4578 /* In HTTP keep-alive mode, if we receive a 401, we still have
4579 * a chance of being able to send the visitor again to the same
4580 * server over the same connection. This is required by some
4581 * broken protocols such as NTLM, and anyway whenever there is
4582 * an opportunity for sending the challenge to the proper place,
4583 * it's better to do it (at least it helps with debugging).
4584 */
4585 s->txn.flags |= TX_PREFER_LAST;
4586 }
4587
Willy Tarreauee55dc02010-06-01 10:56:34 +02004588 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 s->req->cons->flags |= SI_FL_INDEP_STR;
4590
Willy Tarreau96e31212011-05-30 18:10:30 +02004591 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004592 s->req->flags |= CF_NEVER_WAIT;
4593 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004594 }
4595
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 /* if the request buffer is not empty, it means we're
4597 * about to process another request, so send pending
4598 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004599 * Just don't do this if the buffer is close to be full,
4600 * because the request will wait for it to flush a little
4601 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004603 if (s->req->buf->i) {
4604 if (s->rep->buf->o &&
4605 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4606 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004607 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004608 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004609
4610 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004611 channel_auto_read(s->req);
4612 channel_auto_close(s->req);
4613 channel_auto_read(s->rep);
4614 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615
Willy Tarreau27375622013-12-17 00:00:28 +01004616 /* we're in keep-alive with an idle connection, monitor it */
4617 si_idle_conn(s->req->cons);
4618
Willy Tarreau342b11c2010-11-24 16:22:09 +01004619 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620 s->rep->analysers = 0;
4621
4622 http_silent_debug(__LINE__, s);
4623}
4624
4625
4626/* This function updates the request state machine according to the response
4627 * state machine and buffer flags. It returns 1 if it changes anything (flag
4628 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4629 * it is only used to find when a request/response couple is complete. Both
4630 * this function and its equivalent should loop until both return zero. It
4631 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4632 */
4633int http_sync_req_state(struct session *s)
4634{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004635 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004636 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004637 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004638 unsigned int old_state = txn->req.msg_state;
4639
4640 http_silent_debug(__LINE__, s);
4641 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4642 return 0;
4643
4644 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004645 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004646 * We can shut the read side unless we want to abort_on_close,
4647 * or we have a POST request. The issue with POST requests is
4648 * that some browsers still send a CRLF after the request, and
4649 * this CRLF must be read so that it does not remain in the kernel
4650 * buffers, otherwise a close could cause an RST on some systems
4651 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004652 * Note that if we're using keep-alive on the client side, we'd
4653 * rather poll now and keep the polling enabled for the whole
4654 * session's life than enabling/disabling it between each
4655 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004656 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004657 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4658 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4659 !(s->be->options & PR_O_ABRT_CLOSE) &&
4660 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004661 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662
Willy Tarreau40f151a2012-12-20 12:10:09 +01004663 /* if the server closes the connection, we want to immediately react
4664 * and close the socket to save packets and syscalls.
4665 */
4666 chn->cons->flags |= SI_FL_NOHALF;
4667
Willy Tarreau610ecce2010-01-04 21:15:02 +01004668 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4669 goto wait_other_side;
4670
4671 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4672 /* The server has not finished to respond, so we
4673 * don't want to move in order not to upset it.
4674 */
4675 goto wait_other_side;
4676 }
4677
4678 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4679 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004680 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004682 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004683 goto wait_other_side;
4684 }
4685
4686 /* When we get here, it means that both the request and the
4687 * response have finished receiving. Depending on the connection
4688 * mode, we'll have to wait for the last bytes to leave in either
4689 * direction, and sometimes for a close to be effective.
4690 */
4691
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004692 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4693 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004694 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4695 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004696 }
4697 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4698 /* Option forceclose is set, or either side wants to close,
4699 * let's enforce it now that we're not expecting any new
4700 * data to come. The caller knows the session is complete
4701 * once both states are CLOSED.
4702 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004703 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4704 channel_shutr_now(chn);
4705 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004706 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004707 }
4708 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004709 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4710 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004711 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004712 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4713 channel_auto_read(chn);
4714 txn->req.msg_state = HTTP_MSG_TUNNEL;
4715 chn->flags |= CF_NEVER_WAIT;
4716 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004717 }
4718
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004719 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004721 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004722
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004723 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004724 txn->req.msg_state = HTTP_MSG_CLOSING;
4725 goto http_msg_closing;
4726 }
4727 else {
4728 txn->req.msg_state = HTTP_MSG_CLOSED;
4729 goto http_msg_closed;
4730 }
4731 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004732 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004733 }
4734
4735 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4736 http_msg_closing:
4737 /* nothing else to forward, just waiting for the output buffer
4738 * to be empty and for the shutw_now to take effect.
4739 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004740 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 txn->req.msg_state = HTTP_MSG_CLOSED;
4742 goto http_msg_closed;
4743 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004744 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 txn->req.msg_state = HTTP_MSG_ERROR;
4746 goto wait_other_side;
4747 }
4748 }
4749
4750 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4751 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004752 /* see above in MSG_DONE why we only do this in these states */
4753 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4754 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4755 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004756 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757 goto wait_other_side;
4758 }
4759
4760 wait_other_side:
4761 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004762 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004763}
4764
4765
4766/* This function updates the response state machine according to the request
4767 * state machine and buffer flags. It returns 1 if it changes anything (flag
4768 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4769 * it is only used to find when a request/response couple is complete. Both
4770 * this function and its equivalent should loop until both return zero. It
4771 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4772 */
4773int http_sync_res_state(struct session *s)
4774{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004775 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004776 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004777 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004778 unsigned int old_state = txn->rsp.msg_state;
4779
4780 http_silent_debug(__LINE__, s);
4781 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4782 return 0;
4783
4784 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4785 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004786 * still monitor the server connection for a possible close
4787 * while the request is being uploaded, so we don't disable
4788 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004789 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004790 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004791
4792 if (txn->req.msg_state == HTTP_MSG_ERROR)
4793 goto wait_other_side;
4794
4795 if (txn->req.msg_state < HTTP_MSG_DONE) {
4796 /* The client seems to still be sending data, probably
4797 * because we got an error response during an upload.
4798 * We have the choice of either breaking the connection
4799 * or letting it pass through. Let's do the later.
4800 */
4801 goto wait_other_side;
4802 }
4803
4804 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4805 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004806 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004808 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809 goto wait_other_side;
4810 }
4811
4812 /* When we get here, it means that both the request and the
4813 * response have finished receiving. Depending on the connection
4814 * mode, we'll have to wait for the last bytes to leave in either
4815 * direction, and sometimes for a close to be effective.
4816 */
4817
4818 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4819 /* Server-close mode : shut read and wait for the request
4820 * side to close its output buffer. The caller will detect
4821 * when we're in DONE and the other is in CLOSED and will
4822 * catch that for the final cleanup.
4823 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004824 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4825 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004826 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004827 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4828 /* Option forceclose is set, or either side wants to close,
4829 * let's enforce it now that we're not expecting any new
4830 * data to come. The caller knows the session is complete
4831 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004832 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004833 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4834 channel_shutr_now(chn);
4835 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004836 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837 }
4838 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004839 /* The last possible modes are keep-alive and tunnel. Tunnel will
4840 * need to forward remaining data. Keep-alive will need to monitor
4841 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004842 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004843 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004844 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004845 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4846 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004847 }
4848
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004849 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004850 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004851 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004852 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4853 goto http_msg_closing;
4854 }
4855 else {
4856 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4857 goto http_msg_closed;
4858 }
4859 }
4860 goto wait_other_side;
4861 }
4862
4863 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4864 http_msg_closing:
4865 /* nothing else to forward, just waiting for the output buffer
4866 * to be empty and for the shutw_now to take effect.
4867 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004868 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4870 goto http_msg_closed;
4871 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004872 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004873 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004874 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004875 if (objt_server(s->target))
4876 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004877 goto wait_other_side;
4878 }
4879 }
4880
4881 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4882 http_msg_closed:
4883 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004884 bi_erase(chn);
4885 channel_auto_close(chn);
4886 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004887 goto wait_other_side;
4888 }
4889
4890 wait_other_side:
4891 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004892 /* We force the response to leave immediately if we're waiting for the
4893 * other side, since there is no pending shutdown to push it out.
4894 */
4895 if (!channel_is_empty(chn))
4896 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004897 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004898}
4899
4900
4901/* Resync the request and response state machines. Return 1 if either state
4902 * changes.
4903 */
4904int http_resync_states(struct session *s)
4905{
4906 struct http_txn *txn = &s->txn;
4907 int old_req_state = txn->req.msg_state;
4908 int old_res_state = txn->rsp.msg_state;
4909
4910 http_silent_debug(__LINE__, s);
4911 http_sync_req_state(s);
4912 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004913 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004914 if (!http_sync_res_state(s))
4915 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004916 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004917 if (!http_sync_req_state(s))
4918 break;
4919 }
4920 http_silent_debug(__LINE__, s);
4921 /* OK, both state machines agree on a compatible state.
4922 * There are a few cases we're interested in :
4923 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4924 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4925 * directions, so let's simply disable both analysers.
4926 * - HTTP_MSG_CLOSED on the response only means we must abort the
4927 * request.
4928 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4929 * with server-close mode means we've completed one request and we
4930 * must re-initialize the server connection.
4931 */
4932
4933 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4934 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4935 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4936 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4937 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004938 channel_auto_close(s->req);
4939 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004940 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004941 channel_auto_close(s->rep);
4942 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004943 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004944 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4945 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004946 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004947 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004948 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004949 channel_auto_close(s->rep);
4950 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004951 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004952 channel_abort(s->req);
4953 channel_auto_close(s->req);
4954 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004955 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004957 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4958 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004960 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4961 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4962 /* server-close/keep-alive: terminate this transaction,
4963 * possibly killing the server connection and reinitialize
4964 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 */
4966 http_end_txn_clean_session(s);
4967 }
4968
4969 http_silent_debug(__LINE__, s);
4970 return txn->req.msg_state != old_req_state ||
4971 txn->rsp.msg_state != old_res_state;
4972}
4973
Willy Tarreaud98cf932009-12-27 22:54:55 +01004974/* This function is an analyser which forwards request body (including chunk
4975 * sizes if any). It is called as soon as we must forward, even if we forward
4976 * zero byte. The only situation where it must not be called is when we're in
4977 * tunnel mode and we want to forward till the close. It's used both to forward
4978 * remaining data and to resync after end of body. It expects the msg_state to
4979 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4980 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004981 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004982 * bytes of pending data + the headers if not already done (between sol and sov).
4983 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004985int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004986{
4987 struct http_txn *txn = &s->txn;
4988 struct http_msg *msg = &s->txn.req;
4989
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004990 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4991 return 0;
4992
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004993 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004994 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004995 /* Output closed while we were sending data. We must abort and
4996 * wake the other side up.
4997 */
4998 msg->msg_state = HTTP_MSG_ERROR;
4999 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005000 return 1;
5001 }
5002
Willy Tarreau80a92c02014-03-12 10:41:13 +01005003 /* Some post-connect processing might want us to refrain from starting to
5004 * forward data. Currently, the only reason for this is "balance url_param"
5005 * whichs need to parse/process the request after we've enabled forwarding.
5006 */
5007 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5008 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5009 channel_auto_connect(req);
5010 goto missing_data;
5011 }
5012 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5013 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005014
5015 /* Note that we don't have to send 100-continue back because we don't
5016 * need the data to complete our job, and it's up to the server to
5017 * decide whether to return 100, 417 or anything else in return of
5018 * an "Expect: 100-continue" header.
5019 */
5020
5021 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005022 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02005023 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02005024 * is still null. We must save the body in msg->next because it
5025 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005026 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005027 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005028
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005029 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005030 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005031 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005032 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005033 }
5034
Willy Tarreau80a92c02014-03-12 10:41:13 +01005035 /* in most states, we should abort in case of early close */
5036 channel_auto_close(req);
5037
Willy Tarreaud98cf932009-12-27 22:54:55 +01005038 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02005039 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005040
Willy Tarreau610ecce2010-01-04 21:15:02 +01005041 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005042 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005043 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005044 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005045 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005046 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005047 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005048 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005049 }
Willy Tarreau5523b322009-12-29 12:05:52 +01005050
Willy Tarreaucaabe412010-01-03 23:08:28 +01005051 if (msg->msg_state == HTTP_MSG_DATA) {
5052 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005053 if (req->to_forward) {
5054 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005055 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005056 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005057
5058 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005059 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005060 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005062 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005063 }
5064 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005065 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005066 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005067 * TRAILERS state.
5068 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005069 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005070
Willy Tarreau54d23df2012-10-25 19:04:45 +02005071 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005072 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005073 else if (ret < 0) {
5074 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005075 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005076 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005077 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005078 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005079 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005080 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005081 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005082 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005083 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005084
5085 if (ret == 0)
5086 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005087 else if (ret < 0) {
5088 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005089 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005090 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005091 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005092 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005093 /* we're in MSG_CHUNK_SIZE now */
5094 }
5095 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005096 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005097
5098 if (ret == 0)
5099 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005100 else if (ret < 0) {
5101 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005102 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005103 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005104 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005105 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005106 /* we're in HTTP_MSG_DONE now */
5107 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005109 int old_state = msg->msg_state;
5110
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005112 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005113 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5114 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005115 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116 if (http_resync_states(s)) {
5117 /* some state changes occurred, maybe the analyser
5118 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005119 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005120 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005121 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005122 /* request errors are most likely due to
5123 * the server aborting the transfer.
5124 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005125 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005126 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005127 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005128 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005130 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005132 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005133
5134 /* If "option abortonclose" is set on the backend, we
5135 * want to monitor the client's connection and forward
5136 * any shutdown notification to the server, which will
5137 * decide whether to close or to go on processing the
5138 * request.
5139 */
5140 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005141 channel_auto_read(req);
5142 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005143 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005144 else if (s->txn.meth == HTTP_METH_POST) {
5145 /* POST requests may require to read extra CRLF
5146 * sent by broken browsers and which could cause
5147 * an RST to be sent upon close on some systems
5148 * (eg: Linux).
5149 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005150 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005151 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005152
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005154 }
5155 }
5156
Willy Tarreaud98cf932009-12-27 22:54:55 +01005157 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005158 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005159 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005160 if (!(s->flags & SN_ERR_MASK))
5161 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005162 if (!(s->flags & SN_FINST_MASK)) {
5163 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5164 s->flags |= SN_FINST_H;
5165 else
5166 s->flags |= SN_FINST_D;
5167 }
5168
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005169 s->fe->fe_counters.cli_aborts++;
5170 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005171 if (objt_server(s->target))
5172 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005173
5174 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005175 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005176
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005177 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005178 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005179 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005181 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005182 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005183 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005184 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005185 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005186
Willy Tarreau5c620922011-05-11 19:56:11 +02005187 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005188 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005189 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005190 * modes are already handled by the stream sock layer. We must not do
5191 * this in content-length mode because it could present the MSG_MORE
5192 * flag with the last block of forwarded data, which would cause an
5193 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005194 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005195 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005196 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005197
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005199 return 0;
5200
Willy Tarreaud98cf932009-12-27 22:54:55 +01005201 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005202 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005203 if (s->listener->counters)
5204 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005205 return_bad_req_stats_ok:
5206 txn->req.msg_state = HTTP_MSG_ERROR;
5207 if (txn->status) {
5208 /* Note: we don't send any error if some data were already sent */
5209 stream_int_retnclose(req->prod, NULL);
5210 } else {
5211 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005212 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005213 }
5214 req->analysers = 0;
5215 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005216
5217 if (!(s->flags & SN_ERR_MASK))
5218 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005219 if (!(s->flags & SN_FINST_MASK)) {
5220 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5221 s->flags |= SN_FINST_H;
5222 else
5223 s->flags |= SN_FINST_D;
5224 }
5225 return 0;
5226
5227 aborted_xfer:
5228 txn->req.msg_state = HTTP_MSG_ERROR;
5229 if (txn->status) {
5230 /* Note: we don't send any error if some data were already sent */
5231 stream_int_retnclose(req->prod, NULL);
5232 } else {
5233 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005234 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005235 }
5236 req->analysers = 0;
5237 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5238
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005239 s->fe->fe_counters.srv_aborts++;
5240 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005241 if (objt_server(s->target))
5242 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005243
5244 if (!(s->flags & SN_ERR_MASK))
5245 s->flags |= SN_ERR_SRVCL;
5246 if (!(s->flags & SN_FINST_MASK)) {
5247 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5248 s->flags |= SN_FINST_H;
5249 else
5250 s->flags |= SN_FINST_D;
5251 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005252 return 0;
5253}
5254
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5256 * processing can continue on next analysers, or zero if it either needs more
5257 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5258 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5259 * when it has nothing left to do, and may remove any analyser when it wants to
5260 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005261 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005262int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005263{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 struct http_txn *txn = &s->txn;
5265 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005266 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005267 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005269 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005270
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005271 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 +02005272 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005273 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005274 rep,
5275 rep->rex, rep->wex,
5276 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005277 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005278 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005279
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005280 /*
5281 * Now parse the partial (or complete) lines.
5282 * We will check the response syntax, and also join multi-line
5283 * headers. An index of all the lines will be elaborated while
5284 * parsing.
5285 *
5286 * For the parsing, we use a 28 states FSM.
5287 *
5288 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005289 * rep->buf->p = beginning of response
5290 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5291 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005292 * msg->eol = end of current header or line (LF or CRLF)
5293 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005294 */
5295
Willy Tarreau83e3af02009-12-28 17:39:57 +01005296 /* There's a protected area at the end of the buffer for rewriting
5297 * purposes. We don't want to start to parse the request if the
5298 * protected area is affected, because we may have to move processed
5299 * data later, which is much more complicated.
5300 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005301 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005302 if (unlikely(!channel_reserved(rep))) {
5303 /* some data has still not left the buffer, wake us once that's done */
5304 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5305 goto abort_response;
5306 channel_dont_close(rep);
5307 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005308 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005309 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005310 }
5311
Willy Tarreau379357a2013-06-08 12:55:46 +02005312 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5313 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5314 buffer_slow_realign(rep->buf);
5315
Willy Tarreau9b28e032012-10-12 23:49:43 +02005316 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005317 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005318 }
5319
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005320 /* 1: we might have to print this header in debug mode */
5321 if (unlikely((global.mode & MODE_DEBUG) &&
5322 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005323 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005324 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005325
Willy Tarreau9b28e032012-10-12 23:49:43 +02005326 sol = rep->buf->p;
5327 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005329
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 sol += hdr_idx_first_pos(&txn->hdr_idx);
5331 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005332
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005333 while (cur_idx) {
5334 eol = sol + txn->hdr_idx.v[cur_idx].len;
5335 debug_hdr("srvhdr", s, sol, eol);
5336 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5337 cur_idx = txn->hdr_idx.v[cur_idx].next;
5338 }
5339 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005340
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005341 /*
5342 * Now we quickly check if we have found a full valid response.
5343 * If not so, we check the FD and buffer states before leaving.
5344 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005345 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005346 * responses are checked first.
5347 *
5348 * Depending on whether the client is still there or not, we
5349 * may send an error response back or not. Note that normally
5350 * we should only check for HTTP status there, and check I/O
5351 * errors somewhere else.
5352 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005353
Willy Tarreau655dce92009-11-08 13:10:58 +01005354 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005355 /* Invalid response */
5356 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5357 /* we detected a parsing error. We want to archive this response
5358 * in the dedicated proxy area for later troubleshooting.
5359 */
5360 hdr_response_bad:
5361 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005362 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005363
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005364 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005365 if (objt_server(s->target)) {
5366 objt_server(s->target)->counters.failed_resp++;
5367 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005368 }
Willy Tarreau64648412010-03-05 10:41:54 +01005369 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005370 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005371 rep->analysers = 0;
5372 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005373 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005374 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005375 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005376
5377 if (!(s->flags & SN_ERR_MASK))
5378 s->flags |= SN_ERR_PRXCOND;
5379 if (!(s->flags & SN_FINST_MASK))
5380 s->flags |= SN_FINST_H;
5381
5382 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005383 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005384
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005385 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005386 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005387 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005388 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389 goto hdr_response_bad;
5390 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005391
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005392 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005393 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005394 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005395 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005396 else if (txn->flags & TX_NOT_FIRST)
5397 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005398
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005399 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005400 if (objt_server(s->target)) {
5401 objt_server(s->target)->counters.failed_resp++;
5402 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005403 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005404
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005405 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005406 rep->analysers = 0;
5407 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005408 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005409 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005410 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005411
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005412 if (!(s->flags & SN_ERR_MASK))
5413 s->flags |= SN_ERR_SRVCL;
5414 if (!(s->flags & SN_FINST_MASK))
5415 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005416 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005417 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005418
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005420 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005421 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005422 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005423 else if (txn->flags & TX_NOT_FIRST)
5424 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005425
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005426 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005427 if (objt_server(s->target)) {
5428 objt_server(s->target)->counters.failed_resp++;
5429 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005430 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005431
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005432 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433 rep->analysers = 0;
5434 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005435 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005436 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005437 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005438
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005439 if (!(s->flags & SN_ERR_MASK))
5440 s->flags |= SN_ERR_SRVTO;
5441 if (!(s->flags & SN_FINST_MASK))
5442 s->flags |= SN_FINST_H;
5443 return 0;
5444 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005445
Willy Tarreauf003d372012-11-26 13:35:37 +01005446 /* client abort with an abortonclose */
5447 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5448 s->fe->fe_counters.cli_aborts++;
5449 s->be->be_counters.cli_aborts++;
5450 if (objt_server(s->target))
5451 objt_server(s->target)->counters.cli_aborts++;
5452
5453 rep->analysers = 0;
5454 channel_auto_close(rep);
5455
5456 txn->status = 400;
5457 bi_erase(rep);
5458 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5459
5460 if (!(s->flags & SN_ERR_MASK))
5461 s->flags |= SN_ERR_CLICL;
5462 if (!(s->flags & SN_FINST_MASK))
5463 s->flags |= SN_FINST_H;
5464
5465 /* process_session() will take care of the error */
5466 return 0;
5467 }
5468
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005469 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005470 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005471 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005472 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005473 else if (txn->flags & TX_NOT_FIRST)
5474 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005475
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005476 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005477 if (objt_server(s->target)) {
5478 objt_server(s->target)->counters.failed_resp++;
5479 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005480 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005481
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005482 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005483 rep->analysers = 0;
5484 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005485 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005486 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005487 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005489 if (!(s->flags & SN_ERR_MASK))
5490 s->flags |= SN_ERR_SRVCL;
5491 if (!(s->flags & SN_FINST_MASK))
5492 s->flags |= SN_FINST_H;
5493 return 0;
5494 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005495
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005496 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005497 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005498 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005499 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005500 else if (txn->flags & TX_NOT_FIRST)
5501 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005502
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005503 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005504 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005505 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005506
5507 if (!(s->flags & SN_ERR_MASK))
5508 s->flags |= SN_ERR_CLICL;
5509 if (!(s->flags & SN_FINST_MASK))
5510 s->flags |= SN_FINST_H;
5511
5512 /* process_session() will take care of the error */
5513 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005514 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005515
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005516 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005517 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005518 return 0;
5519 }
5520
5521 /* More interesting part now : we know that we have a complete
5522 * response which at least looks like HTTP. We have an indicator
5523 * of each header's length, so we can parse them quickly.
5524 */
5525
5526 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005527 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005529 /*
5530 * 1: get the status code
5531 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005532 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005533 if (n < 1 || n > 5)
5534 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005535 /* when the client triggers a 4xx from the server, it's most often due
5536 * to a missing object or permission. These events should be tracked
5537 * because if they happen often, it may indicate a brute force or a
5538 * vulnerability scan.
5539 */
5540 if (n == 4)
5541 session_inc_http_err_ctr(s);
5542
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005543 if (objt_server(s->target))
5544 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005545
Willy Tarreau5b154472009-12-21 20:11:07 +01005546 /* check if the response is HTTP/1.1 or above */
5547 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005548 ((rep->buf->p[5] > '1') ||
5549 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005550 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005551
5552 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005553 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 +01005554
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005555 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005556 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005557
Willy Tarreau9b28e032012-10-12 23:49:43 +02005558 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005559
Willy Tarreau39650402010-03-15 19:44:39 +01005560 /* Adjust server's health based on status code. Note: status codes 501
5561 * and 505 are triggered on demand by client request, so we must not
5562 * count them as server failures.
5563 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005564 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005565 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005566 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005567 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005568 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005569 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005570
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005571 /*
5572 * 2: check for cacheability.
5573 */
5574
5575 switch (txn->status) {
5576 case 200:
5577 case 203:
5578 case 206:
5579 case 300:
5580 case 301:
5581 case 410:
5582 /* RFC2616 @13.4:
5583 * "A response received with a status code of
5584 * 200, 203, 206, 300, 301 or 410 MAY be stored
5585 * by a cache (...) unless a cache-control
5586 * directive prohibits caching."
5587 *
5588 * RFC2616 @9.5: POST method :
5589 * "Responses to this method are not cacheable,
5590 * unless the response includes appropriate
5591 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005592 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005593 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005594 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005595 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5596 break;
5597 default:
5598 break;
5599 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005600
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005601 /*
5602 * 3: we may need to capture headers
5603 */
5604 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005605 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005606 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005607 txn->rsp.cap, s->fe->rsp_cap);
5608
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005609 /* 4: determine the transfer-length.
5610 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5611 * the presence of a message-body in a RESPONSE and its transfer length
5612 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005613 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005614 * All responses to the HEAD request method MUST NOT include a
5615 * message-body, even though the presence of entity-header fields
5616 * might lead one to believe they do. All 1xx (informational), 204
5617 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5618 * message-body. All other responses do include a message-body,
5619 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005620 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005621 * 1. Any response which "MUST NOT" include a message-body (such as the
5622 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5623 * always terminated by the first empty line after the header fields,
5624 * regardless of the entity-header fields present in the message.
5625 *
5626 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5627 * the "chunked" transfer-coding (Section 6.2) is used, the
5628 * transfer-length is defined by the use of this transfer-coding.
5629 * If a Transfer-Encoding header field is present and the "chunked"
5630 * transfer-coding is not present, the transfer-length is defined by
5631 * the sender closing the connection.
5632 *
5633 * 3. If a Content-Length header field is present, its decimal value in
5634 * OCTETs represents both the entity-length and the transfer-length.
5635 * If a message is received with both a Transfer-Encoding header
5636 * field and a Content-Length header field, the latter MUST be ignored.
5637 *
5638 * 4. If the message uses the media type "multipart/byteranges", and
5639 * the transfer-length is not otherwise specified, then this self-
5640 * delimiting media type defines the transfer-length. This media
5641 * type MUST NOT be used unless the sender knows that the recipient
5642 * can parse it; the presence in a request of a Range header with
5643 * multiple byte-range specifiers from a 1.1 client implies that the
5644 * client can parse multipart/byteranges responses.
5645 *
5646 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005647 */
5648
5649 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005650 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005651 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005652 * FIXME: should we parse anyway and return an error on chunked encoding ?
5653 */
5654 if (txn->meth == HTTP_METH_HEAD ||
5655 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005656 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005657 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005658 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005659 goto skip_content_length;
5660 }
5661
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005662 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005663 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005664 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005665 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005666 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005667 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5668 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005669 /* bad transfer-encoding (chunked followed by something else) */
5670 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005671 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005672 break;
5673 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005674 }
5675
5676 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5677 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005678 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005679 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005680 signed long long cl;
5681
Willy Tarreauad14f752011-09-02 20:33:27 +02005682 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005683 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005684 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005685 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005686
Willy Tarreauad14f752011-09-02 20:33:27 +02005687 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005688 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005689 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005690 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005691
Willy Tarreauad14f752011-09-02 20:33:27 +02005692 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005693 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005694 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005695 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005696
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005697 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005698 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005699 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005700 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005701
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005702 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005703 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005704 }
5705
William Lallemand82fe75c2012-10-23 10:25:10 +02005706 if (s->fe->comp || s->be->comp)
5707 select_compression_response_header(s, rep->buf);
5708
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005709 /* FIXME: we should also implement the multipart/byterange method.
5710 * For now on, we resort to close mode in this case (unknown length).
5711 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005712skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005713
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005714 /* end of job, return OK */
5715 rep->analysers &= ~an_bit;
5716 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005717 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005718 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005719
5720 abort_keep_alive:
5721 /* A keep-alive request to the server failed on a network error.
5722 * The client is required to retry. We need to close without returning
5723 * any other information so that the client retries.
5724 */
5725 txn->status = 0;
5726 rep->analysers = 0;
5727 s->req->analysers = 0;
5728 channel_auto_close(rep);
5729 s->logs.logwait = 0;
5730 s->logs.level = 0;
5731 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5732 bi_erase(rep);
5733 stream_int_retnclose(rep->cons, NULL);
5734 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005735}
5736
5737/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005738 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5739 * and updates t->rep->analysers. It might make sense to explode it into several
5740 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005741 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005742int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005743{
5744 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005745 struct http_msg *msg = &txn->rsp;
5746 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005747 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005748 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005749
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005750 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 +02005751 now_ms, __FUNCTION__,
5752 t,
5753 rep,
5754 rep->rex, rep->wex,
5755 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005756 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005757 rep->analysers);
5758
Willy Tarreau655dce92009-11-08 13:10:58 +01005759 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005760 return 0;
5761
5762 rep->analysers &= ~an_bit;
5763 rep->analyse_exp = TICK_ETERNITY;
5764
Willy Tarreau5b154472009-12-21 20:11:07 +01005765 /* Now we have to check if we need to modify the Connection header.
5766 * This is more difficult on the response than it is on the request,
5767 * because we can have two different HTTP versions and we don't know
5768 * how the client will interprete a response. For instance, let's say
5769 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5770 * HTTP/1.1 response without any header. Maybe it will bound itself to
5771 * HTTP/1.0 because it only knows about it, and will consider the lack
5772 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5773 * the lack of header as a keep-alive. Thus we will use two flags
5774 * indicating how a request MAY be understood by the client. In case
5775 * of multiple possibilities, we'll fix the header to be explicit. If
5776 * ambiguous cases such as both close and keepalive are seen, then we
5777 * will fall back to explicit close. Note that we won't take risks with
5778 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005779 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005780 */
5781
Willy Tarreaudc008c52010-02-01 16:20:08 +01005782 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5783 txn->status == 101)) {
5784 /* Either we've established an explicit tunnel, or we're
5785 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005786 * to understand the next protocols. We have to switch to tunnel
5787 * mode, so that we transfer the request and responses then let
5788 * this protocol pass unmodified. When we later implement specific
5789 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005790 * header which contains information about that protocol for
5791 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005792 */
5793 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5794 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005795 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5796 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005797 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5798 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005799 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005800
Willy Tarreau70dffda2014-01-30 03:07:23 +01005801 /* this situation happens when combining pretend-keepalive with httpclose. */
5802 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5803 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5804 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5805 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5806
Willy Tarreau60466522010-01-18 19:08:45 +01005807 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005808 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005809 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5810 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005811
Willy Tarreau60466522010-01-18 19:08:45 +01005812 /* now adjust header transformations depending on current state */
5813 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5814 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5815 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005816 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005817 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005818 }
Willy Tarreau60466522010-01-18 19:08:45 +01005819 else { /* SCL / KAL */
5820 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005821 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005822 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005823 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005824
Willy Tarreau60466522010-01-18 19:08:45 +01005825 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005826 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005827
Willy Tarreau60466522010-01-18 19:08:45 +01005828 /* Some keep-alive responses are converted to Server-close if
5829 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005830 */
Willy Tarreau60466522010-01-18 19:08:45 +01005831 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5832 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005833 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005834 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005835 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005836 }
5837
Willy Tarreau7959a552013-09-23 16:44:27 +02005838 /* we want to have the response time before we start processing it */
5839 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5840
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005841 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005842 /*
5843 * 3: we will have to evaluate the filters.
5844 * As opposed to version 1.2, now they will be evaluated in the
5845 * filters order and not in the header order. This means that
5846 * each filter has to be validated among all headers.
5847 *
5848 * Filters are tried with ->be first, then with ->fe if it is
5849 * different from ->be.
5850 */
5851
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005852 cur_proxy = t->be;
5853 while (1) {
5854 struct proxy *rule_set = cur_proxy;
5855
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005856 /* evaluate http-response rules */
5857 if (!http_res_last_rule)
5858 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5859
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005860 /* try headers filters */
5861 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005862 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005863 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005864 if (objt_server(t->target)) {
5865 objt_server(t->target)->counters.failed_resp++;
5866 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005867 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005868 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005870 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005871 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005872 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005873 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005874 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005875 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876 if (!(t->flags & SN_ERR_MASK))
5877 t->flags |= SN_ERR_PRXCOND;
5878 if (!(t->flags & SN_FINST_MASK))
5879 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005880 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005881 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005882 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005884 /* has the response been denied ? */
5885 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005886 if (objt_server(t->target))
5887 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005888
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005889 t->be->be_counters.denied_resp++;
5890 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005891 if (t->listener->counters)
5892 t->listener->counters->denied_resp++;
5893
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005894 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005895 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005896
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005897 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005898 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005899 if (txn->status < 200)
5900 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005901 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005902 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005903 ret = acl_pass(ret);
5904 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5905 ret = !ret;
5906 if (!ret)
5907 continue;
5908 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005909 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005910 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005911 }
5912
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005913 /* check whether we're already working on the frontend */
5914 if (cur_proxy == t->fe)
5915 break;
5916 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005917 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005918
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005919 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005920 * We may be facing a 100-continue response, in which case this
5921 * is not the right response, and we're waiting for the next one.
5922 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005923 * next one.
5924 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005925 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005926 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005927 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005928 msg->msg_state = HTTP_MSG_RPBEFORE;
5929 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005930 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005931 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5932 return 1;
5933 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005934 else if (unlikely(txn->status < 200))
5935 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005936
5937 /* we don't have any 1xx status code now */
5938
5939 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005940 * 4: check for server cookie.
5941 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005942 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5943 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005944 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005945
Willy Tarreaubaaee002006-06-26 02:48:02 +02005946
Willy Tarreaua15645d2007-03-18 16:22:39 +01005947 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005948 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005949 */
Willy Tarreau67402132012-05-31 20:40:20 +02005950 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005951 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005952
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005953 /*
5954 * 6: add server cookie in the response if needed
5955 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005956 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005957 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005958 (!(t->flags & SN_DIRECT) ||
5959 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5960 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5961 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5962 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005963 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005964 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005965 /* the server is known, it's not the one the client requested, or the
5966 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005967 * insert a set-cookie here, except if we want to insert only on POST
5968 * requests and this one isn't. Note that servers which don't have cookies
5969 * (eg: some backup servers) will return a full cookie removal request.
5970 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005971 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005972 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005973 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5974 t->be->cookie_name);
5975 }
5976 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005977 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005978
5979 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5980 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005981 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5982 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5983 trash.len += 5;
5984
Willy Tarreauef4f3912010-10-07 21:00:29 +02005985 if (t->be->cookie_maxlife) {
5986 /* emit first_date, which is either the original one or
5987 * the current date.
5988 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005989 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005990 s30tob64(txn->cookie_first_date ?
5991 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005992 (date.tv_sec+3) >> 2, trash.str + trash.len);
5993 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005994 }
5995 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005996 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005997 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005998
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005999 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006000 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006001
Willy Tarreau4992dd22012-05-31 21:02:17 +02006002 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006003 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006004
6005 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006006 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006007
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006008 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006009 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006010
Willy Tarreauf1348312010-10-07 15:54:11 +02006011 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006012 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02006013 /* the server did not change, only the date was updated */
6014 txn->flags |= TX_SCK_UPDATED;
6015 else
6016 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006017
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006018 /* Here, we will tell an eventual cache on the client side that we don't
6019 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6020 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6021 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
6022 */
Willy Tarreau67402132012-05-31 20:40:20 +02006023 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02006024
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006025 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6026
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006027 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01006028 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006029 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006030 }
6031 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006032
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006033 /*
6034 * 7: check if result will be cacheable with a cookie.
6035 * We'll block the response if security checks have caught
6036 * nasty things such as a cacheable cookie.
6037 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006038 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6039 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006040 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006041
6042 /* we're in presence of a cacheable response containing
6043 * a set-cookie header. We'll block it as requested by
6044 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006045 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006046 if (objt_server(t->target))
6047 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006048
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006049 t->be->be_counters.denied_resp++;
6050 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006051 if (t->listener->counters)
6052 t->listener->counters->denied_resp++;
6053
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006054 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006055 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006056 send_log(t->be, LOG_ALERT,
6057 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006058 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006059 goto return_srv_prx_502;
6060 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006061
6062 /*
Willy Tarreau60466522010-01-18 19:08:45 +01006063 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01006064 * If an "Upgrade" token is found, the header is left untouched in order
6065 * not to have to deal with some client bugs : some of them fail an upgrade
6066 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006067 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006068 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6069 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01006070 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6071 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006072 unsigned int want_flags = 0;
6073
6074 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6075 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6076 /* we want a keep-alive response here. Keep-alive header
6077 * required if either side is not 1.1.
6078 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006079 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006080 want_flags |= TX_CON_KAL_SET;
6081 }
6082 else {
6083 /* we want a close response here. Close header required if
6084 * the server is 1.1, regardless of the client.
6085 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006086 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006087 want_flags |= TX_CON_CLO_SET;
6088 }
6089
6090 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006091 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006092 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006093
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006094 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006095 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006096 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006097 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006098
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006099 /*************************************************************
6100 * OK, that's finished for the headers. We have done what we *
6101 * could. Let's switch to the DATA state. *
6102 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006103
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006104 /* if the user wants to log as soon as possible, without counting
6105 * bytes from the server, then this is the right moment. We have
6106 * to temporarily assign bytes_out to log what we currently have.
6107 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006108 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006109 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6110 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006111 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006112 t->logs.bytes_out = 0;
6113 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006114
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006115 /* Note: we must not try to cheat by jumping directly to DATA,
6116 * otherwise we would not let the client side wake up.
6117 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006118
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006119 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006120 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006121 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006122}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006123
Willy Tarreaud98cf932009-12-27 22:54:55 +01006124/* This function is an analyser which forwards response body (including chunk
6125 * sizes if any). It is called as soon as we must forward, even if we forward
6126 * zero byte. The only situation where it must not be called is when we're in
6127 * tunnel mode and we want to forward till the close. It's used both to forward
6128 * remaining data and to resync after end of body. It expects the msg_state to
6129 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6130 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006131 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006132 * bytes of pending data + the headers if not already done (between sol and sov).
6133 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006134 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006135int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006136{
6137 struct http_txn *txn = &s->txn;
6138 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02006139 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02006140 static struct buffer *tmpbuf = NULL;
6141 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006142 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006143 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006144
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006145 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6146 return 0;
6147
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006148 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006149 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006150 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006151 /* Output closed while we were sending data. We must abort and
6152 * wake the other side up.
6153 */
6154 msg->msg_state = HTTP_MSG_ERROR;
6155 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006156 return 1;
6157 }
6158
Willy Tarreau4fe41902010-06-07 22:27:41 +02006159 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006160 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006161
William Lallemand82fe75c2012-10-23 10:25:10 +02006162 /* this is the first time we need the compression buffer */
6163 if (s->comp_algo != NULL && tmpbuf == NULL) {
6164 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6165 goto aborted_xfer; /* no memory */
6166 }
6167
Willy Tarreaud98cf932009-12-27 22:54:55 +01006168 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006169 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006170 * rep->buf.p still points to the beginning of the message and msg->sol
6171 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006172 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006173 channel_forward(res, msg->sov);
6174 msg->next = 0;
6175 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006176
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006177 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006178 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006179 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006180 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006181 }
6182
William Lallemand82fe75c2012-10-23 10:25:10 +02006183 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006184 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006185 if (ret < 0) {
6186 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006187 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006188 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006189 compressing = 1;
6190 }
6191
Willy Tarreaud98cf932009-12-27 22:54:55 +01006192 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006193 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006194 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006195 if (s->comp_algo == NULL) {
6196 bytes = msg->sov - msg->sol;
6197 if (msg->chunk_len || bytes) {
6198 msg->sol = msg->sov;
6199 msg->next -= bytes; /* will be forwarded */
6200 msg->chunk_len += bytes;
6201 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6202 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006203 }
6204
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006205 switch (msg->msg_state - HTTP_MSG_DATA) {
6206 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006207 if (compressing) {
6208 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6209 if (ret < 0)
6210 goto aborted_xfer;
6211 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006212
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006213 if (res->to_forward || msg->chunk_len) {
6214 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006215 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006216 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006217
6218 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006219 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006220 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006221 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006222 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006223 if (compressing && consumed_data) {
6224 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6225 compressing = 0;
6226 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006227 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006228 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006229 /* fall through for HTTP_MSG_CHUNK_CRLF */
6230
6231 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6232 /* we want the CRLF after the data */
6233
6234 ret = http_skip_chunk_crlf(msg);
6235 if (ret == 0)
6236 goto missing_data;
6237 else if (ret < 0) {
6238 if (msg->err_pos >= 0)
6239 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6240 goto return_bad_res;
6241 }
6242 /* skipping data in buffer for compression */
6243 if (compressing) {
6244 b_adv(res->buf, msg->next);
6245 msg->next = 0;
6246 msg->sov = 0;
6247 msg->sol = 0;
6248 }
6249 /* we're in MSG_CHUNK_SIZE now, fall through */
6250
6251 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006252 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006253 * set ->sov and ->next to point to the body and switch to DATA or
6254 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006255 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006256
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006257 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006258 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006259 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006260 else if (ret < 0) {
6261 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006262 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006263 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006264 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006265 if (compressing) {
6266 if (likely(msg->chunk_len > 0)) {
6267 /* skipping data if we are in compression mode */
6268 b_adv(res->buf, msg->next);
6269 msg->next = 0;
6270 msg->sov = 0;
6271 msg->sol = 0;
6272 } else {
6273 if (consumed_data) {
6274 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6275 compressing = 0;
6276 }
6277 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006278 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006279 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006280 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006281
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006282 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6283 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006284 if (ret == 0)
6285 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006286 else if (ret < 0) {
6287 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006288 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006289 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006290 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006291 if (s->comp_algo != NULL) {
6292 /* forwarding trailers */
6293 channel_forward(res, msg->next);
6294 msg->next = 0;
6295 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006296 /* we're in HTTP_MSG_DONE now, but we might still have
6297 * some data pending, so let's loop over once.
6298 */
6299 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006300
6301 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006302 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006303
6304 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006305 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006306 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6307 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006308 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006309 if (http_resync_states(s)) {
6310 http_silent_debug(__LINE__, s);
6311 /* some state changes occurred, maybe the analyser
6312 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006313 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006314 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006315 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006316 /* response errors are most likely due to
6317 * the client aborting the transfer.
6318 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006319 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006320 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006321 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006322 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006323 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006324 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006325 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006326 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006327 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006328 }
6329 }
6330
Willy Tarreaud98cf932009-12-27 22:54:55 +01006331 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006332 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006333 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6334 compressing = 0;
6335 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006336
6337 if (res->flags & CF_SHUTW)
6338 goto aborted_xfer;
6339
6340 /* stop waiting for data if the input is closed before the end. If the
6341 * client side was already closed, it means that the client has aborted,
6342 * so we don't want to count this as a server abort. Otherwise it's a
6343 * server abort.
6344 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006345 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006346 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6347 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006348 if (!(s->flags & SN_ERR_MASK))
6349 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006350 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006351 if (objt_server(s->target))
6352 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006353 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006354 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006355
Willy Tarreau40dba092010-03-04 18:14:51 +01006356 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006357 if (!s->req->analysers)
6358 goto return_bad_res;
6359
Willy Tarreauea953162012-05-18 23:41:28 +02006360 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006361 if (s->comp_algo == NULL) {
6362 bytes = msg->sov - msg->sol;
6363 if (msg->chunk_len || bytes) {
6364 msg->sol = msg->sov;
6365 msg->next -= bytes; /* will be forwarded */
6366 msg->chunk_len += bytes;
6367 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6368 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006369 }
6370
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006371 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006372 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006373 * Similarly, with keep-alive on the client side, we don't want to forward a
6374 * close.
6375 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006376 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006377 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6378 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006379 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006380
Willy Tarreau5c620922011-05-11 19:56:11 +02006381 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006382 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006383 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006384 * modes are already handled by the stream sock layer. We must not do
6385 * this in content-length mode because it could present the MSG_MORE
6386 * flag with the last block of forwarded data, which would cause an
6387 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006388 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006389 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006390 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006391
Willy Tarreaud98cf932009-12-27 22:54:55 +01006392 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006393 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006394 return 0;
6395
Willy Tarreau40dba092010-03-04 18:14:51 +01006396 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006397 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006398 if (objt_server(s->target))
6399 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006400
6401 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006402 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006403 /* don't send any error message as we're in the body */
6404 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006405 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006406 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006407 if (objt_server(s->target))
6408 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006409
6410 if (!(s->flags & SN_ERR_MASK))
6411 s->flags |= SN_ERR_PRXCOND;
6412 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006413 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006414 return 0;
6415
6416 aborted_xfer:
6417 txn->rsp.msg_state = HTTP_MSG_ERROR;
6418 /* don't send any error message as we're in the body */
6419 stream_int_retnclose(res->cons, NULL);
6420 res->analysers = 0;
6421 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6422
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006423 s->fe->fe_counters.cli_aborts++;
6424 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006425 if (objt_server(s->target))
6426 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006427
6428 if (!(s->flags & SN_ERR_MASK))
6429 s->flags |= SN_ERR_CLICL;
6430 if (!(s->flags & SN_FINST_MASK))
6431 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006432 return 0;
6433}
6434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006435/* Iterate the same filter through all request headers.
6436 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006437 * Since it can manage the switch to another backend, it updates the per-proxy
6438 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006439 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006440int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006442 char term;
6443 char *cur_ptr, *cur_end, *cur_next;
6444 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006445 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006446 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006447 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 last_hdr = 0;
6450
Willy Tarreau9b28e032012-10-12 23:49:43 +02006451 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006452 old_idx = 0;
6453
6454 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006455 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006457 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006458 (exp->action == ACT_ALLOW ||
6459 exp->action == ACT_DENY ||
6460 exp->action == ACT_TARPIT))
6461 return 0;
6462
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006463 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 if (!cur_idx)
6465 break;
6466
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006467 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 cur_ptr = cur_next;
6469 cur_end = cur_ptr + cur_hdr->len;
6470 cur_next = cur_end + cur_hdr->cr + 1;
6471
6472 /* Now we have one header between cur_ptr and cur_end,
6473 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006474 */
6475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 /* The annoying part is that pattern matching needs
6477 * that we modify the contents to null-terminate all
6478 * strings before testing them.
6479 */
6480
6481 term = *cur_end;
6482 *cur_end = '\0';
6483
6484 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6485 switch (exp->action) {
6486 case ACT_SETBE:
6487 /* It is not possible to jump a second time.
6488 * FIXME: should we return an HTTP/500 here so that
6489 * the admin knows there's a problem ?
6490 */
6491 if (t->be != t->fe)
6492 break;
6493
6494 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006495 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496 last_hdr = 1;
6497 break;
6498
6499 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006500 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006501 last_hdr = 1;
6502 break;
6503
6504 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006505 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507 break;
6508
6509 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006510 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 break;
6513
6514 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006515 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6516 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006517 /* FIXME: if the user adds a newline in the replacement, the
6518 * index will not be recalculated for now, and the new line
6519 * will not be counted as a new header.
6520 */
6521
6522 cur_end += delta;
6523 cur_next += delta;
6524 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006525 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 break;
6527
6528 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006529 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530 cur_next += delta;
6531
Willy Tarreaufa355d42009-11-29 18:12:29 +01006532 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006533 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6534 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006535 cur_hdr->len = 0;
6536 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006537 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006538 break;
6539
6540 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006541 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006542 if (cur_end)
6543 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006545 /* keep the link from this header to next one in case of later
6546 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 old_idx = cur_idx;
6549 }
6550 return 0;
6551}
6552
6553
6554/* Apply the filter to the request line.
6555 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6556 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 * Since it can manage the switch to another backend, it updates the per-proxy
6558 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006559 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006560int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561{
6562 char term;
6563 char *cur_ptr, *cur_end;
6564 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006565 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006566 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567
Willy Tarreau3d300592007-03-18 18:34:41 +01006568 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006570 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 (exp->action == ACT_ALLOW ||
6572 exp->action == ACT_DENY ||
6573 exp->action == ACT_TARPIT))
6574 return 0;
6575 else if (exp->action == ACT_REMOVE)
6576 return 0;
6577
6578 done = 0;
6579
Willy Tarreau9b28e032012-10-12 23:49:43 +02006580 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006581 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006582
6583 /* Now we have the request line between cur_ptr and cur_end */
6584
6585 /* The annoying part is that pattern matching needs
6586 * that we modify the contents to null-terminate all
6587 * strings before testing them.
6588 */
6589
6590 term = *cur_end;
6591 *cur_end = '\0';
6592
6593 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6594 switch (exp->action) {
6595 case ACT_SETBE:
6596 /* It is not possible to jump a second time.
6597 * FIXME: should we return an HTTP/500 here so that
6598 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006599 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006600 if (t->be != t->fe)
6601 break;
6602
6603 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006604 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006605 done = 1;
6606 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006608 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006609 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006610 done = 1;
6611 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006613 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006614 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006615 done = 1;
6616 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006618 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006619 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006620 done = 1;
6621 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006623 case ACT_REPLACE:
6624 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006625 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6626 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006627 /* FIXME: if the user adds a newline in the replacement, the
6628 * index will not be recalculated for now, and the new line
6629 * will not be counted as a new header.
6630 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006631
Willy Tarreaufa355d42009-11-29 18:12:29 +01006632 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006633 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006634 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006635 HTTP_MSG_RQMETH,
6636 cur_ptr, cur_end + 1,
6637 NULL, NULL);
6638 if (unlikely(!cur_end))
6639 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006641 /* we have a full request and we know that we have either a CR
6642 * or an LF at <ptr>.
6643 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006644 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6645 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 /* there is no point trying this regex on headers */
6647 return 1;
6648 }
6649 }
6650 *cur_end = term; /* restore the string terminator */
6651 return done;
6652}
Willy Tarreau97de6242006-12-27 17:18:38 +01006653
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654
Willy Tarreau58f10d72006-12-04 02:26:12 +01006655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006656/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006657 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006658 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006659 * unparsable request. Since it can manage the switch to another backend, it
6660 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006662int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006663{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006664 struct http_txn *txn = &s->txn;
6665 struct hdr_exp *exp;
6666
6667 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006668 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006670 /*
6671 * The interleaving of transformations and verdicts
6672 * makes it difficult to decide to continue or stop
6673 * the evaluation.
6674 */
6675
Willy Tarreau6c123b12010-01-28 20:22:06 +01006676 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6677 break;
6678
Willy Tarreau3d300592007-03-18 18:34:41 +01006679 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006680 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006681 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006682 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006683
6684 /* if this filter had a condition, evaluate it now and skip to
6685 * next filter if the condition does not match.
6686 */
6687 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006688 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006689 ret = acl_pass(ret);
6690 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6691 ret = !ret;
6692
6693 if (!ret)
6694 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006695 }
6696
6697 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006698 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006699 if (unlikely(ret < 0))
6700 return -1;
6701
6702 if (likely(ret == 0)) {
6703 /* The filter did not match the request, it can be
6704 * iterated through all headers.
6705 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006706 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006707 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006708 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006709 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710}
6711
6712
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713
Willy Tarreau58f10d72006-12-04 02:26:12 +01006714/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006715 * Try to retrieve the server associated to the appsession.
6716 * If the server is found, it's assigned to the session.
6717 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006718void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006719 struct http_txn *txn = &t->txn;
6720 appsess *asession = NULL;
6721 char *sessid_temp = NULL;
6722
Cyril Bontéb21570a2009-11-29 20:04:48 +01006723 if (len > t->be->appsession_len) {
6724 len = t->be->appsession_len;
6725 }
6726
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006727 if (t->be->options2 & PR_O2_AS_REQL) {
6728 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006729 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006730 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006731 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006732 }
6733
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006734 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006735 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6736 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6737 return;
6738 }
6739
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006740 memcpy(txn->sessid, buf, len);
6741 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006742 }
6743
6744 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6745 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6746 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6747 return;
6748 }
6749
Cyril Bontéb21570a2009-11-29 20:04:48 +01006750 memcpy(sessid_temp, buf, len);
6751 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006752
6753 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6754 /* free previously allocated memory */
6755 pool_free2(apools.sessid, sessid_temp);
6756
6757 if (asession != NULL) {
6758 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6759 if (!(t->be->options2 & PR_O2_AS_REQL))
6760 asession->request_count++;
6761
6762 if (asession->serverid != NULL) {
6763 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006764
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006765 while (srv) {
6766 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006767 if ((srv->state & SRV_RUNNING) ||
6768 (t->be->options & PR_O_PERSIST) ||
6769 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006770 /* we found the server and it's usable */
6771 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006772 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006773 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006774 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006775
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006776 break;
6777 } else {
6778 txn->flags &= ~TX_CK_MASK;
6779 txn->flags |= TX_CK_DOWN;
6780 }
6781 }
6782 srv = srv->next;
6783 }
6784 }
6785 }
6786}
6787
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006788/* Find the end of a cookie value contained between <s> and <e>. It works the
6789 * same way as with headers above except that the semi-colon also ends a token.
6790 * See RFC2965 for more information. Note that it requires a valid header to
6791 * return a valid result.
6792 */
6793char *find_cookie_value_end(char *s, const char *e)
6794{
6795 int quoted, qdpair;
6796
6797 quoted = qdpair = 0;
6798 for (; s < e; s++) {
6799 if (qdpair) qdpair = 0;
6800 else if (quoted) {
6801 if (*s == '\\') qdpair = 1;
6802 else if (*s == '"') quoted = 0;
6803 }
6804 else if (*s == '"') quoted = 1;
6805 else if (*s == ',' || *s == ';') return s;
6806 }
6807 return s;
6808}
6809
6810/* Delete a value in a header between delimiters <from> and <next> in buffer
6811 * <buf>. The number of characters displaced is returned, and the pointer to
6812 * the first delimiter is updated if required. The function tries as much as
6813 * possible to respect the following principles :
6814 * - replace <from> delimiter by the <next> one unless <from> points to a
6815 * colon, in which case <next> is simply removed
6816 * - set exactly one space character after the new first delimiter, unless
6817 * there are not enough characters in the block being moved to do so.
6818 * - remove unneeded spaces before the previous delimiter and after the new
6819 * one.
6820 *
6821 * It is the caller's responsibility to ensure that :
6822 * - <from> points to a valid delimiter or the colon ;
6823 * - <next> points to a valid delimiter or the final CR/LF ;
6824 * - there are non-space chars before <from> ;
6825 * - there is a CR/LF at or after <next>.
6826 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006827int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006828{
6829 char *prev = *from;
6830
6831 if (*prev == ':') {
6832 /* We're removing the first value, preserve the colon and add a
6833 * space if possible.
6834 */
6835 if (!http_is_crlf[(unsigned char)*next])
6836 next++;
6837 prev++;
6838 if (prev < next)
6839 *prev++ = ' ';
6840
6841 while (http_is_spht[(unsigned char)*next])
6842 next++;
6843 } else {
6844 /* Remove useless spaces before the old delimiter. */
6845 while (http_is_spht[(unsigned char)*(prev-1)])
6846 prev--;
6847 *from = prev;
6848
6849 /* copy the delimiter and if possible a space if we're
6850 * not at the end of the line.
6851 */
6852 if (!http_is_crlf[(unsigned char)*next]) {
6853 *prev++ = *next++;
6854 if (prev + 1 < next)
6855 *prev++ = ' ';
6856 while (http_is_spht[(unsigned char)*next])
6857 next++;
6858 }
6859 }
6860 return buffer_replace2(buf, prev, next, NULL, 0);
6861}
6862
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006863/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006864 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 * desirable to call it only when needed. This code is quite complex because
6866 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6867 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006869void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006870{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006871 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006873 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006874 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6875 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006876
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006878 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006879 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006881 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006883 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006884
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006885 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 hdr_beg = hdr_next;
6887 hdr_end = hdr_beg + cur_hdr->len;
6888 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 /* We have one full header between hdr_beg and hdr_end, and the
6891 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006892 * "Cookie:" headers.
6893 */
6894
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006896 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897 old_idx = cur_idx;
6898 continue;
6899 }
6900
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 del_from = NULL; /* nothing to be deleted */
6902 preserve_hdr = 0; /* assume we may kill the whole header */
6903
Willy Tarreau58f10d72006-12-04 02:26:12 +01006904 /* Now look for cookies. Conforming to RFC2109, we have to support
6905 * attributes whose name begin with a '$', and associate them with
6906 * the right cookie, if we want to delete this cookie.
6907 * So there are 3 cases for each cookie read :
6908 * 1) it's a special attribute, beginning with a '$' : ignore it.
6909 * 2) it's a server id cookie that we *MAY* want to delete : save
6910 * some pointers on it (last semi-colon, beginning of cookie...)
6911 * 3) it's an application cookie : we *MAY* have to delete a previous
6912 * "special" cookie.
6913 * At the end of loop, if a "special" cookie remains, we may have to
6914 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 * *MUST* delete it.
6916 *
6917 * Note: RFC2965 is unclear about the processing of spaces around
6918 * the equal sign in the ATTR=VALUE form. A careful inspection of
6919 * the RFC explicitly allows spaces before it, and not within the
6920 * tokens (attrs or values). An inspection of RFC2109 allows that
6921 * too but section 10.1.3 lets one think that spaces may be allowed
6922 * after the equal sign too, resulting in some (rare) buggy
6923 * implementations trying to do that. So let's do what servers do.
6924 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6925 * allowed quoted strings in values, with any possible character
6926 * after a backslash, including control chars and delimitors, which
6927 * causes parsing to become ambiguous. Browsers also allow spaces
6928 * within values even without quotes.
6929 *
6930 * We have to keep multiple pointers in order to support cookie
6931 * removal at the beginning, middle or end of header without
6932 * corrupting the header. All of these headers are valid :
6933 *
6934 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6935 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6936 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6937 * | | | | | | | | |
6938 * | | | | | | | | hdr_end <--+
6939 * | | | | | | | +--> next
6940 * | | | | | | +----> val_end
6941 * | | | | | +-----------> val_beg
6942 * | | | | +--------------> equal
6943 * | | | +----------------> att_end
6944 * | | +---------------------> att_beg
6945 * | +--------------------------> prev
6946 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006947 */
6948
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006949 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6950 /* Iterate through all cookies on this line */
6951
6952 /* find att_beg */
6953 att_beg = prev + 1;
6954 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6955 att_beg++;
6956
6957 /* find att_end : this is the first character after the last non
6958 * space before the equal. It may be equal to hdr_end.
6959 */
6960 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 while (equal < hdr_end) {
6963 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006964 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006965 if (http_is_spht[(unsigned char)*equal++])
6966 continue;
6967 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968 }
6969
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006970 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6971 * is between <att_beg> and <equal>, both may be identical.
6972 */
6973
6974 /* look for end of cookie if there is an equal sign */
6975 if (equal < hdr_end && *equal == '=') {
6976 /* look for the beginning of the value */
6977 val_beg = equal + 1;
6978 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6979 val_beg++;
6980
6981 /* find the end of the value, respecting quotes */
6982 next = find_cookie_value_end(val_beg, hdr_end);
6983
6984 /* make val_end point to the first white space or delimitor after the value */
6985 val_end = next;
6986 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6987 val_end--;
6988 } else {
6989 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006990 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006991
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006992 /* We have nothing to do with attributes beginning with '$'. However,
6993 * they will automatically be removed if a header before them is removed,
6994 * since they're supposed to be linked together.
6995 */
6996 if (*att_beg == '$')
6997 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 /* Ignore cookies with no equal sign */
7000 if (equal == next) {
7001 /* This is not our cookie, so we must preserve it. But if we already
7002 * scheduled another cookie for removal, we cannot remove the
7003 * complete header, but we can remove the previous block itself.
7004 */
7005 preserve_hdr = 1;
7006 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007007 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007008 val_end += delta;
7009 next += delta;
7010 hdr_end += delta;
7011 hdr_next += delta;
7012 cur_hdr->len += delta;
7013 http_msg_move_end(&txn->req, delta);
7014 prev = del_from;
7015 del_from = NULL;
7016 }
7017 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007018 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007020 /* if there are spaces around the equal sign, we need to
7021 * strip them otherwise we'll get trouble for cookie captures,
7022 * or even for rewrites. Since this happens extremely rarely,
7023 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007024 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007025 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7026 int stripped_before = 0;
7027 int stripped_after = 0;
7028
7029 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007030 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007031 equal += stripped_before;
7032 val_beg += stripped_before;
7033 }
7034
7035 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007036 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007037 val_beg += stripped_after;
7038 stripped_before += stripped_after;
7039 }
7040
7041 val_end += stripped_before;
7042 next += stripped_before;
7043 hdr_end += stripped_before;
7044 hdr_next += stripped_before;
7045 cur_hdr->len += stripped_before;
7046 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007050 /* First, let's see if we want to capture this cookie. We check
7051 * that we don't already have a client side cookie, because we
7052 * can only capture one. Also as an optimisation, we ignore
7053 * cookies shorter than the declared name.
7054 */
7055 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7056 (val_end - att_beg >= t->fe->capture_namelen) &&
7057 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7058 int log_len = val_end - att_beg;
7059
7060 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7061 Alert("HTTP logging : out of memory.\n");
7062 } else {
7063 if (log_len > t->fe->capture_len)
7064 log_len = t->fe->capture_len;
7065 memcpy(txn->cli_cookie, att_beg, log_len);
7066 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069
Willy Tarreaubca99692010-10-06 19:25:55 +02007070 /* Persistence cookies in passive, rewrite or insert mode have the
7071 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007073 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007074 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007075 * For cookies in prefix mode, the form is :
7076 *
7077 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007078 */
7079 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7080 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
7081 struct server *srv = t->be->srv;
7082 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007084 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7085 * have the server ID between val_beg and delim, and the original cookie between
7086 * delim+1 and val_end. Otherwise, delim==val_end :
7087 *
7088 * Cookie: NAME=SRV; # in all but prefix modes
7089 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7090 * | || || | |+-> next
7091 * | || || | +--> val_end
7092 * | || || +---------> delim
7093 * | || |+------------> val_beg
7094 * | || +-------------> att_end = equal
7095 * | |+-----------------> att_beg
7096 * | +------------------> prev
7097 * +-------------------------> hdr_beg
7098 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099
Willy Tarreau67402132012-05-31 20:40:20 +02007100 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 for (delim = val_beg; delim < val_end; delim++)
7102 if (*delim == COOKIE_DELIM)
7103 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007104 } else {
7105 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007106 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007107 /* Now check if the cookie contains a date field, which would
7108 * appear after a vertical bar ('|') just after the server name
7109 * and before the delimiter.
7110 */
7111 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7112 if (vbar1) {
7113 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007114 * right is the last seen date. It is a base64 encoded
7115 * 30-bit value representing the UNIX date since the
7116 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007117 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007118 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007119 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007120 if (val_end - vbar1 >= 5) {
7121 val = b64tos30(vbar1);
7122 if (val > 0)
7123 txn->cookie_last_date = val << 2;
7124 }
7125 /* look for a second vertical bar */
7126 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7127 if (vbar1 && (val_end - vbar1 > 5)) {
7128 val = b64tos30(vbar1 + 1);
7129 if (val > 0)
7130 txn->cookie_first_date = val << 2;
7131 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007132 }
7133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007134
Willy Tarreauf64d1412010-10-07 20:06:11 +02007135 /* if the cookie has an expiration date and the proxy wants to check
7136 * it, then we do that now. We first check if the cookie is too old,
7137 * then only if it has expired. We detect strict overflow because the
7138 * time resolution here is not great (4 seconds). Cookies with dates
7139 * in the future are ignored if their offset is beyond one day. This
7140 * allows an admin to fix timezone issues without expiring everyone
7141 * and at the same time avoids keeping unwanted side effects for too
7142 * long.
7143 */
7144 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007145 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7146 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007147 txn->flags &= ~TX_CK_MASK;
7148 txn->flags |= TX_CK_OLD;
7149 delim = val_beg; // let's pretend we have not found the cookie
7150 txn->cookie_first_date = 0;
7151 txn->cookie_last_date = 0;
7152 }
7153 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007154 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7155 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007156 txn->flags &= ~TX_CK_MASK;
7157 txn->flags |= TX_CK_EXPIRED;
7158 delim = val_beg; // let's pretend we have not found the cookie
7159 txn->cookie_first_date = 0;
7160 txn->cookie_last_date = 0;
7161 }
7162
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007163 /* Here, we'll look for the first running server which supports the cookie.
7164 * This allows to share a same cookie between several servers, for example
7165 * to dedicate backup servers to specific servers only.
7166 * However, to prevent clients from sticking to cookie-less backup server
7167 * when they have incidentely learned an empty cookie, we simply ignore
7168 * empty cookies and mark them as invalid.
7169 * The same behaviour is applied when persistence must be ignored.
7170 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007171 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007172 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007173
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007174 while (srv) {
7175 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7176 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7177 if ((srv->state & SRV_RUNNING) ||
7178 (t->be->options & PR_O_PERSIST) ||
7179 (t->flags & SN_FORCE_PRST)) {
7180 /* we found the server and we can use it */
7181 txn->flags &= ~TX_CK_MASK;
7182 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7183 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007184 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007185 break;
7186 } else {
7187 /* we found a server, but it's down,
7188 * mark it as such and go on in case
7189 * another one is available.
7190 */
7191 txn->flags &= ~TX_CK_MASK;
7192 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007193 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007194 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007195 srv = srv->next;
7196 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007197
Willy Tarreauf64d1412010-10-07 20:06:11 +02007198 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007199 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007200 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007201 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7202 txn->flags |= TX_CK_UNUSED;
7203 else
7204 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007207 /* depending on the cookie mode, we may have to either :
7208 * - delete the complete cookie if we're in insert+indirect mode, so that
7209 * the server never sees it ;
7210 * - remove the server id from the cookie value, and tag the cookie as an
7211 * application cookie so that it does not get accidentely removed later,
7212 * if we're in cookie prefix mode
7213 */
Willy Tarreau67402132012-05-31 20:40:20 +02007214 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007215 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007216
Willy Tarreau9b28e032012-10-12 23:49:43 +02007217 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007218 val_end += delta;
7219 next += delta;
7220 hdr_end += delta;
7221 hdr_next += delta;
7222 cur_hdr->len += delta;
7223 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007224
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007225 del_from = NULL;
7226 preserve_hdr = 1; /* we want to keep this cookie */
7227 }
7228 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007229 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007230 del_from = prev;
7231 }
7232 } else {
7233 /* This is not our cookie, so we must preserve it. But if we already
7234 * scheduled another cookie for removal, we cannot remove the
7235 * complete header, but we can remove the previous block itself.
7236 */
7237 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007238
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007239 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007240 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007241 if (att_beg >= del_from)
7242 att_beg += delta;
7243 if (att_end >= del_from)
7244 att_end += delta;
7245 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007246 val_end += delta;
7247 next += delta;
7248 hdr_end += delta;
7249 hdr_next += delta;
7250 cur_hdr->len += delta;
7251 http_msg_move_end(&txn->req, delta);
7252 prev = del_from;
7253 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007254 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007255 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007256
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007257 /* Look for the appsession cookie unless persistence must be ignored */
7258 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7259 int cmp_len, value_len;
7260 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007262 if (t->be->options2 & PR_O2_AS_PFX) {
7263 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7264 value_begin = att_beg + t->be->appsession_name_len;
7265 value_len = val_end - att_beg - t->be->appsession_name_len;
7266 } else {
7267 cmp_len = att_end - att_beg;
7268 value_begin = val_beg;
7269 value_len = val_end - val_beg;
7270 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007271
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007272 /* let's see if the cookie is our appcookie */
7273 if (cmp_len == t->be->appsession_name_len &&
7274 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7275 manage_client_side_appsession(t, value_begin, value_len);
7276 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007277 }
7278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007279 /* continue with next cookie on this header line */
7280 att_beg = next;
7281 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007283 /* There are no more cookies on this line.
7284 * We may still have one (or several) marked for deletion at the
7285 * end of the line. We must do this now in two ways :
7286 * - if some cookies must be preserved, we only delete from the
7287 * mark to the end of line ;
7288 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007289 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007290 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007291 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007292 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007293 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007294 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007295 cur_hdr->len += delta;
7296 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007297 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007298
7299 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007300 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7301 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007302 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007303 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007304 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007305 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007306 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007307 }
7308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007309 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007311 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007312}
7313
7314
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315/* Iterate the same filter through all response headers contained in <rtr>.
7316 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7317 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007318int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319{
7320 char term;
7321 char *cur_ptr, *cur_end, *cur_next;
7322 int cur_idx, old_idx, last_hdr;
7323 struct http_txn *txn = &t->txn;
7324 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007325 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326
7327 last_hdr = 0;
7328
Willy Tarreau9b28e032012-10-12 23:49:43 +02007329 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 old_idx = 0;
7331
7332 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007333 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007335 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 (exp->action == ACT_ALLOW ||
7337 exp->action == ACT_DENY))
7338 return 0;
7339
7340 cur_idx = txn->hdr_idx.v[old_idx].next;
7341 if (!cur_idx)
7342 break;
7343
7344 cur_hdr = &txn->hdr_idx.v[cur_idx];
7345 cur_ptr = cur_next;
7346 cur_end = cur_ptr + cur_hdr->len;
7347 cur_next = cur_end + cur_hdr->cr + 1;
7348
7349 /* Now we have one header between cur_ptr and cur_end,
7350 * and the next header starts at cur_next.
7351 */
7352
7353 /* The annoying part is that pattern matching needs
7354 * that we modify the contents to null-terminate all
7355 * strings before testing them.
7356 */
7357
7358 term = *cur_end;
7359 *cur_end = '\0';
7360
7361 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7362 switch (exp->action) {
7363 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007364 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365 last_hdr = 1;
7366 break;
7367
7368 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007369 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370 last_hdr = 1;
7371 break;
7372
7373 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007374 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7375 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 /* FIXME: if the user adds a newline in the replacement, the
7377 * index will not be recalculated for now, and the new line
7378 * will not be counted as a new header.
7379 */
7380
7381 cur_end += delta;
7382 cur_next += delta;
7383 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007384 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385 break;
7386
7387 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007388 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389 cur_next += delta;
7390
Willy Tarreaufa355d42009-11-29 18:12:29 +01007391 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7393 txn->hdr_idx.used--;
7394 cur_hdr->len = 0;
7395 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007396 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007397 break;
7398
7399 }
7400 }
7401 if (cur_end)
7402 *cur_end = term; /* restore the string terminator */
7403
7404 /* keep the link from this header to next one in case of later
7405 * removal of next header.
7406 */
7407 old_idx = cur_idx;
7408 }
7409 return 0;
7410}
7411
7412
7413/* Apply the filter to the status line in the response buffer <rtr>.
7414 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7415 * or -1 if a replacement resulted in an invalid status line.
7416 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007417int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418{
7419 char term;
7420 char *cur_ptr, *cur_end;
7421 int done;
7422 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007423 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007424
7425
Willy Tarreau3d300592007-03-18 18:34:41 +01007426 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007428 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007429 (exp->action == ACT_ALLOW ||
7430 exp->action == ACT_DENY))
7431 return 0;
7432 else if (exp->action == ACT_REMOVE)
7433 return 0;
7434
7435 done = 0;
7436
Willy Tarreau9b28e032012-10-12 23:49:43 +02007437 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007438 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007439
7440 /* Now we have the status line between cur_ptr and cur_end */
7441
7442 /* The annoying part is that pattern matching needs
7443 * that we modify the contents to null-terminate all
7444 * strings before testing them.
7445 */
7446
7447 term = *cur_end;
7448 *cur_end = '\0';
7449
7450 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7451 switch (exp->action) {
7452 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007453 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007454 done = 1;
7455 break;
7456
7457 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007458 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007459 done = 1;
7460 break;
7461
7462 case ACT_REPLACE:
7463 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007464 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7465 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007466 /* FIXME: if the user adds a newline in the replacement, the
7467 * index will not be recalculated for now, and the new line
7468 * will not be counted as a new header.
7469 */
7470
Willy Tarreaufa355d42009-11-29 18:12:29 +01007471 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007472 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007473 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007474 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475 cur_ptr, cur_end + 1,
7476 NULL, NULL);
7477 if (unlikely(!cur_end))
7478 return -1;
7479
7480 /* we have a full respnse and we know that we have either a CR
7481 * or an LF at <ptr>.
7482 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007483 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007484 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485 /* there is no point trying this regex on headers */
7486 return 1;
7487 }
7488 }
7489 *cur_end = term; /* restore the string terminator */
7490 return done;
7491}
7492
7493
7494
7495/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007496 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007497 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7498 * unparsable response.
7499 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007500int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007502 struct http_txn *txn = &s->txn;
7503 struct hdr_exp *exp;
7504
7505 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506 int ret;
7507
7508 /*
7509 * The interleaving of transformations and verdicts
7510 * makes it difficult to decide to continue or stop
7511 * the evaluation.
7512 */
7513
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007514 if (txn->flags & TX_SVDENY)
7515 break;
7516
Willy Tarreau3d300592007-03-18 18:34:41 +01007517 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7519 exp->action == ACT_PASS)) {
7520 exp = exp->next;
7521 continue;
7522 }
7523
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007524 /* if this filter had a condition, evaluate it now and skip to
7525 * next filter if the condition does not match.
7526 */
7527 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007528 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007529 ret = acl_pass(ret);
7530 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7531 ret = !ret;
7532 if (!ret)
7533 continue;
7534 }
7535
Willy Tarreaua15645d2007-03-18 16:22:39 +01007536 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007537 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007538 if (unlikely(ret < 0))
7539 return -1;
7540
7541 if (likely(ret == 0)) {
7542 /* The filter did not match the response, it can be
7543 * iterated through all headers.
7544 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007545 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007546 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547 }
7548 return 0;
7549}
7550
7551
Willy Tarreaua15645d2007-03-18 16:22:39 +01007552/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007553 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007554 * desirable to call it only when needed. This function is also used when we
7555 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007557void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007558{
7559 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007560 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007561 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007562 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007563 char *hdr_beg, *hdr_end, *hdr_next;
7564 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007565
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566 /* Iterate through the headers.
7567 * we start with the start line.
7568 */
7569 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007570 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007571
7572 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7573 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007574 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007575
7576 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 hdr_beg = hdr_next;
7578 hdr_end = hdr_beg + cur_hdr->len;
7579 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007580
Willy Tarreau24581ba2010-08-31 22:39:35 +02007581 /* We have one full header between hdr_beg and hdr_end, and the
7582 * next header starts at hdr_next. We're only interested in
7583 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 */
7585
Willy Tarreau24581ba2010-08-31 22:39:35 +02007586 is_cookie2 = 0;
7587 prev = hdr_beg + 10;
7588 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007589 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007590 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7591 if (!val) {
7592 old_idx = cur_idx;
7593 continue;
7594 }
7595 is_cookie2 = 1;
7596 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007597 }
7598
Willy Tarreau24581ba2010-08-31 22:39:35 +02007599 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7600 * <prev> points to the colon.
7601 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007602 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603
Willy Tarreau24581ba2010-08-31 22:39:35 +02007604 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7605 * check-cache is enabled) and we are not interested in checking
7606 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007607 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007608 if (t->be->cookie_name == NULL &&
7609 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007610 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611 return;
7612
Willy Tarreau24581ba2010-08-31 22:39:35 +02007613 /* OK so now we know we have to process this response cookie.
7614 * The format of the Set-Cookie header is slightly different
7615 * from the format of the Cookie header in that it does not
7616 * support the comma as a cookie delimiter (thus the header
7617 * cannot be folded) because the Expires attribute described in
7618 * the original Netscape's spec may contain an unquoted date
7619 * with a comma inside. We have to live with this because
7620 * many browsers don't support Max-Age and some browsers don't
7621 * support quoted strings. However the Set-Cookie2 header is
7622 * clean.
7623 *
7624 * We have to keep multiple pointers in order to support cookie
7625 * removal at the beginning, middle or end of header without
7626 * corrupting the header (in case of set-cookie2). A special
7627 * pointer, <scav> points to the beginning of the set-cookie-av
7628 * fields after the first semi-colon. The <next> pointer points
7629 * either to the end of line (set-cookie) or next unquoted comma
7630 * (set-cookie2). All of these headers are valid :
7631 *
7632 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7633 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7634 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7635 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7636 * | | | | | | | | | |
7637 * | | | | | | | | +-> next hdr_end <--+
7638 * | | | | | | | +------------> scav
7639 * | | | | | | +--------------> val_end
7640 * | | | | | +--------------------> val_beg
7641 * | | | | +----------------------> equal
7642 * | | | +------------------------> att_end
7643 * | | +----------------------------> att_beg
7644 * | +------------------------------> prev
7645 * +-----------------------------------------> hdr_beg
7646 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647
Willy Tarreau24581ba2010-08-31 22:39:35 +02007648 for (; prev < hdr_end; prev = next) {
7649 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650
Willy Tarreau24581ba2010-08-31 22:39:35 +02007651 /* find att_beg */
7652 att_beg = prev + 1;
7653 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7654 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655
Willy Tarreau24581ba2010-08-31 22:39:35 +02007656 /* find att_end : this is the first character after the last non
7657 * space before the equal. It may be equal to hdr_end.
7658 */
7659 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007660
Willy Tarreau24581ba2010-08-31 22:39:35 +02007661 while (equal < hdr_end) {
7662 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7663 break;
7664 if (http_is_spht[(unsigned char)*equal++])
7665 continue;
7666 att_end = equal;
7667 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007668
Willy Tarreau24581ba2010-08-31 22:39:35 +02007669 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7670 * is between <att_beg> and <equal>, both may be identical.
7671 */
7672
7673 /* look for end of cookie if there is an equal sign */
7674 if (equal < hdr_end && *equal == '=') {
7675 /* look for the beginning of the value */
7676 val_beg = equal + 1;
7677 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7678 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679
Willy Tarreau24581ba2010-08-31 22:39:35 +02007680 /* find the end of the value, respecting quotes */
7681 next = find_cookie_value_end(val_beg, hdr_end);
7682
7683 /* make val_end point to the first white space or delimitor after the value */
7684 val_end = next;
7685 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7686 val_end--;
7687 } else {
7688 /* <equal> points to next comma, semi-colon or EOL */
7689 val_beg = val_end = next = equal;
7690 }
7691
7692 if (next < hdr_end) {
7693 /* Set-Cookie2 supports multiple cookies, and <next> points to
7694 * a colon or semi-colon before the end. So skip all attr-value
7695 * pairs and look for the next comma. For Set-Cookie, since
7696 * commas are permitted in values, skip to the end.
7697 */
7698 if (is_cookie2)
7699 next = find_hdr_value_end(next, hdr_end);
7700 else
7701 next = hdr_end;
7702 }
7703
7704 /* Now everything is as on the diagram above */
7705
7706 /* Ignore cookies with no equal sign */
7707 if (equal == val_end)
7708 continue;
7709
7710 /* If there are spaces around the equal sign, we need to
7711 * strip them otherwise we'll get trouble for cookie captures,
7712 * or even for rewrites. Since this happens extremely rarely,
7713 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007714 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007715 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7716 int stripped_before = 0;
7717 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007718
Willy Tarreau24581ba2010-08-31 22:39:35 +02007719 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007720 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007721 equal += stripped_before;
7722 val_beg += stripped_before;
7723 }
7724
7725 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007726 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007727 val_beg += stripped_after;
7728 stripped_before += stripped_after;
7729 }
7730
7731 val_end += stripped_before;
7732 next += stripped_before;
7733 hdr_end += stripped_before;
7734 hdr_next += stripped_before;
7735 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007736 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007737 }
7738
7739 /* First, let's see if we want to capture this cookie. We check
7740 * that we don't already have a server side cookie, because we
7741 * can only capture one. Also as an optimisation, we ignore
7742 * cookies shorter than the declared name.
7743 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007744 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007745 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007746 (val_end - att_beg >= t->fe->capture_namelen) &&
7747 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7748 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007749 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007750 Alert("HTTP logging : out of memory.\n");
7751 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007752 else {
7753 if (log_len > t->fe->capture_len)
7754 log_len = t->fe->capture_len;
7755 memcpy(txn->srv_cookie, att_beg, log_len);
7756 txn->srv_cookie[log_len] = 0;
7757 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007758 }
7759
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007760 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007762 if (!(t->flags & SN_IGNORE_PRST) &&
7763 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7764 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007765 /* assume passive cookie by default */
7766 txn->flags &= ~TX_SCK_MASK;
7767 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007768
7769 /* If the cookie is in insert mode on a known server, we'll delete
7770 * this occurrence because we'll insert another one later.
7771 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007772 * a direct access.
7773 */
Willy Tarreau67402132012-05-31 20:40:20 +02007774 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007775 /* The "preserve" flag was set, we don't want to touch the
7776 * server's cookie.
7777 */
7778 }
Willy Tarreau67402132012-05-31 20:40:20 +02007779 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7780 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007781 /* this cookie must be deleted */
7782 if (*prev == ':' && next == hdr_end) {
7783 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007784 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007785 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7786 txn->hdr_idx.used--;
7787 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007788 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007789 hdr_next += delta;
7790 http_msg_move_end(&txn->rsp, delta);
7791 /* note: while both invalid now, <next> and <hdr_end>
7792 * are still equal, so the for() will stop as expected.
7793 */
7794 } else {
7795 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007796 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007797 next = prev;
7798 hdr_end += delta;
7799 hdr_next += delta;
7800 cur_hdr->len += delta;
7801 http_msg_move_end(&txn->rsp, delta);
7802 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007803 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007804 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007805 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007806 }
Willy Tarreau67402132012-05-31 20:40:20 +02007807 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007808 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 * with this server since we know it.
7810 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007811 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007812 next += delta;
7813 hdr_end += delta;
7814 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007816 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817
Willy Tarreauf1348312010-10-07 15:54:11 +02007818 txn->flags &= ~TX_SCK_MASK;
7819 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007821 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007822 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007823 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007825 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007826 next += delta;
7827 hdr_end += delta;
7828 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007830 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831
Willy Tarreau827aee92011-03-10 16:55:02 +01007832 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007833 txn->flags &= ~TX_SCK_MASK;
7834 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 }
7836 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007837 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7838 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007839 int cmp_len, value_len;
7840 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841
Cyril Bontéb21570a2009-11-29 20:04:48 +01007842 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007843 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7844 value_begin = att_beg + t->be->appsession_name_len;
7845 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007846 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007847 cmp_len = att_end - att_beg;
7848 value_begin = val_beg;
7849 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007850 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007851
Cyril Bonté17530c32010-04-06 21:11:10 +02007852 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007853 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7854 /* free a possibly previously allocated memory */
7855 pool_free2(apools.sessid, txn->sessid);
7856
Cyril Bontéb21570a2009-11-29 20:04:48 +01007857 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007858 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007859 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7860 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7861 return;
7862 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007863 memcpy(txn->sessid, value_begin, value_len);
7864 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007866 }
7867 /* that's done for this cookie, check the next one on the same
7868 * line when next != hdr_end (only if is_cookie2).
7869 */
7870 }
7871 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007873 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007874
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007875 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007876 appsess *asession = NULL;
7877 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007878 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007879 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007880 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007881 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7882 Alert("Not enough Memory process_srv():asession:calloc().\n");
7883 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7884 return;
7885 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007886 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7887
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007888 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7889 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7890 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007891 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007892 return;
7893 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007894 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007895 asession->sessid[t->be->appsession_len] = 0;
7896
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007897 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007898 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007899 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007900 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007901 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007902 return;
7903 }
7904 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007905 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007906
7907 asession->request_count = 0;
7908 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7909 }
7910
7911 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7912 asession->request_count++;
7913 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914}
7915
7916
Willy Tarreaua15645d2007-03-18 16:22:39 +01007917/*
7918 * Check if response is cacheable or not. Updates t->flags.
7919 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007920void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007921{
7922 struct http_txn *txn = &t->txn;
7923 char *p1, *p2;
7924
7925 char *cur_ptr, *cur_end, *cur_next;
7926 int cur_idx;
7927
Willy Tarreau5df51872007-11-25 16:20:08 +01007928 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929 return;
7930
7931 /* Iterate through the headers.
7932 * we start with the start line.
7933 */
7934 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007935 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936
7937 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7938 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007939 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940
7941 cur_hdr = &txn->hdr_idx.v[cur_idx];
7942 cur_ptr = cur_next;
7943 cur_end = cur_ptr + cur_hdr->len;
7944 cur_next = cur_end + cur_hdr->cr + 1;
7945
7946 /* We have one full header between cur_ptr and cur_end, and the
7947 * next header starts at cur_next. We're only interested in
7948 * "Cookie:" headers.
7949 */
7950
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007951 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7952 if (val) {
7953 if ((cur_end - (cur_ptr + val) >= 8) &&
7954 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7955 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7956 return;
7957 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958 }
7959
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007960 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7961 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 continue;
7963
7964 /* OK, right now we know we have a cache-control header at cur_ptr */
7965
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007966 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967
7968 if (p1 >= cur_end) /* no more info */
7969 continue;
7970
7971 /* p1 is at the beginning of the value */
7972 p2 = p1;
7973
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007974 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975 p2++;
7976
7977 /* we have a complete value between p1 and p2 */
7978 if (p2 < cur_end && *p2 == '=') {
7979 /* we have something of the form no-cache="set-cookie" */
7980 if ((cur_end - p1 >= 21) &&
7981 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7982 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007983 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984 continue;
7985 }
7986
7987 /* OK, so we know that either p2 points to the end of string or to a comma */
7988 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007989 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7991 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7992 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007993 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 return;
7995 }
7996
7997 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007998 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999 continue;
8000 }
8001 }
8002}
8003
8004
Willy Tarreau58f10d72006-12-04 02:26:12 +01008005/*
8006 * Try to retrieve a known appsession in the URI, then the associated server.
8007 * If the server is found, it's assigned to the session.
8008 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008009void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008010{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008011 char *end_params, *first_param, *cur_param, *next_param;
8012 char separator;
8013 int value_len;
8014
8015 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008016
Willy Tarreaue2e27a52007-04-01 00:01:37 +02008017 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02008018 (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 +01008019 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008020 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008021
Cyril Bontéb21570a2009-11-29 20:04:48 +01008022 first_param = NULL;
8023 switch (mode) {
8024 case PR_O2_AS_M_PP:
8025 first_param = memchr(begin, ';', len);
8026 break;
8027 case PR_O2_AS_M_QS:
8028 first_param = memchr(begin, '?', len);
8029 break;
8030 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008031
Cyril Bontéb21570a2009-11-29 20:04:48 +01008032 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008033 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008034 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008035
Cyril Bontéb21570a2009-11-29 20:04:48 +01008036 switch (mode) {
8037 case PR_O2_AS_M_PP:
8038 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8039 end_params = (char *) begin + len;
8040 }
8041 separator = ';';
8042 break;
8043 case PR_O2_AS_M_QS:
8044 end_params = (char *) begin + len;
8045 separator = '&';
8046 break;
8047 default:
8048 /* unknown mode, shouldn't happen */
8049 return;
8050 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008051
Cyril Bontéb21570a2009-11-29 20:04:48 +01008052 cur_param = next_param = end_params;
8053 while (cur_param > first_param) {
8054 cur_param--;
8055 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8056 /* let's see if this is the appsession parameter */
8057 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
8058 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
8059 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
8060 /* Cool... it's the right one */
8061 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8062 value_len = MIN(t->be->appsession_len, next_param - cur_param);
8063 if (value_len > 0) {
8064 manage_client_side_appsession(t, cur_param, value_len);
8065 }
8066 break;
8067 }
8068 next_param = cur_param;
8069 }
8070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008071#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008072 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02008073 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008074#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008075}
8076
Willy Tarreaub2513902006-12-17 14:52:38 +01008077/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008078 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008079 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008080 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008081 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008082 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008083 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008084 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008085 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008086int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008087{
8088 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008089 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008090 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008091
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008092 if (!uri_auth)
8093 return 0;
8094
Cyril Bonté70be45d2010-10-12 00:14:35 +02008095 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008096 return 0;
8097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008098 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008099 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008100 return 0;
8101
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008102 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008103 return 0;
8104
Willy Tarreaub2513902006-12-17 14:52:38 +01008105 return 1;
8106}
8107
Willy Tarreau4076a152009-04-02 15:18:36 +02008108/*
8109 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008110 * By default it tries to report the error position as msg->err_pos. However if
8111 * this one is not set, it will then report msg->next, which is the last known
8112 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008113 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008114 */
8115void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008116 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008117 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008118{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008119 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008120 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008121
Willy Tarreau9b28e032012-10-12 23:49:43 +02008122 es->len = MIN(chn->buf->i, sizeof(es->buf));
8123 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008124 len1 = MIN(len1, es->len);
8125 len2 = es->len - len1; /* remaining data if buffer wraps */
8126
Willy Tarreau9b28e032012-10-12 23:49:43 +02008127 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008128 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008129 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008130
Willy Tarreau4076a152009-04-02 15:18:36 +02008131 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008132 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008133 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008134 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008135
Willy Tarreau4076a152009-04-02 15:18:36 +02008136 es->when = date; // user-visible date
8137 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008138 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008139 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008140 if (objt_conn(s->req->prod->end))
8141 es->src = __objt_conn(s->req->prod->end)->addr.from;
8142 else
8143 memset(&es->src, 0, sizeof(es->src));
8144
Willy Tarreau078272e2010-12-12 12:46:33 +01008145 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008146 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008147 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008148 es->s_flags = s->flags;
8149 es->t_flags = s->txn.flags;
8150 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008151 es->b_out = chn->buf->o;
8152 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008153 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008154 es->m_clen = msg->chunk_len;
8155 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008156}
Willy Tarreaub2513902006-12-17 14:52:38 +01008157
Willy Tarreau294c4732011-12-16 21:35:50 +01008158/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8159 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8160 * performed over the whole headers. Otherwise it must contain a valid header
8161 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8162 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8163 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8164 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008165 * -1. The value fetch stops at commas, so this function is suited for use with
8166 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008167 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008168 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008169unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008170 struct hdr_idx *idx, int occ,
8171 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008172{
Willy Tarreau294c4732011-12-16 21:35:50 +01008173 struct hdr_ctx local_ctx;
8174 char *ptr_hist[MAX_HDR_HISTORY];
8175 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008176 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008177 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008178
Willy Tarreau294c4732011-12-16 21:35:50 +01008179 if (!ctx) {
8180 local_ctx.idx = 0;
8181 ctx = &local_ctx;
8182 }
8183
Willy Tarreaubce70882009-09-07 11:51:47 +02008184 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008185 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008186 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008187 occ--;
8188 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008189 *vptr = ctx->line + ctx->val;
8190 *vlen = ctx->vlen;
8191 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008192 }
8193 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008194 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008195 }
8196
8197 /* negative occurrence, we scan all the list then walk back */
8198 if (-occ > MAX_HDR_HISTORY)
8199 return 0;
8200
Willy Tarreau294c4732011-12-16 21:35:50 +01008201 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008202 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008203 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8204 len_hist[hist_ptr] = ctx->vlen;
8205 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008206 hist_ptr = 0;
8207 found++;
8208 }
8209 if (-occ > found)
8210 return 0;
8211 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008212 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8213 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8214 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008215 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008216 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008217 if (hist_ptr >= MAX_HDR_HISTORY)
8218 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008219 *vptr = ptr_hist[hist_ptr];
8220 *vlen = len_hist[hist_ptr];
8221 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008222}
8223
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008224/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8225 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8226 * performed over the whole headers. Otherwise it must contain a valid header
8227 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8228 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8229 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8230 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8231 * -1. This function differs from http_get_hdr() in that it only returns full
8232 * line header values and does not stop at commas.
8233 * The return value is 0 if nothing was found, or non-zero otherwise.
8234 */
8235unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8236 struct hdr_idx *idx, int occ,
8237 struct hdr_ctx *ctx, char **vptr, int *vlen)
8238{
8239 struct hdr_ctx local_ctx;
8240 char *ptr_hist[MAX_HDR_HISTORY];
8241 int len_hist[MAX_HDR_HISTORY];
8242 unsigned int hist_ptr;
8243 int found;
8244
8245 if (!ctx) {
8246 local_ctx.idx = 0;
8247 ctx = &local_ctx;
8248 }
8249
8250 if (occ >= 0) {
8251 /* search from the beginning */
8252 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8253 occ--;
8254 if (occ <= 0) {
8255 *vptr = ctx->line + ctx->val;
8256 *vlen = ctx->vlen;
8257 return 1;
8258 }
8259 }
8260 return 0;
8261 }
8262
8263 /* negative occurrence, we scan all the list then walk back */
8264 if (-occ > MAX_HDR_HISTORY)
8265 return 0;
8266
8267 found = hist_ptr = 0;
8268 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8269 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8270 len_hist[hist_ptr] = ctx->vlen;
8271 if (++hist_ptr >= MAX_HDR_HISTORY)
8272 hist_ptr = 0;
8273 found++;
8274 }
8275 if (-occ > found)
8276 return 0;
8277 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8278 * find occurrence -occ, so we have to check [hist_ptr+occ].
8279 */
8280 hist_ptr += occ;
8281 if (hist_ptr >= MAX_HDR_HISTORY)
8282 hist_ptr -= MAX_HDR_HISTORY;
8283 *vptr = ptr_hist[hist_ptr];
8284 *vlen = len_hist[hist_ptr];
8285 return 1;
8286}
8287
Willy Tarreaubaaee002006-06-26 02:48:02 +02008288/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008289 * Print a debug line with a header. Always stop at the first CR or LF char,
8290 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8291 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008292 */
8293void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8294{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008295 int max;
8296 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008297 dir,
8298 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8299 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 +02008300
8301 for (max = 0; start + max < end; max++)
8302 if (start[max] == '\r' || start[max] == '\n')
8303 break;
8304
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008305 UBOUND(max, trash.size - trash.len - 3);
8306 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8307 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008308 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008309}
8310
Willy Tarreau0937bc42009-12-22 15:03:09 +01008311/*
8312 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8313 * the required fields are properly allocated and that we only need to (re)init
8314 * them. This should be used before processing any new request.
8315 */
8316void http_init_txn(struct session *s)
8317{
8318 struct http_txn *txn = &s->txn;
8319 struct proxy *fe = s->fe;
8320
8321 txn->flags = 0;
8322 txn->status = -1;
8323
Willy Tarreauf64d1412010-10-07 20:06:11 +02008324 txn->cookie_first_date = 0;
8325 txn->cookie_last_date = 0;
8326
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008327 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008328 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008329 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008330 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008331 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008332 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008333 txn->req.chunk_len = 0LL;
8334 txn->req.body_len = 0LL;
8335 txn->rsp.chunk_len = 0LL;
8336 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008337 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8338 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008339 txn->req.chn = s->req;
8340 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008341
8342 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008343
8344 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8345 if (fe->options2 & PR_O2_REQBUG_OK)
8346 txn->req.err_pos = -1; /* let buggy requests pass */
8347
Willy Tarreau46023632010-01-07 22:51:47 +01008348 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008349 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8350
Willy Tarreau46023632010-01-07 22:51:47 +01008351 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008352 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8353
8354 if (txn->hdr_idx.v)
8355 hdr_idx_init(&txn->hdr_idx);
8356}
8357
8358/* to be used at the end of a transaction */
8359void http_end_txn(struct session *s)
8360{
8361 struct http_txn *txn = &s->txn;
8362
Willy Tarreau75195602014-03-11 15:48:55 +01008363 /* release any possible compression context */
8364 if (s->flags & SN_COMP_READY)
8365 s->comp_algo->end(&s->comp_ctx);
8366 s->comp_algo = NULL;
8367 s->flags &= ~SN_COMP_READY;
8368
Willy Tarreau0937bc42009-12-22 15:03:09 +01008369 /* these ones will have been dynamically allocated */
8370 pool_free2(pool2_requri, txn->uri);
8371 pool_free2(pool2_capture, txn->cli_cookie);
8372 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008373 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008374 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008375
William Lallemanda73203e2012-03-12 12:48:57 +01008376 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008377 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008378 txn->uri = NULL;
8379 txn->srv_cookie = NULL;
8380 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008381
8382 if (txn->req.cap) {
8383 struct cap_hdr *h;
8384 for (h = s->fe->req_cap; h; h = h->next)
8385 pool_free2(h->pool, txn->req.cap[h->index]);
8386 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8387 }
8388
8389 if (txn->rsp.cap) {
8390 struct cap_hdr *h;
8391 for (h = s->fe->rsp_cap; h; h = h->next)
8392 pool_free2(h->pool, txn->rsp.cap[h->index]);
8393 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8394 }
8395
Willy Tarreau0937bc42009-12-22 15:03:09 +01008396}
8397
8398/* to be used at the end of a transaction to prepare a new one */
8399void http_reset_txn(struct session *s)
8400{
8401 http_end_txn(s);
8402 http_init_txn(s);
8403
8404 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008405 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008406 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008407 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008408 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008409 /* re-init store persistence */
8410 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008411 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008412
Willy Tarreau0937bc42009-12-22 15:03:09 +01008413 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008414
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008415 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008416
Willy Tarreau739cfba2010-01-25 23:11:14 +01008417 /* We must trim any excess data from the response buffer, because we
8418 * may have blocked an invalid response from a server that we don't
8419 * want to accidentely forward once we disable the analysers, nor do
8420 * we want those data to come along with next response. A typical
8421 * example of such data would be from a buggy server responding to
8422 * a HEAD with some data, or sending more than the advertised
8423 * content-length.
8424 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008425 if (unlikely(s->rep->buf->i))
8426 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008427
Willy Tarreau0937bc42009-12-22 15:03:09 +01008428 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008429 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008430
Willy Tarreaud04e8582010-05-31 12:31:35 +02008431 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008432 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008433
8434 s->req->rex = TICK_ETERNITY;
8435 s->req->wex = TICK_ETERNITY;
8436 s->req->analyse_exp = TICK_ETERNITY;
8437 s->rep->rex = TICK_ETERNITY;
8438 s->rep->wex = TICK_ETERNITY;
8439 s->rep->analyse_exp = TICK_ETERNITY;
8440}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008441
Willy Tarreauff011f22011-01-06 17:51:27 +01008442void free_http_req_rules(struct list *r) {
8443 struct http_req_rule *tr, *pr;
8444
8445 list_for_each_entry_safe(pr, tr, r, list) {
8446 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008447 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008448 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008449
8450 free(pr);
8451 }
8452}
8453
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008454/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008455struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8456{
8457 struct http_req_rule *rule;
8458 int cur_arg;
8459
8460 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8461 if (!rule) {
8462 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008463 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008464 }
8465
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008466 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008467 rule->action = HTTP_REQ_ACT_ALLOW;
8468 cur_arg = 1;
8469 } else if (!strcmp(args[0], "deny")) {
8470 rule->action = HTTP_REQ_ACT_DENY;
8471 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008472 } else if (!strcmp(args[0], "tarpit")) {
8473 rule->action = HTTP_REQ_ACT_TARPIT;
8474 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008475 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008476 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008477 cur_arg = 1;
8478
8479 while(*args[cur_arg]) {
8480 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008481 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008482 cur_arg+=2;
8483 continue;
8484 } else
8485 break;
8486 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008487 } else if (!strcmp(args[0], "set-nice")) {
8488 rule->action = HTTP_REQ_ACT_SET_NICE;
8489 cur_arg = 1;
8490
8491 if (!*args[cur_arg] ||
8492 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8493 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8494 file, linenum, args[0]);
8495 goto out_err;
8496 }
8497 rule->arg.nice = atoi(args[cur_arg]);
8498 if (rule->arg.nice < -1024)
8499 rule->arg.nice = -1024;
8500 else if (rule->arg.nice > 1024)
8501 rule->arg.nice = 1024;
8502 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008503 } else if (!strcmp(args[0], "set-tos")) {
8504#ifdef IP_TOS
8505 char *err;
8506 rule->action = HTTP_REQ_ACT_SET_TOS;
8507 cur_arg = 1;
8508
8509 if (!*args[cur_arg] ||
8510 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8511 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8512 file, linenum, args[0]);
8513 goto out_err;
8514 }
8515
8516 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8517 if (err && *err != '\0') {
8518 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8519 file, linenum, err, args[0]);
8520 goto out_err;
8521 }
8522 cur_arg++;
8523#else
8524 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8525 goto out_err;
8526#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008527 } else if (!strcmp(args[0], "set-mark")) {
8528#ifdef SO_MARK
8529 char *err;
8530 rule->action = HTTP_REQ_ACT_SET_MARK;
8531 cur_arg = 1;
8532
8533 if (!*args[cur_arg] ||
8534 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8535 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8536 file, linenum, args[0]);
8537 goto out_err;
8538 }
8539
8540 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8541 if (err && *err != '\0') {
8542 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8543 file, linenum, err, args[0]);
8544 goto out_err;
8545 }
8546 cur_arg++;
8547 global.last_checks |= LSTCHK_NETADM;
8548#else
8549 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8550 goto out_err;
8551#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008552 } else if (!strcmp(args[0], "set-log-level")) {
8553 rule->action = HTTP_REQ_ACT_SET_LOGL;
8554 cur_arg = 1;
8555
8556 if (!*args[cur_arg] ||
8557 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8558 bad_log_level:
8559 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8560 file, linenum, args[0]);
8561 goto out_err;
8562 }
8563 if (strcmp(args[cur_arg], "silent") == 0)
8564 rule->arg.loglevel = -1;
8565 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8566 goto bad_log_level;
8567 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008568 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8569 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8570 cur_arg = 1;
8571
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008572 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8573 (*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 +01008574 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8575 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008576 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008577 }
8578
8579 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8580 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8581 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008582
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008583 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008584 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008585 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8586 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008587 free(proxy->conf.lfs_file);
8588 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8589 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008590 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008591 } else if (strcmp(args[0], "redirect") == 0) {
8592 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008593 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008594
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008595 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008596 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8597 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8598 goto out_err;
8599 }
8600
8601 /* this redirect rule might already contain a parsed condition which
8602 * we'll pass to the http-request rule.
8603 */
8604 rule->action = HTTP_REQ_ACT_REDIR;
8605 rule->arg.redir = redir;
8606 rule->cond = redir->cond;
8607 redir->cond = NULL;
8608 cur_arg = 2;
8609 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008610 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008611 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 +01008612 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008613 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008614 }
8615
8616 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8617 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008618 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008619
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008620 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8621 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8622 file, linenum, args[0], errmsg);
8623 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008624 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008625 }
8626 rule->cond = cond;
8627 }
8628 else if (*args[cur_arg]) {
8629 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8630 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8631 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008632 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008633 }
8634
8635 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008636 out_err:
8637 free(rule);
8638 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008639}
8640
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008641/* parse an "http-respose" rule */
8642struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8643{
8644 struct http_res_rule *rule;
8645 int cur_arg;
8646
8647 rule = calloc(1, sizeof(*rule));
8648 if (!rule) {
8649 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8650 goto out_err;
8651 }
8652
8653 if (!strcmp(args[0], "allow")) {
8654 rule->action = HTTP_RES_ACT_ALLOW;
8655 cur_arg = 1;
8656 } else if (!strcmp(args[0], "deny")) {
8657 rule->action = HTTP_RES_ACT_DENY;
8658 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008659 } else if (!strcmp(args[0], "set-nice")) {
8660 rule->action = HTTP_RES_ACT_SET_NICE;
8661 cur_arg = 1;
8662
8663 if (!*args[cur_arg] ||
8664 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8665 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8666 file, linenum, args[0]);
8667 goto out_err;
8668 }
8669 rule->arg.nice = atoi(args[cur_arg]);
8670 if (rule->arg.nice < -1024)
8671 rule->arg.nice = -1024;
8672 else if (rule->arg.nice > 1024)
8673 rule->arg.nice = 1024;
8674 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008675 } else if (!strcmp(args[0], "set-tos")) {
8676#ifdef IP_TOS
8677 char *err;
8678 rule->action = HTTP_RES_ACT_SET_TOS;
8679 cur_arg = 1;
8680
8681 if (!*args[cur_arg] ||
8682 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8683 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8684 file, linenum, args[0]);
8685 goto out_err;
8686 }
8687
8688 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8689 if (err && *err != '\0') {
8690 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8691 file, linenum, err, args[0]);
8692 goto out_err;
8693 }
8694 cur_arg++;
8695#else
8696 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8697 goto out_err;
8698#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008699 } else if (!strcmp(args[0], "set-mark")) {
8700#ifdef SO_MARK
8701 char *err;
8702 rule->action = HTTP_RES_ACT_SET_MARK;
8703 cur_arg = 1;
8704
8705 if (!*args[cur_arg] ||
8706 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8707 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8708 file, linenum, args[0]);
8709 goto out_err;
8710 }
8711
8712 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8713 if (err && *err != '\0') {
8714 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8715 file, linenum, err, args[0]);
8716 goto out_err;
8717 }
8718 cur_arg++;
8719 global.last_checks |= LSTCHK_NETADM;
8720#else
8721 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8722 goto out_err;
8723#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008724 } else if (!strcmp(args[0], "set-log-level")) {
8725 rule->action = HTTP_RES_ACT_SET_LOGL;
8726 cur_arg = 1;
8727
8728 if (!*args[cur_arg] ||
8729 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8730 bad_log_level:
8731 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8732 file, linenum, args[0]);
8733 goto out_err;
8734 }
8735 if (strcmp(args[cur_arg], "silent") == 0)
8736 rule->arg.loglevel = -1;
8737 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8738 goto bad_log_level;
8739 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008740 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8741 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8742 cur_arg = 1;
8743
8744 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8745 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8746 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8747 file, linenum, args[0]);
8748 goto out_err;
8749 }
8750
8751 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8752 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8753 LIST_INIT(&rule->arg.hdr_add.fmt);
8754
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008755 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008756 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008757 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8758 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008759 free(proxy->conf.lfs_file);
8760 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8761 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008762 cur_arg += 2;
8763 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008764 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 +02008765 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8766 goto out_err;
8767 }
8768
8769 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8770 struct acl_cond *cond;
8771 char *errmsg = NULL;
8772
8773 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8774 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8775 file, linenum, args[0], errmsg);
8776 free(errmsg);
8777 goto out_err;
8778 }
8779 rule->cond = cond;
8780 }
8781 else if (*args[cur_arg]) {
8782 Alert("parsing [%s:%d]: 'http-response %s' expects"
8783 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8784 file, linenum, args[0], args[cur_arg]);
8785 goto out_err;
8786 }
8787
8788 return rule;
8789 out_err:
8790 free(rule);
8791 return NULL;
8792}
8793
Willy Tarreau4baae242012-12-27 12:00:31 +01008794/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008795 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8796 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008797 */
8798struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008799 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008800{
8801 struct redirect_rule *rule;
8802 int cur_arg;
8803 int type = REDIRECT_TYPE_NONE;
8804 int code = 302;
8805 const char *destination = NULL;
8806 const char *cookie = NULL;
8807 int cookie_set = 0;
8808 unsigned int flags = REDIRECT_FLAG_NONE;
8809 struct acl_cond *cond = NULL;
8810
8811 cur_arg = 0;
8812 while (*(args[cur_arg])) {
8813 if (strcmp(args[cur_arg], "location") == 0) {
8814 if (!*args[cur_arg + 1])
8815 goto missing_arg;
8816
8817 type = REDIRECT_TYPE_LOCATION;
8818 cur_arg++;
8819 destination = args[cur_arg];
8820 }
8821 else if (strcmp(args[cur_arg], "prefix") == 0) {
8822 if (!*args[cur_arg + 1])
8823 goto missing_arg;
8824
8825 type = REDIRECT_TYPE_PREFIX;
8826 cur_arg++;
8827 destination = args[cur_arg];
8828 }
8829 else if (strcmp(args[cur_arg], "scheme") == 0) {
8830 if (!*args[cur_arg + 1])
8831 goto missing_arg;
8832
8833 type = REDIRECT_TYPE_SCHEME;
8834 cur_arg++;
8835 destination = args[cur_arg];
8836 }
8837 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8838 if (!*args[cur_arg + 1])
8839 goto missing_arg;
8840
8841 cur_arg++;
8842 cookie = args[cur_arg];
8843 cookie_set = 1;
8844 }
8845 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8846 if (!*args[cur_arg + 1])
8847 goto missing_arg;
8848
8849 cur_arg++;
8850 cookie = args[cur_arg];
8851 cookie_set = 0;
8852 }
8853 else if (strcmp(args[cur_arg], "code") == 0) {
8854 if (!*args[cur_arg + 1])
8855 goto missing_arg;
8856
8857 cur_arg++;
8858 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008859 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008860 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008861 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008862 args[cur_arg - 1], args[cur_arg]);
8863 return NULL;
8864 }
8865 }
8866 else if (!strcmp(args[cur_arg],"drop-query")) {
8867 flags |= REDIRECT_FLAG_DROP_QS;
8868 }
8869 else if (!strcmp(args[cur_arg],"append-slash")) {
8870 flags |= REDIRECT_FLAG_APPEND_SLASH;
8871 }
8872 else if (strcmp(args[cur_arg], "if") == 0 ||
8873 strcmp(args[cur_arg], "unless") == 0) {
8874 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8875 if (!cond) {
8876 memprintf(errmsg, "error in condition: %s", *errmsg);
8877 return NULL;
8878 }
8879 break;
8880 }
8881 else {
8882 memprintf(errmsg,
8883 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8884 args[cur_arg]);
8885 return NULL;
8886 }
8887 cur_arg++;
8888 }
8889
8890 if (type == REDIRECT_TYPE_NONE) {
8891 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8892 return NULL;
8893 }
8894
8895 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8896 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008897 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008898
8899 if (!use_fmt) {
8900 /* old-style static redirect rule */
8901 rule->rdr_str = strdup(destination);
8902 rule->rdr_len = strlen(destination);
8903 }
8904 else {
8905 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008906
8907 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8908 * if prefix == "/", we don't want to add anything, otherwise it
8909 * makes it hard for the user to configure a self-redirection.
8910 */
8911 proxy->conf.args.ctx = ARGC_RDR;
8912 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008913 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008914 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8915 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008916 free(curproxy->conf.lfs_file);
8917 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8918 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008919 }
8920 }
8921
Willy Tarreau4baae242012-12-27 12:00:31 +01008922 if (cookie) {
8923 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8924 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8925 */
8926 rule->cookie_len = strlen(cookie);
8927 if (cookie_set) {
8928 rule->cookie_str = malloc(rule->cookie_len + 10);
8929 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8930 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8931 rule->cookie_len += 9;
8932 } else {
8933 rule->cookie_str = malloc(rule->cookie_len + 21);
8934 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8935 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8936 rule->cookie_len += 20;
8937 }
8938 }
8939 rule->type = type;
8940 rule->code = code;
8941 rule->flags = flags;
8942 LIST_INIT(&rule->list);
8943 return rule;
8944
8945 missing_arg:
8946 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8947 return NULL;
8948}
8949
Willy Tarreau8797c062007-05-07 00:55:35 +02008950/************************************************************************/
8951/* The code below is dedicated to ACL parsing and matching */
8952/************************************************************************/
8953
8954
Willy Tarreau14174bc2012-04-16 14:34:04 +02008955/* This function ensures that the prerequisites for an L7 fetch are ready,
8956 * which means that a request or response is ready. If some data is missing,
8957 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008958 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8959 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008960 *
8961 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008962 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8963 * decide whether or not an HTTP message is present ;
8964 * 0 if the requested data cannot be fetched or if it is certain that
8965 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008966 * 1 if an HTTP message is ready
8967 */
8968static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008969smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008970 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008971{
8972 struct http_txn *txn = l7;
8973 struct http_msg *msg = &txn->req;
8974
8975 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8976 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8977 */
8978
8979 if (unlikely(!s || !txn))
8980 return 0;
8981
8982 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008983 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008984
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008985 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008986 if (unlikely(!s->req))
8987 return 0;
8988
Willy Tarreauaae75e32013-03-29 12:31:49 +01008989 /* If the buffer does not leave enough free space at the end,
8990 * we must first realign it.
8991 */
8992 if (s->req->buf->p > s->req->buf->data &&
8993 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8994 buffer_slow_realign(s->req->buf);
8995
Willy Tarreau14174bc2012-04-16 14:34:04 +02008996 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008997 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008998 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008999
9000 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009001 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009002 http_msg_analyzer(msg, &txn->hdr_idx);
9003
9004 /* Still no valid request ? */
9005 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009006 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009007 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009008 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009009 }
9010 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009011 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009012 return 0;
9013 }
9014
9015 /* OK we just got a valid HTTP request. We have some minor
9016 * preparation to perform so that further checks can rely
9017 * on HTTP tests.
9018 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009019
9020 /* If the request was parsed but was too large, we must absolutely
9021 * return an error so that it is not processed. At the moment this
9022 * cannot happen, but if the parsers are to change in the future,
9023 * we want this check to be maintained.
9024 */
9025 if (unlikely(s->req->buf->i + s->req->buf->p >
9026 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9027 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009028 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009029 return 1;
9030 }
9031
Willy Tarreau9b28e032012-10-12 23:49:43 +02009032 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009033 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9034 s->flags |= SN_REDIRECTABLE;
9035
Willy Tarreau506d0502013-07-06 13:29:24 +02009036 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9037 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009038 }
9039
Willy Tarreau506d0502013-07-06 13:29:24 +02009040 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009041 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009042 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009043
9044 /* otherwise everything's ready for the request */
9045 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009046 else {
9047 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009048 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9049 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009050 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009051 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009052 }
9053
9054 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009055 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009056 return 1;
9057}
Willy Tarreau8797c062007-05-07 00:55:35 +02009058
Willy Tarreauc0239e02012-04-16 14:42:55 +02009059#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009060 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 +02009061
Willy Tarreau24e32d82012-04-23 23:55:44 +02009062#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009063 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 +02009064
Willy Tarreau8797c062007-05-07 00:55:35 +02009065
9066/* 1. Check on METHOD
9067 * We use the pre-parsed method if it is known, and store its number as an
9068 * integer. If it is unknown, we use the pointer and the length.
9069 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009070static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009071{
9072 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009073 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009074
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009075 len = strlen(text);
9076 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009077
9078 pattern->val.i = meth;
9079 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009080 trash = get_trash_chunk();
9081 if (trash->size < len) {
9082 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9083 len, trash->size);
9084 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009085 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009086 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009087 pattern->len = len;
9088 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009089 else {
9090 pattern->ptr.str = NULL;
9091 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009092 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009093 return 1;
9094}
9095
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009096/* This function fetches the method of current HTTP request and stores
9097 * it in the global pattern struct as a chunk. There are two possibilities :
9098 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9099 * in <len> and <ptr> is NULL ;
9100 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9101 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009102 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009103 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009104static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009105smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009106 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009107{
9108 int meth;
9109 struct http_txn *txn = l7;
9110
Willy Tarreau24e32d82012-04-23 23:55:44 +02009111 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009112
Willy Tarreau8797c062007-05-07 00:55:35 +02009113 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009114 smp->type = SMP_T_METH;
9115 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009116 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009117 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9118 /* ensure the indexes are not affected */
9119 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009120 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009121 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9122 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009123 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009124 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009125 return 1;
9126}
9127
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009128/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009129static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009130{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009131 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009132 struct pattern_list *lst;
9133 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009134
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009135 list_for_each_entry(lst, &expr->patterns, list) {
9136 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009137
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009138 /* well-known method */
9139 if (pattern->val.i != HTTP_METH_OTHER) {
9140 if (smp->data.meth.meth == pattern->val.i)
9141 return pattern;
9142 else
9143 continue;
9144 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009145
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009146 /* Other method, we must compare the strings */
9147 if (pattern->len != smp->data.meth.str.len)
9148 continue;
9149
9150 icase = pattern->flags & PAT_F_IGNORE_CASE;
9151 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9152 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9153 return pattern;
9154 }
9155 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009156}
9157
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009158static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009159smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009160 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009161{
9162 struct http_txn *txn = l7;
9163 char *ptr;
9164 int len;
9165
Willy Tarreauc0239e02012-04-16 14:42:55 +02009166 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009167
Willy Tarreau8797c062007-05-07 00:55:35 +02009168 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009169 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009170
9171 while ((len-- > 0) && (*ptr++ != '/'));
9172 if (len <= 0)
9173 return 0;
9174
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009175 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009176 smp->data.str.str = ptr;
9177 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009178
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009179 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009180 return 1;
9181}
9182
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009183static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009184smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009185 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009186{
9187 struct http_txn *txn = l7;
9188 char *ptr;
9189 int len;
9190
Willy Tarreauc0239e02012-04-16 14:42:55 +02009191 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009192
Willy Tarreauf26b2522012-12-14 08:33:14 +01009193 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9194 return 0;
9195
Willy Tarreau8797c062007-05-07 00:55:35 +02009196 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009197 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009198
9199 while ((len-- > 0) && (*ptr++ != '/'));
9200 if (len <= 0)
9201 return 0;
9202
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009203 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009204 smp->data.str.str = ptr;
9205 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009206
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009207 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009208 return 1;
9209}
9210
9211/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009212static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009213smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009214 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009215{
9216 struct http_txn *txn = l7;
9217 char *ptr;
9218 int len;
9219
Willy Tarreauc0239e02012-04-16 14:42:55 +02009220 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009221
Willy Tarreauf26b2522012-12-14 08:33:14 +01009222 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9223 return 0;
9224
Willy Tarreau8797c062007-05-07 00:55:35 +02009225 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009226 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009227
Willy Tarreauf853c462012-04-23 18:53:56 +02009228 smp->type = SMP_T_UINT;
9229 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009230 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009231 return 1;
9232}
9233
9234/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009235static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009236smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009237 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009238{
9239 struct http_txn *txn = l7;
9240
Willy Tarreauc0239e02012-04-16 14:42:55 +02009241 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009242
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009243 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009244 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009245 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009246 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009247 return 1;
9248}
9249
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009250static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009251smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009252 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009253{
9254 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009255 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009256
Willy Tarreauc0239e02012-04-16 14:42:55 +02009257 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009258
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009259 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009260 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009261 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009262
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009263 smp->type = SMP_T_IPV4;
9264 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009265 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009266 return 1;
9267}
9268
9269static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009270smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009271 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009272{
9273 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009274 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009275
Willy Tarreauc0239e02012-04-16 14:42:55 +02009276 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009277
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009278 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009279 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9280 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009281
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009282 smp->type = SMP_T_UINT;
9283 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009284 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009285 return 1;
9286}
9287
Willy Tarreau185b5c42012-04-26 15:11:51 +02009288/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9289 * Accepts an optional argument of type string containing the header field name,
9290 * and an optional argument of type signed or unsigned integer to request an
9291 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009292 * headers are considered from the first one. It does not stop on commas and
9293 * returns full lines instead (useful for User-Agent or Date for example).
9294 */
9295static int
9296smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009297 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009298{
9299 struct http_txn *txn = l7;
9300 struct hdr_idx *idx = &txn->hdr_idx;
9301 struct hdr_ctx *ctx = smp->ctx.a[0];
9302 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9303 int occ = 0;
9304 const char *name_str = NULL;
9305 int name_len = 0;
9306
9307 if (!ctx) {
9308 /* first call */
9309 ctx = &static_hdr_ctx;
9310 ctx->idx = 0;
9311 smp->ctx.a[0] = ctx;
9312 }
9313
9314 if (args) {
9315 if (args[0].type != ARGT_STR)
9316 return 0;
9317 name_str = args[0].data.str.str;
9318 name_len = args[0].data.str.len;
9319
9320 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9321 occ = args[1].data.uint;
9322 }
9323
9324 CHECK_HTTP_MESSAGE_FIRST();
9325
9326 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9327 /* search for header from the beginning */
9328 ctx->idx = 0;
9329
9330 if (!occ && !(opt & SMP_OPT_ITERATE))
9331 /* no explicit occurrence and single fetch => last header by default */
9332 occ = -1;
9333
9334 if (!occ)
9335 /* prepare to report multiple occurrences for ACL fetches */
9336 smp->flags |= SMP_F_NOT_LAST;
9337
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009338 smp->type = SMP_T_STR;
9339 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009340 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9341 return 1;
9342
9343 smp->flags &= ~SMP_F_NOT_LAST;
9344 return 0;
9345}
9346
9347/* 6. Check on HTTP header count. The number of occurrences is returned.
9348 * Accepts exactly 1 argument of type string. It does not stop on commas and
9349 * returns full lines instead (useful for User-Agent or Date for example).
9350 */
9351static int
9352smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009353 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009354{
9355 struct http_txn *txn = l7;
9356 struct hdr_idx *idx = &txn->hdr_idx;
9357 struct hdr_ctx ctx;
9358 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9359 int cnt;
9360
9361 if (!args || args->type != ARGT_STR)
9362 return 0;
9363
9364 CHECK_HTTP_MESSAGE_FIRST();
9365
9366 ctx.idx = 0;
9367 cnt = 0;
9368 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9369 cnt++;
9370
9371 smp->type = SMP_T_UINT;
9372 smp->data.uint = cnt;
9373 smp->flags = SMP_F_VOL_HDR;
9374 return 1;
9375}
9376
9377/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9378 * Accepts an optional argument of type string containing the header field name,
9379 * and an optional argument of type signed or unsigned integer to request an
9380 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009381 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009382 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009383static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009384smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009385 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009386{
9387 struct http_txn *txn = l7;
9388 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009389 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009390 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009391 int occ = 0;
9392 const char *name_str = NULL;
9393 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009394
Willy Tarreaua890d072013-04-02 12:01:06 +02009395 if (!ctx) {
9396 /* first call */
9397 ctx = &static_hdr_ctx;
9398 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009399 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009400 }
9401
Willy Tarreau185b5c42012-04-26 15:11:51 +02009402 if (args) {
9403 if (args[0].type != ARGT_STR)
9404 return 0;
9405 name_str = args[0].data.str.str;
9406 name_len = args[0].data.str.len;
9407
9408 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9409 occ = args[1].data.uint;
9410 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009411
Willy Tarreaue333ec92012-04-16 16:26:40 +02009412 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009413
Willy Tarreau185b5c42012-04-26 15:11:51 +02009414 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009415 /* search for header from the beginning */
9416 ctx->idx = 0;
9417
Willy Tarreau185b5c42012-04-26 15:11:51 +02009418 if (!occ && !(opt & SMP_OPT_ITERATE))
9419 /* no explicit occurrence and single fetch => last header by default */
9420 occ = -1;
9421
9422 if (!occ)
9423 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009424 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009425
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009426 smp->type = SMP_T_STR;
9427 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009428 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 +02009429 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009430
Willy Tarreau37406352012-04-23 16:16:37 +02009431 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009432 return 0;
9433}
9434
Willy Tarreauc11416f2007-06-17 16:58:38 +02009435/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009436 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009437 */
9438static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009439smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009440 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009441{
9442 struct http_txn *txn = l7;
9443 struct hdr_idx *idx = &txn->hdr_idx;
9444 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009445 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009446 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009447
Willy Tarreau24e32d82012-04-23 23:55:44 +02009448 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009449 return 0;
9450
Willy Tarreaue333ec92012-04-16 16:26:40 +02009451 CHECK_HTTP_MESSAGE_FIRST();
9452
Willy Tarreau33a7e692007-06-10 19:45:56 +02009453 ctx.idx = 0;
9454 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009455 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 +02009456 cnt++;
9457
Willy Tarreauf853c462012-04-23 18:53:56 +02009458 smp->type = SMP_T_UINT;
9459 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009460 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009461 return 1;
9462}
9463
Willy Tarreau185b5c42012-04-26 15:11:51 +02009464/* Fetch an HTTP header's integer value. The integer value is returned. It
9465 * takes a mandatory argument of type string and an optional one of type int
9466 * to designate a specific occurrence. It returns an unsigned integer, which
9467 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009468 */
9469static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009470smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009471 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009472{
Willy Tarreauef38c392013-07-22 16:29:32 +02009473 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009474
Willy Tarreauf853c462012-04-23 18:53:56 +02009475 if (ret > 0) {
9476 smp->type = SMP_T_UINT;
9477 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9478 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009479
Willy Tarreaud53e2422012-04-16 17:21:11 +02009480 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009481}
9482
Cyril Bonté69fa9922012-10-25 00:01:06 +02009483/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9484 * and an optional one of type int to designate a specific occurrence.
9485 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009486 */
9487static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009488smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009489 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009490{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009491 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009492
Willy Tarreauef38c392013-07-22 16:29:32 +02009493 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009494 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9495 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009496 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009497 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009498 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009499 if (smp->data.str.len < temp->size - 1) {
9500 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9501 temp->str[smp->data.str.len] = '\0';
9502 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9503 smp->type = SMP_T_IPV6;
9504 break;
9505 }
9506 }
9507 }
9508
Willy Tarreaud53e2422012-04-16 17:21:11 +02009509 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009510 if (!(smp->flags & SMP_F_NOT_LAST))
9511 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009512 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009513 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009514}
9515
Willy Tarreau737b0c12007-06-10 21:28:46 +02009516/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9517 * the first '/' after the possible hostname, and ends before the possible '?'.
9518 */
9519static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009520smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009521 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009522{
9523 struct http_txn *txn = l7;
9524 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009525
Willy Tarreauc0239e02012-04-16 14:42:55 +02009526 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009527
Willy Tarreau9b28e032012-10-12 23:49:43 +02009528 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009529 ptr = http_get_path(txn);
9530 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009531 return 0;
9532
9533 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009534 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009535 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009536
9537 while (ptr < end && *ptr != '?')
9538 ptr++;
9539
Willy Tarreauf853c462012-04-23 18:53:56 +02009540 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009541 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009542 return 1;
9543}
9544
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009545/* This produces a concatenation of the first occurrence of the Host header
9546 * followed by the path component if it begins with a slash ('/'). This means
9547 * that '*' will not be added, resulting in exactly the first Host entry.
9548 * If no Host header is found, then the path is returned as-is. The returned
9549 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009550 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009551 */
9552static int
9553smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009554 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009555{
9556 struct http_txn *txn = l7;
9557 char *ptr, *end, *beg;
9558 struct hdr_ctx ctx;
9559
9560 CHECK_HTTP_MESSAGE_FIRST();
9561
9562 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009563 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 +02009564 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009565 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009566
9567 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009568 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009569 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009570 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009571 smp->data.str.len = ctx.vlen;
9572
9573 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009574 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 +02009575 beg = http_get_path(txn);
9576 if (!beg)
9577 beg = end;
9578
9579 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9580
9581 if (beg < ptr && *beg == '/') {
9582 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9583 smp->data.str.len += ptr - beg;
9584 }
9585
9586 smp->flags = SMP_F_VOL_1ST;
9587 return 1;
9588}
9589
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009590/* This produces a 32-bit hash of the concatenation of the first occurrence of
9591 * the Host header followed by the path component if it begins with a slash ('/').
9592 * This means that '*' will not be added, resulting in exactly the first Host
9593 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009594 * is hashed using the path hash followed by a full avalanche hash and provides a
9595 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009596 * high-traffic sites without having to store whole paths.
9597 */
9598static int
9599smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009600 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009601{
9602 struct http_txn *txn = l7;
9603 struct hdr_ctx ctx;
9604 unsigned int hash = 0;
9605 char *ptr, *beg, *end;
9606 int len;
9607
9608 CHECK_HTTP_MESSAGE_FIRST();
9609
9610 ctx.idx = 0;
9611 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9612 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9613 ptr = ctx.line + ctx.val;
9614 len = ctx.vlen;
9615 while (len--)
9616 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9617 }
9618
9619 /* now retrieve the path */
9620 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9621 beg = http_get_path(txn);
9622 if (!beg)
9623 beg = end;
9624
9625 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9626
9627 if (beg < ptr && *beg == '/') {
9628 while (beg < ptr)
9629 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9630 }
9631 hash = full_hash(hash);
9632
9633 smp->type = SMP_T_UINT;
9634 smp->data.uint = hash;
9635 smp->flags = SMP_F_VOL_1ST;
9636 return 1;
9637}
9638
Willy Tarreau4a550602012-12-09 14:53:32 +01009639/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009640 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9641 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9642 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009643 * that in environments where IPv6 is insignificant, truncating the output to
9644 * 8 bytes would still work.
9645 */
9646static int
9647smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009648 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009649{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009650 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009651 struct connection *cli_conn = objt_conn(l4->si[0].end);
9652
9653 if (!cli_conn)
9654 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009655
Willy Tarreauef38c392013-07-22 16:29:32 +02009656 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009657 return 0;
9658
Willy Tarreau47ca5452012-12-23 20:22:19 +01009659 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009660 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9661 temp->len += sizeof(smp->data.uint);
9662
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009663 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009664 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009665 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009666 temp->len += 4;
9667 break;
9668 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009669 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009670 temp->len += 16;
9671 break;
9672 default:
9673 return 0;
9674 }
9675
9676 smp->data.str = *temp;
9677 smp->type = SMP_T_BIN;
9678 return 1;
9679}
9680
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009681static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009682smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009683 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009684{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009685 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9686 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9687 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009688
Willy Tarreau24e32d82012-04-23 23:55:44 +02009689 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009690
Willy Tarreauf853c462012-04-23 18:53:56 +02009691 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009692 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009693 return 1;
9694}
9695
Willy Tarreau7f18e522010-10-22 20:04:13 +02009696/* return a valid test if the current request is the first one on the connection */
9697static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009698smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009699 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009700{
9701 if (!s)
9702 return 0;
9703
Willy Tarreauf853c462012-04-23 18:53:56 +02009704 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009705 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009706 return 1;
9707}
9708
Willy Tarreau34db1082012-04-19 17:16:54 +02009709/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009710static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009711smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009712 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009713{
9714
Willy Tarreau24e32d82012-04-23 23:55:44 +02009715 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009716 return 0;
9717
Willy Tarreauc0239e02012-04-16 14:42:55 +02009718 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009719
Willy Tarreauc0239e02012-04-16 14:42:55 +02009720 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009721 return 0;
9722
Willy Tarreauf853c462012-04-23 18:53:56 +02009723 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009724 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009725 return 1;
9726}
Willy Tarreau8797c062007-05-07 00:55:35 +02009727
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009728/* Accepts exactly 1 argument of type userlist */
9729static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009730smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009731 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009732{
9733
9734 if (!args || args->type != ARGT_USR)
9735 return 0;
9736
9737 CHECK_HTTP_MESSAGE_FIRST();
9738
9739 if (!get_http_auth(l4))
9740 return 0;
9741
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009742 /* if the user does not belong to the userlist or has a wrong password,
9743 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009744 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009745 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009746 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9747 return 0;
9748
9749 /* pat_match_auth() will need the user list */
9750 smp->ctx.a[0] = args->data.usr;
9751
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009752 smp->type = SMP_T_STR;
9753 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009754 smp->data.str.str = l4->txn.auth.user;
9755 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009756
9757 return 1;
9758}
9759
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009760/* Try to find the next occurrence of a cookie name in a cookie header value.
9761 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9762 * the cookie value is returned into *value and *value_l, and the function
9763 * returns a pointer to the next pointer to search from if the value was found.
9764 * Otherwise if the cookie was not found, NULL is returned and neither value
9765 * nor value_l are touched. The input <hdr> string should first point to the
9766 * header's value, and the <hdr_end> pointer must point to the first character
9767 * not part of the value. <list> must be non-zero if value may represent a list
9768 * of values (cookie headers). This makes it faster to abort parsing when no
9769 * list is expected.
9770 */
9771static char *
9772extract_cookie_value(char *hdr, const char *hdr_end,
9773 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009774 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009775{
9776 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9777 char *next;
9778
9779 /* we search at least a cookie name followed by an equal, and more
9780 * generally something like this :
9781 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9782 */
9783 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9784 /* Iterate through all cookies on this line */
9785
9786 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9787 att_beg++;
9788
9789 /* find att_end : this is the first character after the last non
9790 * space before the equal. It may be equal to hdr_end.
9791 */
9792 equal = att_end = att_beg;
9793
9794 while (equal < hdr_end) {
9795 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9796 break;
9797 if (http_is_spht[(unsigned char)*equal++])
9798 continue;
9799 att_end = equal;
9800 }
9801
9802 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9803 * is between <att_beg> and <equal>, both may be identical.
9804 */
9805
9806 /* look for end of cookie if there is an equal sign */
9807 if (equal < hdr_end && *equal == '=') {
9808 /* look for the beginning of the value */
9809 val_beg = equal + 1;
9810 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9811 val_beg++;
9812
9813 /* find the end of the value, respecting quotes */
9814 next = find_cookie_value_end(val_beg, hdr_end);
9815
9816 /* make val_end point to the first white space or delimitor after the value */
9817 val_end = next;
9818 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9819 val_end--;
9820 } else {
9821 val_beg = val_end = next = equal;
9822 }
9823
9824 /* We have nothing to do with attributes beginning with '$'. However,
9825 * they will automatically be removed if a header before them is removed,
9826 * since they're supposed to be linked together.
9827 */
9828 if (*att_beg == '$')
9829 continue;
9830
9831 /* Ignore cookies with no equal sign */
9832 if (equal == next)
9833 continue;
9834
9835 /* Now we have the cookie name between att_beg and att_end, and
9836 * its value between val_beg and val_end.
9837 */
9838
9839 if (att_end - att_beg == cookie_name_l &&
9840 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9841 /* let's return this value and indicate where to go on from */
9842 *value = val_beg;
9843 *value_l = val_end - val_beg;
9844 return next + 1;
9845 }
9846
9847 /* Set-Cookie headers only have the name in the first attr=value part */
9848 if (!list)
9849 break;
9850 }
9851
9852 return NULL;
9853}
9854
William Lallemanda43ba4e2014-01-28 18:14:25 +01009855/* Fetch a captured HTTP request header. The index is the position of
9856 * the "capture" option in the configuration file
9857 */
9858static int
9859smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9860 const struct arg *args, struct sample *smp, const char *kw)
9861{
9862 struct proxy *fe = l4->fe;
9863 struct http_txn *txn = l7;
9864 int idx;
9865
9866 if (!args || args->type != ARGT_UINT)
9867 return 0;
9868
9869 idx = args->data.uint;
9870
9871 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9872 return 0;
9873
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009874 smp->type = SMP_T_STR;
9875 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009876 smp->data.str.str = txn->req.cap[idx];
9877 smp->data.str.len = strlen(txn->req.cap[idx]);
9878
9879 return 1;
9880}
9881
9882/* Fetch a captured HTTP response header. The index is the position of
9883 * the "capture" option in the configuration file
9884 */
9885static int
9886smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9887 const struct arg *args, struct sample *smp, const char *kw)
9888{
9889 struct proxy *fe = l4->fe;
9890 struct http_txn *txn = l7;
9891 int idx;
9892
9893 if (!args || args->type != ARGT_UINT)
9894 return 0;
9895
9896 idx = args->data.uint;
9897
9898 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9899 return 0;
9900
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009901 smp->type = SMP_T_STR;
9902 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009903 smp->data.str.str = txn->rsp.cap[idx];
9904 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9905
9906 return 1;
9907}
9908
William Lallemand65ad6e12014-01-31 15:08:02 +01009909/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9910static int
9911smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9912 const struct arg *args, struct sample *smp, const char *kw)
9913{
9914 struct chunk *temp;
9915 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009916 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009917
9918 if (!txn->uri)
9919 return 0;
9920
William Lallemand96a77852014-02-05 00:30:02 +01009921 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009922
William Lallemand96a77852014-02-05 00:30:02 +01009923 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9924 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009925
William Lallemand96a77852014-02-05 00:30:02 +01009926 temp = get_trash_chunk();
9927 temp->str = txn->uri;
9928 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009929 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009930 smp->type = SMP_T_STR;
9931 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009932
9933 return 1;
9934
9935}
9936
9937/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9938static int
9939smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9940 const struct arg *args, struct sample *smp, const char *kw)
9941{
9942 struct chunk *temp;
9943 struct http_txn *txn = l7;
9944 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009945
9946 if (!txn->uri)
9947 return 0;
William Lallemand96a77852014-02-05 00:30:02 +01009948
William Lallemand65ad6e12014-01-31 15:08:02 +01009949 ptr = txn->uri;
9950
9951 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9952 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +01009953
William Lallemand65ad6e12014-01-31 15:08:02 +01009954 if (!*ptr)
9955 return 0;
9956
9957 ptr++; /* skip the space */
9958
9959 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +01009960 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +01009961 if (!ptr)
9962 return 0;
9963 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
9964 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009965
9966 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009967 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009968 smp->type = SMP_T_STR;
9969 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009970
9971 return 1;
9972}
9973
9974
Willy Tarreaue333ec92012-04-16 16:26:40 +02009975/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009976 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009977 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009978 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009979 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009980 * Accepts exactly 1 argument of type string. If the input options indicate
9981 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009982 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009983 */
9984static int
Willy Tarreau51539362012-05-08 12:46:28 +02009985smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009986 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009987{
9988 struct http_txn *txn = l7;
9989 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009990 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009991 const struct http_msg *msg;
9992 const char *hdr_name;
9993 int hdr_name_len;
9994 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009995 int occ = 0;
9996 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009997
Willy Tarreau24e32d82012-04-23 23:55:44 +02009998 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009999 return 0;
10000
Willy Tarreaua890d072013-04-02 12:01:06 +020010001 if (!ctx) {
10002 /* first call */
10003 ctx = &static_hdr_ctx;
10004 ctx->idx = 0;
10005 smp->ctx.a[2] = ctx;
10006 }
10007
Willy Tarreaue333ec92012-04-16 16:26:40 +020010008 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010009
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010010 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010011 msg = &txn->req;
10012 hdr_name = "Cookie";
10013 hdr_name_len = 6;
10014 } else {
10015 msg = &txn->rsp;
10016 hdr_name = "Set-Cookie";
10017 hdr_name_len = 10;
10018 }
10019
Willy Tarreau28376d62012-04-26 21:26:10 +020010020 if (!occ && !(opt & SMP_OPT_ITERATE))
10021 /* no explicit occurrence and single fetch => last cookie by default */
10022 occ = -1;
10023
10024 /* OK so basically here, either we want only one value and it's the
10025 * last one, or we want to iterate over all of them and we fetch the
10026 * next one.
10027 */
10028
Willy Tarreau9b28e032012-10-12 23:49:43 +020010029 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010030 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010031 /* search for the header from the beginning, we must first initialize
10032 * the search parameters.
10033 */
Willy Tarreau37406352012-04-23 16:16:37 +020010034 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010035 ctx->idx = 0;
10036 }
10037
Willy Tarreau28376d62012-04-26 21:26:10 +020010038 smp->flags |= SMP_F_VOL_HDR;
10039
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010040 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010041 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10042 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010043 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10044 goto out;
10045
Willy Tarreau24e32d82012-04-23 23:55:44 +020010046 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010047 continue;
10048
Willy Tarreau37406352012-04-23 16:16:37 +020010049 smp->ctx.a[0] = ctx->line + ctx->val;
10050 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010051 }
10052
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010053 smp->type = SMP_T_STR;
10054 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010055 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010056 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010057 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010058 &smp->data.str.str,
10059 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010060 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010061 found = 1;
10062 if (occ >= 0) {
10063 /* one value was returned into smp->data.str.{str,len} */
10064 smp->flags |= SMP_F_NOT_LAST;
10065 return 1;
10066 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010067 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010068 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010069 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010070 /* all cookie headers and values were scanned. If we're looking for the
10071 * last occurrence, we may return it now.
10072 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010073 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010074 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010075 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010076}
10077
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010078/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010079 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010080 * multiple cookies may be parsed on the same line. The returned sample is of
10081 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010082 */
10083static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010084smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010085 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010086{
10087 struct http_txn *txn = l7;
10088 struct hdr_idx *idx = &txn->hdr_idx;
10089 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010090 const struct http_msg *msg;
10091 const char *hdr_name;
10092 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010093 int cnt;
10094 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010095 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010096
Willy Tarreau24e32d82012-04-23 23:55:44 +020010097 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010098 return 0;
10099
Willy Tarreaue333ec92012-04-16 16:26:40 +020010100 CHECK_HTTP_MESSAGE_FIRST();
10101
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010102 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010103 msg = &txn->req;
10104 hdr_name = "Cookie";
10105 hdr_name_len = 6;
10106 } else {
10107 msg = &txn->rsp;
10108 hdr_name = "Set-Cookie";
10109 hdr_name_len = 10;
10110 }
10111
Willy Tarreau9b28e032012-10-12 23:49:43 +020010112 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010113 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010114 ctx.idx = 0;
10115 cnt = 0;
10116
10117 while (1) {
10118 /* Note: val_beg == NULL every time we need to fetch a new header */
10119 if (!val_beg) {
10120 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10121 break;
10122
Willy Tarreau24e32d82012-04-23 23:55:44 +020010123 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010124 continue;
10125
10126 val_beg = ctx.line + ctx.val;
10127 val_end = val_beg + ctx.vlen;
10128 }
10129
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010130 smp->type = SMP_T_STR;
10131 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010132 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010133 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010134 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010135 &smp->data.str.str,
10136 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010137 cnt++;
10138 }
10139 }
10140
Willy Tarreaub169eba2013-12-16 15:14:43 +010010141 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010142 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010143 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010144 return 1;
10145}
10146
Willy Tarreau51539362012-05-08 12:46:28 +020010147/* Fetch an cookie's integer value. The integer value is returned. It
10148 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10149 */
10150static int
10151smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010152 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010153{
Willy Tarreauef38c392013-07-22 16:29:32 +020010154 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010155
10156 if (ret > 0) {
10157 smp->type = SMP_T_UINT;
10158 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10159 }
10160
10161 return ret;
10162}
10163
Willy Tarreau8797c062007-05-07 00:55:35 +020010164/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010165/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010166/************************************************************************/
10167
David Cournapeau16023ee2010-12-23 20:55:41 +090010168/*
10169 * Given a path string and its length, find the position of beginning of the
10170 * query string. Returns NULL if no query string is found in the path.
10171 *
10172 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10173 *
10174 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10175 */
bedis4c75cca2012-10-05 08:38:24 +020010176static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010177{
10178 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010179
bedis4c75cca2012-10-05 08:38:24 +020010180 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010181 return p ? p + 1 : NULL;
10182}
10183
bedis4c75cca2012-10-05 08:38:24 +020010184static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010185{
bedis4c75cca2012-10-05 08:38:24 +020010186 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010187}
10188
10189/*
10190 * Given a url parameter, find the starting position of the first occurence,
10191 * or NULL if the parameter is not found.
10192 *
10193 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10194 * the function will return query_string+8.
10195 */
10196static char*
10197find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010198 char* url_param_name, size_t url_param_name_l,
10199 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010200{
10201 char *pos, *last;
10202
10203 pos = query_string;
10204 last = query_string + query_string_l - url_param_name_l - 1;
10205
10206 while (pos <= last) {
10207 if (pos[url_param_name_l] == '=') {
10208 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10209 return pos;
10210 pos += url_param_name_l + 1;
10211 }
bedis4c75cca2012-10-05 08:38:24 +020010212 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010213 pos++;
10214 pos++;
10215 }
10216 return NULL;
10217}
10218
10219/*
10220 * Given a url parameter name, returns its value and size into *value and
10221 * *value_l respectively, and returns non-zero. If the parameter is not found,
10222 * zero is returned and value/value_l are not touched.
10223 */
10224static int
10225find_url_param_value(char* path, size_t path_l,
10226 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010227 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010228{
10229 char *query_string, *qs_end;
10230 char *arg_start;
10231 char *value_start, *value_end;
10232
bedis4c75cca2012-10-05 08:38:24 +020010233 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010234 if (!query_string)
10235 return 0;
10236
10237 qs_end = path + path_l;
10238 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010239 url_param_name, url_param_name_l,
10240 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010241 if (!arg_start)
10242 return 0;
10243
10244 value_start = arg_start + url_param_name_l + 1;
10245 value_end = value_start;
10246
bedis4c75cca2012-10-05 08:38:24 +020010247 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010248 value_end++;
10249
10250 *value = value_start;
10251 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010252 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010253}
10254
10255static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010256smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010257 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010258{
bedis4c75cca2012-10-05 08:38:24 +020010259 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010260 struct http_txn *txn = l7;
10261 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010262
bedis4c75cca2012-10-05 08:38:24 +020010263 if (!args || args[0].type != ARGT_STR ||
10264 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010265 return 0;
10266
10267 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010268
bedis4c75cca2012-10-05 08:38:24 +020010269 if (args[1].type)
10270 delim = *args[1].data.str.str;
10271
Willy Tarreau9b28e032012-10-12 23:49:43 +020010272 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010273 args->data.str.str, args->data.str.len,
10274 &smp->data.str.str, &smp->data.str.len,
10275 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010276 return 0;
10277
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010278 smp->type = SMP_T_STR;
10279 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010280 return 1;
10281}
10282
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010283/* Return the signed integer value for the specified url parameter (see url_param
10284 * above).
10285 */
10286static int
10287smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010288 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010289{
Willy Tarreauef38c392013-07-22 16:29:32 +020010290 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010291
10292 if (ret > 0) {
10293 smp->type = SMP_T_UINT;
10294 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10295 }
10296
10297 return ret;
10298}
10299
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010300/* This produces a 32-bit hash of the concatenation of the first occurrence of
10301 * the Host header followed by the path component if it begins with a slash ('/').
10302 * This means that '*' will not be added, resulting in exactly the first Host
10303 * entry. If no Host header is found, then the path is used. The resulting value
10304 * is hashed using the url hash followed by a full avalanche hash and provides a
10305 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10306 * high-traffic sites without having to store whole paths.
10307 * this differs from the base32 functions in that it includes the url parameters
10308 * as well as the path
10309 */
10310static int
10311smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010312 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010313{
10314 struct http_txn *txn = l7;
10315 struct hdr_ctx ctx;
10316 unsigned int hash = 0;
10317 char *ptr, *beg, *end;
10318 int len;
10319
10320 CHECK_HTTP_MESSAGE_FIRST();
10321
10322 ctx.idx = 0;
10323 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10324 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10325 ptr = ctx.line + ctx.val;
10326 len = ctx.vlen;
10327 while (len--)
10328 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10329 }
10330
10331 /* now retrieve the path */
10332 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10333 beg = http_get_path(txn);
10334 if (!beg)
10335 beg = end;
10336
10337 for (ptr = beg; ptr < end ; ptr++);
10338
10339 if (beg < ptr && *beg == '/') {
10340 while (beg < ptr)
10341 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10342 }
10343 hash = full_hash(hash);
10344
10345 smp->type = SMP_T_UINT;
10346 smp->data.uint = hash;
10347 smp->flags = SMP_F_VOL_1ST;
10348 return 1;
10349}
10350
10351/* This concatenates the source address with the 32-bit hash of the Host and
10352 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10353 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10354 * on the source address length. The URL hash is stored before the address so
10355 * that in environments where IPv6 is insignificant, truncating the output to
10356 * 8 bytes would still work.
10357 */
10358static int
10359smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010360 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010361{
10362 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010363 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010364
Willy Tarreaue155ec22013-11-18 18:33:22 +010010365 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010366 return 0;
10367
10368 temp = get_trash_chunk();
10369 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10370 temp->len += sizeof(smp->data.uint);
10371
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010372 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010373 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010374 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010375 temp->len += 4;
10376 break;
10377 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010378 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010379 temp->len += 16;
10380 break;
10381 default:
10382 return 0;
10383 }
10384
10385 smp->data.str = *temp;
10386 smp->type = SMP_T_BIN;
10387 return 1;
10388}
10389
Willy Tarreau185b5c42012-04-26 15:11:51 +020010390/* This function is used to validate the arguments passed to any "hdr" fetch
10391 * keyword. These keywords support an optional positive or negative occurrence
10392 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10393 * is assumed that the types are already the correct ones. Returns 0 on error,
10394 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10395 * error message in case of error, that the caller is responsible for freeing.
10396 * The initial location must either be freeable or NULL.
10397 */
10398static int val_hdr(struct arg *arg, char **err_msg)
10399{
10400 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010401 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010402 return 0;
10403 }
10404 return 1;
10405}
10406
Willy Tarreau276fae92013-07-25 14:36:01 +020010407/* takes an UINT value on input supposed to represent the time since EPOCH,
10408 * adds an optional offset found in args[0] and emits a string representing
10409 * the date in RFC-1123/5322 format.
10410 */
10411static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10412{
10413 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10414 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10415 struct chunk *temp;
10416 struct tm *tm;
10417 time_t curr_date = smp->data.uint;
10418
10419 /* add offset */
10420 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10421 curr_date += args[0].data.sint;
10422
10423 tm = gmtime(&curr_date);
10424
10425 temp = get_trash_chunk();
10426 temp->len = snprintf(temp->str, temp->size - temp->len,
10427 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10428 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10429 tm->tm_hour, tm->tm_min, tm->tm_sec);
10430
10431 smp->data.str = *temp;
10432 smp->type = SMP_T_STR;
10433 return 1;
10434}
10435
Willy Tarreau4a568972010-05-12 08:08:50 +020010436/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010437/* All supported ACL keywords must be declared here. */
10438/************************************************************************/
10439
10440/* Note: must not be declared <const> as its list will be overwritten.
10441 * Please take care of keeping this list alphabetically sorted.
10442 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010443static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010444 { "base", "base", PAT_MATCH_STR },
10445 { "base_beg", "base", PAT_MATCH_BEG },
10446 { "base_dir", "base", PAT_MATCH_DIR },
10447 { "base_dom", "base", PAT_MATCH_DOM },
10448 { "base_end", "base", PAT_MATCH_END },
10449 { "base_len", "base", PAT_MATCH_LEN },
10450 { "base_reg", "base", PAT_MATCH_REG },
10451 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010452
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010453 { "cook", "req.cook", PAT_MATCH_STR },
10454 { "cook_beg", "req.cook", PAT_MATCH_BEG },
10455 { "cook_dir", "req.cook", PAT_MATCH_DIR },
10456 { "cook_dom", "req.cook", PAT_MATCH_DOM },
10457 { "cook_end", "req.cook", PAT_MATCH_END },
10458 { "cook_len", "req.cook", PAT_MATCH_LEN },
10459 { "cook_reg", "req.cook", PAT_MATCH_REG },
10460 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010461
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010462 { "hdr", "req.hdr", PAT_MATCH_STR },
10463 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
10464 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
10465 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
10466 { "hdr_end", "req.hdr", PAT_MATCH_END },
10467 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
10468 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
10469 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010470
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010471 /* these two declarations uses strings with list storage (in place
10472 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
10473 * and delete functions are relative to the list management. The parse
10474 * and match method are related to the corresponding fetch methods. This
10475 * is very particular ACL declaration mode.
10476 */
10477 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
10478 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010479
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010480 { "path", "path", PAT_MATCH_STR },
10481 { "path_beg", "path", PAT_MATCH_BEG },
10482 { "path_dir", "path", PAT_MATCH_DIR },
10483 { "path_dom", "path", PAT_MATCH_DOM },
10484 { "path_end", "path", PAT_MATCH_END },
10485 { "path_len", "path", PAT_MATCH_LEN },
10486 { "path_reg", "path", PAT_MATCH_REG },
10487 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010488
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010489 { "req_ver", "req.ver", PAT_MATCH_STR },
10490 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010491
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010492 { "scook", "res.cook", PAT_MATCH_STR },
10493 { "scook_beg", "res.cook", PAT_MATCH_BEG },
10494 { "scook_dir", "res.cook", PAT_MATCH_DIR },
10495 { "scook_dom", "res.cook", PAT_MATCH_DOM },
10496 { "scook_end", "res.cook", PAT_MATCH_END },
10497 { "scook_len", "res.cook", PAT_MATCH_LEN },
10498 { "scook_reg", "res.cook", PAT_MATCH_REG },
10499 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010500
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010501 { "shdr", "res.hdr", PAT_MATCH_STR },
10502 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
10503 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
10504 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
10505 { "shdr_end", "res.hdr", PAT_MATCH_END },
10506 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
10507 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
10508 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010509
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010510 { "url", "url", PAT_MATCH_STR },
10511 { "url_beg", "url", PAT_MATCH_BEG },
10512 { "url_dir", "url", PAT_MATCH_DIR },
10513 { "url_dom", "url", PAT_MATCH_DOM },
10514 { "url_end", "url", PAT_MATCH_END },
10515 { "url_len", "url", PAT_MATCH_LEN },
10516 { "url_reg", "url", PAT_MATCH_REG },
10517 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010518
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010519 { "urlp", "urlp", PAT_MATCH_STR },
10520 { "urlp_beg", "urlp", PAT_MATCH_BEG },
10521 { "urlp_dir", "urlp", PAT_MATCH_DIR },
10522 { "urlp_dom", "urlp", PAT_MATCH_DOM },
10523 { "urlp_end", "urlp", PAT_MATCH_END },
10524 { "urlp_len", "urlp", PAT_MATCH_LEN },
10525 { "urlp_reg", "urlp", PAT_MATCH_REG },
10526 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010527
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010528 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010529}};
10530
10531/************************************************************************/
10532/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010533/************************************************************************/
10534/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010535static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010536 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010537 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10538 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10539
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010540 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10541 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemand65ad6e12014-01-31 15:08:02 +010010542
William Lallemanda43ba4e2014-01-28 18:14:25 +010010543 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010544 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
10545 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010010546
Willy Tarreau409bcde2013-01-08 00:31:00 +010010547 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10548 * are only here to match the ACL's name, are request-only and are used
10549 * for ACL compatibility only.
10550 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010551 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10552 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010553 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10554 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10555
10556 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10557 * only here to match the ACL's name, are request-only and are used for
10558 * ACL compatibility only.
10559 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010560 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010561 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10562 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10563 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10564
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010565 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010566 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010567 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010568 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010569 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010570
10571 /* HTTP protocol on the request path */
10572 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010573 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010574
10575 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010576 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10577 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010578
10579 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010580 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10581 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010582
Willy Tarreau18ed2562013-01-14 15:56:36 +010010583 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010584 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010585 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10586 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10587
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010588 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010589 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010590 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010591 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10592 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10593 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10594
10595 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010596 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010597 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10598 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10599
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010600 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010601 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010602 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010603 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10604 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10605 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10606
Willy Tarreau409bcde2013-01-08 00:31:00 +010010607 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010608 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010609 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10610 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010611 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010612
10613 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010614 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010615 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10616 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10617 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10618
10619 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010620 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010621 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10622 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010623 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10624 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010625 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10626 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010627 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10628 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010629}};
10630
Willy Tarreau8797c062007-05-07 00:55:35 +020010631
Willy Tarreau276fae92013-07-25 14:36:01 +020010632/* Note: must not be declared <const> as its list will be overwritten */
10633static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10634 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10635 { NULL, NULL, 0, 0, 0 },
10636}};
10637
Willy Tarreau8797c062007-05-07 00:55:35 +020010638__attribute__((constructor))
10639static void __http_protocol_init(void)
10640{
10641 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010642 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010643 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010644}
10645
10646
Willy Tarreau58f10d72006-12-04 02:26:12 +010010647/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010648 * Local variables:
10649 * c-indent-level: 8
10650 * c-basic-offset: 8
10651 * End:
10652 */