blob: 7c0b7fe735974062d621c4bba780a9b8a57ef938 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100238fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240#else
241#error "Check if your OS uses bitfields for fd_sets"
242#endif
243
Willy Tarreau80587432006-12-24 17:47:20 +0100244void init_proto_http()
245{
Willy Tarreau42250582007-04-01 01:30:43 +0200246 int i;
247 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100248 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200249
Willy Tarreau80587432006-12-24 17:47:20 +0100250 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
251 if (!http_err_msgs[msg]) {
252 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
253 abort();
254 }
255
256 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
257 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
258 }
Willy Tarreau42250582007-04-01 01:30:43 +0200259
260 /* initialize the log header encoding map : '{|}"#' should be encoded with
261 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
262 * URL encoding only requires '"', '#' to be encoded as well as non-
263 * printable characters above.
264 */
265 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
266 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200268 for (i = 0; i < 32; i++) {
269 FD_SET(i, hdr_encode_map);
270 FD_SET(i, url_encode_map);
271 }
272 for (i = 127; i < 256; i++) {
273 FD_SET(i, hdr_encode_map);
274 FD_SET(i, url_encode_map);
275 }
276
277 tmp = "\"#{|}";
278 while (*tmp) {
279 FD_SET(*tmp, hdr_encode_map);
280 tmp++;
281 }
282
283 tmp = "\"#";
284 while (*tmp) {
285 FD_SET(*tmp, url_encode_map);
286 tmp++;
287 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200288
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100289 /* initialize the http header encoding map. The draft httpbis define the
290 * header content as:
291 *
292 * HTTP-message = start-line
293 * *( header-field CRLF )
294 * CRLF
295 * [ message-body ]
296 * header-field = field-name ":" OWS field-value OWS
297 * field-value = *( field-content / obs-fold )
298 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
299 * obs-fold = CRLF 1*( SP / HTAB )
300 * field-vchar = VCHAR / obs-text
301 * VCHAR = %x21-7E
302 * obs-text = %x80-FF
303 *
304 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
305 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
306 * "obs-fold" is volontary forgotten because haproxy remove this.
307 */
308 memset(http_encode_map, 0, sizeof(http_encode_map));
309 for (i = 0x00; i <= 0x08; i++)
310 FD_SET(i, http_encode_map);
311 for (i = 0x0a; i <= 0x1f; i++)
312 FD_SET(i, http_encode_map);
313 FD_SET(0x7f, http_encode_map);
314
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200315 /* memory allocations */
316 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100317 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100318}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319
Willy Tarreau53b6c742006-12-17 13:37:46 +0100320/*
321 * We have 26 list of methods (1 per first letter), each of which can have
322 * up to 3 entries (2 valid, 1 null).
323 */
324struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100325 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100326 int len;
327 const char text[8];
328};
329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100330const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331 ['C' - 'A'] = {
332 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
333 },
334 ['D' - 'A'] = {
335 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
336 },
337 ['G' - 'A'] = {
338 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
339 },
340 ['H' - 'A'] = {
341 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
342 },
343 ['P' - 'A'] = {
344 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
345 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
346 },
347 ['T' - 'A'] = {
348 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
349 },
350 /* rest is empty like this :
351 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
352 */
353};
354
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100355const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
356 [HTTP_METH_NONE] = { "", 0 },
357 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
358 [HTTP_METH_GET] = { "GET", 3 },
359 [HTTP_METH_HEAD] = { "HEAD", 4 },
360 [HTTP_METH_POST] = { "POST", 4 },
361 [HTTP_METH_PUT] = { "PUT", 3 },
362 [HTTP_METH_DELETE] = { "DELETE", 6 },
363 [HTTP_METH_TRACE] = { "TRACE", 5 },
364 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
365};
366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100367/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100369 * RFC2616 for those chars.
370 */
371
372const char http_is_spht[256] = {
373 [' '] = 1, ['\t'] = 1,
374};
375
376const char http_is_crlf[256] = {
377 ['\r'] = 1, ['\n'] = 1,
378};
379
380const char http_is_lws[256] = {
381 [' '] = 1, ['\t'] = 1,
382 ['\r'] = 1, ['\n'] = 1,
383};
384
385const char http_is_sep[256] = {
386 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
387 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
388 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
389 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
390 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
391};
392
393const char http_is_ctl[256] = {
394 [0 ... 31] = 1,
395 [127] = 1,
396};
397
398/*
399 * A token is any ASCII char that is neither a separator nor a CTL char.
400 * Do not overwrite values in assignment since gcc-2.95 will not handle
401 * them correctly. Instead, define every non-CTL char's status.
402 */
403const char http_is_token[256] = {
404 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
405 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
406 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
407 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
408 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
409 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
410 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
411 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
412 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
413 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
414 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
415 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
416 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
417 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
418 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
419 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
420 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
421 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
422 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
423 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
424 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
425 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
426 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
427 ['|'] = 1, ['}'] = 0, ['~'] = 1,
428};
429
430
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100431/*
432 * An http ver_token is any ASCII which can be found in an HTTP version,
433 * which includes 'H', 'T', 'P', '/', '.' and any digit.
434 */
435const char http_is_ver_token[256] = {
436 ['.'] = 1, ['/'] = 1,
437 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
438 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
439 ['H'] = 1, ['P'] = 1, ['T'] = 1,
440};
441
442
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100444 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
445 */
446#if defined(DEBUG_FSM)
447static void http_silent_debug(int line, struct session *s)
448{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100449 chunk_printf(&trash,
450 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
451 line,
452 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
453 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
454 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100455
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100456 chunk_printf(&trash,
457 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
458 line,
459 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
460 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
461 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100462}
463#else
464#define http_silent_debug(l,s) do { } while (0)
465#endif
466
467/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100468 * Adds a header and its CRLF at the tail of the message's buffer, just before
469 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100470 * The header is also automatically added to the index <hdr_idx>, and the end
471 * of headers is automatically adjusted. The number of bytes added is returned
472 * on success, otherwise <0 is returned indicating an error.
473 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100474int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475{
476 int bytes, len;
477
478 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200479 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100480 if (!bytes)
481 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100482 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
484}
485
486/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100487 * Adds a header and its CRLF at the tail of the message's buffer, just before
488 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 * the buffer is only opened and the space reserved, but nothing is copied.
490 * The header is also automatically added to the index <hdr_idx>, and the end
491 * of headers is automatically adjusted. The number of bytes added is returned
492 * on success, otherwise <0 is returned indicating an error.
493 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100494int http_header_add_tail2(struct http_msg *msg,
495 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100496{
497 int bytes;
498
Willy Tarreau9b28e032012-10-12 23:49:43 +0200499 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100500 if (!bytes)
501 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100502 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100503 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
504}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505
506/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100507 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
508 * If so, returns the position of the first non-space character relative to
509 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
510 * to return a pointer to the place after the first space. Returns 0 if the
511 * header name does not match. Checks are case-insensitive.
512 */
513int http_header_match2(const char *hdr, const char *end,
514 const char *name, int len)
515{
516 const char *val;
517
518 if (hdr + len >= end)
519 return 0;
520 if (hdr[len] != ':')
521 return 0;
522 if (strncasecmp(hdr, name, len) != 0)
523 return 0;
524 val = hdr + len + 1;
525 while (val < end && HTTP_IS_SPHT(*val))
526 val++;
527 if ((val >= end) && (len + 2 <= end - hdr))
528 return len + 2; /* we may replace starting from second space */
529 return val - hdr;
530}
531
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200532/* Find the first or next occurrence of header <name> in message buffer <sol>
533 * using headers index <idx>, and return it in the <ctx> structure. This
534 * structure holds everything necessary to use the header and find next
535 * occurrence. If its <idx> member is 0, the header is searched from the
536 * beginning. Otherwise, the next occurrence is returned. The function returns
537 * 1 when it finds a value, and 0 when there is no more. It is very similar to
538 * http_find_header2() except that it is designed to work with full-line headers
539 * whose comma is not a delimiter but is part of the syntax. As a special case,
540 * if ctx->val is NULL when searching for a new values of a header, the current
541 * header is rescanned. This allows rescanning after a header deletion.
542 */
543int http_find_full_header2(const char *name, int len,
544 char *sol, struct hdr_idx *idx,
545 struct hdr_ctx *ctx)
546{
547 char *eol, *sov;
548 int cur_idx, old_idx;
549
550 cur_idx = ctx->idx;
551 if (cur_idx) {
552 /* We have previously returned a header, let's search another one */
553 sol = ctx->line;
554 eol = sol + idx->v[cur_idx].len;
555 goto next_hdr;
556 }
557
558 /* first request for this header */
559 sol += hdr_idx_first_pos(idx);
560 old_idx = 0;
561 cur_idx = hdr_idx_first_idx(idx);
562 while (cur_idx) {
563 eol = sol + idx->v[cur_idx].len;
564
565 if (len == 0) {
566 /* No argument was passed, we want any header.
567 * To achieve this, we simply build a fake request. */
568 while (sol + len < eol && sol[len] != ':')
569 len++;
570 name = sol;
571 }
572
573 if ((len < eol - sol) &&
574 (sol[len] == ':') &&
575 (strncasecmp(sol, name, len) == 0)) {
576 ctx->del = len;
577 sov = sol + len + 1;
578 while (sov < eol && http_is_lws[(unsigned char)*sov])
579 sov++;
580
581 ctx->line = sol;
582 ctx->prev = old_idx;
583 ctx->idx = cur_idx;
584 ctx->val = sov - sol;
585 ctx->tws = 0;
586 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
587 eol--;
588 ctx->tws++;
589 }
590 ctx->vlen = eol - sov;
591 return 1;
592 }
593 next_hdr:
594 sol = eol + idx->v[cur_idx].cr + 1;
595 old_idx = cur_idx;
596 cur_idx = idx->v[cur_idx].next;
597 }
598 return 0;
599}
600
Willy Tarreau68085d82010-01-18 14:54:04 +0100601/* Find the end of the header value contained between <s> and <e>. See RFC2616,
602 * par 2.2 for more information. Note that it requires a valid header to return
603 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100605char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200606{
607 int quoted, qdpair;
608
609 quoted = qdpair = 0;
610 for (; s < e; s++) {
611 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200612 else if (quoted) {
613 if (*s == '\\') qdpair = 1;
614 else if (*s == '"') quoted = 0;
615 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 else if (*s == '"') quoted = 1;
617 else if (*s == ',') return s;
618 }
619 return s;
620}
621
622/* Find the first or next occurrence of header <name> in message buffer <sol>
623 * using headers index <idx>, and return it in the <ctx> structure. This
624 * structure holds everything necessary to use the header and find next
625 * occurrence. If its <idx> member is 0, the header is searched from the
626 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 * 1 when it finds a value, and 0 when there is no more. It is designed to work
628 * with headers defined as comma-separated lists. As a special case, if ctx->val
629 * is NULL when searching for a new values of a header, the current header is
630 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200631 */
632int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200634 struct hdr_ctx *ctx)
635{
Willy Tarreau68085d82010-01-18 14:54:04 +0100636 char *eol, *sov;
637 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 cur_idx = ctx->idx;
640 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200641 /* We have previously returned a value, let's search
642 * another one on the same line.
643 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200644 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200645 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100646 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200647 eol = sol + idx->v[cur_idx].len;
648
649 if (sov >= eol)
650 /* no more values in this header */
651 goto next_hdr;
652
Willy Tarreau68085d82010-01-18 14:54:04 +0100653 /* values remaining for this header, skip the comma but save it
654 * for later use (eg: for header deletion).
655 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 sov++;
657 while (sov < eol && http_is_lws[(unsigned char)*sov])
658 sov++;
659
660 goto return_hdr;
661 }
662
663 /* first request for this header */
664 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100665 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200666 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 while (cur_idx) {
668 eol = sol + idx->v[cur_idx].len;
669
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200670 if (len == 0) {
671 /* No argument was passed, we want any header.
672 * To achieve this, we simply build a fake request. */
673 while (sol + len < eol && sol[len] != ':')
674 len++;
675 name = sol;
676 }
677
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 if ((len < eol - sol) &&
679 (sol[len] == ':') &&
680 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100681 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200682 sov = sol + len + 1;
683 while (sov < eol && http_is_lws[(unsigned char)*sov])
684 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100685
Willy Tarreau33a7e692007-06-10 19:45:56 +0200686 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100687 ctx->prev = old_idx;
688 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200689 ctx->idx = cur_idx;
690 ctx->val = sov - sol;
691
692 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200693 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200694 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 eol--;
696 ctx->tws++;
697 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200698 ctx->vlen = eol - sov;
699 return 1;
700 }
701 next_hdr:
702 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100703 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200704 cur_idx = idx->v[cur_idx].next;
705 }
706 return 0;
707}
708
709int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100710 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200711 struct hdr_ctx *ctx)
712{
713 return http_find_header2(name, strlen(name), sol, idx, ctx);
714}
715
Willy Tarreau68085d82010-01-18 14:54:04 +0100716/* Remove one value of a header. This only works on a <ctx> returned by one of
717 * the http_find_header functions. The value is removed, as well as surrounding
718 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100719 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100720 * message <msg>. The new index is returned. If it is zero, it means there is
721 * no more header, so any processing may stop. The ctx is always left in a form
722 * that can be handled by http_find_header2() to find next occurrence.
723 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100724int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100725{
726 int cur_idx = ctx->idx;
727 char *sol = ctx->line;
728 struct hdr_idx_elem *hdr;
729 int delta, skip_comma;
730
731 if (!cur_idx)
732 return 0;
733
734 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200735 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100736 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200737 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100738 http_msg_move_end(msg, delta);
739 idx->used--;
740 hdr->len = 0; /* unused entry */
741 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100742 if (idx->tail == ctx->idx)
743 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100744 ctx->idx = ctx->prev; /* walk back to the end of previous header */
745 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
746 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 return ctx->idx;
749 }
750
751 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
753 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100754 */
755
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200756 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200757 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200758 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 NULL, 0);
760 hdr->len += delta;
761 http_msg_move_end(msg, delta);
762 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200763 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100764 return ctx->idx;
765}
766
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100767/* This function handles a server error at the stream interface level. The
768 * stream interface is assumed to be already in a closed state. An optional
769 * message is copied into the input buffer, and an HTTP status code stored.
770 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100771 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100773static void http_server_error(struct session *t, struct stream_interface *si,
774 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200776 channel_auto_read(si->ob);
777 channel_abort(si->ob);
778 channel_auto_close(si->ob);
779 channel_erase(si->ob);
780 channel_auto_close(si->ib);
781 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100782 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100783 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200784 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 }
786 if (!(t->flags & SN_ERR_MASK))
787 t->flags |= err;
788 if (!(t->flags & SN_FINST_MASK))
789 t->flags |= finst;
790}
791
Willy Tarreau80587432006-12-24 17:47:20 +0100792/* This function returns the appropriate error location for the given session
793 * and message.
794 */
795
Willy Tarreau783f2582012-09-04 12:19:04 +0200796struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100797{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 if (s->be->errmsg[msgnum].str)
799 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100800 else if (s->fe->errmsg[msgnum].str)
801 return &s->fe->errmsg[msgnum];
802 else
803 return &http_err_chunks[msgnum];
804}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805
Willy Tarreau53b6c742006-12-17 13:37:46 +0100806/*
807 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
808 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
809 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100810enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811{
812 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100813 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100814
815 m = ((unsigned)*str - 'A');
816
817 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100818 for (h = http_methods[m]; h->len > 0; h++) {
819 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100820 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100821 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100822 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100823 };
824 return HTTP_METH_OTHER;
825 }
826 return HTTP_METH_NONE;
827
828}
829
Willy Tarreau21d2af32008-02-14 20:25:24 +0100830/* Parse the URI from the given transaction (which is assumed to be in request
831 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
832 * It is returned otherwise.
833 */
834static char *
835http_get_path(struct http_txn *txn)
836{
837 char *ptr, *end;
838
Willy Tarreau9b28e032012-10-12 23:49:43 +0200839 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100840 end = ptr + txn->req.sl.rq.u_l;
841
842 if (ptr >= end)
843 return NULL;
844
845 /* RFC2616, par. 5.1.2 :
846 * Request-URI = "*" | absuri | abspath | authority
847 */
848
849 if (*ptr == '*')
850 return NULL;
851
852 if (isalpha((unsigned char)*ptr)) {
853 /* this is a scheme as described by RFC3986, par. 3.1 */
854 ptr++;
855 while (ptr < end &&
856 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
857 ptr++;
858 /* skip '://' */
859 if (ptr == end || *ptr++ != ':')
860 return NULL;
861 if (ptr == end || *ptr++ != '/')
862 return NULL;
863 if (ptr == end || *ptr++ != '/')
864 return NULL;
865 }
866 /* skip [user[:passwd]@]host[:[port]] */
867
868 while (ptr < end && *ptr != '/')
869 ptr++;
870
871 if (ptr == end)
872 return NULL;
873
874 /* OK, we got the '/' ! */
875 return ptr;
876}
877
William Lallemand65ad6e12014-01-31 15:08:02 +0100878/* Parse the URI from the given string and look for the "/" beginning the PATH.
879 * If not found, return NULL. It is returned otherwise.
880 */
881static char *
882http_get_path_from_string(char *str)
883{
884 char *ptr = str;
885
886 /* RFC2616, par. 5.1.2 :
887 * Request-URI = "*" | absuri | abspath | authority
888 */
889
890 if (*ptr == '*')
891 return NULL;
892
893 if (isalpha((unsigned char)*ptr)) {
894 /* this is a scheme as described by RFC3986, par. 3.1 */
895 ptr++;
896 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
897 ptr++;
898 /* skip '://' */
899 if (*ptr == '\0' || *ptr++ != ':')
900 return NULL;
901 if (*ptr == '\0' || *ptr++ != '/')
902 return NULL;
903 if (*ptr == '\0' || *ptr++ != '/')
904 return NULL;
905 }
906 /* skip [user[:passwd]@]host[:[port]] */
907
908 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
909 ptr++;
910
911 if (*ptr == '\0' || *ptr == ' ')
912 return NULL;
913
914 /* OK, we got the '/' ! */
915 return ptr;
916}
917
Willy Tarreau71241ab2012-12-27 11:30:54 +0100918/* Returns a 302 for a redirectable request that reaches a server working in
919 * in redirect mode. This may only be called just after the stream interface
920 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
921 * follow normal proxy processing. NOTE: this function is designed to support
922 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100924void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925{
926 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100927 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200929 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930
931 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 trash.len = strlen(HTTP_302);
933 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100935 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100936
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100938 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 return;
940
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100941 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100942 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100943 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
944 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100945 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100946
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200947 /* 3: add the request URI. Since it was already forwarded, we need
948 * to temporarily rewind the buffer.
949 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200951 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200952
Willy Tarreauefb453c2008-10-26 20:49:47 +0100953 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200954 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200955
Willy Tarreau9b28e032012-10-12 23:49:43 +0200956 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200957
Willy Tarreauefb453c2008-10-26 20:49:47 +0100958 if (!path)
959 return;
960
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100961 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100962 return;
963
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100964 memcpy(trash.str + trash.len, path, len);
965 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100966
967 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100968 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
969 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100970 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100971 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
972 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100973 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100974
975 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200976 si_shutr(si);
977 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 si->state = SI_ST_CLO;
980
981 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200982 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100985 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500986 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987}
988
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 * that the server side is closed. Note that err_type is actually a
991 * bitmask, where almost only aborts may be cumulated with other
992 * values. We consider that aborted operations are more important
993 * than timeouts or errors due to the fact that nobody else in the
994 * logs might explain incomplete retries. All others should avoid
995 * being cumulated. It should normally not be possible to have multiple
996 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100997 * Note that connection errors appearing on the second request of a keep-alive
998 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001000void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001001{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001002 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003
1004 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001005 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001006 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001008 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001009 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1010 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001012 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001013 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001014 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001015 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001016 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001017 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001019 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1020 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001022 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001023 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001024 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001025 else if (err_type & SI_ET_CONN_RES)
1026 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001027 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1028 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001029 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001030 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001031 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032}
1033
Willy Tarreau42250582007-04-01 01:30:43 +02001034extern const char sess_term_cond[8];
1035extern const char sess_fin_state[8];
1036extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001037struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001038struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001039struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040
Willy Tarreau117f59e2007-03-04 18:17:17 +01001041/*
1042 * Capture headers from message starting at <som> according to header list
1043 * <cap_hdr>, and fill the <idx> structure appropriately.
1044 */
1045void capture_headers(char *som, struct hdr_idx *idx,
1046 char **cap, struct cap_hdr *cap_hdr)
1047{
1048 char *eol, *sol, *col, *sov;
1049 int cur_idx;
1050 struct cap_hdr *h;
1051 int len;
1052
1053 sol = som + hdr_idx_first_pos(idx);
1054 cur_idx = hdr_idx_first_idx(idx);
1055
1056 while (cur_idx) {
1057 eol = sol + idx->v[cur_idx].len;
1058
1059 col = sol;
1060 while (col < eol && *col != ':')
1061 col++;
1062
1063 sov = col + 1;
1064 while (sov < eol && http_is_lws[(unsigned char)*sov])
1065 sov++;
1066
1067 for (h = cap_hdr; h; h = h->next) {
1068 if ((h->namelen == col - sol) &&
1069 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1070 if (cap[h->index] == NULL)
1071 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001072 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001073
1074 if (cap[h->index] == NULL) {
1075 Alert("HTTP capture : out of memory.\n");
1076 continue;
1077 }
1078
1079 len = eol - sov;
1080 if (len > h->len)
1081 len = h->len;
1082
1083 memcpy(cap[h->index], sov, len);
1084 cap[h->index][len]=0;
1085 }
1086 }
1087 sol = eol + idx->v[cur_idx].cr + 1;
1088 cur_idx = idx->v[cur_idx].next;
1089 }
1090}
1091
1092
Willy Tarreau42250582007-04-01 01:30:43 +02001093/* either we find an LF at <ptr> or we jump to <bad>.
1094 */
1095#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1096
1097/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1098 * otherwise to <http_msg_ood> with <state> set to <st>.
1099 */
1100#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1101 ptr++; \
1102 if (likely(ptr < end)) \
1103 goto good; \
1104 else { \
1105 state = (st); \
1106 goto http_msg_ood; \
1107 } \
1108 } while (0)
1109
1110
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001112 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1114 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1115 * will give undefined results.
1116 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1117 * and that msg->sol points to the beginning of the response.
1118 * If a complete line is found (which implies that at least one CR or LF is
1119 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1120 * returned indicating an incomplete line (which does not mean that parts have
1121 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1122 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1123 * upon next call.
1124 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001125 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1127 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001128 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001130const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001131 enum ht_state state, const char *ptr, const char *end,
1132 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001133{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001134 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001135
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001139 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1141
1142 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001143 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001144 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1145 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001146 state = HTTP_MSG_ERROR;
1147 break;
1148
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001152 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001153 goto http_msg_rpcode;
1154 }
1155 if (likely(HTTP_IS_SPHT(*ptr)))
1156 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1157 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001158 state = HTTP_MSG_ERROR;
1159 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001160
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001162 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001163 if (likely(!HTTP_IS_LWS(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1165
1166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001167 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1169 }
1170
1171 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001172 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 http_msg_rsp_reason:
1174 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001176 msg->sl.st.r_l = 0;
1177 goto http_msg_rpline_eol;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001182 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 goto http_msg_rpreason;
1184 }
1185 if (likely(HTTP_IS_SPHT(*ptr)))
1186 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1187 /* so it's a CR/LF, so there is no reason phrase */
1188 goto http_msg_rsp_reason;
1189
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 if (likely(!HTTP_IS_CRLF(*ptr)))
1193 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001194 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 http_msg_rpline_eol:
1196 /* We have seen the end of line. Note that we do not
1197 * necessarily have the \n yet, but at least we know that we
1198 * have EITHER \r OR \n, otherwise the response would not be
1199 * complete. We can then record the response length and return
1200 * to the caller which will be able to register it.
1201 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001202 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 return ptr;
1204
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001206#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001207 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1208 exit(1);
1209#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001210 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001211 }
1212
1213 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001214 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001215 if (ret_state)
1216 *ret_state = state;
1217 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001218 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001219 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001220}
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 * This function parses a request line between <ptr> and <end>, starting with
1224 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1225 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1226 * will give undefined results.
1227 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1228 * and that msg->sol points to the beginning of the request.
1229 * If a complete line is found (which implies that at least one CR or LF is
1230 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1231 * returned indicating an incomplete line (which does not mean that parts have
1232 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1233 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1234 * upon next call.
1235 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001236 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1238 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001239 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001241const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001242 enum ht_state state, const char *ptr, const char *end,
1243 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001245 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001249 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 if (likely(HTTP_IS_TOKEN(*ptr)))
1251 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001254 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 if (likely(HTTP_IS_CRLF(*ptr))) {
1259 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001260 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001262 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001264 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001266 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 msg->sl.rq.v_l = 0;
1268 goto http_msg_rqline_eol;
1269 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001270 state = HTTP_MSG_ERROR;
1271 break;
1272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001276 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 goto http_msg_rquri;
1278 }
1279 if (likely(HTTP_IS_SPHT(*ptr)))
1280 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1281 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1282 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001285 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001286 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001290 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1292 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001293
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001294 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001295 /* non-ASCII chars are forbidden unless option
1296 * accept-invalid-http-request is enabled in the frontend.
1297 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001298 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001299 if (msg->err_pos < -1)
1300 goto invalid_char;
1301 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1304 }
1305
1306 if (likely(HTTP_IS_CRLF(*ptr))) {
1307 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1308 goto http_msg_req09_uri_e;
1309 }
1310
1311 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001312 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001313 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001314 state = HTTP_MSG_ERROR;
1315 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001320 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 goto http_msg_rqver;
1322 }
1323 if (likely(HTTP_IS_SPHT(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1325 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1326 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001330 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332
1333 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001334 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001335 http_msg_rqline_eol:
1336 /* We have seen the end of line. Note that we do not
1337 * necessarily have the \n yet, but at least we know that we
1338 * have EITHER \r OR \n, otherwise the request would not be
1339 * complete. We can then record the request length and return
1340 * to the caller which will be able to register it.
1341 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001342 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001343 return ptr;
1344 }
1345
1346 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001347 state = HTTP_MSG_ERROR;
1348 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001351#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1353 exit(1);
1354#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001355 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001359 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 if (ret_state)
1361 *ret_state = state;
1362 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001363 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001367/*
1368 * Returns the data from Authorization header. Function may be called more
1369 * than once so data is stored in txn->auth_data. When no header is found
1370 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001371 * searching again for something we are unable to find anyway. However, if
1372 * the result if valid, the cache is not reused because we would risk to
1373 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001374 */
1375
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001376/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1377 * set according to global.tune.bufsize.
1378 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001379char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001380
1381int
1382get_http_auth(struct session *s)
1383{
1384
1385 struct http_txn *txn = &s->txn;
1386 struct chunk auth_method;
1387 struct hdr_ctx ctx;
1388 char *h, *p;
1389 int len;
1390
1391#ifdef DEBUG_AUTH
1392 printf("Auth for session %p: %d\n", s, txn->auth.method);
1393#endif
1394
1395 if (txn->auth.method == HTTP_AUTH_WRONG)
1396 return 0;
1397
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001398 txn->auth.method = HTTP_AUTH_WRONG;
1399
1400 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001401
1402 if (txn->flags & TX_USE_PX_CONN) {
1403 h = "Proxy-Authorization";
1404 len = strlen(h);
1405 } else {
1406 h = "Authorization";
1407 len = strlen(h);
1408 }
1409
Willy Tarreau9b28e032012-10-12 23:49:43 +02001410 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411 return 0;
1412
1413 h = ctx.line + ctx.val;
1414
1415 p = memchr(h, ' ', ctx.vlen);
1416 if (!p || p == h)
1417 return 0;
1418
1419 chunk_initlen(&auth_method, h, 0, p-h);
1420 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1421
1422 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1423
1424 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001425 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001426
1427 if (len < 0)
1428 return 0;
1429
1430
1431 get_http_auth_buff[len] = '\0';
1432
1433 p = strchr(get_http_auth_buff, ':');
1434
1435 if (!p)
1436 return 0;
1437
1438 txn->auth.user = get_http_auth_buff;
1439 *p = '\0';
1440 txn->auth.pass = p+1;
1441
1442 txn->auth.method = HTTP_AUTH_BASIC;
1443 return 1;
1444 }
1445
1446 return 0;
1447}
1448
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau8973c702007-01-21 23:58:29 +01001450/*
1451 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001452 * depending on the initial msg->msg_state. The caller is responsible for
1453 * ensuring that the message does not wrap. The function can be preempted
1454 * everywhere when data are missing and recalled at the exact same location
1455 * with no information loss. The message may even be realigned between two
1456 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001457 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001458 * fields. Note that msg->sol will be initialized after completing the first
1459 * state, so that none of the msg pointers has to be initialized prior to the
1460 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001462void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463{
Willy Tarreau3770f232013-12-07 00:01:53 +01001464 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001467
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001469 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 ptr = buf->p + msg->next;
1471 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (unlikely(ptr >= end))
1474 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 /*
1478 * First, states that are specific to the response only.
1479 * We check them first so that request and headers are
1480 * closer to each other (accessed more often).
1481 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001485 /* we have a start of message, but we have to check
1486 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001487 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001488 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001489 if (unlikely(ptr != buf->p)) {
1490 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001491 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001492 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001493 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 }
Willy Tarreau26927362012-05-18 23:22:52 +02001495 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001496 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 hdr_idx_init(idx);
1498 state = HTTP_MSG_RPVER;
1499 goto http_msg_rpver;
1500 }
1501
1502 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1503 goto http_msg_invalid;
1504
1505 if (unlikely(*ptr == '\n'))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1507 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1508 /* stop here */
1509
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001511 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001512 EXPECT_LF_HERE(ptr, http_msg_invalid);
1513 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1514 /* stop here */
1515
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001517 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 case HTTP_MSG_RPVER_SP:
1519 case HTTP_MSG_RPCODE:
1520 case HTTP_MSG_RPCODE_SP:
1521 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001522 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001523 state, ptr, end,
1524 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 if (unlikely(!ptr))
1526 return;
1527
1528 /* we have a full response and we know that we have either a CR
1529 * or an LF at <ptr>.
1530 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001531 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1532
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001533 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 if (likely(*ptr == '\r'))
1535 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1536 goto http_msg_rpline_end;
1537
Willy Tarreau8973c702007-01-21 23:58:29 +01001538 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001539 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001540 /* msg->sol must point to the first of CR or LF. */
1541 EXPECT_LF_HERE(ptr, http_msg_invalid);
1542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1543 /* stop here */
1544
1545 /*
1546 * Second, states that are specific to the request only
1547 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001549 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001551 /* we have a start of message, but we have to check
1552 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001553 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001554 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001555 if (likely(ptr != buf->p)) {
1556 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001557 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001558 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 }
Willy Tarreau26927362012-05-18 23:22:52 +02001561 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001562 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 state = HTTP_MSG_RQMETH;
1564 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1568 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 if (unlikely(*ptr == '\n'))
1571 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1572 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001573 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001579 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001582 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 case HTTP_MSG_RQMETH_SP:
1584 case HTTP_MSG_RQURI:
1585 case HTTP_MSG_RQURI_SP:
1586 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001587 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001588 state, ptr, end,
1589 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 if (unlikely(!ptr))
1591 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 /* we have a full request and we know that we have either a CR
1594 * or an LF at <ptr>.
1595 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001598 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 if (likely(*ptr == '\r'))
1600 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 /* check for HTTP/0.9 request : no version information available.
1606 * msg->sol must point to the first of CR or LF.
1607 */
1608 if (unlikely(msg->sl.rq.v_l == 0))
1609 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8973c702007-01-21 23:58:29 +01001615 /*
1616 * Common states below
1617 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001620 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 if (likely(!HTTP_IS_CRLF(*ptr))) {
1622 goto http_msg_hdr_name;
1623 }
1624
1625 if (likely(*ptr == '\r'))
1626 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1627 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001630 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 /* assumes msg->sol points to the first char */
1632 if (likely(HTTP_IS_TOKEN(*ptr)))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001634
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001635 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001638 if (likely(msg->err_pos < -1) || *ptr == '\n')
1639 goto http_msg_invalid;
1640
1641 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001642 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001643
1644 /* and we still accept this non-token character */
1645 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001648 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001649 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 if (likely(HTTP_IS_SPHT(*ptr)))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001654 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 if (likely(!HTTP_IS_CRLF(*ptr))) {
1657 goto http_msg_hdr_val;
1658 }
1659
1660 if (likely(*ptr == '\r'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1662 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001665 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 EXPECT_LF_HERE(ptr, http_msg_invalid);
1667 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 if (likely(HTTP_IS_SPHT(*ptr))) {
1672 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 for (; buf->p + msg->sov < ptr; msg->sov++)
1674 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 goto http_msg_hdr_l1_sp;
1676 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001677 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001678 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 goto http_msg_complete_header;
1680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001682 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001683 /* assumes msg->sol points to the first char, and msg->sov
1684 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 */
1686 if (likely(!HTTP_IS_CRLF(*ptr)))
1687 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001689 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 /* Note: we could also copy eol into ->eoh so that we have the
1691 * real header end in case it ends with lots of LWS, but is this
1692 * really needed ?
1693 */
1694 if (likely(*ptr == '\r'))
1695 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1696 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001699 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 EXPECT_LF_HERE(ptr, http_msg_invalid);
1701 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001704 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1706 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 for (; buf->p + msg->eol < ptr; msg->eol++)
1708 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 goto http_msg_hdr_val;
1710 }
1711 http_msg_complete_header:
1712 /*
1713 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001714 * Assumes msg->sol points to the first char, msg->sov points
1715 * to the first character of the value and msg->eol to the
1716 * first CR or LF so we know how the line ends. We insert last
1717 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001719 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 idx, idx->tail) < 0))
1721 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001722
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001723 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(!HTTP_IS_CRLF(*ptr))) {
1725 goto http_msg_hdr_name;
1726 }
1727
1728 if (likely(*ptr == '\r'))
1729 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1730 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001731
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001733 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001734 /* Assumes msg->sol points to the first of either CR or LF.
1735 * Sets ->sov and ->next to the total header length, ->eoh to
1736 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1737 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 EXPECT_LF_HERE(ptr, http_msg_invalid);
1739 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001740 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001741 msg->eoh = msg->sol;
1742 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001743 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001744 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001746
1747 case HTTP_MSG_ERROR:
1748 /* this may only happen if we call http_msg_analyser() twice with an error */
1749 break;
1750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001752#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1754 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001755#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001756 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 }
1758 http_msg_ood:
1759 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001760 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001761 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 http_msg_invalid:
1765 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001766 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 return;
1769}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001770
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001771/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1772 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1773 * nothing is done and 1 is returned.
1774 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001775static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776{
1777 int delta;
1778 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001779 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001780
1781 if (msg->sl.rq.v_l != 0)
1782 return 1;
1783
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001784 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1785 if (txn->meth != HTTP_METH_GET)
1786 return 0;
1787
Willy Tarreau9b28e032012-10-12 23:49:43 +02001788 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 delta = 0;
1790
1791 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001792 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1793 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001794 }
1795 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001796 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001797 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001798 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001799 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001800 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001801 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001802 NULL, NULL);
1803 if (unlikely(!cur_end))
1804 return 0;
1805
1806 /* we have a full HTTP/1.0 request now and we know that
1807 * we have either a CR or an LF at <ptr>.
1808 */
1809 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1810 return 1;
1811}
1812
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001813/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001814 * and "keep-alive" values. If we already know that some headers may safely
1815 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001816 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1817 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001818 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1820 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1821 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001822 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001823 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001824void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001825{
Willy Tarreau5b154472009-12-21 20:11:07 +01001826 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001827 const char *hdr_val = "Connection";
1828 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001829
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001831 return;
1832
Willy Tarreau88d349d2010-01-25 12:15:43 +01001833 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1834 hdr_val = "Proxy-Connection";
1835 hdr_len = 16;
1836 }
1837
Willy Tarreau5b154472009-12-21 20:11:07 +01001838 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001839 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001840 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1842 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001843 if (to_del & 2)
1844 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001845 else
1846 txn->flags |= TX_CON_KAL_SET;
1847 }
1848 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1849 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001850 if (to_del & 1)
1851 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001852 else
1853 txn->flags |= TX_CON_CLO_SET;
1854 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001855 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1856 txn->flags |= TX_HDR_CONN_UPG;
1857 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001858 }
1859
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001860 txn->flags |= TX_HDR_CONN_PRS;
1861 return;
1862}
Willy Tarreau5b154472009-12-21 20:11:07 +01001863
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001864/* Apply desired changes on the Connection: header. Values may be removed and/or
1865 * added depending on the <wanted> flags, which are exclusively composed of
1866 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1867 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1868 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001869void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870{
1871 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001872 const char *hdr_val = "Connection";
1873 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874
1875 ctx.idx = 0;
1876
Willy Tarreau88d349d2010-01-25 12:15:43 +01001877
1878 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1879 hdr_val = "Proxy-Connection";
1880 hdr_len = 16;
1881 }
1882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001884 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001885 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1886 if (wanted & TX_CON_KAL_SET)
1887 txn->flags |= TX_CON_KAL_SET;
1888 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001889 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001890 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001891 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1892 if (wanted & TX_CON_CLO_SET)
1893 txn->flags |= TX_CON_CLO_SET;
1894 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001895 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001896 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001897 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898
1899 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1900 return;
1901
1902 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1903 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: close";
1905 hdr_len = 17;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: close";
1908 hdr_len = 23;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912
1913 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1914 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001915 hdr_val = "Connection: keep-alive";
1916 hdr_len = 22;
1917 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1918 hdr_val = "Proxy-Connection: keep-alive";
1919 hdr_len = 28;
1920 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001921 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001922 }
1923 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001924}
1925
Willy Tarreaua458b672012-03-05 11:17:50 +01001926/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001928 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001929 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001931 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001932static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001933{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001934 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001935 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001936 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001937 const char *end = buf->data + buf->size;
1938 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 unsigned int chunk = 0;
1940
1941 /* The chunk size is in the following form, though we are only
1942 * interested in the size and CRLF :
1943 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1944 */
1945 while (1) {
1946 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001947 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001948 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001950 if (c < 0) /* not a hex digit anymore */
1951 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001952 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001953 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001954 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001955 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001956 chunk = (chunk << 4) + c;
1957 }
1958
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001960 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001961 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962
1963 while (http_is_spht[(unsigned char)*ptr]) {
1964 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001965 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001966 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001967 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001968 }
1969
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 /* Up to there, we know that at least one byte is present at *ptr. Check
1971 * for the end of chunk size.
1972 */
1973 while (1) {
1974 if (likely(HTTP_IS_CRLF(*ptr))) {
1975 /* we now have a CR or an LF at ptr */
1976 if (likely(*ptr == '\r')) {
1977 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001978 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001979 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001980 return 0;
1981 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001982
Willy Tarreaud98cf932009-12-27 22:54:55 +01001983 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001984 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001986 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987 /* done */
1988 break;
1989 }
1990 else if (*ptr == ';') {
1991 /* chunk extension, ends at next CRLF */
1992 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001993 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001994 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001995 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001996
1997 while (!HTTP_IS_CRLF(*ptr)) {
1998 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001999 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002000 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 return 0;
2002 }
2003 /* we have a CRLF now, loop above */
2004 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002007 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002008 }
2009
Willy Tarreaud98cf932009-12-27 22:54:55 +01002010 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01002011 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002013 */
Willy Tarreau0161d622013-04-02 01:26:55 +02002014 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002015 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01002016 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002017 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002018 msg->chunk_len = chunk;
2019 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002021 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002022 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002023 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002024 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002025}
2026
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002027/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002028 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029 * the trailers is found, it is automatically scheduled to be forwarded,
2030 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2031 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01002032 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01002033 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01002034 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01002035 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2036 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02002037 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01002038 * matches the length of trailers already parsed and not forwarded. It is also
2039 * important to note that this function is designed to be able to parse wrapped
2040 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002042static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002043{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002044 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002045
Willy Tarreaua458b672012-03-05 11:17:50 +01002046 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002048 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002049 const char *ptr = b_ptr(buf, msg->next);
2050 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002051 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002052
2053 /* scan current line and stop at LF or CRLF */
2054 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002055 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return 0;
2057
2058 if (*ptr == '\n') {
2059 if (!p1)
2060 p1 = ptr;
2061 p2 = ptr;
2062 break;
2063 }
2064
2065 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002066 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002067 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002069 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 p1 = ptr;
2071 }
2072
2073 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 if (ptr >= buf->data + buf->size)
2075 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076 }
2077
2078 /* after LF; point to beginning of next line */
2079 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002080 if (p2 >= buf->data + buf->size)
2081 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002083 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002084 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002085 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002086
2087 /* schedule this line for forwarding */
2088 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002089 if (msg->sov >= buf->size)
2090 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002091
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002093 /* LF/CRLF at beginning of line => end of trailers at p2.
2094 * Everything was scheduled for forwarding, there's nothing
2095 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002096 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002097 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002098 msg->msg_state = HTTP_MSG_DONE;
2099 return 1;
2100 }
2101 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002102 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 }
2104}
2105
Willy Tarreau54d23df2012-10-25 19:04:45 +02002106/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002108 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002109 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2111 * not enough data are available, the function does not change anything and
2112 * returns zero. If a parse error is encountered, the function returns < 0 and
2113 * does not change anything. Note: this function is designed to parse wrapped
2114 * CRLF at the end of the buffer.
2115 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002116static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002118 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002119 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120 int bytes;
2121
2122 /* NB: we'll check data availabilty at the end. It's not a
2123 * problem because whatever we match first will be checked
2124 * against the correct length.
2125 */
2126 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002127 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128 if (*ptr == '\r') {
2129 bytes++;
2130 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002131 if (ptr >= buf->data + buf->size)
2132 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 }
2134
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002135 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 return 0;
2137
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002138 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002140 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002141 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002142
2143 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002144 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002145 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002146 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2147 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002148 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002149 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2150 return 1;
2151}
Willy Tarreau5b154472009-12-21 20:11:07 +01002152
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002153/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2154 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2155 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2156 * Unparsable qvalues return 1000 as "q=1.000".
2157 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002158int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002159{
2160 int q = 1000;
2161
2162 if (!isdigit(*qvalue))
2163 goto out;
2164 q = (*qvalue++ - '0') * 1000;
2165
2166 if (*qvalue++ != '.')
2167 goto out;
2168
2169 if (!isdigit(*qvalue))
2170 goto out;
2171 q += (*qvalue++ - '0') * 100;
2172
2173 if (!isdigit(*qvalue))
2174 goto out;
2175 q += (*qvalue++ - '0') * 10;
2176
2177 if (!isdigit(*qvalue))
2178 goto out;
2179 q += (*qvalue++ - '0') * 1;
2180 out:
2181 if (q > 1000)
2182 q = 1000;
Thierry FOURNIERad903512014-04-11 17:51:01 +02002183 if (*end)
2184 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002185 return q;
2186}
William Lallemand82fe75c2012-10-23 10:25:10 +02002187
2188/*
2189 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002190 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002191int select_compression_request_header(struct session *s, struct buffer *req)
2192{
2193 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002194 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002195 struct hdr_ctx ctx;
2196 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002197 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002198
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002199 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2200 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002201 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2202 */
2203 ctx.idx = 0;
2204 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2205 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002206 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2207 (ctx.vlen < 31 ||
2208 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2209 ctx.line[ctx.val + 30] < '6' ||
2210 (ctx.line[ctx.val + 30] == '6' &&
2211 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2212 s->comp_algo = NULL;
2213 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002214 }
2215
William Lallemand82fe75c2012-10-23 10:25:10 +02002216 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002217 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 +01002218 int best_q = 0;
2219
William Lallemand82fe75c2012-10-23 10:25:10 +02002220 ctx.idx = 0;
2221 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002222 const char *qval;
2223 int q;
2224 int toklen;
2225
2226 /* try to isolate the token from the optional q-value */
2227 toklen = 0;
2228 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2229 toklen++;
2230
2231 qval = ctx.line + ctx.val + toklen;
2232 while (1) {
2233 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2234 qval++;
2235
2236 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2237 qval = NULL;
2238 break;
2239 }
2240 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002241
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2243 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002244
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002245 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2246 qval = NULL;
2247 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002248 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002249 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2250 break;
2251
2252 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2253 qval++;
2254 }
2255
2256 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002257 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002258
2259 if (q <= best_q)
2260 continue;
2261
2262 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2263 if (*(ctx.line + ctx.val) == '*' ||
2264 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2265 s->comp_algo = comp_algo;
2266 best_q = q;
2267 break;
2268 }
2269 }
2270 }
2271 }
2272
2273 /* remove all occurrences of the header when "compression offload" is set */
2274 if (s->comp_algo) {
2275 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2276 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2277 ctx.idx = 0;
2278 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2279 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 }
2281 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002282 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002283 }
2284
2285 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002286 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2287 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002288 if (comp_algo->add_data == identity_add_data) {
2289 s->comp_algo = comp_algo;
2290 return 1;
2291 }
2292 }
2293 }
2294
2295 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002296 return 0;
2297}
2298
2299/*
2300 * Selects a comression algorithm depending of the server response.
2301 */
2302int select_compression_response_header(struct session *s, struct buffer *res)
2303{
2304 struct http_txn *txn = &s->txn;
2305 struct http_msg *msg = &txn->rsp;
2306 struct hdr_ctx ctx;
2307 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002308
2309 /* no common compression algorithm was found in request header */
2310 if (s->comp_algo == NULL)
2311 goto fail;
2312
2313 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002314 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002315 goto fail;
2316
William Lallemandd3002612012-11-26 14:34:47 +01002317 /* 200 only */
2318 if (txn->status != 200)
2319 goto fail;
2320
William Lallemand82fe75c2012-10-23 10:25:10 +02002321 /* Content-Length is null */
2322 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2323 goto fail;
2324
Willy Tarreau667c2a32013-04-09 08:13:58 +02002325 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2326 if (msg->flags & HTTP_MSGF_TE_CHNK)
2327 goto fail;
2328
William Lallemand82fe75c2012-10-23 10:25:10 +02002329 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002330 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2332 goto fail;
2333
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002334 /* no compression when Cache-Control: no-transform is present in the message */
2335 ctx.idx = 0;
2336 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2337 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2338 goto fail;
2339 }
2340
William Lallemand82fe75c2012-10-23 10:25:10 +02002341 comp_type = NULL;
2342
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002343 /* we don't want to compress multipart content-types, nor content-types that are
2344 * not listed in the "compression type" directive if any. If no content-type was
2345 * found but configuration requires one, we don't compress either. Backend has
2346 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002347 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002348 ctx.idx = 0;
2349 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2350 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2351 goto fail;
2352
2353 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2354 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002355 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002356 if (ctx.vlen >= comp_type->name_len &&
2357 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002358 /* this Content-Type should be compressed */
2359 break;
2360 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002361 /* this Content-Type should not be compressed */
2362 if (comp_type == NULL)
2363 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002364 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002365 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002366 else { /* no content-type header */
2367 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2368 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002369 }
2370
William Lallemandd85f9172012-11-09 17:05:39 +01002371 /* limit compression rate */
2372 if (global.comp_rate_lim > 0)
2373 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2374 goto fail;
2375
William Lallemand072a2bf2012-11-20 17:01:01 +01002376 /* limit cpu usage */
2377 if (idle_pct < compress_min_idle)
2378 goto fail;
2379
William Lallemand4c49fae2012-11-07 15:00:23 +01002380 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002381 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002382 goto fail;
2383
William Lallemandec3e3892012-11-12 17:02:18 +01002384 s->flags |= SN_COMP_READY;
2385
William Lallemand82fe75c2012-10-23 10:25:10 +02002386 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002387 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2389 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2390
2391 /* add Transfer-Encoding header */
2392 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2393 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2394
2395 /*
2396 * Add Content-Encoding header when it's not identity encoding.
2397 * RFC 2616 : Identity encoding: This content-coding is used only in the
2398 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2399 * header.
2400 */
2401 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002402 trash.len = 18;
2403 memcpy(trash.str, "Content-Encoding: ", trash.len);
2404 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2405 trash.len += s->comp_algo->name_len;
2406 trash.str[trash.len] = '\0';
2407 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002409 return 1;
2410
2411fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002412 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002413 return 0;
2414}
2415
2416
Willy Tarreaud787e662009-07-07 10:14:51 +02002417/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2418 * processing can continue on next analysers, or zero if it either needs more
2419 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2420 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2421 * when it has nothing left to do, and may remove any analyser when it wants to
2422 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002423 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002424int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002425{
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 /*
2427 * We will parse the partial (or complete) lines.
2428 * We will check the request syntax, and also join multi-line
2429 * headers. An index of all the lines will be elaborated while
2430 * parsing.
2431 *
2432 * For the parsing, we use a 28 states FSM.
2433 *
2434 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002435 * req->buf->p = beginning of request
2436 * req->buf->p + msg->eoh = end of processed headers / start of current one
2437 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002438 * msg->eol = end of current header or line (LF or CRLF)
2439 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002440 *
2441 * At end of parsing, we may perform a capture of the error (if any), and
2442 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2443 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2444 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002446
Willy Tarreau59234e92008-11-30 23:51:27 +01002447 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002448 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 struct http_txn *txn = &s->txn;
2450 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002451 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002452
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002453 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 +01002454 now_ms, __FUNCTION__,
2455 s,
2456 req,
2457 req->rex, req->wex,
2458 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002459 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002460 req->analysers);
2461
Willy Tarreau52a0c602009-08-16 22:45:38 +02002462 /* we're speaking HTTP here, so let's speak HTTP to the client */
2463 s->srv_error = http_return_srv_error;
2464
Willy Tarreau83e3af02009-12-28 17:39:57 +01002465 /* There's a protected area at the end of the buffer for rewriting
2466 * purposes. We don't want to start to parse the request if the
2467 * protected area is affected, because we may have to move processed
2468 * data later, which is much more complicated.
2469 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002470 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002471 if (txn->flags & TX_NOT_FIRST) {
2472 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002473 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002474 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002475 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002476 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002477 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002478 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002479 return 0;
2480 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002481 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2482 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2483 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002484 }
2485
Willy Tarreau065e8332010-01-08 00:30:20 +01002486 /* Note that we have the same problem with the response ; we
2487 * may want to send a redirect, error or anything which requires
2488 * some spare space. So we'll ensure that we have at least
2489 * maxrewrite bytes available in the response buffer before
2490 * processing that one. This will only affect pipelined
2491 * keep-alive requests.
2492 */
2493 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002494 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002495 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2496 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2497 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002498 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002499 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002500 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002501 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002502 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002503 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002504 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002505 return 0;
2506 }
2507 }
2508
Willy Tarreau9b28e032012-10-12 23:49:43 +02002509 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002510 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002511 }
2512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 /* 1: we might have to print this header in debug mode */
2514 if (unlikely((global.mode & MODE_DEBUG) &&
2515 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002516 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002518
Willy Tarreau9b28e032012-10-12 23:49:43 +02002519 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002520 /* this is a bit complex : in case of error on the request line,
2521 * we know that rq.l is still zero, so we display only the part
2522 * up to the end of the line (truncated by debug_hdr).
2523 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002524 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 sol += hdr_idx_first_pos(&txn->hdr_idx);
2528 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002529
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 while (cur_idx) {
2531 eol = sol + txn->hdr_idx.v[cur_idx].len;
2532 debug_hdr("clihdr", s, sol, eol);
2533 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2534 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002535 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 }
2537
Willy Tarreau58f10d72006-12-04 02:26:12 +01002538
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 /*
2540 * Now we quickly check if we have found a full valid request.
2541 * If not so, we check the FD and buffer states before leaving.
2542 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002543 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002544 * requests are checked first. When waiting for a second request
2545 * on a keep-alive session, if we encounter and error, close, t/o,
2546 * we note the error in the session flags but don't set any state.
2547 * Since the error will be noted there, it will not be counted by
2548 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002549 * Last, we may increase some tracked counters' http request errors on
2550 * the cases that are deliberately the client's fault. For instance,
2551 * a timeout or connection reset is not counted as an error. However
2552 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002553 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002554
Willy Tarreau655dce92009-11-08 13:10:58 +01002555 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002556 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002558 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002559 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002560 session_inc_http_req_ctr(s);
2561 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002562 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002565
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 /* 1: Since we are in header mode, if there's no space
2567 * left for headers, we won't be able to free more
2568 * later, so the session will never terminate. We
2569 * must terminate it now.
2570 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002571 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /* FIXME: check if URI is set and return Status
2573 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002574 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002575 session_inc_http_req_ctr(s);
2576 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002577 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002578 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002579 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 goto return_bad_req;
2581 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002582
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002584 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002585 if (!(s->flags & SN_ERR_MASK))
2586 s->flags |= SN_ERR_CLICL;
2587
Willy Tarreaufcffa692010-01-10 14:21:19 +01002588 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002589 goto failed_keep_alive;
2590
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002592 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002593 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002594 session_inc_http_err_ctr(s);
2595 }
2596
Willy Tarreaudc979f22012-12-04 10:39:01 +01002597 txn->status = 400;
2598 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 msg->msg_state = HTTP_MSG_ERROR;
2600 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002601
Willy Tarreauda7ff642010-06-23 11:44:09 +02002602 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002603 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002604 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002605 if (s->listener->counters)
2606 s->listener->counters->failed_req++;
2607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 if (!(s->flags & SN_FINST_MASK))
2609 s->flags |= SN_FINST_R;
2610 return 0;
2611 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002612
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002614 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002615 if (!(s->flags & SN_ERR_MASK))
2616 s->flags |= SN_ERR_CLITO;
2617
Willy Tarreaufcffa692010-01-10 14:21:19 +01002618 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002619 goto failed_keep_alive;
2620
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002622 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002623 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002624 session_inc_http_err_ctr(s);
2625 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002627 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002628 msg->msg_state = HTTP_MSG_ERROR;
2629 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002630
Willy Tarreauda7ff642010-06-23 11:44:09 +02002631 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002632 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002633 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002634 if (s->listener->counters)
2635 s->listener->counters->failed_req++;
2636
Willy Tarreau59234e92008-11-30 23:51:27 +01002637 if (!(s->flags & SN_FINST_MASK))
2638 s->flags |= SN_FINST_R;
2639 return 0;
2640 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002641
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002643 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002644 if (!(s->flags & SN_ERR_MASK))
2645 s->flags |= SN_ERR_CLICL;
2646
Willy Tarreaufcffa692010-01-10 14:21:19 +01002647 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002648 goto failed_keep_alive;
2649
Willy Tarreau4076a152009-04-02 15:18:36 +02002650 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002651 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002653 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 msg->msg_state = HTTP_MSG_ERROR;
2655 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002656
Willy Tarreauda7ff642010-06-23 11:44:09 +02002657 session_inc_http_err_ctr(s);
2658 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002659 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002660 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002661 if (s->listener->counters)
2662 s->listener->counters->failed_req++;
2663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 if (!(s->flags & SN_FINST_MASK))
2665 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002666 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002667 }
2668
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002669 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002670 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2671 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002672#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002673 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 +01002674 /* We need more data, we have to re-enable quick-ack in case we
2675 * previously disabled it, otherwise we might cause the client
2676 * to delay next data.
2677 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002678 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002679 }
2680#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002681
Willy Tarreaufcffa692010-01-10 14:21:19 +01002682 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2683 /* If the client starts to talk, let's fall back to
2684 * request timeout processing.
2685 */
2686 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002687 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002688 }
2689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002691 if (!tick_isset(req->analyse_exp)) {
2692 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2693 (txn->flags & TX_WAIT_NEXT_RQ) &&
2694 tick_isset(s->be->timeout.httpka))
2695 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2696 else
2697 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2698 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002699
Willy Tarreau59234e92008-11-30 23:51:27 +01002700 /* we're not ready yet */
2701 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002702
2703 failed_keep_alive:
2704 /* Here we process low-level errors for keep-alive requests. In
2705 * short, if the request is not the first one and it experiences
2706 * a timeout, read error or shutdown, we just silently close so
2707 * that the client can try again.
2708 */
2709 txn->status = 0;
2710 msg->msg_state = HTTP_MSG_RQBEFORE;
2711 req->analysers = 0;
2712 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002713 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002714 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002715 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002716 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002717 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002718
Willy Tarreaud787e662009-07-07 10:14:51 +02002719 /* OK now we have a complete HTTP request with indexed headers. Let's
2720 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002721 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002722 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002723 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002724 * byte after the last LF. msg->sov points to the first byte of data.
2725 * msg->eol cannot be trusted because it may have been left uninitialized
2726 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002727 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002728
Willy Tarreauda7ff642010-06-23 11:44:09 +02002729 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002730 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2731
Willy Tarreaub16a5742010-01-10 14:46:16 +01002732 if (txn->flags & TX_WAIT_NEXT_RQ) {
2733 /* kill the pending keep-alive timeout */
2734 txn->flags &= ~TX_WAIT_NEXT_RQ;
2735 req->analyse_exp = TICK_ETERNITY;
2736 }
2737
2738
Willy Tarreaud787e662009-07-07 10:14:51 +02002739 /* Maybe we found in invalid header name while we were configured not
2740 * to block on that, so we have to capture it now.
2741 */
2742 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002743 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002744
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 /*
2746 * 1: identify the method
2747 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002748 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002749
2750 /* we can make use of server redirect on GET and HEAD */
2751 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2752 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002753
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 /*
2755 * 2: check if the URI matches the monitor_uri.
2756 * We have to do this for every request which gets in, because
2757 * the monitor-uri is defined by the frontend.
2758 */
2759 if (unlikely((s->fe->monitor_uri_len != 0) &&
2760 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002761 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 s->fe->monitor_uri,
2763 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002764 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002766 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002768
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002770 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002771
Willy Tarreau59234e92008-11-30 23:51:27 +01002772 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002773 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002774 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002775
Willy Tarreau59234e92008-11-30 23:51:27 +01002776 ret = acl_pass(ret);
2777 if (cond->pol == ACL_COND_UNLESS)
2778 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002779
Willy Tarreau59234e92008-11-30 23:51:27 +01002780 if (ret) {
2781 /* we fail this request, let's return 503 service unavail */
2782 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002783 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002784 if (!(s->flags & SN_ERR_MASK))
2785 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002787 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002788 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002789
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 /* nothing to fail, let's reply normaly */
2791 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002792 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002793 if (!(s->flags & SN_ERR_MASK))
2794 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002795 goto return_prx_cond;
2796 }
2797
2798 /*
2799 * 3: Maybe we have to copy the original REQURI for the logs ?
2800 * Note: we cannot log anymore if the request has been
2801 * classified as invalid.
2802 */
2803 if (unlikely(s->logs.logwait & LW_REQ)) {
2804 /* we have a complete HTTP request that we must log */
2805 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2806 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002807
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 if (urilen >= REQURI_LEN)
2809 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002810 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002811 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002812
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002813 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 s->do_log(s);
2815 } else {
2816 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002817 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 }
Willy Tarreau06619262006-12-17 08:37:22 +01002819
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002821 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002822 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002823
Willy Tarreau5b154472009-12-21 20:11:07 +01002824 /* ... and check if the request is HTTP/1.1 or above */
2825 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002826 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2827 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2828 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002829 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002830
2831 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002832 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 +01002833
Willy Tarreau88d349d2010-01-25 12:15:43 +01002834 /* if the frontend has "option http-use-proxy-header", we'll check if
2835 * we have what looks like a proxied connection instead of a connection,
2836 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2837 * Note that this is *not* RFC-compliant, however browsers and proxies
2838 * happen to do that despite being non-standard :-(
2839 * We consider that a request not beginning with either '/' or '*' is
2840 * a proxied connection, which covers both "scheme://location" and
2841 * CONNECT ip:port.
2842 */
2843 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002844 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002845 txn->flags |= TX_USE_PX_CONN;
2846
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002847 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002848 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002849
Willy Tarreau59234e92008-11-30 23:51:27 +01002850 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002851 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002852 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002853 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002854
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002855 /* 6: determine the transfer-length.
2856 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2857 * the presence of a message-body in a REQUEST and its transfer length
2858 * must be determined that way (in order of precedence) :
2859 * 1. The presence of a message-body in a request is signaled by the
2860 * inclusion of a Content-Length or Transfer-Encoding header field
2861 * in the request's header fields. When a request message contains
2862 * both a message-body of non-zero length and a method that does
2863 * not define any semantics for that request message-body, then an
2864 * origin server SHOULD either ignore the message-body or respond
2865 * with an appropriate error message (e.g., 413). A proxy or
2866 * gateway, when presented the same request, SHOULD either forward
2867 * the request inbound with the message- body or ignore the
2868 * message-body when determining a response.
2869 *
2870 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2871 * and the "chunked" transfer-coding (Section 6.2) is used, the
2872 * transfer-length is defined by the use of this transfer-coding.
2873 * If a Transfer-Encoding header field is present and the "chunked"
2874 * transfer-coding is not present, the transfer-length is defined
2875 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002876 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002877 * 3. If a Content-Length header field is present, its decimal value in
2878 * OCTETs represents both the entity-length and the transfer-length.
2879 * If a message is received with both a Transfer-Encoding header
2880 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002881 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002882 * 4. By the server closing the connection. (Closing the connection
2883 * cannot be used to indicate the end of a request body, since that
2884 * would leave no possibility for the server to send back a response.)
2885 *
2886 * Whenever a transfer-coding is applied to a message-body, the set of
2887 * transfer-codings MUST include "chunked", unless the message indicates
2888 * it is terminated by closing the connection. When the "chunked"
2889 * transfer-coding is used, it MUST be the last transfer-coding applied
2890 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002891 */
2892
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002893 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002894 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002895 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002896 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002897 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002898 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002899 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2900 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002901 /* bad transfer-encoding (chunked followed by something else) */
2902 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002903 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002904 break;
2905 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002906 }
2907
Willy Tarreau32b47f42009-10-18 20:55:02 +02002908 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002909 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002910 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002911 signed long long cl;
2912
Willy Tarreauad14f752011-09-02 20:33:27 +02002913 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002914 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002915 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002916 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002917
Willy Tarreauad14f752011-09-02 20:33:27 +02002918 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002919 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002920 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002921 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002922
Willy Tarreauad14f752011-09-02 20:33:27 +02002923 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002924 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002925 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002926 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002927
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002928 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002929 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002930 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002931 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002932
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002933 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002934 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002935 }
2936
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002937 /* bodyless requests have a known length */
2938 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002939 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002940
Willy Tarreaud787e662009-07-07 10:14:51 +02002941 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002942 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002943 req->analyse_exp = TICK_ETERNITY;
2944 return 1;
2945
2946 return_bad_req:
2947 /* We centralize bad requests processing here */
2948 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2949 /* we detected a parsing error. We want to archive this request
2950 * in the dedicated proxy area for later troubleshooting.
2951 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002952 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002953 }
2954
2955 txn->req.msg_state = HTTP_MSG_ERROR;
2956 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002957 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002958
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002959 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002960 if (s->listener->counters)
2961 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002962
2963 return_prx_cond:
2964 if (!(s->flags & SN_ERR_MASK))
2965 s->flags |= SN_ERR_PRXCOND;
2966 if (!(s->flags & SN_FINST_MASK))
2967 s->flags |= SN_FINST_R;
2968
2969 req->analysers = 0;
2970 req->analyse_exp = TICK_ETERNITY;
2971 return 0;
2972}
2973
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002974
Willy Tarreau347a35d2013-11-22 17:51:09 +01002975/* This function prepares an applet to handle the stats. It can deal with the
2976 * "100-continue" expectation, check that admin rules are met for POST requests,
2977 * and program a response message if something was unexpected. It cannot fail
2978 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002979 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002980 * s->target which is supposed to already point to the stats applet. The caller
2981 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002982 */
2983int http_handle_stats(struct session *s, struct channel *req)
2984{
2985 struct stats_admin_rule *stats_admin_rule;
2986 struct stream_interface *si = s->rep->prod;
2987 struct http_txn *txn = &s->txn;
2988 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002989 struct uri_auth *uri_auth = s->be->uri_auth;
2990 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002991 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002992
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002993 appctx = si_appctx(si);
2994 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2995 appctx->st1 = appctx->st2 = 0;
2996 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2997 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002998
2999 uri = msg->chn->buf->p + msg->sl.rq.u;
3000 lookup = uri + uri_auth->uri_len;
3001
3002 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3003 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003004 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003005 break;
3006 }
3007 }
3008
3009 if (uri_auth->refresh) {
3010 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3011 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003012 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003013 break;
3014 }
3015 }
3016 }
3017
3018 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3019 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003020 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003021 break;
3022 }
3023 }
3024
3025 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3026 if (memcmp(h, ";st=", 4) == 0) {
3027 int i;
3028 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003029 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003030 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3031 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003032 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003033 break;
3034 }
3035 }
3036 break;
3037 }
3038 }
3039
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003040 appctx->ctx.stats.scope_str = 0;
3041 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003042 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3043 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3044 int itx = 0;
3045 const char *h2;
3046 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3047 const char *err;
3048
3049 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3050 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003051 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003052 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3053 itx++;
3054 h++;
3055 }
3056
3057 if (itx > STAT_SCOPE_TXT_MAXLEN)
3058 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003059 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003060
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003061 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003062 memcpy(scope_txt, h2, itx);
3063 scope_txt[itx] = '\0';
3064 err = invalid_char(scope_txt);
3065 if (err) {
3066 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003067 appctx->ctx.stats.scope_str = 0;
3068 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003069 }
3070 break;
3071 }
3072 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003073
3074 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003075 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003076 int ret = 1;
3077
3078 if (stats_admin_rule->cond) {
3079 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3080 ret = acl_pass(ret);
3081 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3082 ret = !ret;
3083 }
3084
3085 if (ret) {
3086 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003087 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003088 break;
3089 }
3090 }
3091
3092 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003093 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003094 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003095 if (msg->msg_state < HTTP_MSG_100_SENT) {
3096 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3097 * send an HTTP/1.1 100 Continue intermediate response.
3098 */
3099 if (msg->flags & HTTP_MSGF_VER_11) {
3100 struct hdr_ctx ctx;
3101 ctx.idx = 0;
3102 /* Expect is allowed in 1.1, look for it */
3103 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3104 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3105 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3106 }
3107 }
3108 msg->msg_state = HTTP_MSG_100_SENT;
3109 s->logs.tv_request = now; /* update the request timer to reflect full request */
3110 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003111 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003112 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003113 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003114 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3115 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003116 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003117 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003118 else {
3119 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003120 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003121 }
3122
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003123 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003124 return 1;
3125}
3126
Lukas Tribus67db8df2013-06-23 17:37:13 +02003127/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3128 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3129 */
3130static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3131{
3132#ifdef IP_TOS
3133 if (from.ss_family == AF_INET)
3134 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3135#endif
3136#ifdef IPV6_TCLASS
3137 if (from.ss_family == AF_INET6) {
3138 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3139 /* v4-mapped addresses need IP_TOS */
3140 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3141 else
3142 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3143 }
3144#endif
3145}
3146
Willy Tarreau20b0de52012-12-24 15:45:22 +01003147/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003148 * transaction <txn>. Returns the first rule that prevents further processing
3149 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3150 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3151 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003152 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003153static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003154http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003155{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003156 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003157 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003158 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003159
Willy Tarreauff011f22011-01-06 17:51:27 +01003160 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003161 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003162 continue;
3163
Willy Tarreau96257ec2012-12-27 10:46:37 +01003164 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003165 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003166 int ret;
3167
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003168 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003169 ret = acl_pass(ret);
3170
Willy Tarreauff011f22011-01-06 17:51:27 +01003171 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003172 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003173
3174 if (!ret) /* condition not matched */
3175 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003176 }
3177
Willy Tarreau20b0de52012-12-24 15:45:22 +01003178
Willy Tarreau96257ec2012-12-27 10:46:37 +01003179 switch (rule->action) {
3180 case HTTP_REQ_ACT_ALLOW:
3181 return NULL; /* "allow" rules are OK */
3182
3183 case HTTP_REQ_ACT_DENY:
3184 txn->flags |= TX_CLDENY;
3185 return rule;
3186
Willy Tarreauccbcc372012-12-27 12:37:57 +01003187 case HTTP_REQ_ACT_TARPIT:
3188 txn->flags |= TX_CLTARPIT;
3189 return rule;
3190
Willy Tarreau96257ec2012-12-27 10:46:37 +01003191 case HTTP_REQ_ACT_AUTH:
3192 return rule;
3193
Willy Tarreau81499eb2012-12-27 12:19:02 +01003194 case HTTP_REQ_ACT_REDIR:
3195 return rule;
3196
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003197 case HTTP_REQ_ACT_SET_NICE:
3198 s->task->nice = rule->arg.nice;
3199 break;
3200
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003201 case HTTP_REQ_ACT_SET_TOS:
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 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003204 break;
3205
Willy Tarreau51347ed2013-06-11 19:34:13 +02003206 case HTTP_REQ_ACT_SET_MARK:
3207#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003208 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003209 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003210#endif
3211 break;
3212
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003213 case HTTP_REQ_ACT_SET_LOGL:
3214 s->logs.level = rule->arg.loglevel;
3215 break;
3216
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003217 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003218 case HTTP_REQ_ACT_SET_HDR:
3219 ctx.idx = 0;
3220 /* remove all occurrences of the header */
3221 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3222 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3223 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003224 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003225 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3226 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003227 /* now fall through to header addition */
3228
3229 case HTTP_REQ_ACT_ADD_HDR:
3230 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3231 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3232 trash.len = rule->arg.hdr_add.name_len;
3233 trash.str[trash.len++] = ':';
3234 trash.str[trash.len++] = ' ';
3235 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3236 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3237 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003238 }
3239 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003240
3241 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003242 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003243}
3244
Willy Tarreau71241ab2012-12-27 11:30:54 +01003245
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003246/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3247 * transaction <txn>. Returns the first rule that prevents further processing
3248 * of the response (deny, ...) or NULL if it executed all rules or stopped
3249 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3250 * rule.
3251 */
3252static struct http_res_rule *
3253http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3254{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003255 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003256 struct http_res_rule *rule;
3257 struct hdr_ctx ctx;
3258
3259 list_for_each_entry(rule, rules, list) {
3260 if (rule->action >= HTTP_RES_ACT_MAX)
3261 continue;
3262
3263 /* check optional condition */
3264 if (rule->cond) {
3265 int ret;
3266
3267 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3268 ret = acl_pass(ret);
3269
3270 if (rule->cond->pol == ACL_COND_UNLESS)
3271 ret = !ret;
3272
3273 if (!ret) /* condition not matched */
3274 continue;
3275 }
3276
3277
3278 switch (rule->action) {
3279 case HTTP_RES_ACT_ALLOW:
3280 return NULL; /* "allow" rules are OK */
3281
3282 case HTTP_RES_ACT_DENY:
3283 txn->flags |= TX_SVDENY;
3284 return rule;
3285
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003286 case HTTP_RES_ACT_SET_NICE:
3287 s->task->nice = rule->arg.nice;
3288 break;
3289
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003290 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003291 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003292 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003293 break;
3294
Willy Tarreau51347ed2013-06-11 19:34:13 +02003295 case HTTP_RES_ACT_SET_MARK:
3296#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003297 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003298 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003299#endif
3300 break;
3301
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003302 case HTTP_RES_ACT_SET_LOGL:
3303 s->logs.level = rule->arg.loglevel;
3304 break;
3305
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003306 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003307 case HTTP_RES_ACT_SET_HDR:
3308 ctx.idx = 0;
3309 /* remove all occurrences of the header */
3310 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3311 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3312 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3313 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003314 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3315 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003316 /* now fall through to header addition */
3317
3318 case HTTP_RES_ACT_ADD_HDR:
3319 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3320 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3321 trash.len = rule->arg.hdr_add.name_len;
3322 trash.str[trash.len++] = ':';
3323 trash.str[trash.len++] = ' ';
3324 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3325 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3326 break;
3327 }
3328 }
3329
3330 /* we reached the end of the rules, nothing to report */
3331 return NULL;
3332}
3333
3334
Willy Tarreau71241ab2012-12-27 11:30:54 +01003335/* Perform an HTTP redirect based on the information in <rule>. The function
3336 * returns non-zero on success, or zero in case of a, irrecoverable error such
3337 * as too large a request to build a valid response.
3338 */
3339static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3340{
3341 struct http_msg *msg = &txn->req;
3342 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003343 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003344
3345 /* build redirect message */
3346 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003347 case 308:
3348 msg_fmt = HTTP_308;
3349 break;
3350 case 307:
3351 msg_fmt = HTTP_307;
3352 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003353 case 303:
3354 msg_fmt = HTTP_303;
3355 break;
3356 case 301:
3357 msg_fmt = HTTP_301;
3358 break;
3359 case 302:
3360 default:
3361 msg_fmt = HTTP_302;
3362 break;
3363 }
3364
3365 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3366 return 0;
3367
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003368 location = trash.str + trash.len;
3369
Willy Tarreau71241ab2012-12-27 11:30:54 +01003370 switch(rule->type) {
3371 case REDIRECT_TYPE_SCHEME: {
3372 const char *path;
3373 const char *host;
3374 struct hdr_ctx ctx;
3375 int pathlen;
3376 int hostlen;
3377
3378 host = "";
3379 hostlen = 0;
3380 ctx.idx = 0;
3381 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3382 host = ctx.line + ctx.val;
3383 hostlen = ctx.vlen;
3384 }
3385
3386 path = http_get_path(txn);
3387 /* build message using path */
3388 if (path) {
3389 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3390 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3391 int qs = 0;
3392 while (qs < pathlen) {
3393 if (path[qs] == '?') {
3394 pathlen = qs;
3395 break;
3396 }
3397 qs++;
3398 }
3399 }
3400 } else {
3401 path = "/";
3402 pathlen = 1;
3403 }
3404
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003405 if (rule->rdr_str) { /* this is an old "redirect" rule */
3406 /* check if we can add scheme + "://" + host + path */
3407 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3408 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003409
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003410 /* add scheme */
3411 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3412 trash.len += rule->rdr_len;
3413 }
3414 else {
3415 /* add scheme with executing log format */
3416 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003417
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003418 /* check if we can add scheme + "://" + host + path */
3419 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3420 return 0;
3421 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003422 /* add "://" */
3423 memcpy(trash.str + trash.len, "://", 3);
3424 trash.len += 3;
3425
3426 /* add host */
3427 memcpy(trash.str + trash.len, host, hostlen);
3428 trash.len += hostlen;
3429
3430 /* add path */
3431 memcpy(trash.str + trash.len, path, pathlen);
3432 trash.len += pathlen;
3433
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003434 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003435 if (trash.len && trash.str[trash.len - 1] != '/' &&
3436 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3437 if (trash.len > trash.size - 5)
3438 return 0;
3439 trash.str[trash.len] = '/';
3440 trash.len++;
3441 }
3442
3443 break;
3444 }
3445 case REDIRECT_TYPE_PREFIX: {
3446 const char *path;
3447 int pathlen;
3448
3449 path = http_get_path(txn);
3450 /* build message using path */
3451 if (path) {
3452 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3453 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3454 int qs = 0;
3455 while (qs < pathlen) {
3456 if (path[qs] == '?') {
3457 pathlen = qs;
3458 break;
3459 }
3460 qs++;
3461 }
3462 }
3463 } else {
3464 path = "/";
3465 pathlen = 1;
3466 }
3467
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003468 if (rule->rdr_str) { /* this is an old "redirect" rule */
3469 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3470 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003471
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003472 /* add prefix. Note that if prefix == "/", we don't want to
3473 * add anything, otherwise it makes it hard for the user to
3474 * configure a self-redirection.
3475 */
3476 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3477 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3478 trash.len += rule->rdr_len;
3479 }
3480 }
3481 else {
3482 /* add prefix with executing log format */
3483 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3484
3485 /* Check length */
3486 if (trash.len + pathlen > trash.size - 4)
3487 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003488 }
3489
3490 /* add path */
3491 memcpy(trash.str + trash.len, path, pathlen);
3492 trash.len += pathlen;
3493
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003494 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003495 if (trash.len && trash.str[trash.len - 1] != '/' &&
3496 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3497 if (trash.len > trash.size - 5)
3498 return 0;
3499 trash.str[trash.len] = '/';
3500 trash.len++;
3501 }
3502
3503 break;
3504 }
3505 case REDIRECT_TYPE_LOCATION:
3506 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003507 if (rule->rdr_str) { /* this is an old "redirect" rule */
3508 if (trash.len + rule->rdr_len > trash.size - 4)
3509 return 0;
3510
3511 /* add location */
3512 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3513 trash.len += rule->rdr_len;
3514 }
3515 else {
3516 /* add location with executing log format */
3517 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003518
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003519 /* Check left length */
3520 if (trash.len > trash.size - 4)
3521 return 0;
3522 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003523 break;
3524 }
3525
3526 if (rule->cookie_len) {
3527 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3528 trash.len += 14;
3529 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3530 trash.len += rule->cookie_len;
3531 memcpy(trash.str + trash.len, "\r\n", 2);
3532 trash.len += 2;
3533 }
3534
3535 /* add end of headers and the keep-alive/close status.
3536 * We may choose to set keep-alive if the Location begins
3537 * with a slash, because the client will come back to the
3538 * same server.
3539 */
3540 txn->status = rule->code;
3541 /* let's log the request time */
3542 s->logs.tv_request = now;
3543
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003544 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003545 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3546 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3547 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3548 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3549 /* keep-alive possible */
3550 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3551 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3552 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3553 trash.len += 30;
3554 } else {
3555 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3556 trash.len += 24;
3557 }
3558 }
3559 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3560 trash.len += 4;
3561 bo_inject(txn->rsp.chn, trash.str, trash.len);
3562 /* "eat" the request */
3563 bi_fast_delete(txn->req.chn->buf, msg->sov);
3564 msg->sov = 0;
3565 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3566 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3567 txn->req.msg_state = HTTP_MSG_CLOSED;
3568 txn->rsp.msg_state = HTTP_MSG_DONE;
3569 } else {
3570 /* keep-alive not possible */
3571 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3572 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3573 trash.len += 29;
3574 } else {
3575 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3576 trash.len += 23;
3577 }
3578 stream_int_retnclose(txn->req.chn->prod, &trash);
3579 txn->req.chn->analysers = 0;
3580 }
3581
3582 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003583 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003584 if (!(s->flags & SN_FINST_MASK))
3585 s->flags |= SN_FINST_R;
3586
3587 return 1;
3588}
3589
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003590/* This stream analyser runs all HTTP request processing which is common to
3591 * frontends and backends, which means blocking ACLs, filters, connection-close,
3592 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003593 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003594 * either needs more data or wants to immediately abort the request (eg: deny,
3595 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003596 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003597int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003598{
Willy Tarreaud787e662009-07-07 10:14:51 +02003599 struct http_txn *txn = &s->txn;
3600 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003602 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003603 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003604 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003605
Willy Tarreau655dce92009-11-08 13:10:58 +01003606 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003607 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003608 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003609 return 0;
3610 }
3611
Willy Tarreau3a816292009-07-07 10:55:49 +02003612 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003613 req->analyse_exp = TICK_ETERNITY;
3614
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003615 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 +02003616 now_ms, __FUNCTION__,
3617 s,
3618 req,
3619 req->rex, req->wex,
3620 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003621 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003622 req->analysers);
3623
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003624 /* first check whether we have some ACLs set to block this request */
3625 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003626 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003627
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003628 ret = acl_pass(ret);
3629 if (cond->pol == ACL_COND_UNLESS)
3630 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003631
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003632 if (ret) {
3633 txn->status = 403;
3634 /* let's log the request time */
3635 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003636 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003637 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003638 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003639 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003640 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003641
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003642 /* just in case we have some per-backend tracking */
3643 session_inc_be_http_req_ctr(s);
3644
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003645 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003646 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003647
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003648 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003649 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003650 if (stats_check_uri(s->rep->prod, txn, px)) {
3651 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003652 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3653 txn->status = 500;
3654 s->logs.tv_request = now;
3655 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003656
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003657 if (!(s->flags & SN_ERR_MASK))
3658 s->flags |= SN_ERR_RESOURCE;
3659 goto return_prx_cond;
3660 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003661 /* parse the whole stats request and extract the relevant information */
3662 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003663 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 +01003664 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003665 }
3666
Willy Tarreau3b44e722013-11-16 10:28:23 +01003667 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3668 * blocked by an http-request rule.
3669 */
3670 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003671 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003672 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003673 }
Willy Tarreau06619262006-12-17 08:37:22 +01003674
Willy Tarreau3b44e722013-11-16 10:28:23 +01003675 /* return a 403 if either rule has blocked */
3676 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003677 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003678 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003679 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003680 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003681 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003682 s->fe->fe_counters.denied_req++;
3683 if (s->fe != s->be)
3684 s->be->be_counters.denied_req++;
3685 if (s->listener->counters)
3686 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003687 goto return_prx_cond;
3688 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003689
3690 /* When a connection is tarpitted, we use the tarpit timeout,
3691 * which may be the same as the connect timeout if unspecified.
3692 * If unset, then set it to zero because we really want it to
3693 * eventually expire. We build the tarpit as an analyser.
3694 */
3695 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003696 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003697 /* wipe the request out so that we can drop the connection early
3698 * if the client closes first.
3699 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003700 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003701 req->analysers = 0; /* remove switching rules etc... */
3702 req->analysers |= AN_REQ_HTTP_TARPIT;
3703 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3704 if (!req->analyse_exp)
3705 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003706 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003707 s->fe->fe_counters.denied_req++;
3708 if (s->fe != s->be)
3709 s->be->be_counters.denied_req++;
3710 if (s->listener->counters)
3711 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003712 return 1;
3713 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003714 }
Willy Tarreau06619262006-12-17 08:37:22 +01003715
Willy Tarreau70dffda2014-01-30 03:07:23 +01003716 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003717 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003718 * Option httpclose by itself sets tunnel mode where headers are mangled.
3719 * However, if another mode is set, it will affect it (eg: server-close/
3720 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3721 * if FE and BE have the same settings (common). The method consists in
3722 * checking if options changed between the two calls (implying that either
3723 * one is non-null, or one of them is non-null and we are there for the first
3724 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003725 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003726
Willy Tarreau416ce612014-01-31 15:45:34 +01003727 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003728 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003729 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003730
Willy Tarreau70dffda2014-01-30 03:07:23 +01003731 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3732 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3733 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3734 tmp = TX_CON_WANT_TUN;
3735
3736 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3737 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3738 tmp = TX_CON_WANT_TUN;
3739 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003740
3741 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003742 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3743 /* option httpclose + server_close => forceclose */
3744 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3745 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3746 tmp = TX_CON_WANT_CLO;
3747 else
3748 tmp = TX_CON_WANT_SCL;
3749 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003750
3751 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3752 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003753 tmp = TX_CON_WANT_CLO;
3754
Willy Tarreau5b154472009-12-21 20:11:07 +01003755 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3756 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003757
Willy Tarreau416ce612014-01-31 15:45:34 +01003758 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3759 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003760 /* parse the Connection header and possibly clean it */
3761 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003762 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003763 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3764 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003765 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003766 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003767 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003768 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003769 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003770
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003771 /* check if client or config asks for explicit close in KAL/SCL */
3772 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3773 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3774 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003775 (!(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 +01003776 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003777 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003778 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3779 }
Willy Tarreau78599912009-10-17 20:12:21 +02003780
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003781 /* we can be blocked here because the request needs to be authenticated,
3782 * either to pass or to access stats.
3783 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003784 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003785 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003786
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003787 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003788 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003789
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003790 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003791 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003792 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003793 /* on 401 we still count one error, because normal browsing
3794 * won't significantly increase the counter but brute force
3795 * attempts will.
3796 */
3797 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003798 goto return_prx_cond;
3799 }
3800
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003801 /* add request headers from the rule sets in the same order */
3802 list_for_each_entry(wl, &px->req_add, list) {
3803 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003804 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003805 ret = acl_pass(ret);
3806 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3807 ret = !ret;
3808 if (!ret)
3809 continue;
3810 }
3811
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003812 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003813 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003814 }
3815
3816 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3817 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3818 goto return_bad_req;
3819 req->analyse_exp = TICK_ETERNITY;
3820 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003821 }
3822
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003823 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003824 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003825 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3826 s->fe->fe_counters.intercepted_req++;
3827
3828 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3829 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3830 if (!(s->flags & SN_FINST_MASK))
3831 s->flags |= SN_FINST_R;
3832
3833 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003834 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003835 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003836 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003837
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003838 /* check whether we have some ACLs set to redirect this request */
3839 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003840 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003841 int ret;
3842
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003843 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003844 ret = acl_pass(ret);
3845 if (rule->cond->pol == ACL_COND_UNLESS)
3846 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003847 if (!ret)
3848 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003849 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003850 if (!http_apply_redirect_rule(rule, s, txn))
3851 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003852
Willy Tarreau71241ab2012-12-27 11:30:54 +01003853 req->analyse_exp = TICK_ETERNITY;
3854 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003855 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003856
Willy Tarreau2be39392010-01-03 17:24:51 +01003857 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3858 * If this happens, then the data will not come immediately, so we must
3859 * send all what we have without waiting. Note that due to the small gain
3860 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003861 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003862 * itself once used.
3863 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003864 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003865
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003866 /* that's OK for us now, let's move on to next analysers */
3867 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003868
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003869 return_bad_req:
3870 /* We centralize bad requests processing here */
3871 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3872 /* we detected a parsing error. We want to archive this request
3873 * in the dedicated proxy area for later troubleshooting.
3874 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003875 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003876 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003877
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003878 txn->req.msg_state = HTTP_MSG_ERROR;
3879 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003880 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003881
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003882 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003883 if (s->listener->counters)
3884 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003885
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003886 return_prx_cond:
3887 if (!(s->flags & SN_ERR_MASK))
3888 s->flags |= SN_ERR_PRXCOND;
3889 if (!(s->flags & SN_FINST_MASK))
3890 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003891
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003892 req->analysers = 0;
3893 req->analyse_exp = TICK_ETERNITY;
3894 return 0;
3895}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003896
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003897/* This function performs all the processing enabled for the current request.
3898 * It returns 1 if the processing can continue on next analysers, or zero if it
3899 * needs more data, encounters an error, or wants to immediately abort the
3900 * request. It relies on buffers flags, and updates s->req->analysers.
3901 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003902int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003903{
3904 struct http_txn *txn = &s->txn;
3905 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003906 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003907
Willy Tarreau655dce92009-11-08 13:10:58 +01003908 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003909 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003910 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003911 return 0;
3912 }
3913
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003914 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 +02003915 now_ms, __FUNCTION__,
3916 s,
3917 req,
3918 req->rex, req->wex,
3919 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003920 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003921 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003922
William Lallemand82fe75c2012-10-23 10:25:10 +02003923 if (s->fe->comp || s->be->comp)
3924 select_compression_request_header(s, req->buf);
3925
Willy Tarreau59234e92008-11-30 23:51:27 +01003926 /*
3927 * Right now, we know that we have processed the entire headers
3928 * and that unwanted requests have been filtered out. We can do
3929 * whatever we want with the remaining request. Also, now we
3930 * may have separate values for ->fe, ->be.
3931 */
Willy Tarreau06619262006-12-17 08:37:22 +01003932
Willy Tarreau59234e92008-11-30 23:51:27 +01003933 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003934 * If HTTP PROXY is set we simply get remote server address parsing
3935 * incoming request. Note that this requires that a connection is
3936 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003937 */
3938 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003939 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003940 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003941
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003942 /* Note that for now we don't reuse existing proxy connections */
3943 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003944 txn->req.msg_state = HTTP_MSG_ERROR;
3945 txn->status = 500;
3946 req->analysers = 0;
3947 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3948
3949 if (!(s->flags & SN_ERR_MASK))
3950 s->flags |= SN_ERR_RESOURCE;
3951 if (!(s->flags & SN_FINST_MASK))
3952 s->flags |= SN_FINST_R;
3953
3954 return 0;
3955 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003956
3957 path = http_get_path(txn);
3958 url2sa(req->buf->p + msg->sl.rq.u,
3959 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003960 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003961 /* if the path was found, we have to remove everything between
3962 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3963 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3964 * u_l characters by a single "/".
3965 */
3966 if (path) {
3967 char *cur_ptr = req->buf->p;
3968 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3969 int delta;
3970
3971 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
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 }
3977 else {
3978 char *cur_ptr = req->buf->p;
3979 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3980 int delta;
3981
3982 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3983 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3984 http_msg_move_end(&txn->req, delta);
3985 cur_end += delta;
3986 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3987 goto return_bad_req;
3988 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003990
Willy Tarreau59234e92008-11-30 23:51:27 +01003991 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003992 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003993 * Note that doing so might move headers in the request, but
3994 * the fields will stay coherent and the URI will not move.
3995 * This should only be performed in the backend.
3996 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003997 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003998 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3999 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004000
Willy Tarreau59234e92008-11-30 23:51:27 +01004001 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004002 * 8: the appsession cookie was looked up very early in 1.2,
4003 * so let's do the same now.
4004 */
4005
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004006 /* It needs to look into the URI unless persistence must be ignored */
4007 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004008 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 +01004009 }
4010
William Lallemanda73203e2012-03-12 12:48:57 +01004011 /* add unique-id if "header-unique-id" is specified */
4012
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004013 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4014 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4015 goto return_bad_req;
4016 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004017 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004018 }
William Lallemanda73203e2012-03-12 12:48:57 +01004019
4020 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004021 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4022 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004023 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004024 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004025 goto return_bad_req;
4026 }
4027
Cyril Bontéb21570a2009-11-29 20:04:48 +01004028 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004029 * 9: add X-Forwarded-For if either the frontend or the backend
4030 * asks for it.
4031 */
4032 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004033 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004034 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004035 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4036 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004037 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004038 /* The header is set to be added only if none is present
4039 * and we found it, so don't do anything.
4040 */
4041 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004042 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004043 /* Add an X-Forwarded-For header unless the source IP is
4044 * in the 'except' network range.
4045 */
4046 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004047 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004048 != s->fe->except_net.s_addr) &&
4049 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004050 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004051 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004052 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004053 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004054 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004055
4056 /* Note: we rely on the backend to get the header name to be used for
4057 * x-forwarded-for, because the header is really meant for the backends.
4058 * However, if the backend did not specify any option, we have to rely
4059 * on the frontend's header name.
4060 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004061 if (s->be->fwdfor_hdr_len) {
4062 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004063 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004064 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004065 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004066 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004067 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004068 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 +01004069
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004070 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004071 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004072 }
4073 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004074 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004075 /* FIXME: for the sake of completeness, we should also support
4076 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004077 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004078 int len;
4079 char pn[INET6_ADDRSTRLEN];
4080 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004081 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004082 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004083
Willy Tarreau59234e92008-11-30 23:51:27 +01004084 /* Note: we rely on the backend to get the header name to be used for
4085 * x-forwarded-for, because the header is really meant for the backends.
4086 * However, if the backend did not specify any option, we have to rely
4087 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004088 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004089 if (s->be->fwdfor_hdr_len) {
4090 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004091 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004092 } else {
4093 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004094 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004095 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004096 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004097
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004098 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004099 goto return_bad_req;
4100 }
4101 }
4102
4103 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004104 * 10: add X-Original-To if either the frontend or the backend
4105 * asks for it.
4106 */
4107 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4108
4109 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004110 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004111 /* Add an X-Original-To header unless the destination IP is
4112 * in the 'except' network range.
4113 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004114 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004115
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004116 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004117 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004118 (((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 +02004119 != s->fe->except_to.s_addr) &&
4120 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004121 (((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 +02004122 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004123 int len;
4124 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004125 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004126
4127 /* Note: we rely on the backend to get the header name to be used for
4128 * x-original-to, because the header is really meant for the backends.
4129 * However, if the backend did not specify any option, we have to rely
4130 * on the frontend's header name.
4131 */
4132 if (s->be->orgto_hdr_len) {
4133 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004134 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004135 } else {
4136 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004137 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004138 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004139 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 +02004140
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004141 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004142 goto return_bad_req;
4143 }
4144 }
4145 }
4146
Willy Tarreau50fc7772012-11-11 22:19:57 +01004147 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4148 * If an "Upgrade" token is found, the header is left untouched in order not to have
4149 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4150 * "Upgrade" is present in the Connection header.
4151 */
4152 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4153 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
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 Tarreaubbf0b372010-01-18 16:54:40 +01004156 unsigned int want_flags = 0;
4157
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004158 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004159 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004160 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4161 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004162 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004163 want_flags |= TX_CON_CLO_SET;
4164 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004165 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004166 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4167 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004168 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004169 want_flags |= TX_CON_KAL_SET;
4170 }
4171
4172 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004173 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004174 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004175
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004176
Willy Tarreau522d6c02009-12-06 18:49:18 +01004177 /* If we have no server assigned yet and we're balancing on url_param
4178 * with a POST request, we may be interested in checking the body for
4179 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004180 */
4181 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4182 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004183 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004184 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004185 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004186 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004187
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004188 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004189 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004190#ifdef TCP_QUICKACK
4191 /* We expect some data from the client. Unless we know for sure
4192 * we already have a full request, we have to re-enable quick-ack
4193 * in case we previously disabled it, otherwise we might cause
4194 * the client to delay further data.
4195 */
4196 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004197 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004198 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004199 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004200 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004201#endif
4202 }
Willy Tarreau03945942009-12-22 16:50:27 +01004203
Willy Tarreau59234e92008-11-30 23:51:27 +01004204 /*************************************************************
4205 * OK, that's finished for the headers. We have done what we *
4206 * could. Let's switch to the DATA state. *
4207 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004208 req->analyse_exp = TICK_ETERNITY;
4209 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004210
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004211 /* if the server closes the connection, we want to immediately react
4212 * and close the socket to save packets and syscalls.
4213 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004214 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4215 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004216
Willy Tarreau59234e92008-11-30 23:51:27 +01004217 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004218 /* OK let's go on with the BODY now */
4219 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004220
Willy Tarreau59234e92008-11-30 23:51:27 +01004221 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004222 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004223 /* we detected a parsing error. We want to archive this request
4224 * in the dedicated proxy area for later troubleshooting.
4225 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004226 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004227 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004228
Willy Tarreau59234e92008-11-30 23:51:27 +01004229 txn->req.msg_state = HTTP_MSG_ERROR;
4230 txn->status = 400;
4231 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004232 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004233
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004234 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004235 if (s->listener->counters)
4236 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004237
Willy Tarreau59234e92008-11-30 23:51:27 +01004238 if (!(s->flags & SN_ERR_MASK))
4239 s->flags |= SN_ERR_PRXCOND;
4240 if (!(s->flags & SN_FINST_MASK))
4241 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004242 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004243}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004244
Willy Tarreau60b85b02008-11-30 23:28:40 +01004245/* This function is an analyser which processes the HTTP tarpit. It always
4246 * returns zero, at the beginning because it prevents any other processing
4247 * from occurring, and at the end because it terminates the request.
4248 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004249int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004250{
4251 struct http_txn *txn = &s->txn;
4252
4253 /* This connection is being tarpitted. The CLIENT side has
4254 * already set the connect expiration date to the right
4255 * timeout. We just have to check that the client is still
4256 * there and that the timeout has not expired.
4257 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004258 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004259 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004260 !tick_is_expired(req->analyse_exp, now_ms))
4261 return 0;
4262
4263 /* We will set the queue timer to the time spent, just for
4264 * logging purposes. We fake a 500 server error, so that the
4265 * attacker will not suspect his connection has been tarpitted.
4266 * It will not cause trouble to the logs because we can exclude
4267 * the tarpitted connections by filtering on the 'PT' status flags.
4268 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004269 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4270
4271 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004272 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004273 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004274
4275 req->analysers = 0;
4276 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004277
Willy Tarreau60b85b02008-11-30 23:28:40 +01004278 if (!(s->flags & SN_ERR_MASK))
4279 s->flags |= SN_ERR_PRXCOND;
4280 if (!(s->flags & SN_FINST_MASK))
4281 s->flags |= SN_FINST_T;
4282 return 0;
4283}
4284
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004285/* This function is an analyser which waits for the HTTP request body. It waits
4286 * for either the buffer to be full, or the full advertised contents to have
4287 * reached the buffer. It must only be called after the standard HTTP request
4288 * processing has occurred, because it expects the request to be parsed and will
4289 * look for the Expect header. It may send a 100-Continue interim response. It
4290 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4291 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4292 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004293 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004294int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004295{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004296 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004297 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004298
4299 /* We have to parse the HTTP request body to find any required data.
4300 * "balance url_param check_post" should have been the only way to get
4301 * into this. We were brought here after HTTP header analysis, so all
4302 * related structures are ready.
4303 */
4304
Willy Tarreau890988f2014-04-10 11:59:33 +02004305 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4306 /* This is the first call */
4307 if (msg->msg_state < HTTP_MSG_BODY)
4308 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004309
Willy Tarreau890988f2014-04-10 11:59:33 +02004310 if (msg->msg_state < HTTP_MSG_100_SENT) {
4311 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4312 * send an HTTP/1.1 100 Continue intermediate response.
4313 */
4314 if (msg->flags & HTTP_MSGF_VER_11) {
4315 struct hdr_ctx ctx;
4316 ctx.idx = 0;
4317 /* Expect is allowed in 1.1, look for it */
4318 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4319 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4320 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4321 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004322 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004323 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004324 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004325
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004326 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004327 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004328 * is still null. We must save the body in msg->next because it
4329 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004330 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004331 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004332
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004333 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004334 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4335 else
4336 msg->msg_state = HTTP_MSG_DATA;
4337 }
4338
Willy Tarreau890988f2014-04-10 11:59:33 +02004339 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4340 /* We're in content-length mode, we just have to wait for enough data. */
4341 if (req->buf->i - msg->sov < msg->body_len)
4342 goto missing_data;
4343
4344 /* OK we have everything we need now */
4345 goto http_end;
4346 }
4347
4348 /* OK here we're parsing a chunked-encoded message */
4349
Willy Tarreau522d6c02009-12-06 18:49:18 +01004350 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004351 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004352 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004353 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004354 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004355 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004356
Willy Tarreau115acb92009-12-26 13:56:06 +01004357 if (!ret)
4358 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004359 else if (ret < 0) {
4360 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004361 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004362 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004363 }
4364
Willy Tarreaud98cf932009-12-27 22:54:55 +01004365 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004366 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004367 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004368 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004369 if (msg->msg_state == HTTP_MSG_TRAILERS)
4370 goto http_end;
4371
Willy Tarreau226071e2014-04-10 11:55:45 +02004372 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004373 goto http_end;
4374
4375 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004376 /* we get here if we need to wait for more data. If the buffer is full,
4377 * we have the maximum we can expect.
4378 */
4379 if (buffer_full(req->buf, global.tune.maxrewrite))
4380 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004381
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004382 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004383 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004384 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004385
4386 if (!(s->flags & SN_ERR_MASK))
4387 s->flags |= SN_ERR_CLITO;
4388 if (!(s->flags & SN_FINST_MASK))
4389 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004390 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004391 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004392
4393 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004394 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004395 /* Not enough data. We'll re-use the http-request
4396 * timeout here. Ideally, we should set the timeout
4397 * relative to the accept() date. We just set the
4398 * request timeout once at the beginning of the
4399 * request.
4400 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004401 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004402 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004403 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004404 return 0;
4405 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004406
4407 http_end:
4408 /* The situation will not evolve, so let's give up on the analysis. */
4409 s->logs.tv_request = now; /* update the request timer to reflect full request */
4410 req->analysers &= ~an_bit;
4411 req->analyse_exp = TICK_ETERNITY;
4412 return 1;
4413
4414 return_bad_req: /* let's centralize all bad requests */
4415 txn->req.msg_state = HTTP_MSG_ERROR;
4416 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004417 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004418
Willy Tarreau79ebac62010-06-07 13:47:49 +02004419 if (!(s->flags & SN_ERR_MASK))
4420 s->flags |= SN_ERR_PRXCOND;
4421 if (!(s->flags & SN_FINST_MASK))
4422 s->flags |= SN_FINST_R;
4423
Willy Tarreau522d6c02009-12-06 18:49:18 +01004424 return_err_msg:
4425 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004426 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004427 if (s->listener->counters)
4428 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004429 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004430}
4431
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004432/* send a server's name with an outgoing request over an established connection.
4433 * Note: this function is designed to be called once the request has been scheduled
4434 * for being forwarded. This is the reason why it rewinds the buffer before
4435 * proceeding.
4436 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004437int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004438
4439 struct hdr_ctx ctx;
4440
Mark Lamourinec2247f02012-01-04 13:02:01 -05004441 char *hdr_name = be->server_id_hdr_name;
4442 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004443 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004444 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004445 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004446
William Lallemandd9e90662012-01-30 17:27:17 +01004447 ctx.idx = 0;
4448
Willy Tarreau9b28e032012-10-12 23:49:43 +02004449 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004450 if (old_o) {
4451 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004452 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004453 }
4454
Willy Tarreau9b28e032012-10-12 23:49:43 +02004455 old_i = chn->buf->i;
4456 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004457 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004458 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004459 }
4460
4461 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004462 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004463 memcpy(hdr_val, hdr_name, hdr_name_len);
4464 hdr_val += hdr_name_len;
4465 *hdr_val++ = ':';
4466 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004467 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4468 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004469
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004470 if (old_o) {
4471 /* If this was a forwarded request, we must readjust the amount of
4472 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004473 * variations. Note that if the request was already scheduled for
4474 * forwarding, it had its req->sol pointing to the body, which
4475 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004476 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004477 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004478 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004479 }
4480
Mark Lamourinec2247f02012-01-04 13:02:01 -05004481 return 0;
4482}
4483
Willy Tarreau610ecce2010-01-04 21:15:02 +01004484/* Terminate current transaction and prepare a new one. This is very tricky
4485 * right now but it works.
4486 */
4487void http_end_txn_clean_session(struct session *s)
4488{
Willy Tarreau068621e2013-12-23 15:11:25 +01004489 int prev_status = s->txn.status;
4490
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 /* FIXME: We need a more portable way of releasing a backend's and a
4492 * server's connections. We need a safer way to reinitialize buffer
4493 * flags. We also need a more accurate method for computing per-request
4494 * data.
4495 */
4496 http_silent_debug(__LINE__, s);
4497
Willy Tarreau4213a112013-12-15 10:25:42 +01004498 /* unless we're doing keep-alive, we want to quickly close the connection
4499 * to the server.
4500 */
4501 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4502 !si_conn_ready(s->req->cons)) {
4503 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4504 si_shutr(s->req->cons);
4505 si_shutw(s->req->cons);
4506 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004507
4508 http_silent_debug(__LINE__, s);
4509
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004510 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004512 if (unlikely(s->srv_conn))
4513 sess_change_server(s, NULL);
4514 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515
4516 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4517 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004518 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004519
4520 if (s->txn.status) {
4521 int n;
4522
4523 n = s->txn.status / 100;
4524 if (n < 1 || n > 5)
4525 n = 0;
4526
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004527 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004528 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004529 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004530 s->fe->fe_counters.p.http.comp_rsp++;
4531 }
Willy Tarreau24657792010-02-26 10:30:28 +01004532 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004533 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004534 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004535 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004536 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004537 s->be->be_counters.p.http.comp_rsp++;
4538 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004539 }
4540
4541 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004542 s->logs.bytes_in -= s->req->buf->i;
4543 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544
4545 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004546 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 !(s->flags & SN_MONITOR) &&
4548 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4549 s->do_log(s);
4550 }
4551
4552 s->logs.accept_date = date; /* user-visible date for logging */
4553 s->logs.tv_accept = now; /* corrected date for internal use */
4554 tv_zero(&s->logs.tv_request);
4555 s->logs.t_queue = -1;
4556 s->logs.t_connect = -1;
4557 s->logs.t_data = -1;
4558 s->logs.t_close = 0;
4559 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4560 s->logs.srv_queue_size = 0; /* we will get this number soon */
4561
Willy Tarreau9b28e032012-10-12 23:49:43 +02004562 s->logs.bytes_in = s->req->total = s->req->buf->i;
4563 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564
4565 if (s->pend_pos)
4566 pendconn_free(s->pend_pos);
4567
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004568 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004569 if (s->flags & SN_CURR_SESS) {
4570 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004571 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004572 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004573 if (may_dequeue_tasks(objt_server(s->target), s->be))
4574 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004575 }
4576
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004577 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004578
Willy Tarreau4213a112013-12-15 10:25:42 +01004579 /* only release our endpoint if we don't intend to reuse the
4580 * connection.
4581 */
4582 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4583 !si_conn_ready(s->req->cons)) {
4584 si_release_endpoint(s->req->cons);
4585 }
4586
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004589 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004591 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004592 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4593 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 +02004594 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 +01004595 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004596
Willy Tarreau610ecce2010-01-04 21:15:02 +01004597 s->txn.meth = 0;
4598 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004599 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004600
4601 if (prev_status == 401 || prev_status == 407) {
4602 /* In HTTP keep-alive mode, if we receive a 401, we still have
4603 * a chance of being able to send the visitor again to the same
4604 * server over the same connection. This is required by some
4605 * broken protocols such as NTLM, and anyway whenever there is
4606 * an opportunity for sending the challenge to the proper place,
4607 * it's better to do it (at least it helps with debugging).
4608 */
4609 s->txn.flags |= TX_PREFER_LAST;
4610 }
4611
Willy Tarreauee55dc02010-06-01 10:56:34 +02004612 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004613 s->req->cons->flags |= SI_FL_INDEP_STR;
4614
Willy Tarreau96e31212011-05-30 18:10:30 +02004615 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004616 s->req->flags |= CF_NEVER_WAIT;
4617 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004618 }
4619
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620 /* if the request buffer is not empty, it means we're
4621 * about to process another request, so send pending
4622 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004623 * Just don't do this if the buffer is close to be full,
4624 * because the request will wait for it to flush a little
4625 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004627 if (s->req->buf->i) {
4628 if (s->rep->buf->o &&
4629 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4630 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004631 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004632 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004633
4634 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004635 channel_auto_read(s->req);
4636 channel_auto_close(s->req);
4637 channel_auto_read(s->rep);
4638 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639
Willy Tarreau27375622013-12-17 00:00:28 +01004640 /* we're in keep-alive with an idle connection, monitor it */
4641 si_idle_conn(s->req->cons);
4642
Willy Tarreau342b11c2010-11-24 16:22:09 +01004643 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004644 s->rep->analysers = 0;
4645
4646 http_silent_debug(__LINE__, s);
4647}
4648
4649
4650/* This function updates the request state machine according to the response
4651 * state machine and buffer flags. It returns 1 if it changes anything (flag
4652 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4653 * it is only used to find when a request/response couple is complete. Both
4654 * this function and its equivalent should loop until both return zero. It
4655 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4656 */
4657int http_sync_req_state(struct session *s)
4658{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004659 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004661 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 unsigned int old_state = txn->req.msg_state;
4663
4664 http_silent_debug(__LINE__, s);
4665 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4666 return 0;
4667
4668 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004669 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004670 * We can shut the read side unless we want to abort_on_close,
4671 * or we have a POST request. The issue with POST requests is
4672 * that some browsers still send a CRLF after the request, and
4673 * this CRLF must be read so that it does not remain in the kernel
4674 * buffers, otherwise a close could cause an RST on some systems
4675 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004676 * Note that if we're using keep-alive on the client side, we'd
4677 * rather poll now and keep the polling enabled for the whole
4678 * session's life than enabling/disabling it between each
4679 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004680 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004681 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4682 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4683 !(s->be->options & PR_O_ABRT_CLOSE) &&
4684 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004685 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004686
Willy Tarreau40f151a2012-12-20 12:10:09 +01004687 /* if the server closes the connection, we want to immediately react
4688 * and close the socket to save packets and syscalls.
4689 */
4690 chn->cons->flags |= SI_FL_NOHALF;
4691
Willy Tarreau610ecce2010-01-04 21:15:02 +01004692 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4693 goto wait_other_side;
4694
4695 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4696 /* The server has not finished to respond, so we
4697 * don't want to move in order not to upset it.
4698 */
4699 goto wait_other_side;
4700 }
4701
4702 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4703 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004704 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004705 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004706 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004707 goto wait_other_side;
4708 }
4709
4710 /* When we get here, it means that both the request and the
4711 * response have finished receiving. Depending on the connection
4712 * mode, we'll have to wait for the last bytes to leave in either
4713 * direction, and sometimes for a close to be effective.
4714 */
4715
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004716 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4717 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004718 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4719 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004720 }
4721 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4722 /* Option forceclose is set, or either side wants to close,
4723 * let's enforce it now that we're not expecting any new
4724 * data to come. The caller knows the session is complete
4725 * once both states are CLOSED.
4726 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004727 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4728 channel_shutr_now(chn);
4729 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004731 }
4732 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004733 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4734 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004735 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004736 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4737 channel_auto_read(chn);
4738 txn->req.msg_state = HTTP_MSG_TUNNEL;
4739 chn->flags |= CF_NEVER_WAIT;
4740 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 }
4742
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004743 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004744 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004745 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004746
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004747 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 txn->req.msg_state = HTTP_MSG_CLOSING;
4749 goto http_msg_closing;
4750 }
4751 else {
4752 txn->req.msg_state = HTTP_MSG_CLOSED;
4753 goto http_msg_closed;
4754 }
4755 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004756 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757 }
4758
4759 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4760 http_msg_closing:
4761 /* nothing else to forward, just waiting for the output buffer
4762 * to be empty and for the shutw_now to take effect.
4763 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004764 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004765 txn->req.msg_state = HTTP_MSG_CLOSED;
4766 goto http_msg_closed;
4767 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004768 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 txn->req.msg_state = HTTP_MSG_ERROR;
4770 goto wait_other_side;
4771 }
4772 }
4773
4774 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4775 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004776 /* see above in MSG_DONE why we only do this in these states */
4777 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4778 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4779 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004780 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004781 goto wait_other_side;
4782 }
4783
4784 wait_other_side:
4785 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004786 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787}
4788
4789
4790/* This function updates the response state machine according to the request
4791 * state machine and buffer flags. It returns 1 if it changes anything (flag
4792 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4793 * it is only used to find when a request/response couple is complete. Both
4794 * this function and its equivalent should loop until both return zero. It
4795 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4796 */
4797int http_sync_res_state(struct session *s)
4798{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004799 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004801 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802 unsigned int old_state = txn->rsp.msg_state;
4803
4804 http_silent_debug(__LINE__, s);
4805 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4806 return 0;
4807
4808 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4809 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004810 * still monitor the server connection for a possible close
4811 * while the request is being uploaded, so we don't disable
4812 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004814 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004815
4816 if (txn->req.msg_state == HTTP_MSG_ERROR)
4817 goto wait_other_side;
4818
4819 if (txn->req.msg_state < HTTP_MSG_DONE) {
4820 /* The client seems to still be sending data, probably
4821 * because we got an error response during an upload.
4822 * We have the choice of either breaking the connection
4823 * or letting it pass through. Let's do the later.
4824 */
4825 goto wait_other_side;
4826 }
4827
4828 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4829 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004830 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004832 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004833 goto wait_other_side;
4834 }
4835
4836 /* When we get here, it means that both the request and the
4837 * response have finished receiving. Depending on the connection
4838 * mode, we'll have to wait for the last bytes to leave in either
4839 * direction, and sometimes for a close to be effective.
4840 */
4841
4842 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4843 /* Server-close mode : shut read and wait for the request
4844 * side to close its output buffer. The caller will detect
4845 * when we're in DONE and the other is in CLOSED and will
4846 * catch that for the final cleanup.
4847 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004848 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4849 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004850 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004851 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4852 /* Option forceclose is set, or either side wants to close,
4853 * let's enforce it now that we're not expecting any new
4854 * data to come. The caller knows the session is complete
4855 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004856 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004857 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4858 channel_shutr_now(chn);
4859 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004860 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004861 }
4862 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004863 /* The last possible modes are keep-alive and tunnel. Tunnel will
4864 * need to forward remaining data. Keep-alive will need to monitor
4865 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004866 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004867 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004868 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004869 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4870 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004871 }
4872
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004873 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004874 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004875 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004876 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4877 goto http_msg_closing;
4878 }
4879 else {
4880 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4881 goto http_msg_closed;
4882 }
4883 }
4884 goto wait_other_side;
4885 }
4886
4887 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4888 http_msg_closing:
4889 /* nothing else to forward, just waiting for the output buffer
4890 * to be empty and for the shutw_now to take effect.
4891 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004892 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004893 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4894 goto http_msg_closed;
4895 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004896 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004897 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004898 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004899 if (objt_server(s->target))
4900 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901 goto wait_other_side;
4902 }
4903 }
4904
4905 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4906 http_msg_closed:
4907 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004908 bi_erase(chn);
4909 channel_auto_close(chn);
4910 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004911 goto wait_other_side;
4912 }
4913
4914 wait_other_side:
4915 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004916 /* We force the response to leave immediately if we're waiting for the
4917 * other side, since there is no pending shutdown to push it out.
4918 */
4919 if (!channel_is_empty(chn))
4920 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004921 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922}
4923
4924
4925/* Resync the request and response state machines. Return 1 if either state
4926 * changes.
4927 */
4928int http_resync_states(struct session *s)
4929{
4930 struct http_txn *txn = &s->txn;
4931 int old_req_state = txn->req.msg_state;
4932 int old_res_state = txn->rsp.msg_state;
4933
4934 http_silent_debug(__LINE__, s);
4935 http_sync_req_state(s);
4936 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004937 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938 if (!http_sync_res_state(s))
4939 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004940 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 if (!http_sync_req_state(s))
4942 break;
4943 }
4944 http_silent_debug(__LINE__, s);
4945 /* OK, both state machines agree on a compatible state.
4946 * There are a few cases we're interested in :
4947 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4948 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4949 * directions, so let's simply disable both analysers.
4950 * - HTTP_MSG_CLOSED on the response only means we must abort the
4951 * request.
4952 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4953 * with server-close mode means we've completed one request and we
4954 * must re-initialize the server connection.
4955 */
4956
4957 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4958 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4959 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4960 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4961 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004962 channel_auto_close(s->req);
4963 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004964 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004965 channel_auto_close(s->rep);
4966 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004968 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4969 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004970 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004971 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004972 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004973 channel_auto_close(s->rep);
4974 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004975 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004976 channel_abort(s->req);
4977 channel_auto_close(s->req);
4978 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004979 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004980 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004981 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4982 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004983 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004984 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4985 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4986 /* server-close/keep-alive: terminate this transaction,
4987 * possibly killing the server connection and reinitialize
4988 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989 */
4990 http_end_txn_clean_session(s);
4991 }
4992
4993 http_silent_debug(__LINE__, s);
4994 return txn->req.msg_state != old_req_state ||
4995 txn->rsp.msg_state != old_res_state;
4996}
4997
Willy Tarreaud98cf932009-12-27 22:54:55 +01004998/* This function is an analyser which forwards request body (including chunk
4999 * sizes if any). It is called as soon as we must forward, even if we forward
5000 * zero byte. The only situation where it must not be called is when we're in
5001 * tunnel mode and we want to forward till the close. It's used both to forward
5002 * remaining data and to resync after end of body. It expects the msg_state to
5003 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5004 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005005 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005006 * bytes of pending data + the headers if not already done (between sol and sov).
5007 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005008 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005009int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005010{
5011 struct http_txn *txn = &s->txn;
5012 struct http_msg *msg = &s->txn.req;
5013
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005014 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5015 return 0;
5016
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005017 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005018 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005019 /* Output closed while we were sending data. We must abort and
5020 * wake the other side up.
5021 */
5022 msg->msg_state = HTTP_MSG_ERROR;
5023 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005024 return 1;
5025 }
5026
Willy Tarreau80a92c02014-03-12 10:41:13 +01005027 /* Some post-connect processing might want us to refrain from starting to
5028 * forward data. Currently, the only reason for this is "balance url_param"
5029 * whichs need to parse/process the request after we've enabled forwarding.
5030 */
5031 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5032 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5033 channel_auto_connect(req);
5034 goto missing_data;
5035 }
5036 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5037 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005038
5039 /* Note that we don't have to send 100-continue back because we don't
5040 * need the data to complete our job, and it's up to the server to
5041 * decide whether to return 100, 417 or anything else in return of
5042 * an "Expect: 100-continue" header.
5043 */
5044
5045 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005046 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02005047 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02005048 * is still null. We must save the body in msg->next because it
5049 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005050 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005051 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005052
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005053 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005054 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005055 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005056 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005057 }
5058
Willy Tarreau80a92c02014-03-12 10:41:13 +01005059 /* in most states, we should abort in case of early close */
5060 channel_auto_close(req);
5061
Willy Tarreaud98cf932009-12-27 22:54:55 +01005062 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02005063 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005064
Willy Tarreau610ecce2010-01-04 21:15:02 +01005065 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005066 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005067 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005068 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005069 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005070 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005071 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005072 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005073 }
Willy Tarreau5523b322009-12-29 12:05:52 +01005074
Willy Tarreaucaabe412010-01-03 23:08:28 +01005075 if (msg->msg_state == HTTP_MSG_DATA) {
5076 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005077 if (req->to_forward) {
5078 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005079 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005080 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005081
5082 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005083 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005084 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005085 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005086 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005087 }
5088 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005089 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005090 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005091 * TRAILERS state.
5092 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005093 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005094
Willy Tarreau54d23df2012-10-25 19:04:45 +02005095 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005096 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005097 else if (ret < 0) {
5098 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005099 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005100 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005101 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005102 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005103 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005104 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005105 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005106 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005107 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005108
5109 if (ret == 0)
5110 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005111 else if (ret < 0) {
5112 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005113 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005114 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005115 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005116 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005117 /* we're in MSG_CHUNK_SIZE now */
5118 }
5119 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005120 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005121
5122 if (ret == 0)
5123 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005124 else if (ret < 0) {
5125 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005126 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005127 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005128 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005129 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005130 /* we're in HTTP_MSG_DONE now */
5131 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005132 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005133 int old_state = msg->msg_state;
5134
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005136 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005137 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5138 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005139 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140 if (http_resync_states(s)) {
5141 /* some state changes occurred, maybe the analyser
5142 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005143 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005144 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005145 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005146 /* request errors are most likely due to
5147 * the server aborting the transfer.
5148 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005149 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005150 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005151 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005152 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005154 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005155 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005156 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005157
5158 /* If "option abortonclose" is set on the backend, we
5159 * want to monitor the client's connection and forward
5160 * any shutdown notification to the server, which will
5161 * decide whether to close or to go on processing the
5162 * request.
5163 */
5164 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005165 channel_auto_read(req);
5166 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005167 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005168 else if (s->txn.meth == HTTP_METH_POST) {
5169 /* POST requests may require to read extra CRLF
5170 * sent by broken browsers and which could cause
5171 * an RST to be sent upon close on some systems
5172 * (eg: Linux).
5173 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005174 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005175 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005176
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005178 }
5179 }
5180
Willy Tarreaud98cf932009-12-27 22:54:55 +01005181 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005182 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005183 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005184 if (!(s->flags & SN_ERR_MASK))
5185 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005186 if (!(s->flags & SN_FINST_MASK)) {
5187 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5188 s->flags |= SN_FINST_H;
5189 else
5190 s->flags |= SN_FINST_D;
5191 }
5192
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005193 s->fe->fe_counters.cli_aborts++;
5194 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 if (objt_server(s->target))
5196 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005197
5198 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005199 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005200
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005201 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005202 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005203 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005205 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005206 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005207 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005208 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005209 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005210
Willy Tarreau5c620922011-05-11 19:56:11 +02005211 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005212 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005213 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005214 * modes are already handled by the stream sock layer. We must not do
5215 * this in content-length mode because it could present the MSG_MORE
5216 * flag with the last block of forwarded data, which would cause an
5217 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005218 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005219 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005220 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005221
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005223 return 0;
5224
Willy Tarreaud98cf932009-12-27 22:54:55 +01005225 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005226 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005227 if (s->listener->counters)
5228 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005229 return_bad_req_stats_ok:
5230 txn->req.msg_state = HTTP_MSG_ERROR;
5231 if (txn->status) {
5232 /* Note: we don't send any error if some data were already sent */
5233 stream_int_retnclose(req->prod, NULL);
5234 } else {
5235 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005236 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005237 }
5238 req->analysers = 0;
5239 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005240
5241 if (!(s->flags & SN_ERR_MASK))
5242 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005243 if (!(s->flags & SN_FINST_MASK)) {
5244 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5245 s->flags |= SN_FINST_H;
5246 else
5247 s->flags |= SN_FINST_D;
5248 }
5249 return 0;
5250
5251 aborted_xfer:
5252 txn->req.msg_state = HTTP_MSG_ERROR;
5253 if (txn->status) {
5254 /* Note: we don't send any error if some data were already sent */
5255 stream_int_retnclose(req->prod, NULL);
5256 } else {
5257 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005258 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005259 }
5260 req->analysers = 0;
5261 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5262
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005263 s->fe->fe_counters.srv_aborts++;
5264 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005265 if (objt_server(s->target))
5266 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005267
5268 if (!(s->flags & SN_ERR_MASK))
5269 s->flags |= SN_ERR_SRVCL;
5270 if (!(s->flags & SN_FINST_MASK)) {
5271 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5272 s->flags |= SN_FINST_H;
5273 else
5274 s->flags |= SN_FINST_D;
5275 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276 return 0;
5277}
5278
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005279/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5280 * processing can continue on next analysers, or zero if it either needs more
5281 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5282 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5283 * when it has nothing left to do, and may remove any analyser when it wants to
5284 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005285 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005286int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005287{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005288 struct http_txn *txn = &s->txn;
5289 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005290 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005291 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005293 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005294
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005295 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 +02005296 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005297 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005298 rep,
5299 rep->rex, rep->wex,
5300 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005301 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005302 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005303
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005304 /*
5305 * Now parse the partial (or complete) lines.
5306 * We will check the response syntax, and also join multi-line
5307 * headers. An index of all the lines will be elaborated while
5308 * parsing.
5309 *
5310 * For the parsing, we use a 28 states FSM.
5311 *
5312 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005313 * rep->buf->p = beginning of response
5314 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5315 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005316 * msg->eol = end of current header or line (LF or CRLF)
5317 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005318 */
5319
Willy Tarreau83e3af02009-12-28 17:39:57 +01005320 /* There's a protected area at the end of the buffer for rewriting
5321 * purposes. We don't want to start to parse the request if the
5322 * protected area is affected, because we may have to move processed
5323 * data later, which is much more complicated.
5324 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005325 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005326 if (unlikely(!channel_reserved(rep))) {
5327 /* some data has still not left the buffer, wake us once that's done */
5328 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5329 goto abort_response;
5330 channel_dont_close(rep);
5331 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005332 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005333 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005334 }
5335
Willy Tarreau379357a2013-06-08 12:55:46 +02005336 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5337 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5338 buffer_slow_realign(rep->buf);
5339
Willy Tarreau9b28e032012-10-12 23:49:43 +02005340 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005341 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005342 }
5343
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005344 /* 1: we might have to print this header in debug mode */
5345 if (unlikely((global.mode & MODE_DEBUG) &&
5346 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005347 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005348 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005349
Willy Tarreau9b28e032012-10-12 23:49:43 +02005350 sol = rep->buf->p;
5351 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005352 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005353
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005354 sol += hdr_idx_first_pos(&txn->hdr_idx);
5355 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005356
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005357 while (cur_idx) {
5358 eol = sol + txn->hdr_idx.v[cur_idx].len;
5359 debug_hdr("srvhdr", s, sol, eol);
5360 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5361 cur_idx = txn->hdr_idx.v[cur_idx].next;
5362 }
5363 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005364
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005365 /*
5366 * Now we quickly check if we have found a full valid response.
5367 * If not so, we check the FD and buffer states before leaving.
5368 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005369 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005370 * responses are checked first.
5371 *
5372 * Depending on whether the client is still there or not, we
5373 * may send an error response back or not. Note that normally
5374 * we should only check for HTTP status there, and check I/O
5375 * errors somewhere else.
5376 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005377
Willy Tarreau655dce92009-11-08 13:10:58 +01005378 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005379 /* Invalid response */
5380 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5381 /* we detected a parsing error. We want to archive this response
5382 * in the dedicated proxy area for later troubleshooting.
5383 */
5384 hdr_response_bad:
5385 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005386 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005387
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005388 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005389 if (objt_server(s->target)) {
5390 objt_server(s->target)->counters.failed_resp++;
5391 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005392 }
Willy Tarreau64648412010-03-05 10:41:54 +01005393 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005394 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005395 rep->analysers = 0;
5396 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005397 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005398 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005399 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005400
5401 if (!(s->flags & SN_ERR_MASK))
5402 s->flags |= SN_ERR_PRXCOND;
5403 if (!(s->flags & SN_FINST_MASK))
5404 s->flags |= SN_FINST_H;
5405
5406 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005407 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005408
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005409 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005410 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005411 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005412 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413 goto hdr_response_bad;
5414 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005415
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005416 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005417 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005418 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005419 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005420 else if (txn->flags & TX_NOT_FIRST)
5421 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005422
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005423 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005424 if (objt_server(s->target)) {
5425 objt_server(s->target)->counters.failed_resp++;
5426 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005427 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005428
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005429 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005430 rep->analysers = 0;
5431 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005432 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005433 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005434 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005435
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005436 if (!(s->flags & SN_ERR_MASK))
5437 s->flags |= SN_ERR_SRVCL;
5438 if (!(s->flags & SN_FINST_MASK))
5439 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005440 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005441 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005442
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005443 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005444 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005445 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005446 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005447 else if (txn->flags & TX_NOT_FIRST)
5448 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005449
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005450 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005451 if (objt_server(s->target)) {
5452 objt_server(s->target)->counters.failed_resp++;
5453 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005454 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005455
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005456 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005457 rep->analysers = 0;
5458 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005459 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005460 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005461 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005462
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 if (!(s->flags & SN_ERR_MASK))
5464 s->flags |= SN_ERR_SRVTO;
5465 if (!(s->flags & SN_FINST_MASK))
5466 s->flags |= SN_FINST_H;
5467 return 0;
5468 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005469
Willy Tarreauf003d372012-11-26 13:35:37 +01005470 /* client abort with an abortonclose */
5471 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5472 s->fe->fe_counters.cli_aborts++;
5473 s->be->be_counters.cli_aborts++;
5474 if (objt_server(s->target))
5475 objt_server(s->target)->counters.cli_aborts++;
5476
5477 rep->analysers = 0;
5478 channel_auto_close(rep);
5479
5480 txn->status = 400;
5481 bi_erase(rep);
5482 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5483
5484 if (!(s->flags & SN_ERR_MASK))
5485 s->flags |= SN_ERR_CLICL;
5486 if (!(s->flags & SN_FINST_MASK))
5487 s->flags |= SN_FINST_H;
5488
5489 /* process_session() will take care of the error */
5490 return 0;
5491 }
5492
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005493 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005494 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005495 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005496 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005497 else if (txn->flags & TX_NOT_FIRST)
5498 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005499
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005500 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005501 if (objt_server(s->target)) {
5502 objt_server(s->target)->counters.failed_resp++;
5503 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005504 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005505
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005506 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005507 rep->analysers = 0;
5508 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005509 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005510 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005511 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005512
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005513 if (!(s->flags & SN_ERR_MASK))
5514 s->flags |= SN_ERR_SRVCL;
5515 if (!(s->flags & SN_FINST_MASK))
5516 s->flags |= SN_FINST_H;
5517 return 0;
5518 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005519
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005520 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005521 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005522 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005523 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005524 else if (txn->flags & TX_NOT_FIRST)
5525 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005526
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005527 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005529 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005530
5531 if (!(s->flags & SN_ERR_MASK))
5532 s->flags |= SN_ERR_CLICL;
5533 if (!(s->flags & SN_FINST_MASK))
5534 s->flags |= SN_FINST_H;
5535
5536 /* process_session() will take care of the error */
5537 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005538 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005539
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005540 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005541 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005542 return 0;
5543 }
5544
5545 /* More interesting part now : we know that we have a complete
5546 * response which at least looks like HTTP. We have an indicator
5547 * of each header's length, so we can parse them quickly.
5548 */
5549
5550 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005551 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005552
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005553 /*
5554 * 1: get the status code
5555 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005556 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005557 if (n < 1 || n > 5)
5558 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005559 /* when the client triggers a 4xx from the server, it's most often due
5560 * to a missing object or permission. These events should be tracked
5561 * because if they happen often, it may indicate a brute force or a
5562 * vulnerability scan.
5563 */
5564 if (n == 4)
5565 session_inc_http_err_ctr(s);
5566
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005567 if (objt_server(s->target))
5568 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005569
Willy Tarreau5b154472009-12-21 20:11:07 +01005570 /* check if the response is HTTP/1.1 or above */
5571 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005572 ((rep->buf->p[5] > '1') ||
5573 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005574 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005575
5576 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005577 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 +01005578
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005579 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005580 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005581
Willy Tarreau9b28e032012-10-12 23:49:43 +02005582 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005583
Willy Tarreau39650402010-03-15 19:44:39 +01005584 /* Adjust server's health based on status code. Note: status codes 501
5585 * and 505 are triggered on demand by client request, so we must not
5586 * count them as server failures.
5587 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005588 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005589 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005590 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005591 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005592 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005593 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005594
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005595 /*
5596 * 2: check for cacheability.
5597 */
5598
5599 switch (txn->status) {
5600 case 200:
5601 case 203:
5602 case 206:
5603 case 300:
5604 case 301:
5605 case 410:
5606 /* RFC2616 @13.4:
5607 * "A response received with a status code of
5608 * 200, 203, 206, 300, 301 or 410 MAY be stored
5609 * by a cache (...) unless a cache-control
5610 * directive prohibits caching."
5611 *
5612 * RFC2616 @9.5: POST method :
5613 * "Responses to this method are not cacheable,
5614 * unless the response includes appropriate
5615 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005616 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005617 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005618 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005619 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5620 break;
5621 default:
5622 break;
5623 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005624
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005625 /*
5626 * 3: we may need to capture headers
5627 */
5628 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005629 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005630 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005631 txn->rsp.cap, s->fe->rsp_cap);
5632
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005633 /* 4: determine the transfer-length.
5634 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5635 * the presence of a message-body in a RESPONSE and its transfer length
5636 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005637 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005638 * All responses to the HEAD request method MUST NOT include a
5639 * message-body, even though the presence of entity-header fields
5640 * might lead one to believe they do. All 1xx (informational), 204
5641 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5642 * message-body. All other responses do include a message-body,
5643 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005644 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005645 * 1. Any response which "MUST NOT" include a message-body (such as the
5646 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5647 * always terminated by the first empty line after the header fields,
5648 * regardless of the entity-header fields present in the message.
5649 *
5650 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5651 * the "chunked" transfer-coding (Section 6.2) is used, the
5652 * transfer-length is defined by the use of this transfer-coding.
5653 * If a Transfer-Encoding header field is present and the "chunked"
5654 * transfer-coding is not present, the transfer-length is defined by
5655 * the sender closing the connection.
5656 *
5657 * 3. If a Content-Length header field is present, its decimal value in
5658 * OCTETs represents both the entity-length and the transfer-length.
5659 * If a message is received with both a Transfer-Encoding header
5660 * field and a Content-Length header field, the latter MUST be ignored.
5661 *
5662 * 4. If the message uses the media type "multipart/byteranges", and
5663 * the transfer-length is not otherwise specified, then this self-
5664 * delimiting media type defines the transfer-length. This media
5665 * type MUST NOT be used unless the sender knows that the recipient
5666 * can parse it; the presence in a request of a Range header with
5667 * multiple byte-range specifiers from a 1.1 client implies that the
5668 * client can parse multipart/byteranges responses.
5669 *
5670 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005671 */
5672
5673 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005674 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005675 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005676 * FIXME: should we parse anyway and return an error on chunked encoding ?
5677 */
5678 if (txn->meth == HTTP_METH_HEAD ||
5679 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005680 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005681 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005682 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005683 goto skip_content_length;
5684 }
5685
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005686 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005687 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005688 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005689 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005690 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005691 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5692 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005693 /* bad transfer-encoding (chunked followed by something else) */
5694 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005695 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005696 break;
5697 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005698 }
5699
5700 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5701 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005702 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005703 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005704 signed long long cl;
5705
Willy Tarreauad14f752011-09-02 20:33:27 +02005706 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005707 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005708 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005709 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005710
Willy Tarreauad14f752011-09-02 20:33:27 +02005711 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005712 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005713 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005714 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005715
Willy Tarreauad14f752011-09-02 20:33:27 +02005716 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005717 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005718 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005719 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005720
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005721 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005722 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005723 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005724 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005725
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005726 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005727 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005728 }
5729
William Lallemand82fe75c2012-10-23 10:25:10 +02005730 if (s->fe->comp || s->be->comp)
5731 select_compression_response_header(s, rep->buf);
5732
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005733 /* FIXME: we should also implement the multipart/byterange method.
5734 * For now on, we resort to close mode in this case (unknown length).
5735 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005736skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005737
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005738 /* end of job, return OK */
5739 rep->analysers &= ~an_bit;
5740 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005741 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005742 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005743
5744 abort_keep_alive:
5745 /* A keep-alive request to the server failed on a network error.
5746 * The client is required to retry. We need to close without returning
5747 * any other information so that the client retries.
5748 */
5749 txn->status = 0;
5750 rep->analysers = 0;
5751 s->req->analysers = 0;
5752 channel_auto_close(rep);
5753 s->logs.logwait = 0;
5754 s->logs.level = 0;
5755 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5756 bi_erase(rep);
5757 stream_int_retnclose(rep->cons, NULL);
5758 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005759}
5760
5761/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005762 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5763 * and updates t->rep->analysers. It might make sense to explode it into several
5764 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005765 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005766int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005767{
5768 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005769 struct http_msg *msg = &txn->rsp;
5770 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005771 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005772 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005773
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005774 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005775 now_ms, __FUNCTION__,
5776 t,
5777 rep,
5778 rep->rex, rep->wex,
5779 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005780 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005781 rep->analysers);
5782
Willy Tarreau655dce92009-11-08 13:10:58 +01005783 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005784 return 0;
5785
5786 rep->analysers &= ~an_bit;
5787 rep->analyse_exp = TICK_ETERNITY;
5788
Willy Tarreau5b154472009-12-21 20:11:07 +01005789 /* Now we have to check if we need to modify the Connection header.
5790 * This is more difficult on the response than it is on the request,
5791 * because we can have two different HTTP versions and we don't know
5792 * how the client will interprete a response. For instance, let's say
5793 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5794 * HTTP/1.1 response without any header. Maybe it will bound itself to
5795 * HTTP/1.0 because it only knows about it, and will consider the lack
5796 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5797 * the lack of header as a keep-alive. Thus we will use two flags
5798 * indicating how a request MAY be understood by the client. In case
5799 * of multiple possibilities, we'll fix the header to be explicit. If
5800 * ambiguous cases such as both close and keepalive are seen, then we
5801 * will fall back to explicit close. Note that we won't take risks with
5802 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005803 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005804 */
5805
Willy Tarreaudc008c52010-02-01 16:20:08 +01005806 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5807 txn->status == 101)) {
5808 /* Either we've established an explicit tunnel, or we're
5809 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005810 * to understand the next protocols. We have to switch to tunnel
5811 * mode, so that we transfer the request and responses then let
5812 * this protocol pass unmodified. When we later implement specific
5813 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005814 * header which contains information about that protocol for
5815 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005816 */
5817 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5818 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005819 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5820 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005821 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5822 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005823 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005824
Willy Tarreau70dffda2014-01-30 03:07:23 +01005825 /* this situation happens when combining pretend-keepalive with httpclose. */
5826 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5827 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5828 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5829 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5830
Willy Tarreau60466522010-01-18 19:08:45 +01005831 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005832 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005833 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5834 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005835
Willy Tarreau60466522010-01-18 19:08:45 +01005836 /* now adjust header transformations depending on current state */
5837 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5838 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5839 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005840 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005841 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005842 }
Willy Tarreau60466522010-01-18 19:08:45 +01005843 else { /* SCL / KAL */
5844 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005845 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005846 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005847 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005848
Willy Tarreau60466522010-01-18 19:08:45 +01005849 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005850 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005851
Willy Tarreau60466522010-01-18 19:08:45 +01005852 /* Some keep-alive responses are converted to Server-close if
5853 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005854 */
Willy Tarreau60466522010-01-18 19:08:45 +01005855 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5856 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005857 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005858 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005859 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005860 }
5861
Willy Tarreau7959a552013-09-23 16:44:27 +02005862 /* we want to have the response time before we start processing it */
5863 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5864
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005866 /*
5867 * 3: we will have to evaluate the filters.
5868 * As opposed to version 1.2, now they will be evaluated in the
5869 * filters order and not in the header order. This means that
5870 * each filter has to be validated among all headers.
5871 *
5872 * Filters are tried with ->be first, then with ->fe if it is
5873 * different from ->be.
5874 */
5875
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876 cur_proxy = t->be;
5877 while (1) {
5878 struct proxy *rule_set = cur_proxy;
5879
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005880 /* evaluate http-response rules */
5881 if (!http_res_last_rule)
5882 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005884 /* try headers filters */
5885 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005886 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005887 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005888 if (objt_server(t->target)) {
5889 objt_server(t->target)->counters.failed_resp++;
5890 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005891 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005892 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005893 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005894 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005895 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005896 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005897 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005898 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005899 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005900 if (!(t->flags & SN_ERR_MASK))
5901 t->flags |= SN_ERR_PRXCOND;
5902 if (!(t->flags & SN_FINST_MASK))
5903 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005904 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005905 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005906 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005907
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005908 /* has the response been denied ? */
5909 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005910 if (objt_server(t->target))
5911 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005912
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005913 t->be->be_counters.denied_resp++;
5914 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005915 if (t->listener->counters)
5916 t->listener->counters->denied_resp++;
5917
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005918 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005919 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005920
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005921 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005922 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005923 if (txn->status < 200)
5924 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005925 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005926 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005927 ret = acl_pass(ret);
5928 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5929 ret = !ret;
5930 if (!ret)
5931 continue;
5932 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005933 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005934 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005935 }
5936
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937 /* check whether we're already working on the frontend */
5938 if (cur_proxy == t->fe)
5939 break;
5940 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005941 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005942
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005943 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005944 * We may be facing a 100-continue response, in which case this
5945 * is not the right response, and we're waiting for the next one.
5946 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005947 * next one.
5948 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005949 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005950 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005951 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005952 msg->msg_state = HTTP_MSG_RPBEFORE;
5953 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005954 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005955 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5956 return 1;
5957 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005958 else if (unlikely(txn->status < 200))
5959 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005960
5961 /* we don't have any 1xx status code now */
5962
5963 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005964 * 4: check for server cookie.
5965 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005966 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5967 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005968 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005969
Willy Tarreaubaaee002006-06-26 02:48:02 +02005970
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005972 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005973 */
Willy Tarreau67402132012-05-31 20:40:20 +02005974 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005975 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005977 /*
5978 * 6: add server cookie in the response if needed
5979 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005980 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005981 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005982 (!(t->flags & SN_DIRECT) ||
5983 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5984 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5985 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5986 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005987 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005988 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005989 /* the server is known, it's not the one the client requested, or the
5990 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005991 * insert a set-cookie here, except if we want to insert only on POST
5992 * requests and this one isn't. Note that servers which don't have cookies
5993 * (eg: some backup servers) will return a full cookie removal request.
5994 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005995 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005996 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005997 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5998 t->be->cookie_name);
5999 }
6000 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006001 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006002
6003 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
6004 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006005 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6006 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6007 trash.len += 5;
6008
Willy Tarreauef4f3912010-10-07 21:00:29 +02006009 if (t->be->cookie_maxlife) {
6010 /* emit first_date, which is either the original one or
6011 * the current date.
6012 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006013 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006014 s30tob64(txn->cookie_first_date ?
6015 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006016 (date.tv_sec+3) >> 2, trash.str + trash.len);
6017 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006018 }
6019 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006020 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02006021 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006022
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006023 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006024 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006025
Willy Tarreau4992dd22012-05-31 21:02:17 +02006026 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006027 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006028
6029 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006030 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006031
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006032 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006033 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006034
Willy Tarreauf1348312010-10-07 15:54:11 +02006035 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006036 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02006037 /* the server did not change, only the date was updated */
6038 txn->flags |= TX_SCK_UPDATED;
6039 else
6040 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006041
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006042 /* Here, we will tell an eventual cache on the client side that we don't
6043 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6044 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6045 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
6046 */
Willy Tarreau67402132012-05-31 20:40:20 +02006047 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02006048
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006049 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6050
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006051 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01006052 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006053 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006054 }
6055 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006056
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006057 /*
6058 * 7: check if result will be cacheable with a cookie.
6059 * We'll block the response if security checks have caught
6060 * nasty things such as a cacheable cookie.
6061 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006062 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6063 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006064 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006065
6066 /* we're in presence of a cacheable response containing
6067 * a set-cookie header. We'll block it as requested by
6068 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006069 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006070 if (objt_server(t->target))
6071 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006072
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006073 t->be->be_counters.denied_resp++;
6074 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006075 if (t->listener->counters)
6076 t->listener->counters->denied_resp++;
6077
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006078 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006079 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006080 send_log(t->be, LOG_ALERT,
6081 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006082 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006083 goto return_srv_prx_502;
6084 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006085
6086 /*
Willy Tarreau60466522010-01-18 19:08:45 +01006087 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01006088 * If an "Upgrade" token is found, the header is left untouched in order
6089 * not to have to deal with some client bugs : some of them fail an upgrade
6090 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006091 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006092 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6093 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01006094 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6095 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006096 unsigned int want_flags = 0;
6097
6098 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6099 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6100 /* we want a keep-alive response here. Keep-alive header
6101 * required if either side is not 1.1.
6102 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006103 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006104 want_flags |= TX_CON_KAL_SET;
6105 }
6106 else {
6107 /* we want a close response here. Close header required if
6108 * the server is 1.1, regardless of the client.
6109 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006110 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006111 want_flags |= TX_CON_CLO_SET;
6112 }
6113
6114 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006115 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006116 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006117
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006118 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006119 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006120 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006121 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006122
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006123 /*************************************************************
6124 * OK, that's finished for the headers. We have done what we *
6125 * could. Let's switch to the DATA state. *
6126 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006127
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006128 /* if the user wants to log as soon as possible, without counting
6129 * bytes from the server, then this is the right moment. We have
6130 * to temporarily assign bytes_out to log what we currently have.
6131 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006132 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006133 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6134 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006135 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006136 t->logs.bytes_out = 0;
6137 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006138
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006139 /* Note: we must not try to cheat by jumping directly to DATA,
6140 * otherwise we would not let the client side wake up.
6141 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006142
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006143 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006144 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006145 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006146}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006147
Willy Tarreaud98cf932009-12-27 22:54:55 +01006148/* This function is an analyser which forwards response body (including chunk
6149 * sizes if any). It is called as soon as we must forward, even if we forward
6150 * zero byte. The only situation where it must not be called is when we're in
6151 * tunnel mode and we want to forward till the close. It's used both to forward
6152 * remaining data and to resync after end of body. It expects the msg_state to
6153 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6154 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006155 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006156 * bytes of pending data + the headers if not already done (between sol and sov).
6157 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006158 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006159int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006160{
6161 struct http_txn *txn = &s->txn;
6162 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02006163 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02006164 static struct buffer *tmpbuf = NULL;
6165 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006166 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006167 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006168
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006169 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6170 return 0;
6171
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006172 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006173 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006174 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006175 /* Output closed while we were sending data. We must abort and
6176 * wake the other side up.
6177 */
6178 msg->msg_state = HTTP_MSG_ERROR;
6179 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006180 return 1;
6181 }
6182
Willy Tarreau4fe41902010-06-07 22:27:41 +02006183 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006184 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006185
William Lallemand82fe75c2012-10-23 10:25:10 +02006186 /* this is the first time we need the compression buffer */
6187 if (s->comp_algo != NULL && tmpbuf == NULL) {
6188 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6189 goto aborted_xfer; /* no memory */
6190 }
6191
Willy Tarreaud98cf932009-12-27 22:54:55 +01006192 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006193 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006194 * rep->buf.p still points to the beginning of the message and msg->sol
6195 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006196 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006197 channel_forward(res, msg->sov);
6198 msg->next = 0;
6199 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006200
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006201 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006202 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006203 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006204 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006205 }
6206
William Lallemand82fe75c2012-10-23 10:25:10 +02006207 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006208 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006209 if (ret < 0) {
6210 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006211 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006212 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006213 compressing = 1;
6214 }
6215
Willy Tarreaud98cf932009-12-27 22:54:55 +01006216 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006217 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006218 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006219 if (s->comp_algo == NULL) {
6220 bytes = msg->sov - msg->sol;
6221 if (msg->chunk_len || bytes) {
6222 msg->sol = msg->sov;
6223 msg->next -= bytes; /* will be forwarded */
6224 msg->chunk_len += bytes;
6225 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6226 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006227 }
6228
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006229 switch (msg->msg_state - HTTP_MSG_DATA) {
6230 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006231 if (compressing) {
6232 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6233 if (ret < 0)
6234 goto aborted_xfer;
6235 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006236
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006237 if (res->to_forward || msg->chunk_len) {
6238 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006239 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006240 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006241
6242 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006243 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006244 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006245 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006246 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006247 if (compressing && consumed_data) {
6248 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6249 compressing = 0;
6250 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006251 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006252 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006253 /* fall through for HTTP_MSG_CHUNK_CRLF */
6254
6255 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6256 /* we want the CRLF after the data */
6257
6258 ret = http_skip_chunk_crlf(msg);
6259 if (ret == 0)
6260 goto missing_data;
6261 else if (ret < 0) {
6262 if (msg->err_pos >= 0)
6263 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6264 goto return_bad_res;
6265 }
6266 /* skipping data in buffer for compression */
6267 if (compressing) {
6268 b_adv(res->buf, msg->next);
6269 msg->next = 0;
6270 msg->sov = 0;
6271 msg->sol = 0;
6272 }
6273 /* we're in MSG_CHUNK_SIZE now, fall through */
6274
6275 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006276 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006277 * set ->sov and ->next to point to the body and switch to DATA or
6278 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006279 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006280
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006281 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006282 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006283 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006284 else if (ret < 0) {
6285 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006286 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006287 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006288 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006289 if (compressing) {
6290 if (likely(msg->chunk_len > 0)) {
6291 /* skipping data if we are in compression mode */
6292 b_adv(res->buf, msg->next);
6293 msg->next = 0;
6294 msg->sov = 0;
6295 msg->sol = 0;
6296 } else {
6297 if (consumed_data) {
6298 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6299 compressing = 0;
6300 }
6301 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006302 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006303 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006304 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006305
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006306 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6307 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006308 if (ret == 0)
6309 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006310 else if (ret < 0) {
6311 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006312 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006313 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006314 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006315 if (s->comp_algo != NULL) {
6316 /* forwarding trailers */
6317 channel_forward(res, msg->next);
6318 msg->next = 0;
6319 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006320 /* we're in HTTP_MSG_DONE now, but we might still have
6321 * some data pending, so let's loop over once.
6322 */
6323 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006324
6325 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006326 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006327
6328 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006329 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006330 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6331 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006332 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006333 if (http_resync_states(s)) {
6334 http_silent_debug(__LINE__, s);
6335 /* some state changes occurred, maybe the analyser
6336 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006337 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006338 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006339 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006340 /* response errors are most likely due to
6341 * the client aborting the transfer.
6342 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006343 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006344 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006345 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006346 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006347 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006348 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006349 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006350 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006351 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006352 }
6353 }
6354
Willy Tarreaud98cf932009-12-27 22:54:55 +01006355 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006356 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006357 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6358 compressing = 0;
6359 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006360
6361 if (res->flags & CF_SHUTW)
6362 goto aborted_xfer;
6363
6364 /* stop waiting for data if the input is closed before the end. If the
6365 * client side was already closed, it means that the client has aborted,
6366 * so we don't want to count this as a server abort. Otherwise it's a
6367 * server abort.
6368 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006369 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006370 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6371 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006372 if (!(s->flags & SN_ERR_MASK))
6373 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006374 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006375 if (objt_server(s->target))
6376 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006377 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006378 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006379
Willy Tarreau40dba092010-03-04 18:14:51 +01006380 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006381 if (!s->req->analysers)
6382 goto return_bad_res;
6383
Willy Tarreauea953162012-05-18 23:41:28 +02006384 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006385 if (s->comp_algo == NULL) {
6386 bytes = msg->sov - msg->sol;
6387 if (msg->chunk_len || bytes) {
6388 msg->sol = msg->sov;
6389 msg->next -= bytes; /* will be forwarded */
6390 msg->chunk_len += bytes;
6391 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6392 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006393 }
6394
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006395 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006396 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006397 * Similarly, with keep-alive on the client side, we don't want to forward a
6398 * close.
6399 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006400 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006401 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6402 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006403 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006404
Willy Tarreau5c620922011-05-11 19:56:11 +02006405 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006406 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006407 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006408 * modes are already handled by the stream sock layer. We must not do
6409 * this in content-length mode because it could present the MSG_MORE
6410 * flag with the last block of forwarded data, which would cause an
6411 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006412 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006413 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006414 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006415
Willy Tarreaud98cf932009-12-27 22:54:55 +01006416 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006417 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006418 return 0;
6419
Willy Tarreau40dba092010-03-04 18:14:51 +01006420 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006421 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006422 if (objt_server(s->target))
6423 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006424
6425 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006426 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006427 /* don't send any error message as we're in the body */
6428 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006429 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006430 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006431 if (objt_server(s->target))
6432 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006433
6434 if (!(s->flags & SN_ERR_MASK))
6435 s->flags |= SN_ERR_PRXCOND;
6436 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006437 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006438 return 0;
6439
6440 aborted_xfer:
6441 txn->rsp.msg_state = HTTP_MSG_ERROR;
6442 /* don't send any error message as we're in the body */
6443 stream_int_retnclose(res->cons, NULL);
6444 res->analysers = 0;
6445 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6446
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006447 s->fe->fe_counters.cli_aborts++;
6448 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006449 if (objt_server(s->target))
6450 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006451
6452 if (!(s->flags & SN_ERR_MASK))
6453 s->flags |= SN_ERR_CLICL;
6454 if (!(s->flags & SN_FINST_MASK))
6455 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006456 return 0;
6457}
6458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006459/* Iterate the same filter through all request headers.
6460 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006461 * Since it can manage the switch to another backend, it updates the per-proxy
6462 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006464int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006465{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 char term;
6467 char *cur_ptr, *cur_end, *cur_next;
6468 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006469 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006470 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006471 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006473 last_hdr = 0;
6474
Willy Tarreau9b28e032012-10-12 23:49:43 +02006475 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 old_idx = 0;
6477
6478 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006481 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006482 (exp->action == ACT_ALLOW ||
6483 exp->action == ACT_DENY ||
6484 exp->action == ACT_TARPIT))
6485 return 0;
6486
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006487 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 if (!cur_idx)
6489 break;
6490
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006491 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006492 cur_ptr = cur_next;
6493 cur_end = cur_ptr + cur_hdr->len;
6494 cur_next = cur_end + cur_hdr->cr + 1;
6495
6496 /* Now we have one header between cur_ptr and cur_end,
6497 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006498 */
6499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006500 /* The annoying part is that pattern matching needs
6501 * that we modify the contents to null-terminate all
6502 * strings before testing them.
6503 */
6504
6505 term = *cur_end;
6506 *cur_end = '\0';
6507
6508 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6509 switch (exp->action) {
6510 case ACT_SETBE:
6511 /* It is not possible to jump a second time.
6512 * FIXME: should we return an HTTP/500 here so that
6513 * the admin knows there's a problem ?
6514 */
6515 if (t->be != t->fe)
6516 break;
6517
6518 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006519 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 last_hdr = 1;
6521 break;
6522
6523 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006524 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 last_hdr = 1;
6526 break;
6527
6528 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006529 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 break;
6532
6533 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006534 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006535 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 break;
6537
6538 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006539 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6540 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 /* FIXME: if the user adds a newline in the replacement, the
6542 * index will not be recalculated for now, and the new line
6543 * will not be counted as a new header.
6544 */
6545
6546 cur_end += delta;
6547 cur_next += delta;
6548 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006549 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 break;
6551
6552 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006553 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006554 cur_next += delta;
6555
Willy Tarreaufa355d42009-11-29 18:12:29 +01006556 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006557 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6558 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006559 cur_hdr->len = 0;
6560 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006561 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562 break;
6563
6564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566 if (cur_end)
6567 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 /* keep the link from this header to next one in case of later
6570 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006572 old_idx = cur_idx;
6573 }
6574 return 0;
6575}
6576
6577
6578/* Apply the filter to the request line.
6579 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6580 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006581 * Since it can manage the switch to another backend, it updates the per-proxy
6582 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006583 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006584int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006585{
6586 char term;
6587 char *cur_ptr, *cur_end;
6588 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006589 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006590 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006591
Willy Tarreau3d300592007-03-18 18:34:41 +01006592 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006593 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006594 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 (exp->action == ACT_ALLOW ||
6596 exp->action == ACT_DENY ||
6597 exp->action == ACT_TARPIT))
6598 return 0;
6599 else if (exp->action == ACT_REMOVE)
6600 return 0;
6601
6602 done = 0;
6603
Willy Tarreau9b28e032012-10-12 23:49:43 +02006604 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006605 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006606
6607 /* Now we have the request line between cur_ptr and cur_end */
6608
6609 /* The annoying part is that pattern matching needs
6610 * that we modify the contents to null-terminate all
6611 * strings before testing them.
6612 */
6613
6614 term = *cur_end;
6615 *cur_end = '\0';
6616
6617 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6618 switch (exp->action) {
6619 case ACT_SETBE:
6620 /* It is not possible to jump a second time.
6621 * FIXME: should we return an HTTP/500 here so that
6622 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006623 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006624 if (t->be != t->fe)
6625 break;
6626
6627 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006628 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006629 done = 1;
6630 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006632 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006633 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006634 done = 1;
6635 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006637 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006638 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006639 done = 1;
6640 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006642 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006643 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006644 done = 1;
6645 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006647 case ACT_REPLACE:
6648 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006649 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6650 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006651 /* FIXME: if the user adds a newline in the replacement, the
6652 * index will not be recalculated for now, and the new line
6653 * will not be counted as a new header.
6654 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006655
Willy Tarreaufa355d42009-11-29 18:12:29 +01006656 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006657 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006658 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 HTTP_MSG_RQMETH,
6660 cur_ptr, cur_end + 1,
6661 NULL, NULL);
6662 if (unlikely(!cur_end))
6663 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006664
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006665 /* we have a full request and we know that we have either a CR
6666 * or an LF at <ptr>.
6667 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006668 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6669 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006670 /* there is no point trying this regex on headers */
6671 return 1;
6672 }
6673 }
6674 *cur_end = term; /* restore the string terminator */
6675 return done;
6676}
Willy Tarreau97de6242006-12-27 17:18:38 +01006677
Willy Tarreau58f10d72006-12-04 02:26:12 +01006678
Willy Tarreau58f10d72006-12-04 02:26:12 +01006679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006680/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006681 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006682 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006683 * unparsable request. Since it can manage the switch to another backend, it
6684 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006685 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006686int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006687{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006688 struct http_txn *txn = &s->txn;
6689 struct hdr_exp *exp;
6690
6691 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006692 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006694 /*
6695 * The interleaving of transformations and verdicts
6696 * makes it difficult to decide to continue or stop
6697 * the evaluation.
6698 */
6699
Willy Tarreau6c123b12010-01-28 20:22:06 +01006700 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6701 break;
6702
Willy Tarreau3d300592007-03-18 18:34:41 +01006703 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006704 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006705 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006706 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006707
6708 /* if this filter had a condition, evaluate it now and skip to
6709 * next filter if the condition does not match.
6710 */
6711 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006712 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006713 ret = acl_pass(ret);
6714 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6715 ret = !ret;
6716
6717 if (!ret)
6718 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006719 }
6720
6721 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006722 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006723 if (unlikely(ret < 0))
6724 return -1;
6725
6726 if (likely(ret == 0)) {
6727 /* The filter did not match the request, it can be
6728 * iterated through all headers.
6729 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006730 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006731 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006732 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006733 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734}
6735
6736
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006739 * Try to retrieve the server associated to the appsession.
6740 * If the server is found, it's assigned to the session.
6741 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006742void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006743 struct http_txn *txn = &t->txn;
6744 appsess *asession = NULL;
6745 char *sessid_temp = NULL;
6746
Cyril Bontéb21570a2009-11-29 20:04:48 +01006747 if (len > t->be->appsession_len) {
6748 len = t->be->appsession_len;
6749 }
6750
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006751 if (t->be->options2 & PR_O2_AS_REQL) {
6752 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006753 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006754 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006755 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006756 }
6757
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006758 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006759 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6760 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6761 return;
6762 }
6763
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006764 memcpy(txn->sessid, buf, len);
6765 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006766 }
6767
6768 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6769 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6770 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6771 return;
6772 }
6773
Cyril Bontéb21570a2009-11-29 20:04:48 +01006774 memcpy(sessid_temp, buf, len);
6775 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006776
6777 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6778 /* free previously allocated memory */
6779 pool_free2(apools.sessid, sessid_temp);
6780
6781 if (asession != NULL) {
6782 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6783 if (!(t->be->options2 & PR_O2_AS_REQL))
6784 asession->request_count++;
6785
6786 if (asession->serverid != NULL) {
6787 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006788
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006789 while (srv) {
6790 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006791 if ((srv->state & SRV_RUNNING) ||
6792 (t->be->options & PR_O_PERSIST) ||
6793 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006794 /* we found the server and it's usable */
6795 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006796 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006797 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006798 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006799
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006800 break;
6801 } else {
6802 txn->flags &= ~TX_CK_MASK;
6803 txn->flags |= TX_CK_DOWN;
6804 }
6805 }
6806 srv = srv->next;
6807 }
6808 }
6809 }
6810}
6811
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006812/* Find the end of a cookie value contained between <s> and <e>. It works the
6813 * same way as with headers above except that the semi-colon also ends a token.
6814 * See RFC2965 for more information. Note that it requires a valid header to
6815 * return a valid result.
6816 */
6817char *find_cookie_value_end(char *s, const char *e)
6818{
6819 int quoted, qdpair;
6820
6821 quoted = qdpair = 0;
6822 for (; s < e; s++) {
6823 if (qdpair) qdpair = 0;
6824 else if (quoted) {
6825 if (*s == '\\') qdpair = 1;
6826 else if (*s == '"') quoted = 0;
6827 }
6828 else if (*s == '"') quoted = 1;
6829 else if (*s == ',' || *s == ';') return s;
6830 }
6831 return s;
6832}
6833
6834/* Delete a value in a header between delimiters <from> and <next> in buffer
6835 * <buf>. The number of characters displaced is returned, and the pointer to
6836 * the first delimiter is updated if required. The function tries as much as
6837 * possible to respect the following principles :
6838 * - replace <from> delimiter by the <next> one unless <from> points to a
6839 * colon, in which case <next> is simply removed
6840 * - set exactly one space character after the new first delimiter, unless
6841 * there are not enough characters in the block being moved to do so.
6842 * - remove unneeded spaces before the previous delimiter and after the new
6843 * one.
6844 *
6845 * It is the caller's responsibility to ensure that :
6846 * - <from> points to a valid delimiter or the colon ;
6847 * - <next> points to a valid delimiter or the final CR/LF ;
6848 * - there are non-space chars before <from> ;
6849 * - there is a CR/LF at or after <next>.
6850 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006851int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006852{
6853 char *prev = *from;
6854
6855 if (*prev == ':') {
6856 /* We're removing the first value, preserve the colon and add a
6857 * space if possible.
6858 */
6859 if (!http_is_crlf[(unsigned char)*next])
6860 next++;
6861 prev++;
6862 if (prev < next)
6863 *prev++ = ' ';
6864
6865 while (http_is_spht[(unsigned char)*next])
6866 next++;
6867 } else {
6868 /* Remove useless spaces before the old delimiter. */
6869 while (http_is_spht[(unsigned char)*(prev-1)])
6870 prev--;
6871 *from = prev;
6872
6873 /* copy the delimiter and if possible a space if we're
6874 * not at the end of the line.
6875 */
6876 if (!http_is_crlf[(unsigned char)*next]) {
6877 *prev++ = *next++;
6878 if (prev + 1 < next)
6879 *prev++ = ' ';
6880 while (http_is_spht[(unsigned char)*next])
6881 next++;
6882 }
6883 }
6884 return buffer_replace2(buf, prev, next, NULL, 0);
6885}
6886
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006887/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006888 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 * desirable to call it only when needed. This code is quite complex because
6890 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6891 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006892 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006893void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006895 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006897 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6899 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006902 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006903 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006904
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006905 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006907 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006909 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006910 hdr_beg = hdr_next;
6911 hdr_end = hdr_beg + cur_hdr->len;
6912 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006913
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 /* We have one full header between hdr_beg and hdr_end, and the
6915 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006916 * "Cookie:" headers.
6917 */
6918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006920 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921 old_idx = cur_idx;
6922 continue;
6923 }
6924
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006925 del_from = NULL; /* nothing to be deleted */
6926 preserve_hdr = 0; /* assume we may kill the whole header */
6927
Willy Tarreau58f10d72006-12-04 02:26:12 +01006928 /* Now look for cookies. Conforming to RFC2109, we have to support
6929 * attributes whose name begin with a '$', and associate them with
6930 * the right cookie, if we want to delete this cookie.
6931 * So there are 3 cases for each cookie read :
6932 * 1) it's a special attribute, beginning with a '$' : ignore it.
6933 * 2) it's a server id cookie that we *MAY* want to delete : save
6934 * some pointers on it (last semi-colon, beginning of cookie...)
6935 * 3) it's an application cookie : we *MAY* have to delete a previous
6936 * "special" cookie.
6937 * At the end of loop, if a "special" cookie remains, we may have to
6938 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006939 * *MUST* delete it.
6940 *
6941 * Note: RFC2965 is unclear about the processing of spaces around
6942 * the equal sign in the ATTR=VALUE form. A careful inspection of
6943 * the RFC explicitly allows spaces before it, and not within the
6944 * tokens (attrs or values). An inspection of RFC2109 allows that
6945 * too but section 10.1.3 lets one think that spaces may be allowed
6946 * after the equal sign too, resulting in some (rare) buggy
6947 * implementations trying to do that. So let's do what servers do.
6948 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6949 * allowed quoted strings in values, with any possible character
6950 * after a backslash, including control chars and delimitors, which
6951 * causes parsing to become ambiguous. Browsers also allow spaces
6952 * within values even without quotes.
6953 *
6954 * We have to keep multiple pointers in order to support cookie
6955 * removal at the beginning, middle or end of header without
6956 * corrupting the header. All of these headers are valid :
6957 *
6958 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6959 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6960 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6961 * | | | | | | | | |
6962 * | | | | | | | | hdr_end <--+
6963 * | | | | | | | +--> next
6964 * | | | | | | +----> val_end
6965 * | | | | | +-----------> val_beg
6966 * | | | | +--------------> equal
6967 * | | | +----------------> att_end
6968 * | | +---------------------> att_beg
6969 * | +--------------------------> prev
6970 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006971 */
6972
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006973 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6974 /* Iterate through all cookies on this line */
6975
6976 /* find att_beg */
6977 att_beg = prev + 1;
6978 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6979 att_beg++;
6980
6981 /* find att_end : this is the first character after the last non
6982 * space before the equal. It may be equal to hdr_end.
6983 */
6984 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006986 while (equal < hdr_end) {
6987 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006988 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006989 if (http_is_spht[(unsigned char)*equal++])
6990 continue;
6991 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006992 }
6993
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6995 * is between <att_beg> and <equal>, both may be identical.
6996 */
6997
6998 /* look for end of cookie if there is an equal sign */
6999 if (equal < hdr_end && *equal == '=') {
7000 /* look for the beginning of the value */
7001 val_beg = equal + 1;
7002 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7003 val_beg++;
7004
7005 /* find the end of the value, respecting quotes */
7006 next = find_cookie_value_end(val_beg, hdr_end);
7007
7008 /* make val_end point to the first white space or delimitor after the value */
7009 val_end = next;
7010 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7011 val_end--;
7012 } else {
7013 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007014 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007016 /* We have nothing to do with attributes beginning with '$'. However,
7017 * they will automatically be removed if a header before them is removed,
7018 * since they're supposed to be linked together.
7019 */
7020 if (*att_beg == '$')
7021 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007022
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007023 /* Ignore cookies with no equal sign */
7024 if (equal == next) {
7025 /* This is not our cookie, so we must preserve it. But if we already
7026 * scheduled another cookie for removal, we cannot remove the
7027 * complete header, but we can remove the previous block itself.
7028 */
7029 preserve_hdr = 1;
7030 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007031 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007032 val_end += delta;
7033 next += delta;
7034 hdr_end += delta;
7035 hdr_next += delta;
7036 cur_hdr->len += delta;
7037 http_msg_move_end(&txn->req, delta);
7038 prev = del_from;
7039 del_from = NULL;
7040 }
7041 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007042 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007044 /* if there are spaces around the equal sign, we need to
7045 * strip them otherwise we'll get trouble for cookie captures,
7046 * or even for rewrites. Since this happens extremely rarely,
7047 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007049 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7050 int stripped_before = 0;
7051 int stripped_after = 0;
7052
7053 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007054 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 equal += stripped_before;
7056 val_beg += stripped_before;
7057 }
7058
7059 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007060 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007061 val_beg += stripped_after;
7062 stripped_before += stripped_after;
7063 }
7064
7065 val_end += stripped_before;
7066 next += stripped_before;
7067 hdr_end += stripped_before;
7068 hdr_next += stripped_before;
7069 cur_hdr->len += stripped_before;
7070 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007074 /* First, let's see if we want to capture this cookie. We check
7075 * that we don't already have a client side cookie, because we
7076 * can only capture one. Also as an optimisation, we ignore
7077 * cookies shorter than the declared name.
7078 */
7079 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7080 (val_end - att_beg >= t->fe->capture_namelen) &&
7081 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7082 int log_len = val_end - att_beg;
7083
7084 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7085 Alert("HTTP logging : out of memory.\n");
7086 } else {
7087 if (log_len > t->fe->capture_len)
7088 log_len = t->fe->capture_len;
7089 memcpy(txn->cli_cookie, att_beg, log_len);
7090 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093
Willy Tarreaubca99692010-10-06 19:25:55 +02007094 /* Persistence cookies in passive, rewrite or insert mode have the
7095 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007096 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007097 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007098 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007099 * For cookies in prefix mode, the form is :
7100 *
7101 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007102 */
7103 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7104 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
7105 struct server *srv = t->be->srv;
7106 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7109 * have the server ID between val_beg and delim, and the original cookie between
7110 * delim+1 and val_end. Otherwise, delim==val_end :
7111 *
7112 * Cookie: NAME=SRV; # in all but prefix modes
7113 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7114 * | || || | |+-> next
7115 * | || || | +--> val_end
7116 * | || || +---------> delim
7117 * | || |+------------> val_beg
7118 * | || +-------------> att_end = equal
7119 * | |+-----------------> att_beg
7120 * | +------------------> prev
7121 * +-------------------------> hdr_beg
7122 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123
Willy Tarreau67402132012-05-31 20:40:20 +02007124 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007125 for (delim = val_beg; delim < val_end; delim++)
7126 if (*delim == COOKIE_DELIM)
7127 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007128 } else {
7129 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007130 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007131 /* Now check if the cookie contains a date field, which would
7132 * appear after a vertical bar ('|') just after the server name
7133 * and before the delimiter.
7134 */
7135 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7136 if (vbar1) {
7137 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007138 * right is the last seen date. It is a base64 encoded
7139 * 30-bit value representing the UNIX date since the
7140 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007141 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007142 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007143 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007144 if (val_end - vbar1 >= 5) {
7145 val = b64tos30(vbar1);
7146 if (val > 0)
7147 txn->cookie_last_date = val << 2;
7148 }
7149 /* look for a second vertical bar */
7150 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7151 if (vbar1 && (val_end - vbar1 > 5)) {
7152 val = b64tos30(vbar1 + 1);
7153 if (val > 0)
7154 txn->cookie_first_date = val << 2;
7155 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007156 }
7157 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007158
Willy Tarreauf64d1412010-10-07 20:06:11 +02007159 /* if the cookie has an expiration date and the proxy wants to check
7160 * it, then we do that now. We first check if the cookie is too old,
7161 * then only if it has expired. We detect strict overflow because the
7162 * time resolution here is not great (4 seconds). Cookies with dates
7163 * in the future are ignored if their offset is beyond one day. This
7164 * allows an admin to fix timezone issues without expiring everyone
7165 * and at the same time avoids keeping unwanted side effects for too
7166 * long.
7167 */
7168 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007169 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7170 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007171 txn->flags &= ~TX_CK_MASK;
7172 txn->flags |= TX_CK_OLD;
7173 delim = val_beg; // let's pretend we have not found the cookie
7174 txn->cookie_first_date = 0;
7175 txn->cookie_last_date = 0;
7176 }
7177 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007178 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7179 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007180 txn->flags &= ~TX_CK_MASK;
7181 txn->flags |= TX_CK_EXPIRED;
7182 delim = val_beg; // let's pretend we have not found the cookie
7183 txn->cookie_first_date = 0;
7184 txn->cookie_last_date = 0;
7185 }
7186
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007187 /* Here, we'll look for the first running server which supports the cookie.
7188 * This allows to share a same cookie between several servers, for example
7189 * to dedicate backup servers to specific servers only.
7190 * However, to prevent clients from sticking to cookie-less backup server
7191 * when they have incidentely learned an empty cookie, we simply ignore
7192 * empty cookies and mark them as invalid.
7193 * The same behaviour is applied when persistence must be ignored.
7194 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007195 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007196 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007197
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007198 while (srv) {
7199 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7200 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7201 if ((srv->state & SRV_RUNNING) ||
7202 (t->be->options & PR_O_PERSIST) ||
7203 (t->flags & SN_FORCE_PRST)) {
7204 /* we found the server and we can use it */
7205 txn->flags &= ~TX_CK_MASK;
7206 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7207 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007208 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007209 break;
7210 } else {
7211 /* we found a server, but it's down,
7212 * mark it as such and go on in case
7213 * another one is available.
7214 */
7215 txn->flags &= ~TX_CK_MASK;
7216 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007217 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007218 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007219 srv = srv->next;
7220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007221
Willy Tarreauf64d1412010-10-07 20:06:11 +02007222 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007223 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007224 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007225 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7226 txn->flags |= TX_CK_UNUSED;
7227 else
7228 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007229 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007231 /* depending on the cookie mode, we may have to either :
7232 * - delete the complete cookie if we're in insert+indirect mode, so that
7233 * the server never sees it ;
7234 * - remove the server id from the cookie value, and tag the cookie as an
7235 * application cookie so that it does not get accidentely removed later,
7236 * if we're in cookie prefix mode
7237 */
Willy Tarreau67402132012-05-31 20:40:20 +02007238 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007239 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007240
Willy Tarreau9b28e032012-10-12 23:49:43 +02007241 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007242 val_end += delta;
7243 next += delta;
7244 hdr_end += delta;
7245 hdr_next += delta;
7246 cur_hdr->len += delta;
7247 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007248
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007249 del_from = NULL;
7250 preserve_hdr = 1; /* we want to keep this cookie */
7251 }
7252 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007253 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007254 del_from = prev;
7255 }
7256 } else {
7257 /* This is not our cookie, so we must preserve it. But if we already
7258 * scheduled another cookie for removal, we cannot remove the
7259 * complete header, but we can remove the previous block itself.
7260 */
7261 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007263 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007264 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007265 if (att_beg >= del_from)
7266 att_beg += delta;
7267 if (att_end >= del_from)
7268 att_end += delta;
7269 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007270 val_end += delta;
7271 next += delta;
7272 hdr_end += delta;
7273 hdr_next += delta;
7274 cur_hdr->len += delta;
7275 http_msg_move_end(&txn->req, delta);
7276 prev = del_from;
7277 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007278 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007279 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007281 /* Look for the appsession cookie unless persistence must be ignored */
7282 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7283 int cmp_len, value_len;
7284 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007285
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007286 if (t->be->options2 & PR_O2_AS_PFX) {
7287 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7288 value_begin = att_beg + t->be->appsession_name_len;
7289 value_len = val_end - att_beg - t->be->appsession_name_len;
7290 } else {
7291 cmp_len = att_end - att_beg;
7292 value_begin = val_beg;
7293 value_len = val_end - val_beg;
7294 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007295
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007296 /* let's see if the cookie is our appcookie */
7297 if (cmp_len == t->be->appsession_name_len &&
7298 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7299 manage_client_side_appsession(t, value_begin, value_len);
7300 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007301 }
7302
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007303 /* continue with next cookie on this header line */
7304 att_beg = next;
7305 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007307 /* There are no more cookies on this line.
7308 * We may still have one (or several) marked for deletion at the
7309 * end of the line. We must do this now in two ways :
7310 * - if some cookies must be preserved, we only delete from the
7311 * mark to the end of line ;
7312 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007313 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007314 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007316 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007317 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007318 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007319 cur_hdr->len += delta;
7320 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007321 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322
7323 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007324 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7325 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007326 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007327 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007329 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007330 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007331 }
7332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007333 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007335 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336}
7337
7338
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339/* Iterate the same filter through all response headers contained in <rtr>.
7340 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7341 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007342int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343{
7344 char term;
7345 char *cur_ptr, *cur_end, *cur_next;
7346 int cur_idx, old_idx, last_hdr;
7347 struct http_txn *txn = &t->txn;
7348 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007349 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350
7351 last_hdr = 0;
7352
Willy Tarreau9b28e032012-10-12 23:49:43 +02007353 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354 old_idx = 0;
7355
7356 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007357 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007358 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007359 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360 (exp->action == ACT_ALLOW ||
7361 exp->action == ACT_DENY))
7362 return 0;
7363
7364 cur_idx = txn->hdr_idx.v[old_idx].next;
7365 if (!cur_idx)
7366 break;
7367
7368 cur_hdr = &txn->hdr_idx.v[cur_idx];
7369 cur_ptr = cur_next;
7370 cur_end = cur_ptr + cur_hdr->len;
7371 cur_next = cur_end + cur_hdr->cr + 1;
7372
7373 /* Now we have one header between cur_ptr and cur_end,
7374 * and the next header starts at cur_next.
7375 */
7376
7377 /* The annoying part is that pattern matching needs
7378 * that we modify the contents to null-terminate all
7379 * strings before testing them.
7380 */
7381
7382 term = *cur_end;
7383 *cur_end = '\0';
7384
7385 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7386 switch (exp->action) {
7387 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007388 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389 last_hdr = 1;
7390 break;
7391
7392 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007393 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394 last_hdr = 1;
7395 break;
7396
7397 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007398 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7399 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007400 /* FIXME: if the user adds a newline in the replacement, the
7401 * index will not be recalculated for now, and the new line
7402 * will not be counted as a new header.
7403 */
7404
7405 cur_end += delta;
7406 cur_next += delta;
7407 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007408 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409 break;
7410
7411 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007412 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007413 cur_next += delta;
7414
Willy Tarreaufa355d42009-11-29 18:12:29 +01007415 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7417 txn->hdr_idx.used--;
7418 cur_hdr->len = 0;
7419 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007420 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421 break;
7422
7423 }
7424 }
7425 if (cur_end)
7426 *cur_end = term; /* restore the string terminator */
7427
7428 /* keep the link from this header to next one in case of later
7429 * removal of next header.
7430 */
7431 old_idx = cur_idx;
7432 }
7433 return 0;
7434}
7435
7436
7437/* Apply the filter to the status line in the response buffer <rtr>.
7438 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7439 * or -1 if a replacement resulted in an invalid status line.
7440 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007441int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007442{
7443 char term;
7444 char *cur_ptr, *cur_end;
7445 int done;
7446 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007447 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007448
7449
Willy Tarreau3d300592007-03-18 18:34:41 +01007450 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007451 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007452 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453 (exp->action == ACT_ALLOW ||
7454 exp->action == ACT_DENY))
7455 return 0;
7456 else if (exp->action == ACT_REMOVE)
7457 return 0;
7458
7459 done = 0;
7460
Willy Tarreau9b28e032012-10-12 23:49:43 +02007461 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007462 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007463
7464 /* Now we have the status line between cur_ptr and cur_end */
7465
7466 /* The annoying part is that pattern matching needs
7467 * that we modify the contents to null-terminate all
7468 * strings before testing them.
7469 */
7470
7471 term = *cur_end;
7472 *cur_end = '\0';
7473
7474 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7475 switch (exp->action) {
7476 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007477 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007478 done = 1;
7479 break;
7480
7481 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007482 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007483 done = 1;
7484 break;
7485
7486 case ACT_REPLACE:
7487 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007488 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7489 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490 /* FIXME: if the user adds a newline in the replacement, the
7491 * index will not be recalculated for now, and the new line
7492 * will not be counted as a new header.
7493 */
7494
Willy Tarreaufa355d42009-11-29 18:12:29 +01007495 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007497 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007498 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007499 cur_ptr, cur_end + 1,
7500 NULL, NULL);
7501 if (unlikely(!cur_end))
7502 return -1;
7503
7504 /* we have a full respnse and we know that we have either a CR
7505 * or an LF at <ptr>.
7506 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007507 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007508 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509 /* there is no point trying this regex on headers */
7510 return 1;
7511 }
7512 }
7513 *cur_end = term; /* restore the string terminator */
7514 return done;
7515}
7516
7517
7518
7519/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007520 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007521 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7522 * unparsable response.
7523 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007524int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007526 struct http_txn *txn = &s->txn;
7527 struct hdr_exp *exp;
7528
7529 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530 int ret;
7531
7532 /*
7533 * The interleaving of transformations and verdicts
7534 * makes it difficult to decide to continue or stop
7535 * the evaluation.
7536 */
7537
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007538 if (txn->flags & TX_SVDENY)
7539 break;
7540
Willy Tarreau3d300592007-03-18 18:34:41 +01007541 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007542 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7543 exp->action == ACT_PASS)) {
7544 exp = exp->next;
7545 continue;
7546 }
7547
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007548 /* if this filter had a condition, evaluate it now and skip to
7549 * next filter if the condition does not match.
7550 */
7551 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007552 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007553 ret = acl_pass(ret);
7554 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7555 ret = !ret;
7556 if (!ret)
7557 continue;
7558 }
7559
Willy Tarreaua15645d2007-03-18 16:22:39 +01007560 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007561 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007562 if (unlikely(ret < 0))
7563 return -1;
7564
7565 if (likely(ret == 0)) {
7566 /* The filter did not match the response, it can be
7567 * iterated through all headers.
7568 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007569 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007570 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007571 }
7572 return 0;
7573}
7574
7575
Willy Tarreaua15645d2007-03-18 16:22:39 +01007576/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007577 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007578 * desirable to call it only when needed. This function is also used when we
7579 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007580 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007581void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007582{
7583 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007584 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007586 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007587 char *hdr_beg, *hdr_end, *hdr_next;
7588 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007589
Willy Tarreaua15645d2007-03-18 16:22:39 +01007590 /* Iterate through the headers.
7591 * we start with the start line.
7592 */
7593 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007594 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007595
7596 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7597 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007598 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007599
7600 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007601 hdr_beg = hdr_next;
7602 hdr_end = hdr_beg + cur_hdr->len;
7603 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604
Willy Tarreau24581ba2010-08-31 22:39:35 +02007605 /* We have one full header between hdr_beg and hdr_end, and the
7606 * next header starts at hdr_next. We're only interested in
7607 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608 */
7609
Willy Tarreau24581ba2010-08-31 22:39:35 +02007610 is_cookie2 = 0;
7611 prev = hdr_beg + 10;
7612 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007613 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007614 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7615 if (!val) {
7616 old_idx = cur_idx;
7617 continue;
7618 }
7619 is_cookie2 = 1;
7620 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007621 }
7622
Willy Tarreau24581ba2010-08-31 22:39:35 +02007623 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7624 * <prev> points to the colon.
7625 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007626 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627
Willy Tarreau24581ba2010-08-31 22:39:35 +02007628 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7629 * check-cache is enabled) and we are not interested in checking
7630 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007631 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007632 if (t->be->cookie_name == NULL &&
7633 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007634 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007635 return;
7636
Willy Tarreau24581ba2010-08-31 22:39:35 +02007637 /* OK so now we know we have to process this response cookie.
7638 * The format of the Set-Cookie header is slightly different
7639 * from the format of the Cookie header in that it does not
7640 * support the comma as a cookie delimiter (thus the header
7641 * cannot be folded) because the Expires attribute described in
7642 * the original Netscape's spec may contain an unquoted date
7643 * with a comma inside. We have to live with this because
7644 * many browsers don't support Max-Age and some browsers don't
7645 * support quoted strings. However the Set-Cookie2 header is
7646 * clean.
7647 *
7648 * We have to keep multiple pointers in order to support cookie
7649 * removal at the beginning, middle or end of header without
7650 * corrupting the header (in case of set-cookie2). A special
7651 * pointer, <scav> points to the beginning of the set-cookie-av
7652 * fields after the first semi-colon. The <next> pointer points
7653 * either to the end of line (set-cookie) or next unquoted comma
7654 * (set-cookie2). All of these headers are valid :
7655 *
7656 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7657 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7658 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7659 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7660 * | | | | | | | | | |
7661 * | | | | | | | | +-> next hdr_end <--+
7662 * | | | | | | | +------------> scav
7663 * | | | | | | +--------------> val_end
7664 * | | | | | +--------------------> val_beg
7665 * | | | | +----------------------> equal
7666 * | | | +------------------------> att_end
7667 * | | +----------------------------> att_beg
7668 * | +------------------------------> prev
7669 * +-----------------------------------------> hdr_beg
7670 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671
Willy Tarreau24581ba2010-08-31 22:39:35 +02007672 for (; prev < hdr_end; prev = next) {
7673 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674
Willy Tarreau24581ba2010-08-31 22:39:35 +02007675 /* find att_beg */
7676 att_beg = prev + 1;
7677 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7678 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679
Willy Tarreau24581ba2010-08-31 22:39:35 +02007680 /* find att_end : this is the first character after the last non
7681 * space before the equal. It may be equal to hdr_end.
7682 */
7683 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007684
Willy Tarreau24581ba2010-08-31 22:39:35 +02007685 while (equal < hdr_end) {
7686 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7687 break;
7688 if (http_is_spht[(unsigned char)*equal++])
7689 continue;
7690 att_end = equal;
7691 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692
Willy Tarreau24581ba2010-08-31 22:39:35 +02007693 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7694 * is between <att_beg> and <equal>, both may be identical.
7695 */
7696
7697 /* look for end of cookie if there is an equal sign */
7698 if (equal < hdr_end && *equal == '=') {
7699 /* look for the beginning of the value */
7700 val_beg = equal + 1;
7701 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7702 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007703
Willy Tarreau24581ba2010-08-31 22:39:35 +02007704 /* find the end of the value, respecting quotes */
7705 next = find_cookie_value_end(val_beg, hdr_end);
7706
7707 /* make val_end point to the first white space or delimitor after the value */
7708 val_end = next;
7709 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7710 val_end--;
7711 } else {
7712 /* <equal> points to next comma, semi-colon or EOL */
7713 val_beg = val_end = next = equal;
7714 }
7715
7716 if (next < hdr_end) {
7717 /* Set-Cookie2 supports multiple cookies, and <next> points to
7718 * a colon or semi-colon before the end. So skip all attr-value
7719 * pairs and look for the next comma. For Set-Cookie, since
7720 * commas are permitted in values, skip to the end.
7721 */
7722 if (is_cookie2)
7723 next = find_hdr_value_end(next, hdr_end);
7724 else
7725 next = hdr_end;
7726 }
7727
7728 /* Now everything is as on the diagram above */
7729
7730 /* Ignore cookies with no equal sign */
7731 if (equal == val_end)
7732 continue;
7733
7734 /* If there are spaces around the equal sign, we need to
7735 * strip them otherwise we'll get trouble for cookie captures,
7736 * or even for rewrites. Since this happens extremely rarely,
7737 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007738 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007739 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7740 int stripped_before = 0;
7741 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007742
Willy Tarreau24581ba2010-08-31 22:39:35 +02007743 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007744 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007745 equal += stripped_before;
7746 val_beg += stripped_before;
7747 }
7748
7749 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007750 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007751 val_beg += stripped_after;
7752 stripped_before += stripped_after;
7753 }
7754
7755 val_end += stripped_before;
7756 next += stripped_before;
7757 hdr_end += stripped_before;
7758 hdr_next += stripped_before;
7759 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007760 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007761 }
7762
7763 /* First, let's see if we want to capture this cookie. We check
7764 * that we don't already have a server side cookie, because we
7765 * can only capture one. Also as an optimisation, we ignore
7766 * cookies shorter than the declared name.
7767 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007768 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007769 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007770 (val_end - att_beg >= t->fe->capture_namelen) &&
7771 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7772 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007773 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007774 Alert("HTTP logging : out of memory.\n");
7775 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007776 else {
7777 if (log_len > t->fe->capture_len)
7778 log_len = t->fe->capture_len;
7779 memcpy(txn->srv_cookie, att_beg, log_len);
7780 txn->srv_cookie[log_len] = 0;
7781 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007782 }
7783
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007784 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007785 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007786 if (!(t->flags & SN_IGNORE_PRST) &&
7787 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7788 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007789 /* assume passive cookie by default */
7790 txn->flags &= ~TX_SCK_MASK;
7791 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007792
7793 /* If the cookie is in insert mode on a known server, we'll delete
7794 * this occurrence because we'll insert another one later.
7795 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007796 * a direct access.
7797 */
Willy Tarreau67402132012-05-31 20:40:20 +02007798 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007799 /* The "preserve" flag was set, we don't want to touch the
7800 * server's cookie.
7801 */
7802 }
Willy Tarreau67402132012-05-31 20:40:20 +02007803 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7804 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007805 /* this cookie must be deleted */
7806 if (*prev == ':' && next == hdr_end) {
7807 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007808 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007809 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7810 txn->hdr_idx.used--;
7811 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007812 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007813 hdr_next += delta;
7814 http_msg_move_end(&txn->rsp, delta);
7815 /* note: while both invalid now, <next> and <hdr_end>
7816 * are still equal, so the for() will stop as expected.
7817 */
7818 } else {
7819 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007820 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007821 next = prev;
7822 hdr_end += delta;
7823 hdr_next += delta;
7824 cur_hdr->len += delta;
7825 http_msg_move_end(&txn->rsp, delta);
7826 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007827 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007828 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007829 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007830 }
Willy Tarreau67402132012-05-31 20:40:20 +02007831 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007832 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007833 * with this server since we know it.
7834 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007835 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007836 next += delta;
7837 hdr_end += delta;
7838 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007840 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841
Willy Tarreauf1348312010-10-07 15:54:11 +02007842 txn->flags &= ~TX_SCK_MASK;
7843 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007845 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007847 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007849 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007850 next += delta;
7851 hdr_end += delta;
7852 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007854 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855
Willy Tarreau827aee92011-03-10 16:55:02 +01007856 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007857 txn->flags &= ~TX_SCK_MASK;
7858 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007859 }
7860 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007861 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7862 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007863 int cmp_len, value_len;
7864 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865
Cyril Bontéb21570a2009-11-29 20:04:48 +01007866 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007867 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7868 value_begin = att_beg + t->be->appsession_name_len;
7869 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007870 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007871 cmp_len = att_end - att_beg;
7872 value_begin = val_beg;
7873 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007874 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007875
Cyril Bonté17530c32010-04-06 21:11:10 +02007876 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007877 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7878 /* free a possibly previously allocated memory */
7879 pool_free2(apools.sessid, txn->sessid);
7880
Cyril Bontéb21570a2009-11-29 20:04:48 +01007881 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007882 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007883 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7884 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7885 return;
7886 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007887 memcpy(txn->sessid, value_begin, value_len);
7888 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007889 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007890 }
7891 /* that's done for this cookie, check the next one on the same
7892 * line when next != hdr_end (only if is_cookie2).
7893 */
7894 }
7895 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007897 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007898
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007899 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007900 appsess *asession = NULL;
7901 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007902 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007903 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007904 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007905 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7906 Alert("Not enough Memory process_srv():asession:calloc().\n");
7907 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7908 return;
7909 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007910 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7911
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007912 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7913 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7914 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007915 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007916 return;
7917 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007918 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007919 asession->sessid[t->be->appsession_len] = 0;
7920
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007921 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007922 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007923 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007924 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007925 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007926 return;
7927 }
7928 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007929 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007930
7931 asession->request_count = 0;
7932 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7933 }
7934
7935 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7936 asession->request_count++;
7937 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007938}
7939
7940
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941/*
7942 * Check if response is cacheable or not. Updates t->flags.
7943 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007944void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007945{
7946 struct http_txn *txn = &t->txn;
7947 char *p1, *p2;
7948
7949 char *cur_ptr, *cur_end, *cur_next;
7950 int cur_idx;
7951
Willy Tarreau5df51872007-11-25 16:20:08 +01007952 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953 return;
7954
7955 /* Iterate through the headers.
7956 * we start with the start line.
7957 */
7958 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007959 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007960
7961 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7962 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007963 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964
7965 cur_hdr = &txn->hdr_idx.v[cur_idx];
7966 cur_ptr = cur_next;
7967 cur_end = cur_ptr + cur_hdr->len;
7968 cur_next = cur_end + cur_hdr->cr + 1;
7969
7970 /* We have one full header between cur_ptr and cur_end, and the
7971 * next header starts at cur_next. We're only interested in
7972 * "Cookie:" headers.
7973 */
7974
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007975 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7976 if (val) {
7977 if ((cur_end - (cur_ptr + val) >= 8) &&
7978 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7979 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7980 return;
7981 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007982 }
7983
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007984 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7985 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986 continue;
7987
7988 /* OK, right now we know we have a cache-control header at cur_ptr */
7989
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007990 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991
7992 if (p1 >= cur_end) /* no more info */
7993 continue;
7994
7995 /* p1 is at the beginning of the value */
7996 p2 = p1;
7997
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007998 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999 p2++;
8000
8001 /* we have a complete value between p1 and p2 */
8002 if (p2 < cur_end && *p2 == '=') {
8003 /* we have something of the form no-cache="set-cookie" */
8004 if ((cur_end - p1 >= 21) &&
8005 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8006 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008007 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008 continue;
8009 }
8010
8011 /* OK, so we know that either p2 points to the end of string or to a comma */
8012 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008013 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008014 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8015 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8016 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008017 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 return;
8019 }
8020
8021 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 continue;
8024 }
8025 }
8026}
8027
8028
Willy Tarreau58f10d72006-12-04 02:26:12 +01008029/*
8030 * Try to retrieve a known appsession in the URI, then the associated server.
8031 * If the server is found, it's assigned to the session.
8032 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008033void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008034{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008035 char *end_params, *first_param, *cur_param, *next_param;
8036 char separator;
8037 int value_len;
8038
8039 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008040
Willy Tarreaue2e27a52007-04-01 00:01:37 +02008041 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02008042 (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 +01008043 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008044 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008045
Cyril Bontéb21570a2009-11-29 20:04:48 +01008046 first_param = NULL;
8047 switch (mode) {
8048 case PR_O2_AS_M_PP:
8049 first_param = memchr(begin, ';', len);
8050 break;
8051 case PR_O2_AS_M_QS:
8052 first_param = memchr(begin, '?', len);
8053 break;
8054 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008055
Cyril Bontéb21570a2009-11-29 20:04:48 +01008056 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008057 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008058 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008059
Cyril Bontéb21570a2009-11-29 20:04:48 +01008060 switch (mode) {
8061 case PR_O2_AS_M_PP:
8062 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8063 end_params = (char *) begin + len;
8064 }
8065 separator = ';';
8066 break;
8067 case PR_O2_AS_M_QS:
8068 end_params = (char *) begin + len;
8069 separator = '&';
8070 break;
8071 default:
8072 /* unknown mode, shouldn't happen */
8073 return;
8074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008075
Cyril Bontéb21570a2009-11-29 20:04:48 +01008076 cur_param = next_param = end_params;
8077 while (cur_param > first_param) {
8078 cur_param--;
8079 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8080 /* let's see if this is the appsession parameter */
8081 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
8082 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
8083 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
8084 /* Cool... it's the right one */
8085 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8086 value_len = MIN(t->be->appsession_len, next_param - cur_param);
8087 if (value_len > 0) {
8088 manage_client_side_appsession(t, cur_param, value_len);
8089 }
8090 break;
8091 }
8092 next_param = cur_param;
8093 }
8094 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008095#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008096 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02008097 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008098#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008099}
8100
Willy Tarreaub2513902006-12-17 14:52:38 +01008101/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008102 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008103 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008104 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008105 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008106 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008107 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008108 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008109 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008110int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008111{
8112 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008113 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008114 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008115
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008116 if (!uri_auth)
8117 return 0;
8118
Cyril Bonté70be45d2010-10-12 00:14:35 +02008119 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008120 return 0;
8121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008122 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008123 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008124 return 0;
8125
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008126 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008127 return 0;
8128
Willy Tarreaub2513902006-12-17 14:52:38 +01008129 return 1;
8130}
8131
Willy Tarreau4076a152009-04-02 15:18:36 +02008132/*
8133 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008134 * By default it tries to report the error position as msg->err_pos. However if
8135 * this one is not set, it will then report msg->next, which is the last known
8136 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008137 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008138 */
8139void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008140 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008141 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008142{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008143 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008144 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008145
Willy Tarreau9b28e032012-10-12 23:49:43 +02008146 es->len = MIN(chn->buf->i, sizeof(es->buf));
8147 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008148 len1 = MIN(len1, es->len);
8149 len2 = es->len - len1; /* remaining data if buffer wraps */
8150
Willy Tarreau9b28e032012-10-12 23:49:43 +02008151 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008152 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008153 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008154
Willy Tarreau4076a152009-04-02 15:18:36 +02008155 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008156 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008157 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008158 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008159
Willy Tarreau4076a152009-04-02 15:18:36 +02008160 es->when = date; // user-visible date
8161 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008162 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008163 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008164 if (objt_conn(s->req->prod->end))
8165 es->src = __objt_conn(s->req->prod->end)->addr.from;
8166 else
8167 memset(&es->src, 0, sizeof(es->src));
8168
Willy Tarreau078272e2010-12-12 12:46:33 +01008169 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008170 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008171 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008172 es->s_flags = s->flags;
8173 es->t_flags = s->txn.flags;
8174 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008175 es->b_out = chn->buf->o;
8176 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008177 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008178 es->m_clen = msg->chunk_len;
8179 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008180}
Willy Tarreaub2513902006-12-17 14:52:38 +01008181
Willy Tarreau294c4732011-12-16 21:35:50 +01008182/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8183 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8184 * performed over the whole headers. Otherwise it must contain a valid header
8185 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8186 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8187 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8188 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008189 * -1. The value fetch stops at commas, so this function is suited for use with
8190 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008191 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008192 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008193unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008194 struct hdr_idx *idx, int occ,
8195 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008196{
Willy Tarreau294c4732011-12-16 21:35:50 +01008197 struct hdr_ctx local_ctx;
8198 char *ptr_hist[MAX_HDR_HISTORY];
8199 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008200 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008201 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008202
Willy Tarreau294c4732011-12-16 21:35:50 +01008203 if (!ctx) {
8204 local_ctx.idx = 0;
8205 ctx = &local_ctx;
8206 }
8207
Willy Tarreaubce70882009-09-07 11:51:47 +02008208 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008209 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008210 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008211 occ--;
8212 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008213 *vptr = ctx->line + ctx->val;
8214 *vlen = ctx->vlen;
8215 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008216 }
8217 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008218 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008219 }
8220
8221 /* negative occurrence, we scan all the list then walk back */
8222 if (-occ > MAX_HDR_HISTORY)
8223 return 0;
8224
Willy Tarreau294c4732011-12-16 21:35:50 +01008225 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008226 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008227 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8228 len_hist[hist_ptr] = ctx->vlen;
8229 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008230 hist_ptr = 0;
8231 found++;
8232 }
8233 if (-occ > found)
8234 return 0;
8235 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008236 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8237 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8238 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008239 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008240 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008241 if (hist_ptr >= MAX_HDR_HISTORY)
8242 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008243 *vptr = ptr_hist[hist_ptr];
8244 *vlen = len_hist[hist_ptr];
8245 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008246}
8247
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008248/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8249 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8250 * performed over the whole headers. Otherwise it must contain a valid header
8251 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8252 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8253 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8254 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8255 * -1. This function differs from http_get_hdr() in that it only returns full
8256 * line header values and does not stop at commas.
8257 * The return value is 0 if nothing was found, or non-zero otherwise.
8258 */
8259unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8260 struct hdr_idx *idx, int occ,
8261 struct hdr_ctx *ctx, char **vptr, int *vlen)
8262{
8263 struct hdr_ctx local_ctx;
8264 char *ptr_hist[MAX_HDR_HISTORY];
8265 int len_hist[MAX_HDR_HISTORY];
8266 unsigned int hist_ptr;
8267 int found;
8268
8269 if (!ctx) {
8270 local_ctx.idx = 0;
8271 ctx = &local_ctx;
8272 }
8273
8274 if (occ >= 0) {
8275 /* search from the beginning */
8276 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8277 occ--;
8278 if (occ <= 0) {
8279 *vptr = ctx->line + ctx->val;
8280 *vlen = ctx->vlen;
8281 return 1;
8282 }
8283 }
8284 return 0;
8285 }
8286
8287 /* negative occurrence, we scan all the list then walk back */
8288 if (-occ > MAX_HDR_HISTORY)
8289 return 0;
8290
8291 found = hist_ptr = 0;
8292 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8293 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8294 len_hist[hist_ptr] = ctx->vlen;
8295 if (++hist_ptr >= MAX_HDR_HISTORY)
8296 hist_ptr = 0;
8297 found++;
8298 }
8299 if (-occ > found)
8300 return 0;
8301 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8302 * find occurrence -occ, so we have to check [hist_ptr+occ].
8303 */
8304 hist_ptr += occ;
8305 if (hist_ptr >= MAX_HDR_HISTORY)
8306 hist_ptr -= MAX_HDR_HISTORY;
8307 *vptr = ptr_hist[hist_ptr];
8308 *vlen = len_hist[hist_ptr];
8309 return 1;
8310}
8311
Willy Tarreaubaaee002006-06-26 02:48:02 +02008312/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008313 * Print a debug line with a header. Always stop at the first CR or LF char,
8314 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8315 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008316 */
8317void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8318{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008319 int max;
8320 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008321 dir,
8322 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8323 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 +02008324
8325 for (max = 0; start + max < end; max++)
8326 if (start[max] == '\r' || start[max] == '\n')
8327 break;
8328
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008329 UBOUND(max, trash.size - trash.len - 3);
8330 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8331 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008332 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008333}
8334
Willy Tarreau0937bc42009-12-22 15:03:09 +01008335/*
8336 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8337 * the required fields are properly allocated and that we only need to (re)init
8338 * them. This should be used before processing any new request.
8339 */
8340void http_init_txn(struct session *s)
8341{
8342 struct http_txn *txn = &s->txn;
8343 struct proxy *fe = s->fe;
8344
8345 txn->flags = 0;
8346 txn->status = -1;
8347
Willy Tarreauf64d1412010-10-07 20:06:11 +02008348 txn->cookie_first_date = 0;
8349 txn->cookie_last_date = 0;
8350
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008351 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008352 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008353 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008354 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008355 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008356 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008357 txn->req.chunk_len = 0LL;
8358 txn->req.body_len = 0LL;
8359 txn->rsp.chunk_len = 0LL;
8360 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008361 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8362 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008363 txn->req.chn = s->req;
8364 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008365
8366 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008367
8368 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8369 if (fe->options2 & PR_O2_REQBUG_OK)
8370 txn->req.err_pos = -1; /* let buggy requests pass */
8371
Willy Tarreau46023632010-01-07 22:51:47 +01008372 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008373 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8374
Willy Tarreau46023632010-01-07 22:51:47 +01008375 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008376 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8377
8378 if (txn->hdr_idx.v)
8379 hdr_idx_init(&txn->hdr_idx);
8380}
8381
8382/* to be used at the end of a transaction */
8383void http_end_txn(struct session *s)
8384{
8385 struct http_txn *txn = &s->txn;
8386
Willy Tarreau75195602014-03-11 15:48:55 +01008387 /* release any possible compression context */
8388 if (s->flags & SN_COMP_READY)
8389 s->comp_algo->end(&s->comp_ctx);
8390 s->comp_algo = NULL;
8391 s->flags &= ~SN_COMP_READY;
8392
Willy Tarreau0937bc42009-12-22 15:03:09 +01008393 /* these ones will have been dynamically allocated */
8394 pool_free2(pool2_requri, txn->uri);
8395 pool_free2(pool2_capture, txn->cli_cookie);
8396 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008397 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008398 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008399
William Lallemanda73203e2012-03-12 12:48:57 +01008400 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008401 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008402 txn->uri = NULL;
8403 txn->srv_cookie = NULL;
8404 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008405
8406 if (txn->req.cap) {
8407 struct cap_hdr *h;
8408 for (h = s->fe->req_cap; h; h = h->next)
8409 pool_free2(h->pool, txn->req.cap[h->index]);
8410 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8411 }
8412
8413 if (txn->rsp.cap) {
8414 struct cap_hdr *h;
8415 for (h = s->fe->rsp_cap; h; h = h->next)
8416 pool_free2(h->pool, txn->rsp.cap[h->index]);
8417 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8418 }
8419
Willy Tarreau0937bc42009-12-22 15:03:09 +01008420}
8421
8422/* to be used at the end of a transaction to prepare a new one */
8423void http_reset_txn(struct session *s)
8424{
8425 http_end_txn(s);
8426 http_init_txn(s);
8427
8428 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008429 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008430 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008431 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008432 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008433 /* re-init store persistence */
8434 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008435 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008436
Willy Tarreau0937bc42009-12-22 15:03:09 +01008437 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008438
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008439 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008440
Willy Tarreau739cfba2010-01-25 23:11:14 +01008441 /* We must trim any excess data from the response buffer, because we
8442 * may have blocked an invalid response from a server that we don't
8443 * want to accidentely forward once we disable the analysers, nor do
8444 * we want those data to come along with next response. A typical
8445 * example of such data would be from a buggy server responding to
8446 * a HEAD with some data, or sending more than the advertised
8447 * content-length.
8448 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008449 if (unlikely(s->rep->buf->i))
8450 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008451
Willy Tarreau0937bc42009-12-22 15:03:09 +01008452 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008453 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008454
Willy Tarreaud04e8582010-05-31 12:31:35 +02008455 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008456 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008457
8458 s->req->rex = TICK_ETERNITY;
8459 s->req->wex = TICK_ETERNITY;
8460 s->req->analyse_exp = TICK_ETERNITY;
8461 s->rep->rex = TICK_ETERNITY;
8462 s->rep->wex = TICK_ETERNITY;
8463 s->rep->analyse_exp = TICK_ETERNITY;
8464}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008465
Willy Tarreauff011f22011-01-06 17:51:27 +01008466void free_http_req_rules(struct list *r) {
8467 struct http_req_rule *tr, *pr;
8468
8469 list_for_each_entry_safe(pr, tr, r, list) {
8470 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008471 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008472 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008473
8474 free(pr);
8475 }
8476}
8477
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008478/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008479struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8480{
8481 struct http_req_rule *rule;
8482 int cur_arg;
8483
8484 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8485 if (!rule) {
8486 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008487 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008488 }
8489
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008490 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008491 rule->action = HTTP_REQ_ACT_ALLOW;
8492 cur_arg = 1;
8493 } else if (!strcmp(args[0], "deny")) {
8494 rule->action = HTTP_REQ_ACT_DENY;
8495 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008496 } else if (!strcmp(args[0], "tarpit")) {
8497 rule->action = HTTP_REQ_ACT_TARPIT;
8498 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008499 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008500 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008501 cur_arg = 1;
8502
8503 while(*args[cur_arg]) {
8504 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008505 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008506 cur_arg+=2;
8507 continue;
8508 } else
8509 break;
8510 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008511 } else if (!strcmp(args[0], "set-nice")) {
8512 rule->action = HTTP_REQ_ACT_SET_NICE;
8513 cur_arg = 1;
8514
8515 if (!*args[cur_arg] ||
8516 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8517 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8518 file, linenum, args[0]);
8519 goto out_err;
8520 }
8521 rule->arg.nice = atoi(args[cur_arg]);
8522 if (rule->arg.nice < -1024)
8523 rule->arg.nice = -1024;
8524 else if (rule->arg.nice > 1024)
8525 rule->arg.nice = 1024;
8526 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008527 } else if (!strcmp(args[0], "set-tos")) {
8528#ifdef IP_TOS
8529 char *err;
8530 rule->action = HTTP_REQ_ACT_SET_TOS;
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.tos = strtol(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#else
8548 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8549 goto out_err;
8550#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008551 } else if (!strcmp(args[0], "set-mark")) {
8552#ifdef SO_MARK
8553 char *err;
8554 rule->action = HTTP_REQ_ACT_SET_MARK;
8555 cur_arg = 1;
8556
8557 if (!*args[cur_arg] ||
8558 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8559 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8560 file, linenum, args[0]);
8561 goto out_err;
8562 }
8563
8564 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8565 if (err && *err != '\0') {
8566 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8567 file, linenum, err, args[0]);
8568 goto out_err;
8569 }
8570 cur_arg++;
8571 global.last_checks |= LSTCHK_NETADM;
8572#else
8573 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8574 goto out_err;
8575#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008576 } else if (!strcmp(args[0], "set-log-level")) {
8577 rule->action = HTTP_REQ_ACT_SET_LOGL;
8578 cur_arg = 1;
8579
8580 if (!*args[cur_arg] ||
8581 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8582 bad_log_level:
8583 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8584 file, linenum, args[0]);
8585 goto out_err;
8586 }
8587 if (strcmp(args[cur_arg], "silent") == 0)
8588 rule->arg.loglevel = -1;
8589 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8590 goto bad_log_level;
8591 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008592 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8593 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8594 cur_arg = 1;
8595
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008596 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8597 (*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 +01008598 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8599 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008600 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008601 }
8602
8603 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8604 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8605 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008606
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008607 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008608 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008609 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8610 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008611 free(proxy->conf.lfs_file);
8612 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8613 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008614 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008615 } else if (strcmp(args[0], "del-header") == 0) {
8616 rule->action = HTTP_REQ_ACT_DEL_HDR;
8617 cur_arg = 1;
8618
8619 if (!*args[cur_arg] ||
8620 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8621 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8622 file, linenum, args[0]);
8623 goto out_err;
8624 }
8625
8626 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8627 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8628
8629 proxy->conf.args.ctx = ARGC_HRQ;
8630 free(proxy->conf.lfs_file);
8631 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8632 proxy->conf.lfs_line = proxy->conf.args.line;
8633 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008634 } else if (strcmp(args[0], "redirect") == 0) {
8635 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008636 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008637
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008638 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008639 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8640 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8641 goto out_err;
8642 }
8643
8644 /* this redirect rule might already contain a parsed condition which
8645 * we'll pass to the http-request rule.
8646 */
8647 rule->action = HTTP_REQ_ACT_REDIR;
8648 rule->arg.redir = redir;
8649 rule->cond = redir->cond;
8650 redir->cond = NULL;
8651 cur_arg = 2;
8652 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008653 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008654 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 +01008655 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008656 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008657 }
8658
8659 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8660 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008661 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008662
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008663 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8664 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8665 file, linenum, args[0], errmsg);
8666 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008667 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008668 }
8669 rule->cond = cond;
8670 }
8671 else if (*args[cur_arg]) {
8672 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8673 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8674 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008675 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008676 }
8677
8678 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008679 out_err:
8680 free(rule);
8681 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008682}
8683
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008684/* parse an "http-respose" rule */
8685struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8686{
8687 struct http_res_rule *rule;
8688 int cur_arg;
8689
8690 rule = calloc(1, sizeof(*rule));
8691 if (!rule) {
8692 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8693 goto out_err;
8694 }
8695
8696 if (!strcmp(args[0], "allow")) {
8697 rule->action = HTTP_RES_ACT_ALLOW;
8698 cur_arg = 1;
8699 } else if (!strcmp(args[0], "deny")) {
8700 rule->action = HTTP_RES_ACT_DENY;
8701 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008702 } else if (!strcmp(args[0], "set-nice")) {
8703 rule->action = HTTP_RES_ACT_SET_NICE;
8704 cur_arg = 1;
8705
8706 if (!*args[cur_arg] ||
8707 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8708 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8709 file, linenum, args[0]);
8710 goto out_err;
8711 }
8712 rule->arg.nice = atoi(args[cur_arg]);
8713 if (rule->arg.nice < -1024)
8714 rule->arg.nice = -1024;
8715 else if (rule->arg.nice > 1024)
8716 rule->arg.nice = 1024;
8717 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008718 } else if (!strcmp(args[0], "set-tos")) {
8719#ifdef IP_TOS
8720 char *err;
8721 rule->action = HTTP_RES_ACT_SET_TOS;
8722 cur_arg = 1;
8723
8724 if (!*args[cur_arg] ||
8725 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8726 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8727 file, linenum, args[0]);
8728 goto out_err;
8729 }
8730
8731 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8732 if (err && *err != '\0') {
8733 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8734 file, linenum, err, args[0]);
8735 goto out_err;
8736 }
8737 cur_arg++;
8738#else
8739 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8740 goto out_err;
8741#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008742 } else if (!strcmp(args[0], "set-mark")) {
8743#ifdef SO_MARK
8744 char *err;
8745 rule->action = HTTP_RES_ACT_SET_MARK;
8746 cur_arg = 1;
8747
8748 if (!*args[cur_arg] ||
8749 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8750 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8751 file, linenum, args[0]);
8752 goto out_err;
8753 }
8754
8755 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8756 if (err && *err != '\0') {
8757 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8758 file, linenum, err, args[0]);
8759 goto out_err;
8760 }
8761 cur_arg++;
8762 global.last_checks |= LSTCHK_NETADM;
8763#else
8764 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8765 goto out_err;
8766#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008767 } else if (!strcmp(args[0], "set-log-level")) {
8768 rule->action = HTTP_RES_ACT_SET_LOGL;
8769 cur_arg = 1;
8770
8771 if (!*args[cur_arg] ||
8772 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8773 bad_log_level:
8774 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8775 file, linenum, args[0]);
8776 goto out_err;
8777 }
8778 if (strcmp(args[cur_arg], "silent") == 0)
8779 rule->arg.loglevel = -1;
8780 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8781 goto bad_log_level;
8782 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008783 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8784 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8785 cur_arg = 1;
8786
8787 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8788 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8789 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8790 file, linenum, args[0]);
8791 goto out_err;
8792 }
8793
8794 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8795 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8796 LIST_INIT(&rule->arg.hdr_add.fmt);
8797
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008798 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008799 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008800 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8801 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008802 free(proxy->conf.lfs_file);
8803 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8804 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008805 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008806 } else if (strcmp(args[0], "del-header") == 0) {
8807 rule->action = HTTP_RES_ACT_DEL_HDR;
8808 cur_arg = 1;
8809
8810 if (!*args[cur_arg] ||
8811 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8812 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8813 file, linenum, args[0]);
8814 goto out_err;
8815 }
8816
8817 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8818 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8819
8820 proxy->conf.args.ctx = ARGC_HRS;
8821 free(proxy->conf.lfs_file);
8822 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8823 proxy->conf.lfs_line = proxy->conf.args.line;
8824 cur_arg += 1;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008825 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008826 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 +02008827 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8828 goto out_err;
8829 }
8830
8831 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8832 struct acl_cond *cond;
8833 char *errmsg = NULL;
8834
8835 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8836 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8837 file, linenum, args[0], errmsg);
8838 free(errmsg);
8839 goto out_err;
8840 }
8841 rule->cond = cond;
8842 }
8843 else if (*args[cur_arg]) {
8844 Alert("parsing [%s:%d]: 'http-response %s' expects"
8845 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8846 file, linenum, args[0], args[cur_arg]);
8847 goto out_err;
8848 }
8849
8850 return rule;
8851 out_err:
8852 free(rule);
8853 return NULL;
8854}
8855
Willy Tarreau4baae242012-12-27 12:00:31 +01008856/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008857 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8858 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008859 */
8860struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008861 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008862{
8863 struct redirect_rule *rule;
8864 int cur_arg;
8865 int type = REDIRECT_TYPE_NONE;
8866 int code = 302;
8867 const char *destination = NULL;
8868 const char *cookie = NULL;
8869 int cookie_set = 0;
8870 unsigned int flags = REDIRECT_FLAG_NONE;
8871 struct acl_cond *cond = NULL;
8872
8873 cur_arg = 0;
8874 while (*(args[cur_arg])) {
8875 if (strcmp(args[cur_arg], "location") == 0) {
8876 if (!*args[cur_arg + 1])
8877 goto missing_arg;
8878
8879 type = REDIRECT_TYPE_LOCATION;
8880 cur_arg++;
8881 destination = args[cur_arg];
8882 }
8883 else if (strcmp(args[cur_arg], "prefix") == 0) {
8884 if (!*args[cur_arg + 1])
8885 goto missing_arg;
8886
8887 type = REDIRECT_TYPE_PREFIX;
8888 cur_arg++;
8889 destination = args[cur_arg];
8890 }
8891 else if (strcmp(args[cur_arg], "scheme") == 0) {
8892 if (!*args[cur_arg + 1])
8893 goto missing_arg;
8894
8895 type = REDIRECT_TYPE_SCHEME;
8896 cur_arg++;
8897 destination = args[cur_arg];
8898 }
8899 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8900 if (!*args[cur_arg + 1])
8901 goto missing_arg;
8902
8903 cur_arg++;
8904 cookie = args[cur_arg];
8905 cookie_set = 1;
8906 }
8907 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8908 if (!*args[cur_arg + 1])
8909 goto missing_arg;
8910
8911 cur_arg++;
8912 cookie = args[cur_arg];
8913 cookie_set = 0;
8914 }
8915 else if (strcmp(args[cur_arg], "code") == 0) {
8916 if (!*args[cur_arg + 1])
8917 goto missing_arg;
8918
8919 cur_arg++;
8920 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008921 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008922 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008923 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008924 args[cur_arg - 1], args[cur_arg]);
8925 return NULL;
8926 }
8927 }
8928 else if (!strcmp(args[cur_arg],"drop-query")) {
8929 flags |= REDIRECT_FLAG_DROP_QS;
8930 }
8931 else if (!strcmp(args[cur_arg],"append-slash")) {
8932 flags |= REDIRECT_FLAG_APPEND_SLASH;
8933 }
8934 else if (strcmp(args[cur_arg], "if") == 0 ||
8935 strcmp(args[cur_arg], "unless") == 0) {
8936 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8937 if (!cond) {
8938 memprintf(errmsg, "error in condition: %s", *errmsg);
8939 return NULL;
8940 }
8941 break;
8942 }
8943 else {
8944 memprintf(errmsg,
8945 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8946 args[cur_arg]);
8947 return NULL;
8948 }
8949 cur_arg++;
8950 }
8951
8952 if (type == REDIRECT_TYPE_NONE) {
8953 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8954 return NULL;
8955 }
8956
8957 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8958 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008959 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008960
8961 if (!use_fmt) {
8962 /* old-style static redirect rule */
8963 rule->rdr_str = strdup(destination);
8964 rule->rdr_len = strlen(destination);
8965 }
8966 else {
8967 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008968
8969 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8970 * if prefix == "/", we don't want to add anything, otherwise it
8971 * makes it hard for the user to configure a self-redirection.
8972 */
8973 proxy->conf.args.ctx = ARGC_RDR;
8974 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008975 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008976 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8977 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008978 free(curproxy->conf.lfs_file);
8979 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8980 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008981 }
8982 }
8983
Willy Tarreau4baae242012-12-27 12:00:31 +01008984 if (cookie) {
8985 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8986 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8987 */
8988 rule->cookie_len = strlen(cookie);
8989 if (cookie_set) {
8990 rule->cookie_str = malloc(rule->cookie_len + 10);
8991 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8992 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8993 rule->cookie_len += 9;
8994 } else {
8995 rule->cookie_str = malloc(rule->cookie_len + 21);
8996 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8997 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8998 rule->cookie_len += 20;
8999 }
9000 }
9001 rule->type = type;
9002 rule->code = code;
9003 rule->flags = flags;
9004 LIST_INIT(&rule->list);
9005 return rule;
9006
9007 missing_arg:
9008 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9009 return NULL;
9010}
9011
Willy Tarreau8797c062007-05-07 00:55:35 +02009012/************************************************************************/
9013/* The code below is dedicated to ACL parsing and matching */
9014/************************************************************************/
9015
9016
Willy Tarreau14174bc2012-04-16 14:34:04 +02009017/* This function ensures that the prerequisites for an L7 fetch are ready,
9018 * which means that a request or response is ready. If some data is missing,
9019 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009020 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9021 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009022 *
9023 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009024 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9025 * decide whether or not an HTTP message is present ;
9026 * 0 if the requested data cannot be fetched or if it is certain that
9027 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009028 * 1 if an HTTP message is ready
9029 */
9030static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009031smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009032 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009033{
9034 struct http_txn *txn = l7;
9035 struct http_msg *msg = &txn->req;
9036
9037 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9038 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9039 */
9040
9041 if (unlikely(!s || !txn))
9042 return 0;
9043
9044 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009045 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009046
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009047 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009048 if (unlikely(!s->req))
9049 return 0;
9050
Willy Tarreauaae75e32013-03-29 12:31:49 +01009051 /* If the buffer does not leave enough free space at the end,
9052 * we must first realign it.
9053 */
9054 if (s->req->buf->p > s->req->buf->data &&
9055 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9056 buffer_slow_realign(s->req->buf);
9057
Willy Tarreau14174bc2012-04-16 14:34:04 +02009058 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009059 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009060 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009061
9062 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009063 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009064 http_msg_analyzer(msg, &txn->hdr_idx);
9065
9066 /* Still no valid request ? */
9067 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009068 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009069 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009070 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009071 }
9072 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009073 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009074 return 0;
9075 }
9076
9077 /* OK we just got a valid HTTP request. We have some minor
9078 * preparation to perform so that further checks can rely
9079 * on HTTP tests.
9080 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009081
9082 /* If the request was parsed but was too large, we must absolutely
9083 * return an error so that it is not processed. At the moment this
9084 * cannot happen, but if the parsers are to change in the future,
9085 * we want this check to be maintained.
9086 */
9087 if (unlikely(s->req->buf->i + s->req->buf->p >
9088 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9089 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009090 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009091 return 1;
9092 }
9093
Willy Tarreau9b28e032012-10-12 23:49:43 +02009094 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009095 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9096 s->flags |= SN_REDIRECTABLE;
9097
Willy Tarreau506d0502013-07-06 13:29:24 +02009098 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9099 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009100 }
9101
Willy Tarreau506d0502013-07-06 13:29:24 +02009102 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009103 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009104 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009105
9106 /* otherwise everything's ready for the request */
9107 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009108 else {
9109 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009110 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9111 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009112 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009113 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009114 }
9115
9116 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009117 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009118 return 1;
9119}
Willy Tarreau8797c062007-05-07 00:55:35 +02009120
Willy Tarreauc0239e02012-04-16 14:42:55 +02009121#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009122 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 +02009123
Willy Tarreau24e32d82012-04-23 23:55:44 +02009124#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009125 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 +02009126
Willy Tarreau8797c062007-05-07 00:55:35 +02009127
9128/* 1. Check on METHOD
9129 * We use the pre-parsed method if it is known, and store its number as an
9130 * integer. If it is unknown, we use the pointer and the length.
9131 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009132static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009133{
9134 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009135 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009136
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009137 len = strlen(text);
9138 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009139
9140 pattern->val.i = meth;
9141 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009142 trash = get_trash_chunk();
9143 if (trash->size < len) {
9144 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9145 len, trash->size);
9146 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009147 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009148 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009149 pattern->len = len;
9150 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009151 else {
9152 pattern->ptr.str = NULL;
9153 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009154 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009155 return 1;
9156}
9157
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009158/* This function fetches the method of current HTTP request and stores
9159 * it in the global pattern struct as a chunk. There are two possibilities :
9160 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9161 * in <len> and <ptr> is NULL ;
9162 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9163 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009164 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009165 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009166static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009167smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009168 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009169{
9170 int meth;
9171 struct http_txn *txn = l7;
9172
Willy Tarreau24e32d82012-04-23 23:55:44 +02009173 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009174
Willy Tarreau8797c062007-05-07 00:55:35 +02009175 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009176 smp->type = SMP_T_METH;
9177 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009178 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009179 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9180 /* ensure the indexes are not affected */
9181 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009182 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009183 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9184 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009185 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009186 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009187 return 1;
9188}
9189
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009190/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009191static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009192{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009193 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009194 struct pattern_list *lst;
9195 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009196
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009197 list_for_each_entry(lst, &expr->patterns, list) {
9198 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009199
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009200 /* well-known method */
9201 if (pattern->val.i != HTTP_METH_OTHER) {
9202 if (smp->data.meth.meth == pattern->val.i)
9203 return pattern;
9204 else
9205 continue;
9206 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009207
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009208 /* Other method, we must compare the strings */
9209 if (pattern->len != smp->data.meth.str.len)
9210 continue;
9211
9212 icase = pattern->flags & PAT_F_IGNORE_CASE;
9213 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9214 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9215 return pattern;
9216 }
9217 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009218}
9219
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009220static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009221smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009222 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009223{
9224 struct http_txn *txn = l7;
9225 char *ptr;
9226 int len;
9227
Willy Tarreauc0239e02012-04-16 14:42:55 +02009228 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009229
Willy Tarreau8797c062007-05-07 00:55:35 +02009230 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009231 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009232
9233 while ((len-- > 0) && (*ptr++ != '/'));
9234 if (len <= 0)
9235 return 0;
9236
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009237 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009238 smp->data.str.str = ptr;
9239 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009240
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009241 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009242 return 1;
9243}
9244
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009245static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009246smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009247 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009248{
9249 struct http_txn *txn = l7;
9250 char *ptr;
9251 int len;
9252
Willy Tarreauc0239e02012-04-16 14:42:55 +02009253 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009254
Willy Tarreauf26b2522012-12-14 08:33:14 +01009255 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9256 return 0;
9257
Willy Tarreau8797c062007-05-07 00:55:35 +02009258 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009259 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009260
9261 while ((len-- > 0) && (*ptr++ != '/'));
9262 if (len <= 0)
9263 return 0;
9264
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009265 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009266 smp->data.str.str = ptr;
9267 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009268
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009269 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009270 return 1;
9271}
9272
9273/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009274static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009275smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009276 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009277{
9278 struct http_txn *txn = l7;
9279 char *ptr;
9280 int len;
9281
Willy Tarreauc0239e02012-04-16 14:42:55 +02009282 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009283
Willy Tarreauf26b2522012-12-14 08:33:14 +01009284 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9285 return 0;
9286
Willy Tarreau8797c062007-05-07 00:55:35 +02009287 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009288 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009289
Willy Tarreauf853c462012-04-23 18:53:56 +02009290 smp->type = SMP_T_UINT;
9291 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009292 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009293 return 1;
9294}
9295
9296/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009297static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009298smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009299 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009300{
9301 struct http_txn *txn = l7;
9302
Willy Tarreauc0239e02012-04-16 14:42:55 +02009303 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009304
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009305 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009306 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009307 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009308 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009309 return 1;
9310}
9311
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009312static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009313smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009314 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009315{
9316 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009317 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009318
Willy Tarreauc0239e02012-04-16 14:42:55 +02009319 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009320
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009321 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 +02009322 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009323 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009324
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009325 smp->type = SMP_T_IPV4;
9326 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009327 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009328 return 1;
9329}
9330
9331static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009332smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009333 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009334{
9335 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009336 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009337
Willy Tarreauc0239e02012-04-16 14:42:55 +02009338 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009339
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009340 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 +02009341 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9342 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009343
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009344 smp->type = SMP_T_UINT;
9345 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009346 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009347 return 1;
9348}
9349
Willy Tarreau185b5c42012-04-26 15:11:51 +02009350/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9351 * Accepts an optional argument of type string containing the header field name,
9352 * and an optional argument of type signed or unsigned integer to request an
9353 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009354 * headers are considered from the first one. It does not stop on commas and
9355 * returns full lines instead (useful for User-Agent or Date for example).
9356 */
9357static int
9358smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009359 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009360{
9361 struct http_txn *txn = l7;
9362 struct hdr_idx *idx = &txn->hdr_idx;
9363 struct hdr_ctx *ctx = smp->ctx.a[0];
9364 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9365 int occ = 0;
9366 const char *name_str = NULL;
9367 int name_len = 0;
9368
9369 if (!ctx) {
9370 /* first call */
9371 ctx = &static_hdr_ctx;
9372 ctx->idx = 0;
9373 smp->ctx.a[0] = ctx;
9374 }
9375
9376 if (args) {
9377 if (args[0].type != ARGT_STR)
9378 return 0;
9379 name_str = args[0].data.str.str;
9380 name_len = args[0].data.str.len;
9381
9382 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9383 occ = args[1].data.uint;
9384 }
9385
9386 CHECK_HTTP_MESSAGE_FIRST();
9387
9388 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9389 /* search for header from the beginning */
9390 ctx->idx = 0;
9391
9392 if (!occ && !(opt & SMP_OPT_ITERATE))
9393 /* no explicit occurrence and single fetch => last header by default */
9394 occ = -1;
9395
9396 if (!occ)
9397 /* prepare to report multiple occurrences for ACL fetches */
9398 smp->flags |= SMP_F_NOT_LAST;
9399
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009400 smp->type = SMP_T_STR;
9401 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009402 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9403 return 1;
9404
9405 smp->flags &= ~SMP_F_NOT_LAST;
9406 return 0;
9407}
9408
9409/* 6. Check on HTTP header count. The number of occurrences is returned.
9410 * Accepts exactly 1 argument of type string. It does not stop on commas and
9411 * returns full lines instead (useful for User-Agent or Date for example).
9412 */
9413static int
9414smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009415 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009416{
9417 struct http_txn *txn = l7;
9418 struct hdr_idx *idx = &txn->hdr_idx;
9419 struct hdr_ctx ctx;
9420 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9421 int cnt;
9422
9423 if (!args || args->type != ARGT_STR)
9424 return 0;
9425
9426 CHECK_HTTP_MESSAGE_FIRST();
9427
9428 ctx.idx = 0;
9429 cnt = 0;
9430 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9431 cnt++;
9432
9433 smp->type = SMP_T_UINT;
9434 smp->data.uint = cnt;
9435 smp->flags = SMP_F_VOL_HDR;
9436 return 1;
9437}
9438
9439/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9440 * Accepts an optional argument of type string containing the header field name,
9441 * and an optional argument of type signed or unsigned integer to request an
9442 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009443 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009444 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009445static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009446smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009447 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009448{
9449 struct http_txn *txn = l7;
9450 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009451 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009452 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009453 int occ = 0;
9454 const char *name_str = NULL;
9455 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009456
Willy Tarreaua890d072013-04-02 12:01:06 +02009457 if (!ctx) {
9458 /* first call */
9459 ctx = &static_hdr_ctx;
9460 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009461 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009462 }
9463
Willy Tarreau185b5c42012-04-26 15:11:51 +02009464 if (args) {
9465 if (args[0].type != ARGT_STR)
9466 return 0;
9467 name_str = args[0].data.str.str;
9468 name_len = args[0].data.str.len;
9469
9470 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9471 occ = args[1].data.uint;
9472 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009473
Willy Tarreaue333ec92012-04-16 16:26:40 +02009474 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009475
Willy Tarreau185b5c42012-04-26 15:11:51 +02009476 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009477 /* search for header from the beginning */
9478 ctx->idx = 0;
9479
Willy Tarreau185b5c42012-04-26 15:11:51 +02009480 if (!occ && !(opt & SMP_OPT_ITERATE))
9481 /* no explicit occurrence and single fetch => last header by default */
9482 occ = -1;
9483
9484 if (!occ)
9485 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009486 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009487
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009488 smp->type = SMP_T_STR;
9489 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009490 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 +02009491 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009492
Willy Tarreau37406352012-04-23 16:16:37 +02009493 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009494 return 0;
9495}
9496
Willy Tarreauc11416f2007-06-17 16:58:38 +02009497/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009498 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009499 */
9500static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009501smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009502 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009503{
9504 struct http_txn *txn = l7;
9505 struct hdr_idx *idx = &txn->hdr_idx;
9506 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009507 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009508 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009509
Willy Tarreau24e32d82012-04-23 23:55:44 +02009510 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009511 return 0;
9512
Willy Tarreaue333ec92012-04-16 16:26:40 +02009513 CHECK_HTTP_MESSAGE_FIRST();
9514
Willy Tarreau33a7e692007-06-10 19:45:56 +02009515 ctx.idx = 0;
9516 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009517 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 +02009518 cnt++;
9519
Willy Tarreauf853c462012-04-23 18:53:56 +02009520 smp->type = SMP_T_UINT;
9521 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009522 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009523 return 1;
9524}
9525
Willy Tarreau185b5c42012-04-26 15:11:51 +02009526/* Fetch an HTTP header's integer value. The integer value is returned. It
9527 * takes a mandatory argument of type string and an optional one of type int
9528 * to designate a specific occurrence. It returns an unsigned integer, which
9529 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009530 */
9531static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009532smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009533 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009534{
Willy Tarreauef38c392013-07-22 16:29:32 +02009535 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009536
Willy Tarreauf853c462012-04-23 18:53:56 +02009537 if (ret > 0) {
9538 smp->type = SMP_T_UINT;
9539 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9540 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009541
Willy Tarreaud53e2422012-04-16 17:21:11 +02009542 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009543}
9544
Cyril Bonté69fa9922012-10-25 00:01:06 +02009545/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9546 * and an optional one of type int to designate a specific occurrence.
9547 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009548 */
9549static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009550smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009551 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009552{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009553 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009554
Willy Tarreauef38c392013-07-22 16:29:32 +02009555 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009556 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9557 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009558 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009559 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009560 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009561 if (smp->data.str.len < temp->size - 1) {
9562 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9563 temp->str[smp->data.str.len] = '\0';
9564 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9565 smp->type = SMP_T_IPV6;
9566 break;
9567 }
9568 }
9569 }
9570
Willy Tarreaud53e2422012-04-16 17:21:11 +02009571 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009572 if (!(smp->flags & SMP_F_NOT_LAST))
9573 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009574 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009575 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009576}
9577
Willy Tarreau737b0c12007-06-10 21:28:46 +02009578/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9579 * the first '/' after the possible hostname, and ends before the possible '?'.
9580 */
9581static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009582smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009583 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009584{
9585 struct http_txn *txn = l7;
9586 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009587
Willy Tarreauc0239e02012-04-16 14:42:55 +02009588 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009589
Willy Tarreau9b28e032012-10-12 23:49:43 +02009590 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009591 ptr = http_get_path(txn);
9592 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009593 return 0;
9594
9595 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009596 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009597 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009598
9599 while (ptr < end && *ptr != '?')
9600 ptr++;
9601
Willy Tarreauf853c462012-04-23 18:53:56 +02009602 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009603 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009604 return 1;
9605}
9606
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009607/* This produces a concatenation of the first occurrence of the Host header
9608 * followed by the path component if it begins with a slash ('/'). This means
9609 * that '*' will not be added, resulting in exactly the first Host entry.
9610 * If no Host header is found, then the path is returned as-is. The returned
9611 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009612 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009613 */
9614static int
9615smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009616 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009617{
9618 struct http_txn *txn = l7;
9619 char *ptr, *end, *beg;
9620 struct hdr_ctx ctx;
9621
9622 CHECK_HTTP_MESSAGE_FIRST();
9623
9624 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009625 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 +02009626 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009627 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009628
9629 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009630 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009631 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009632 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009633 smp->data.str.len = ctx.vlen;
9634
9635 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009636 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 +02009637 beg = http_get_path(txn);
9638 if (!beg)
9639 beg = end;
9640
9641 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9642
9643 if (beg < ptr && *beg == '/') {
9644 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9645 smp->data.str.len += ptr - beg;
9646 }
9647
9648 smp->flags = SMP_F_VOL_1ST;
9649 return 1;
9650}
9651
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009652/* This produces a 32-bit hash of the concatenation of the first occurrence of
9653 * the Host header followed by the path component if it begins with a slash ('/').
9654 * This means that '*' will not be added, resulting in exactly the first Host
9655 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009656 * is hashed using the path hash followed by a full avalanche hash and provides a
9657 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009658 * high-traffic sites without having to store whole paths.
9659 */
9660static int
9661smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009662 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009663{
9664 struct http_txn *txn = l7;
9665 struct hdr_ctx ctx;
9666 unsigned int hash = 0;
9667 char *ptr, *beg, *end;
9668 int len;
9669
9670 CHECK_HTTP_MESSAGE_FIRST();
9671
9672 ctx.idx = 0;
9673 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9674 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9675 ptr = ctx.line + ctx.val;
9676 len = ctx.vlen;
9677 while (len--)
9678 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9679 }
9680
9681 /* now retrieve the path */
9682 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9683 beg = http_get_path(txn);
9684 if (!beg)
9685 beg = end;
9686
9687 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9688
9689 if (beg < ptr && *beg == '/') {
9690 while (beg < ptr)
9691 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9692 }
9693 hash = full_hash(hash);
9694
9695 smp->type = SMP_T_UINT;
9696 smp->data.uint = hash;
9697 smp->flags = SMP_F_VOL_1ST;
9698 return 1;
9699}
9700
Willy Tarreau4a550602012-12-09 14:53:32 +01009701/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009702 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9703 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9704 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009705 * that in environments where IPv6 is insignificant, truncating the output to
9706 * 8 bytes would still work.
9707 */
9708static int
9709smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009710 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009711{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009712 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009713 struct connection *cli_conn = objt_conn(l4->si[0].end);
9714
9715 if (!cli_conn)
9716 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009717
Willy Tarreauef38c392013-07-22 16:29:32 +02009718 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009719 return 0;
9720
Willy Tarreau47ca5452012-12-23 20:22:19 +01009721 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009722 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9723 temp->len += sizeof(smp->data.uint);
9724
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009725 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009726 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009727 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009728 temp->len += 4;
9729 break;
9730 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009731 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009732 temp->len += 16;
9733 break;
9734 default:
9735 return 0;
9736 }
9737
9738 smp->data.str = *temp;
9739 smp->type = SMP_T_BIN;
9740 return 1;
9741}
9742
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009743static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009744smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009745 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009746{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009747 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9748 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9749 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009750
Willy Tarreau24e32d82012-04-23 23:55:44 +02009751 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009752
Willy Tarreauf853c462012-04-23 18:53:56 +02009753 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009754 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009755 return 1;
9756}
9757
Willy Tarreau7f18e522010-10-22 20:04:13 +02009758/* return a valid test if the current request is the first one on the connection */
9759static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009760smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009761 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009762{
9763 if (!s)
9764 return 0;
9765
Willy Tarreauf853c462012-04-23 18:53:56 +02009766 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009767 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009768 return 1;
9769}
9770
Willy Tarreau34db1082012-04-19 17:16:54 +02009771/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009772static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009773smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009774 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009775{
9776
Willy Tarreau24e32d82012-04-23 23:55:44 +02009777 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009778 return 0;
9779
Willy Tarreauc0239e02012-04-16 14:42:55 +02009780 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009781
Willy Tarreauc0239e02012-04-16 14:42:55 +02009782 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009783 return 0;
9784
Willy Tarreauf853c462012-04-23 18:53:56 +02009785 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009786 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009787 return 1;
9788}
Willy Tarreau8797c062007-05-07 00:55:35 +02009789
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009790/* Accepts exactly 1 argument of type userlist */
9791static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009792smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009793 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009794{
9795
9796 if (!args || args->type != ARGT_USR)
9797 return 0;
9798
9799 CHECK_HTTP_MESSAGE_FIRST();
9800
9801 if (!get_http_auth(l4))
9802 return 0;
9803
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009804 /* if the user does not belong to the userlist or has a wrong password,
9805 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009806 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009807 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009808 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9809 return 0;
9810
9811 /* pat_match_auth() will need the user list */
9812 smp->ctx.a[0] = args->data.usr;
9813
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009814 smp->type = SMP_T_STR;
9815 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009816 smp->data.str.str = l4->txn.auth.user;
9817 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009818
9819 return 1;
9820}
9821
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009822/* Try to find the next occurrence of a cookie name in a cookie header value.
9823 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9824 * the cookie value is returned into *value and *value_l, and the function
9825 * returns a pointer to the next pointer to search from if the value was found.
9826 * Otherwise if the cookie was not found, NULL is returned and neither value
9827 * nor value_l are touched. The input <hdr> string should first point to the
9828 * header's value, and the <hdr_end> pointer must point to the first character
9829 * not part of the value. <list> must be non-zero if value may represent a list
9830 * of values (cookie headers). This makes it faster to abort parsing when no
9831 * list is expected.
9832 */
9833static char *
9834extract_cookie_value(char *hdr, const char *hdr_end,
9835 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009836 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009837{
9838 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9839 char *next;
9840
9841 /* we search at least a cookie name followed by an equal, and more
9842 * generally something like this :
9843 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9844 */
9845 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9846 /* Iterate through all cookies on this line */
9847
9848 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9849 att_beg++;
9850
9851 /* find att_end : this is the first character after the last non
9852 * space before the equal. It may be equal to hdr_end.
9853 */
9854 equal = att_end = att_beg;
9855
9856 while (equal < hdr_end) {
9857 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9858 break;
9859 if (http_is_spht[(unsigned char)*equal++])
9860 continue;
9861 att_end = equal;
9862 }
9863
9864 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9865 * is between <att_beg> and <equal>, both may be identical.
9866 */
9867
9868 /* look for end of cookie if there is an equal sign */
9869 if (equal < hdr_end && *equal == '=') {
9870 /* look for the beginning of the value */
9871 val_beg = equal + 1;
9872 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9873 val_beg++;
9874
9875 /* find the end of the value, respecting quotes */
9876 next = find_cookie_value_end(val_beg, hdr_end);
9877
9878 /* make val_end point to the first white space or delimitor after the value */
9879 val_end = next;
9880 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9881 val_end--;
9882 } else {
9883 val_beg = val_end = next = equal;
9884 }
9885
9886 /* We have nothing to do with attributes beginning with '$'. However,
9887 * they will automatically be removed if a header before them is removed,
9888 * since they're supposed to be linked together.
9889 */
9890 if (*att_beg == '$')
9891 continue;
9892
9893 /* Ignore cookies with no equal sign */
9894 if (equal == next)
9895 continue;
9896
9897 /* Now we have the cookie name between att_beg and att_end, and
9898 * its value between val_beg and val_end.
9899 */
9900
9901 if (att_end - att_beg == cookie_name_l &&
9902 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9903 /* let's return this value and indicate where to go on from */
9904 *value = val_beg;
9905 *value_l = val_end - val_beg;
9906 return next + 1;
9907 }
9908
9909 /* Set-Cookie headers only have the name in the first attr=value part */
9910 if (!list)
9911 break;
9912 }
9913
9914 return NULL;
9915}
9916
William Lallemanda43ba4e2014-01-28 18:14:25 +01009917/* Fetch a captured HTTP request header. The index is the position of
9918 * the "capture" option in the configuration file
9919 */
9920static int
9921smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9922 const struct arg *args, struct sample *smp, const char *kw)
9923{
9924 struct proxy *fe = l4->fe;
9925 struct http_txn *txn = l7;
9926 int idx;
9927
9928 if (!args || args->type != ARGT_UINT)
9929 return 0;
9930
9931 idx = args->data.uint;
9932
9933 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9934 return 0;
9935
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009936 smp->type = SMP_T_STR;
9937 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009938 smp->data.str.str = txn->req.cap[idx];
9939 smp->data.str.len = strlen(txn->req.cap[idx]);
9940
9941 return 1;
9942}
9943
9944/* Fetch a captured HTTP response header. The index is the position of
9945 * the "capture" option in the configuration file
9946 */
9947static int
9948smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9949 const struct arg *args, struct sample *smp, const char *kw)
9950{
9951 struct proxy *fe = l4->fe;
9952 struct http_txn *txn = l7;
9953 int idx;
9954
9955 if (!args || args->type != ARGT_UINT)
9956 return 0;
9957
9958 idx = args->data.uint;
9959
9960 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9961 return 0;
9962
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009963 smp->type = SMP_T_STR;
9964 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009965 smp->data.str.str = txn->rsp.cap[idx];
9966 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9967
9968 return 1;
9969}
9970
William Lallemand65ad6e12014-01-31 15:08:02 +01009971/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9972static int
9973smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9974 const struct arg *args, struct sample *smp, const char *kw)
9975{
9976 struct chunk *temp;
9977 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009978 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009979
9980 if (!txn->uri)
9981 return 0;
9982
William Lallemand96a77852014-02-05 00:30:02 +01009983 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009984
William Lallemand96a77852014-02-05 00:30:02 +01009985 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9986 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009987
William Lallemand96a77852014-02-05 00:30:02 +01009988 temp = get_trash_chunk();
9989 temp->str = txn->uri;
9990 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009991 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009992 smp->type = SMP_T_STR;
9993 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009994
9995 return 1;
9996
9997}
9998
9999/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10000static int
10001smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10002 const struct arg *args, struct sample *smp, const char *kw)
10003{
10004 struct chunk *temp;
10005 struct http_txn *txn = l7;
10006 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010007
10008 if (!txn->uri)
10009 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010010
William Lallemand65ad6e12014-01-31 15:08:02 +010010011 ptr = txn->uri;
10012
10013 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10014 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010015
William Lallemand65ad6e12014-01-31 15:08:02 +010010016 if (!*ptr)
10017 return 0;
10018
10019 ptr++; /* skip the space */
10020
10021 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010022 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010023 if (!ptr)
10024 return 0;
10025 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10026 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010027
10028 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010029 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010030 smp->type = SMP_T_STR;
10031 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010032
10033 return 1;
10034}
10035
10036
Willy Tarreaue333ec92012-04-16 16:26:40 +020010037/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010038 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010039 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010040 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010041 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010042 * Accepts exactly 1 argument of type string. If the input options indicate
10043 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010044 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010045 */
10046static int
Willy Tarreau51539362012-05-08 12:46:28 +020010047smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010048 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010049{
10050 struct http_txn *txn = l7;
10051 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010052 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010053 const struct http_msg *msg;
10054 const char *hdr_name;
10055 int hdr_name_len;
10056 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010057 int occ = 0;
10058 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010059
Willy Tarreau24e32d82012-04-23 23:55:44 +020010060 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010061 return 0;
10062
Willy Tarreaua890d072013-04-02 12:01:06 +020010063 if (!ctx) {
10064 /* first call */
10065 ctx = &static_hdr_ctx;
10066 ctx->idx = 0;
10067 smp->ctx.a[2] = ctx;
10068 }
10069
Willy Tarreaue333ec92012-04-16 16:26:40 +020010070 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010071
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010072 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010073 msg = &txn->req;
10074 hdr_name = "Cookie";
10075 hdr_name_len = 6;
10076 } else {
10077 msg = &txn->rsp;
10078 hdr_name = "Set-Cookie";
10079 hdr_name_len = 10;
10080 }
10081
Willy Tarreau28376d62012-04-26 21:26:10 +020010082 if (!occ && !(opt & SMP_OPT_ITERATE))
10083 /* no explicit occurrence and single fetch => last cookie by default */
10084 occ = -1;
10085
10086 /* OK so basically here, either we want only one value and it's the
10087 * last one, or we want to iterate over all of them and we fetch the
10088 * next one.
10089 */
10090
Willy Tarreau9b28e032012-10-12 23:49:43 +020010091 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010092 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010093 /* search for the header from the beginning, we must first initialize
10094 * the search parameters.
10095 */
Willy Tarreau37406352012-04-23 16:16:37 +020010096 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010097 ctx->idx = 0;
10098 }
10099
Willy Tarreau28376d62012-04-26 21:26:10 +020010100 smp->flags |= SMP_F_VOL_HDR;
10101
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010102 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010103 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10104 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010105 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10106 goto out;
10107
Willy Tarreau24e32d82012-04-23 23:55:44 +020010108 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010109 continue;
10110
Willy Tarreau37406352012-04-23 16:16:37 +020010111 smp->ctx.a[0] = ctx->line + ctx->val;
10112 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010113 }
10114
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010115 smp->type = SMP_T_STR;
10116 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010117 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010118 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010119 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010120 &smp->data.str.str,
10121 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010122 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010123 found = 1;
10124 if (occ >= 0) {
10125 /* one value was returned into smp->data.str.{str,len} */
10126 smp->flags |= SMP_F_NOT_LAST;
10127 return 1;
10128 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010129 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010130 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010131 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010132 /* all cookie headers and values were scanned. If we're looking for the
10133 * last occurrence, we may return it now.
10134 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010135 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010136 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010137 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010138}
10139
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010140/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010141 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010142 * multiple cookies may be parsed on the same line. The returned sample is of
10143 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010144 */
10145static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010146smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010147 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010148{
10149 struct http_txn *txn = l7;
10150 struct hdr_idx *idx = &txn->hdr_idx;
10151 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010152 const struct http_msg *msg;
10153 const char *hdr_name;
10154 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010155 int cnt;
10156 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010157 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010158
Willy Tarreau24e32d82012-04-23 23:55:44 +020010159 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010160 return 0;
10161
Willy Tarreaue333ec92012-04-16 16:26:40 +020010162 CHECK_HTTP_MESSAGE_FIRST();
10163
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010164 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010165 msg = &txn->req;
10166 hdr_name = "Cookie";
10167 hdr_name_len = 6;
10168 } else {
10169 msg = &txn->rsp;
10170 hdr_name = "Set-Cookie";
10171 hdr_name_len = 10;
10172 }
10173
Willy Tarreau9b28e032012-10-12 23:49:43 +020010174 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010175 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010176 ctx.idx = 0;
10177 cnt = 0;
10178
10179 while (1) {
10180 /* Note: val_beg == NULL every time we need to fetch a new header */
10181 if (!val_beg) {
10182 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10183 break;
10184
Willy Tarreau24e32d82012-04-23 23:55:44 +020010185 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010186 continue;
10187
10188 val_beg = ctx.line + ctx.val;
10189 val_end = val_beg + ctx.vlen;
10190 }
10191
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010192 smp->type = SMP_T_STR;
10193 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010194 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010195 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010196 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010197 &smp->data.str.str,
10198 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010199 cnt++;
10200 }
10201 }
10202
Willy Tarreaub169eba2013-12-16 15:14:43 +010010203 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010204 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010205 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010206 return 1;
10207}
10208
Willy Tarreau51539362012-05-08 12:46:28 +020010209/* Fetch an cookie's integer value. The integer value is returned. It
10210 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10211 */
10212static int
10213smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010214 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010215{
Willy Tarreauef38c392013-07-22 16:29:32 +020010216 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010217
10218 if (ret > 0) {
10219 smp->type = SMP_T_UINT;
10220 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10221 }
10222
10223 return ret;
10224}
10225
Willy Tarreau8797c062007-05-07 00:55:35 +020010226/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010227/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010228/************************************************************************/
10229
David Cournapeau16023ee2010-12-23 20:55:41 +090010230/*
10231 * Given a path string and its length, find the position of beginning of the
10232 * query string. Returns NULL if no query string is found in the path.
10233 *
10234 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10235 *
10236 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10237 */
bedis4c75cca2012-10-05 08:38:24 +020010238static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010239{
10240 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010241
bedis4c75cca2012-10-05 08:38:24 +020010242 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010243 return p ? p + 1 : NULL;
10244}
10245
bedis4c75cca2012-10-05 08:38:24 +020010246static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010247{
bedis4c75cca2012-10-05 08:38:24 +020010248 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010249}
10250
10251/*
10252 * Given a url parameter, find the starting position of the first occurence,
10253 * or NULL if the parameter is not found.
10254 *
10255 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10256 * the function will return query_string+8.
10257 */
10258static char*
10259find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010260 char* url_param_name, size_t url_param_name_l,
10261 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010262{
10263 char *pos, *last;
10264
10265 pos = query_string;
10266 last = query_string + query_string_l - url_param_name_l - 1;
10267
10268 while (pos <= last) {
10269 if (pos[url_param_name_l] == '=') {
10270 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10271 return pos;
10272 pos += url_param_name_l + 1;
10273 }
bedis4c75cca2012-10-05 08:38:24 +020010274 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010275 pos++;
10276 pos++;
10277 }
10278 return NULL;
10279}
10280
10281/*
10282 * Given a url parameter name, returns its value and size into *value and
10283 * *value_l respectively, and returns non-zero. If the parameter is not found,
10284 * zero is returned and value/value_l are not touched.
10285 */
10286static int
10287find_url_param_value(char* path, size_t path_l,
10288 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010289 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010290{
10291 char *query_string, *qs_end;
10292 char *arg_start;
10293 char *value_start, *value_end;
10294
bedis4c75cca2012-10-05 08:38:24 +020010295 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010296 if (!query_string)
10297 return 0;
10298
10299 qs_end = path + path_l;
10300 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010301 url_param_name, url_param_name_l,
10302 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010303 if (!arg_start)
10304 return 0;
10305
10306 value_start = arg_start + url_param_name_l + 1;
10307 value_end = value_start;
10308
bedis4c75cca2012-10-05 08:38:24 +020010309 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010310 value_end++;
10311
10312 *value = value_start;
10313 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010314 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010315}
10316
10317static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010318smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010319 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010320{
bedis4c75cca2012-10-05 08:38:24 +020010321 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010322 struct http_txn *txn = l7;
10323 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010324
bedis4c75cca2012-10-05 08:38:24 +020010325 if (!args || args[0].type != ARGT_STR ||
10326 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010327 return 0;
10328
10329 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010330
bedis4c75cca2012-10-05 08:38:24 +020010331 if (args[1].type)
10332 delim = *args[1].data.str.str;
10333
Willy Tarreau9b28e032012-10-12 23:49:43 +020010334 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010335 args->data.str.str, args->data.str.len,
10336 &smp->data.str.str, &smp->data.str.len,
10337 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010338 return 0;
10339
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010340 smp->type = SMP_T_STR;
10341 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010342 return 1;
10343}
10344
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010345/* Return the signed integer value for the specified url parameter (see url_param
10346 * above).
10347 */
10348static int
10349smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010350 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010351{
Willy Tarreauef38c392013-07-22 16:29:32 +020010352 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010353
10354 if (ret > 0) {
10355 smp->type = SMP_T_UINT;
10356 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10357 }
10358
10359 return ret;
10360}
10361
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010362/* This produces a 32-bit hash of the concatenation of the first occurrence of
10363 * the Host header followed by the path component if it begins with a slash ('/').
10364 * This means that '*' will not be added, resulting in exactly the first Host
10365 * entry. If no Host header is found, then the path is used. The resulting value
10366 * is hashed using the url hash followed by a full avalanche hash and provides a
10367 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10368 * high-traffic sites without having to store whole paths.
10369 * this differs from the base32 functions in that it includes the url parameters
10370 * as well as the path
10371 */
10372static int
10373smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010374 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010375{
10376 struct http_txn *txn = l7;
10377 struct hdr_ctx ctx;
10378 unsigned int hash = 0;
10379 char *ptr, *beg, *end;
10380 int len;
10381
10382 CHECK_HTTP_MESSAGE_FIRST();
10383
10384 ctx.idx = 0;
10385 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10386 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10387 ptr = ctx.line + ctx.val;
10388 len = ctx.vlen;
10389 while (len--)
10390 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10391 }
10392
10393 /* now retrieve the path */
10394 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10395 beg = http_get_path(txn);
10396 if (!beg)
10397 beg = end;
10398
10399 for (ptr = beg; ptr < end ; ptr++);
10400
10401 if (beg < ptr && *beg == '/') {
10402 while (beg < ptr)
10403 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10404 }
10405 hash = full_hash(hash);
10406
10407 smp->type = SMP_T_UINT;
10408 smp->data.uint = hash;
10409 smp->flags = SMP_F_VOL_1ST;
10410 return 1;
10411}
10412
10413/* This concatenates the source address with the 32-bit hash of the Host and
10414 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10415 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10416 * on the source address length. The URL hash is stored before the address so
10417 * that in environments where IPv6 is insignificant, truncating the output to
10418 * 8 bytes would still work.
10419 */
10420static int
10421smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010422 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010423{
10424 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010425 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010426
Willy Tarreaue155ec22013-11-18 18:33:22 +010010427 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010428 return 0;
10429
10430 temp = get_trash_chunk();
10431 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10432 temp->len += sizeof(smp->data.uint);
10433
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010434 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010435 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010436 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010437 temp->len += 4;
10438 break;
10439 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010440 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010441 temp->len += 16;
10442 break;
10443 default:
10444 return 0;
10445 }
10446
10447 smp->data.str = *temp;
10448 smp->type = SMP_T_BIN;
10449 return 1;
10450}
10451
Willy Tarreau185b5c42012-04-26 15:11:51 +020010452/* This function is used to validate the arguments passed to any "hdr" fetch
10453 * keyword. These keywords support an optional positive or negative occurrence
10454 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10455 * is assumed that the types are already the correct ones. Returns 0 on error,
10456 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10457 * error message in case of error, that the caller is responsible for freeing.
10458 * The initial location must either be freeable or NULL.
10459 */
10460static int val_hdr(struct arg *arg, char **err_msg)
10461{
10462 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010463 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010464 return 0;
10465 }
10466 return 1;
10467}
10468
Willy Tarreau276fae92013-07-25 14:36:01 +020010469/* takes an UINT value on input supposed to represent the time since EPOCH,
10470 * adds an optional offset found in args[0] and emits a string representing
10471 * the date in RFC-1123/5322 format.
10472 */
10473static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10474{
10475 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10476 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10477 struct chunk *temp;
10478 struct tm *tm;
10479 time_t curr_date = smp->data.uint;
10480
10481 /* add offset */
10482 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10483 curr_date += args[0].data.sint;
10484
10485 tm = gmtime(&curr_date);
10486
10487 temp = get_trash_chunk();
10488 temp->len = snprintf(temp->str, temp->size - temp->len,
10489 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10490 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10491 tm->tm_hour, tm->tm_min, tm->tm_sec);
10492
10493 smp->data.str = *temp;
10494 smp->type = SMP_T_STR;
10495 return 1;
10496}
10497
Thierry FOURNIERad903512014-04-11 17:51:01 +020010498/* Match language range with language tag. RFC2616 14.4:
10499 *
10500 * A language-range matches a language-tag if it exactly equals
10501 * the tag, or if it exactly equals a prefix of the tag such
10502 * that the first tag character following the prefix is "-".
10503 *
10504 * Return 1 if the strings match, else return 0.
10505 */
10506static inline int language_range_match(const char *range, int range_len,
10507 const char *tag, int tag_len)
10508{
10509 const char *end = range + range_len;
10510 const char *tend = tag + tag_len;
10511 while (range < end) {
10512 if (*range == '-' && tag == tend)
10513 return 1;
10514 if (*range != *tag || tag == tend)
10515 return 0;
10516 range++;
10517 tag++;
10518 }
10519 /* Return true only if the last char of the tag is matched. */
10520 return tag == tend;
10521}
10522
10523/* Arguments: The list of expected value, the number of parts returned and the separator */
10524static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
10525{
10526 const char *al = smp->data.str.str;
10527 const char *end = al + smp->data.str.len;
10528 const char *token;
10529 int toklen;
10530 int qvalue;
10531 const char *str;
10532 const char *w;
10533 int best_q = 0;
10534
10535 /* Set the constant to the sample, because the output of the
10536 * function will be peek in the constant configuration string.
10537 */
10538 smp->flags |= SMP_F_CONST;
10539 smp->data.str.size = 0;
10540 smp->data.str.str = "";
10541 smp->data.str.len = 0;
10542
10543 /* Parse the accept language */
10544 while (1) {
10545
10546 /* Jump spaces, quit if the end is detected. */
10547 while (al < end && isspace(*al))
10548 al++;
10549 if (al >= end)
10550 break;
10551
10552 /* Start of the fisrt word. */
10553 token = al;
10554
10555 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
10556 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
10557 al++;
10558 if (al == token)
10559 goto expect_comma;
10560
10561 /* Length of the token. */
10562 toklen = al - token;
10563 qvalue = 1000;
10564
10565 /* Check if the token exists in the list. If the token not exists,
10566 * jump to the next token.
10567 */
10568 str = args[0].data.str.str;
10569 w = str;
10570 while (1) {
10571 if (*str == ';' || *str == '\0') {
10572 if (language_range_match(token, toklen, w, str-w))
10573 goto look_for_q;
10574 if (*str == '\0')
10575 goto expect_comma;
10576 w = str + 1;
10577 }
10578 str++;
10579 }
10580 goto expect_comma;
10581
10582look_for_q:
10583
10584 /* Jump spaces, quit if the end is detected. */
10585 while (al < end && isspace(*al))
10586 al++;
10587 if (al >= end)
10588 goto process_value;
10589
10590 /* If ',' is found, process the result */
10591 if (*al == ',')
10592 goto process_value;
10593
10594 /* If the character is different from ';', look
10595 * for the end of the header part in best effort.
10596 */
10597 if (*al != ';')
10598 goto expect_comma;
10599
10600 /* Assumes that the char is ';', now expect "q=". */
10601 al++;
10602
10603 /* Jump spaces, process value if the end is detected. */
10604 while (al < end && isspace(*al))
10605 al++;
10606 if (al >= end)
10607 goto process_value;
10608
10609 /* Expect 'q'. If no 'q', continue in best effort */
10610 if (*al != 'q')
10611 goto process_value;
10612 al++;
10613
10614 /* Jump spaces, process value if the end is detected. */
10615 while (al < end && isspace(*al))
10616 al++;
10617 if (al >= end)
10618 goto process_value;
10619
10620 /* Expect '='. If no '=', continue in best effort */
10621 if (*al != '=')
10622 goto process_value;
10623 al++;
10624
10625 /* Jump spaces, process value if the end is detected. */
10626 while (al < end && isspace(*al))
10627 al++;
10628 if (al >= end)
10629 goto process_value;
10630
10631 /* Parse the q value. */
10632 qvalue = parse_qvalue(al, &al);
10633
10634process_value:
10635
10636 /* If the new q value is the best q value, then store the associated
10637 * language in the response. If qvalue is the biggest value (1000),
10638 * break the process.
10639 */
10640 if (qvalue > best_q) {
10641 smp->data.str.str = (char *)w;
10642 smp->data.str.len = str - w;
10643 if (qvalue >= 1000)
10644 break;
10645 best_q = qvalue;
10646 }
10647
10648expect_comma:
10649
10650 /* Expect comma or end. If the end is detected, quit the loop. */
10651 while (al < end && *al != ',')
10652 al++;
10653 if (al >= end)
10654 break;
10655
10656 /* Comma is found, jump it and restart the analyzer. */
10657 al++;
10658 }
10659
10660 /* Set default value if required. */
10661 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
10662 smp->data.str.str = args[1].data.str.str;
10663 smp->data.str.len = args[1].data.str.len;
10664 }
10665
10666 /* Return true only if a matching language was found. */
10667 return smp->data.str.len != 0;
10668}
10669
Willy Tarreau4a568972010-05-12 08:08:50 +020010670/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010671/* All supported ACL keywords must be declared here. */
10672/************************************************************************/
10673
10674/* Note: must not be declared <const> as its list will be overwritten.
10675 * Please take care of keeping this list alphabetically sorted.
10676 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010677static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010678 { "base", "base", PAT_MATCH_STR },
10679 { "base_beg", "base", PAT_MATCH_BEG },
10680 { "base_dir", "base", PAT_MATCH_DIR },
10681 { "base_dom", "base", PAT_MATCH_DOM },
10682 { "base_end", "base", PAT_MATCH_END },
10683 { "base_len", "base", PAT_MATCH_LEN },
10684 { "base_reg", "base", PAT_MATCH_REG },
10685 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010686
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010687 { "cook", "req.cook", PAT_MATCH_STR },
10688 { "cook_beg", "req.cook", PAT_MATCH_BEG },
10689 { "cook_dir", "req.cook", PAT_MATCH_DIR },
10690 { "cook_dom", "req.cook", PAT_MATCH_DOM },
10691 { "cook_end", "req.cook", PAT_MATCH_END },
10692 { "cook_len", "req.cook", PAT_MATCH_LEN },
10693 { "cook_reg", "req.cook", PAT_MATCH_REG },
10694 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010695
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010696 { "hdr", "req.hdr", PAT_MATCH_STR },
10697 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
10698 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
10699 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
10700 { "hdr_end", "req.hdr", PAT_MATCH_END },
10701 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
10702 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
10703 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010704
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010705 /* these two declarations uses strings with list storage (in place
10706 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
10707 * and delete functions are relative to the list management. The parse
10708 * and match method are related to the corresponding fetch methods. This
10709 * is very particular ACL declaration mode.
10710 */
10711 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
10712 { "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 +020010713
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010714 { "path", "path", PAT_MATCH_STR },
10715 { "path_beg", "path", PAT_MATCH_BEG },
10716 { "path_dir", "path", PAT_MATCH_DIR },
10717 { "path_dom", "path", PAT_MATCH_DOM },
10718 { "path_end", "path", PAT_MATCH_END },
10719 { "path_len", "path", PAT_MATCH_LEN },
10720 { "path_reg", "path", PAT_MATCH_REG },
10721 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010722
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010723 { "req_ver", "req.ver", PAT_MATCH_STR },
10724 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010725
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010726 { "scook", "res.cook", PAT_MATCH_STR },
10727 { "scook_beg", "res.cook", PAT_MATCH_BEG },
10728 { "scook_dir", "res.cook", PAT_MATCH_DIR },
10729 { "scook_dom", "res.cook", PAT_MATCH_DOM },
10730 { "scook_end", "res.cook", PAT_MATCH_END },
10731 { "scook_len", "res.cook", PAT_MATCH_LEN },
10732 { "scook_reg", "res.cook", PAT_MATCH_REG },
10733 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010734
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010735 { "shdr", "res.hdr", PAT_MATCH_STR },
10736 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
10737 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
10738 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
10739 { "shdr_end", "res.hdr", PAT_MATCH_END },
10740 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
10741 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
10742 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010743
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010744 { "url", "url", PAT_MATCH_STR },
10745 { "url_beg", "url", PAT_MATCH_BEG },
10746 { "url_dir", "url", PAT_MATCH_DIR },
10747 { "url_dom", "url", PAT_MATCH_DOM },
10748 { "url_end", "url", PAT_MATCH_END },
10749 { "url_len", "url", PAT_MATCH_LEN },
10750 { "url_reg", "url", PAT_MATCH_REG },
10751 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010752
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010753 { "urlp", "urlp", PAT_MATCH_STR },
10754 { "urlp_beg", "urlp", PAT_MATCH_BEG },
10755 { "urlp_dir", "urlp", PAT_MATCH_DIR },
10756 { "urlp_dom", "urlp", PAT_MATCH_DOM },
10757 { "urlp_end", "urlp", PAT_MATCH_END },
10758 { "urlp_len", "urlp", PAT_MATCH_LEN },
10759 { "urlp_reg", "urlp", PAT_MATCH_REG },
10760 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010761
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010762 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010763}};
10764
10765/************************************************************************/
10766/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010767/************************************************************************/
10768/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010769static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010770 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010771 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10772 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10773
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010774 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10775 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemand65ad6e12014-01-31 15:08:02 +010010776
William Lallemanda43ba4e2014-01-28 18:14:25 +010010777 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010778 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
10779 { "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 +010010780
Willy Tarreau409bcde2013-01-08 00:31:00 +010010781 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10782 * are only here to match the ACL's name, are request-only and are used
10783 * for ACL compatibility only.
10784 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010785 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10786 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010787 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10788 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10789
10790 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10791 * only here to match the ACL's name, are request-only and are used for
10792 * ACL compatibility only.
10793 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010794 { "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 +010010795 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10796 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10797 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10798
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010799 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010800 { "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 +010010801 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010802 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010803 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010804
10805 /* HTTP protocol on the request path */
10806 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010807 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010808
10809 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010810 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10811 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010812
10813 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010814 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10815 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010816
Willy Tarreau18ed2562013-01-14 15:56:36 +010010817 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010818 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010819 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10820 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10821
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010822 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010823 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010824 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010825 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10826 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10827 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10828
10829 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010830 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010831 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10832 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10833
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010834 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010835 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010836 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010837 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10838 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10839 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10840
Willy Tarreau409bcde2013-01-08 00:31:00 +010010841 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010842 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010843 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10844 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010845 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010846
10847 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010848 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010849 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10850 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10851 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10852
10853 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010854 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010855 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10856 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010857 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10858 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010859 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10860 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010861 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10862 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010863}};
10864
Willy Tarreau8797c062007-05-07 00:55:35 +020010865
Willy Tarreau276fae92013-07-25 14:36:01 +020010866/* Note: must not be declared <const> as its list will be overwritten */
10867static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020010868 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
10869 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020010870 { NULL, NULL, 0, 0, 0 },
10871}};
10872
Willy Tarreau8797c062007-05-07 00:55:35 +020010873__attribute__((constructor))
10874static void __http_protocol_init(void)
10875{
10876 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010877 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010878 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010879}
10880
10881
Willy Tarreau58f10d72006-12-04 02:26:12 +010010882/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010883 * Local variables:
10884 * c-indent-level: 8
10885 * c-basic-offset: 8
10886 * End:
10887 */