blob: f9554b422469196162f508a107b954584e82b2c4 [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))];
238
239#else
240#error "Check if your OS uses bitfields for fd_sets"
241#endif
242
Willy Tarreau80587432006-12-24 17:47:20 +0100243void init_proto_http()
244{
Willy Tarreau42250582007-04-01 01:30:43 +0200245 int i;
246 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100247 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200248
Willy Tarreau80587432006-12-24 17:47:20 +0100249 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
250 if (!http_err_msgs[msg]) {
251 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
252 abort();
253 }
254
255 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
256 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
257 }
Willy Tarreau42250582007-04-01 01:30:43 +0200258
259 /* initialize the log header encoding map : '{|}"#' should be encoded with
260 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
261 * URL encoding only requires '"', '#' to be encoded as well as non-
262 * printable characters above.
263 */
264 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
265 memset(url_encode_map, 0, sizeof(url_encode_map));
266 for (i = 0; i < 32; i++) {
267 FD_SET(i, hdr_encode_map);
268 FD_SET(i, url_encode_map);
269 }
270 for (i = 127; i < 256; i++) {
271 FD_SET(i, hdr_encode_map);
272 FD_SET(i, url_encode_map);
273 }
274
275 tmp = "\"#{|}";
276 while (*tmp) {
277 FD_SET(*tmp, hdr_encode_map);
278 tmp++;
279 }
280
281 tmp = "\"#";
282 while (*tmp) {
283 FD_SET(*tmp, url_encode_map);
284 tmp++;
285 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200286
287 /* memory allocations */
288 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100289 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100290}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreau53b6c742006-12-17 13:37:46 +0100292/*
293 * We have 26 list of methods (1 per first letter), each of which can have
294 * up to 3 entries (2 valid, 1 null).
295 */
296struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100297 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100298 int len;
299 const char text[8];
300};
301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100302const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100303 ['C' - 'A'] = {
304 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
305 },
306 ['D' - 'A'] = {
307 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
308 },
309 ['G' - 'A'] = {
310 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
311 },
312 ['H' - 'A'] = {
313 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
314 },
315 ['P' - 'A'] = {
316 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
317 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
318 },
319 ['T' - 'A'] = {
320 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
321 },
322 /* rest is empty like this :
323 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
324 */
325};
326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100327/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100329 * RFC2616 for those chars.
330 */
331
332const char http_is_spht[256] = {
333 [' '] = 1, ['\t'] = 1,
334};
335
336const char http_is_crlf[256] = {
337 ['\r'] = 1, ['\n'] = 1,
338};
339
340const char http_is_lws[256] = {
341 [' '] = 1, ['\t'] = 1,
342 ['\r'] = 1, ['\n'] = 1,
343};
344
345const char http_is_sep[256] = {
346 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
347 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
348 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
349 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
350 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
351};
352
353const char http_is_ctl[256] = {
354 [0 ... 31] = 1,
355 [127] = 1,
356};
357
358/*
359 * A token is any ASCII char that is neither a separator nor a CTL char.
360 * Do not overwrite values in assignment since gcc-2.95 will not handle
361 * them correctly. Instead, define every non-CTL char's status.
362 */
363const char http_is_token[256] = {
364 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
365 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
366 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
367 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
368 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
369 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
370 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
371 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
372 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
373 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
374 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
375 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
376 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
377 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
378 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
379 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
380 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
381 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
382 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
383 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
384 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
385 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
386 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
387 ['|'] = 1, ['}'] = 0, ['~'] = 1,
388};
389
390
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100391/*
392 * An http ver_token is any ASCII which can be found in an HTTP version,
393 * which includes 'H', 'T', 'P', '/', '.' and any digit.
394 */
395const char http_is_ver_token[256] = {
396 ['.'] = 1, ['/'] = 1,
397 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
398 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
399 ['H'] = 1, ['P'] = 1, ['T'] = 1,
400};
401
402
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100403/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100404 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
405 */
406#if defined(DEBUG_FSM)
407static void http_silent_debug(int line, struct session *s)
408{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100409 chunk_printf(&trash,
410 "[%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",
411 line,
412 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
413 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);
414 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100415
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100416 chunk_printf(&trash,
417 " %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",
418 line,
419 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
420 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);
421 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100422}
423#else
424#define http_silent_debug(l,s) do { } while (0)
425#endif
426
427/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100428 * Adds a header and its CRLF at the tail of the message's buffer, just before
429 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100430 * The header is also automatically added to the index <hdr_idx>, and the end
431 * of headers is automatically adjusted. The number of bytes added is returned
432 * on success, otherwise <0 is returned indicating an error.
433 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100434int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100435{
436 int bytes, len;
437
438 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200439 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440 if (!bytes)
441 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100442 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
444}
445
446/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100447 * Adds a header and its CRLF at the tail of the message's buffer, just before
448 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100449 * the buffer is only opened and the space reserved, but nothing is copied.
450 * The header is also automatically added to the index <hdr_idx>, and the end
451 * of headers is automatically adjusted. The number of bytes added is returned
452 * on success, otherwise <0 is returned indicating an error.
453 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100454int http_header_add_tail2(struct http_msg *msg,
455 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456{
457 int bytes;
458
Willy Tarreau9b28e032012-10-12 23:49:43 +0200459 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100460 if (!bytes)
461 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100462 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100463 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
466/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100467 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
468 * If so, returns the position of the first non-space character relative to
469 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
470 * to return a pointer to the place after the first space. Returns 0 if the
471 * header name does not match. Checks are case-insensitive.
472 */
473int http_header_match2(const char *hdr, const char *end,
474 const char *name, int len)
475{
476 const char *val;
477
478 if (hdr + len >= end)
479 return 0;
480 if (hdr[len] != ':')
481 return 0;
482 if (strncasecmp(hdr, name, len) != 0)
483 return 0;
484 val = hdr + len + 1;
485 while (val < end && HTTP_IS_SPHT(*val))
486 val++;
487 if ((val >= end) && (len + 2 <= end - hdr))
488 return len + 2; /* we may replace starting from second space */
489 return val - hdr;
490}
491
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200492/* Find the first or next occurrence of header <name> in message buffer <sol>
493 * using headers index <idx>, and return it in the <ctx> structure. This
494 * structure holds everything necessary to use the header and find next
495 * occurrence. If its <idx> member is 0, the header is searched from the
496 * beginning. Otherwise, the next occurrence is returned. The function returns
497 * 1 when it finds a value, and 0 when there is no more. It is very similar to
498 * http_find_header2() except that it is designed to work with full-line headers
499 * whose comma is not a delimiter but is part of the syntax. As a special case,
500 * if ctx->val is NULL when searching for a new values of a header, the current
501 * header is rescanned. This allows rescanning after a header deletion.
502 */
503int http_find_full_header2(const char *name, int len,
504 char *sol, struct hdr_idx *idx,
505 struct hdr_ctx *ctx)
506{
507 char *eol, *sov;
508 int cur_idx, old_idx;
509
510 cur_idx = ctx->idx;
511 if (cur_idx) {
512 /* We have previously returned a header, let's search another one */
513 sol = ctx->line;
514 eol = sol + idx->v[cur_idx].len;
515 goto next_hdr;
516 }
517
518 /* first request for this header */
519 sol += hdr_idx_first_pos(idx);
520 old_idx = 0;
521 cur_idx = hdr_idx_first_idx(idx);
522 while (cur_idx) {
523 eol = sol + idx->v[cur_idx].len;
524
525 if (len == 0) {
526 /* No argument was passed, we want any header.
527 * To achieve this, we simply build a fake request. */
528 while (sol + len < eol && sol[len] != ':')
529 len++;
530 name = sol;
531 }
532
533 if ((len < eol - sol) &&
534 (sol[len] == ':') &&
535 (strncasecmp(sol, name, len) == 0)) {
536 ctx->del = len;
537 sov = sol + len + 1;
538 while (sov < eol && http_is_lws[(unsigned char)*sov])
539 sov++;
540
541 ctx->line = sol;
542 ctx->prev = old_idx;
543 ctx->idx = cur_idx;
544 ctx->val = sov - sol;
545 ctx->tws = 0;
546 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
547 eol--;
548 ctx->tws++;
549 }
550 ctx->vlen = eol - sov;
551 return 1;
552 }
553 next_hdr:
554 sol = eol + idx->v[cur_idx].cr + 1;
555 old_idx = cur_idx;
556 cur_idx = idx->v[cur_idx].next;
557 }
558 return 0;
559}
560
Willy Tarreau68085d82010-01-18 14:54:04 +0100561/* Find the end of the header value contained between <s> and <e>. See RFC2616,
562 * par 2.2 for more information. Note that it requires a valid header to return
563 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100565char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200566{
567 int quoted, qdpair;
568
569 quoted = qdpair = 0;
570 for (; s < e; s++) {
571 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200572 else if (quoted) {
573 if (*s == '\\') qdpair = 1;
574 else if (*s == '"') quoted = 0;
575 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 else if (*s == '"') quoted = 1;
577 else if (*s == ',') return s;
578 }
579 return s;
580}
581
582/* Find the first or next occurrence of header <name> in message buffer <sol>
583 * using headers index <idx>, and return it in the <ctx> structure. This
584 * structure holds everything necessary to use the header and find next
585 * occurrence. If its <idx> member is 0, the header is searched from the
586 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100587 * 1 when it finds a value, and 0 when there is no more. It is designed to work
588 * with headers defined as comma-separated lists. As a special case, if ctx->val
589 * is NULL when searching for a new values of a header, the current header is
590 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200591 */
592int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100593 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594 struct hdr_ctx *ctx)
595{
Willy Tarreau68085d82010-01-18 14:54:04 +0100596 char *eol, *sov;
597 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200598
Willy Tarreau68085d82010-01-18 14:54:04 +0100599 cur_idx = ctx->idx;
600 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200601 /* We have previously returned a value, let's search
602 * another one on the same line.
603 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200605 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 eol = sol + idx->v[cur_idx].len;
608
609 if (sov >= eol)
610 /* no more values in this header */
611 goto next_hdr;
612
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 /* values remaining for this header, skip the comma but save it
614 * for later use (eg: for header deletion).
615 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 sov++;
617 while (sov < eol && http_is_lws[(unsigned char)*sov])
618 sov++;
619
620 goto return_hdr;
621 }
622
623 /* first request for this header */
624 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627 while (cur_idx) {
628 eol = sol + idx->v[cur_idx].len;
629
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200630 if (len == 0) {
631 /* No argument was passed, we want any header.
632 * To achieve this, we simply build a fake request. */
633 while (sol + len < eol && sol[len] != ':')
634 len++;
635 name = sol;
636 }
637
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638 if ((len < eol - sol) &&
639 (sol[len] == ':') &&
640 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200642 sov = sol + len + 1;
643 while (sov < eol && http_is_lws[(unsigned char)*sov])
644 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100645
Willy Tarreau33a7e692007-06-10 19:45:56 +0200646 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100647 ctx->prev = old_idx;
648 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200649 ctx->idx = cur_idx;
650 ctx->val = sov - sol;
651
652 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200653 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200654 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200655 eol--;
656 ctx->tws++;
657 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 ctx->vlen = eol - sov;
659 return 1;
660 }
661 next_hdr:
662 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100663 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200664 cur_idx = idx->v[cur_idx].next;
665 }
666 return 0;
667}
668
669int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 struct hdr_ctx *ctx)
672{
673 return http_find_header2(name, strlen(name), sol, idx, ctx);
674}
675
Willy Tarreau68085d82010-01-18 14:54:04 +0100676/* Remove one value of a header. This only works on a <ctx> returned by one of
677 * the http_find_header functions. The value is removed, as well as surrounding
678 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100679 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * message <msg>. The new index is returned. If it is zero, it means there is
681 * no more header, so any processing may stop. The ctx is always left in a form
682 * that can be handled by http_find_header2() to find next occurrence.
683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100685{
686 int cur_idx = ctx->idx;
687 char *sol = ctx->line;
688 struct hdr_idx_elem *hdr;
689 int delta, skip_comma;
690
691 if (!cur_idx)
692 return 0;
693
694 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100696 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200697 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100698 http_msg_move_end(msg, delta);
699 idx->used--;
700 hdr->len = 0; /* unused entry */
701 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100702 if (idx->tail == ctx->idx)
703 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100704 ctx->idx = ctx->prev; /* walk back to the end of previous header */
705 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
706 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200707 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 return ctx->idx;
709 }
710
711 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200712 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
713 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100714 */
715
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200716 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200717 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200718 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100719 NULL, 0);
720 hdr->len += delta;
721 http_msg_move_end(msg, delta);
722 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 return ctx->idx;
725}
726
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100727/* This function handles a server error at the stream interface level. The
728 * stream interface is assumed to be already in a closed state. An optional
729 * message is copied into the input buffer, and an HTTP status code stored.
730 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100731 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100733static void http_server_error(struct session *t, struct stream_interface *si,
734 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200736 channel_auto_read(si->ob);
737 channel_abort(si->ob);
738 channel_auto_close(si->ob);
739 channel_erase(si->ob);
740 channel_auto_close(si->ib);
741 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100742 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100743 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200744 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 }
746 if (!(t->flags & SN_ERR_MASK))
747 t->flags |= err;
748 if (!(t->flags & SN_FINST_MASK))
749 t->flags |= finst;
750}
751
Willy Tarreau80587432006-12-24 17:47:20 +0100752/* This function returns the appropriate error location for the given session
753 * and message.
754 */
755
Willy Tarreau783f2582012-09-04 12:19:04 +0200756struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100757{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200758 if (s->be->errmsg[msgnum].str)
759 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100760 else if (s->fe->errmsg[msgnum].str)
761 return &s->fe->errmsg[msgnum];
762 else
763 return &http_err_chunks[msgnum];
764}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765
Willy Tarreau53b6c742006-12-17 13:37:46 +0100766/*
767 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
768 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
769 */
Willy Tarreauc8987b32013-12-06 23:43:17 +0100770static enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100771{
772 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100773 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100774
775 m = ((unsigned)*str - 'A');
776
777 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100778 for (h = http_methods[m]; h->len > 0; h++) {
779 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100780 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100781 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100782 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100783 };
784 return HTTP_METH_OTHER;
785 }
786 return HTTP_METH_NONE;
787
788}
789
Willy Tarreau21d2af32008-02-14 20:25:24 +0100790/* Parse the URI from the given transaction (which is assumed to be in request
791 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
792 * It is returned otherwise.
793 */
794static char *
795http_get_path(struct http_txn *txn)
796{
797 char *ptr, *end;
798
Willy Tarreau9b28e032012-10-12 23:49:43 +0200799 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100800 end = ptr + txn->req.sl.rq.u_l;
801
802 if (ptr >= end)
803 return NULL;
804
805 /* RFC2616, par. 5.1.2 :
806 * Request-URI = "*" | absuri | abspath | authority
807 */
808
809 if (*ptr == '*')
810 return NULL;
811
812 if (isalpha((unsigned char)*ptr)) {
813 /* this is a scheme as described by RFC3986, par. 3.1 */
814 ptr++;
815 while (ptr < end &&
816 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
817 ptr++;
818 /* skip '://' */
819 if (ptr == end || *ptr++ != ':')
820 return NULL;
821 if (ptr == end || *ptr++ != '/')
822 return NULL;
823 if (ptr == end || *ptr++ != '/')
824 return NULL;
825 }
826 /* skip [user[:passwd]@]host[:[port]] */
827
828 while (ptr < end && *ptr != '/')
829 ptr++;
830
831 if (ptr == end)
832 return NULL;
833
834 /* OK, we got the '/' ! */
835 return ptr;
836}
837
Willy Tarreau71241ab2012-12-27 11:30:54 +0100838/* Returns a 302 for a redirectable request that reaches a server working in
839 * in redirect mode. This may only be called just after the stream interface
840 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
841 * follow normal proxy processing. NOTE: this function is designed to support
842 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100844void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845{
846 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100847 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200849 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850
851 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100852 trash.len = strlen(HTTP_302);
853 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100855 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100856
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100858 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100859 return;
860
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100861 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100862 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100863 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
864 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100865 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100866
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200867 /* 3: add the request URI. Since it was already forwarded, we need
868 * to temporarily rewind the buffer.
869 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200871 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200872
Willy Tarreauefb453c2008-10-26 20:49:47 +0100873 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200874 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 +0200875
Willy Tarreau9b28e032012-10-12 23:49:43 +0200876 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200877
Willy Tarreauefb453c2008-10-26 20:49:47 +0100878 if (!path)
879 return;
880
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100881 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100882 return;
883
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100884 memcpy(trash.str + trash.len, path, len);
885 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100886
887 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100888 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
889 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100890 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100891 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
892 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100893 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100894
895 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200896 si_shutr(si);
897 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100898 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100899 si->state = SI_ST_CLO;
900
901 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200902 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100903
904 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100905 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906}
907
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100908/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100909 * that the server side is closed. Note that err_type is actually a
910 * bitmask, where almost only aborts may be cumulated with other
911 * values. We consider that aborted operations are more important
912 * than timeouts or errors due to the fact that nobody else in the
913 * logs might explain incomplete retries. All others should avoid
914 * being cumulated. It should normally not be possible to have multiple
915 * aborts at once, but just in case, the first one in sequence is reported.
916 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100917void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100919 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100920
921 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100922 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200923 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100924 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100925 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200926 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100927 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100928 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200929 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200932 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100933 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200935 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100936 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100937 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200938 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200939 else if (err_type & SI_ET_CONN_RES)
940 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
941 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100943 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200944 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100945}
946
Willy Tarreau42250582007-04-01 01:30:43 +0200947extern const char sess_term_cond[8];
948extern const char sess_fin_state[8];
949extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200950struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100951struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100952struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100953
Willy Tarreau117f59e2007-03-04 18:17:17 +0100954/*
955 * Capture headers from message starting at <som> according to header list
956 * <cap_hdr>, and fill the <idx> structure appropriately.
957 */
958void capture_headers(char *som, struct hdr_idx *idx,
959 char **cap, struct cap_hdr *cap_hdr)
960{
961 char *eol, *sol, *col, *sov;
962 int cur_idx;
963 struct cap_hdr *h;
964 int len;
965
966 sol = som + hdr_idx_first_pos(idx);
967 cur_idx = hdr_idx_first_idx(idx);
968
969 while (cur_idx) {
970 eol = sol + idx->v[cur_idx].len;
971
972 col = sol;
973 while (col < eol && *col != ':')
974 col++;
975
976 sov = col + 1;
977 while (sov < eol && http_is_lws[(unsigned char)*sov])
978 sov++;
979
980 for (h = cap_hdr; h; h = h->next) {
981 if ((h->namelen == col - sol) &&
982 (strncasecmp(sol, h->name, h->namelen) == 0)) {
983 if (cap[h->index] == NULL)
984 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200985 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100986
987 if (cap[h->index] == NULL) {
988 Alert("HTTP capture : out of memory.\n");
989 continue;
990 }
991
992 len = eol - sov;
993 if (len > h->len)
994 len = h->len;
995
996 memcpy(cap[h->index], sov, len);
997 cap[h->index][len]=0;
998 }
999 }
1000 sol = eol + idx->v[cur_idx].cr + 1;
1001 cur_idx = idx->v[cur_idx].next;
1002 }
1003}
1004
1005
Willy Tarreau42250582007-04-01 01:30:43 +02001006/* either we find an LF at <ptr> or we jump to <bad>.
1007 */
1008#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1009
1010/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1011 * otherwise to <http_msg_ood> with <state> set to <st>.
1012 */
1013#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1014 ptr++; \
1015 if (likely(ptr < end)) \
1016 goto good; \
1017 else { \
1018 state = (st); \
1019 goto http_msg_ood; \
1020 } \
1021 } while (0)
1022
1023
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001025 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1027 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1028 * will give undefined results.
1029 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1030 * and that msg->sol points to the beginning of the response.
1031 * If a complete line is found (which implies that at least one CR or LF is
1032 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1033 * returned indicating an incomplete line (which does not mean that parts have
1034 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1035 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1036 * upon next call.
1037 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1040 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001041 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001042 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001043const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001044 enum ht_state state, const char *ptr, const char *end,
1045 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001046{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001047 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001048
Willy Tarreau8973c702007-01-21 23:58:29 +01001049 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001050 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001051 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001052 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001053 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1054
1055 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001056 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001057 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1058 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001059 state = HTTP_MSG_ERROR;
1060 break;
1061
Willy Tarreau8973c702007-01-21 23:58:29 +01001062 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001063 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001065 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001066 goto http_msg_rpcode;
1067 }
1068 if (likely(HTTP_IS_SPHT(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1070 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001071 state = HTTP_MSG_ERROR;
1072 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001073
Willy Tarreau8973c702007-01-21 23:58:29 +01001074 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001075 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001076 if (likely(!HTTP_IS_LWS(*ptr)))
1077 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1078
1079 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001081 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1082 }
1083
1084 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001086 http_msg_rsp_reason:
1087 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001088 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 msg->sl.st.r_l = 0;
1090 goto http_msg_rpline_eol;
1091
Willy Tarreau8973c702007-01-21 23:58:29 +01001092 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001093 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001094 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001095 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001096 goto http_msg_rpreason;
1097 }
1098 if (likely(HTTP_IS_SPHT(*ptr)))
1099 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1100 /* so it's a CR/LF, so there is no reason phrase */
1101 goto http_msg_rsp_reason;
1102
Willy Tarreau8973c702007-01-21 23:58:29 +01001103 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001104 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 if (likely(!HTTP_IS_CRLF(*ptr)))
1106 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001107 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001108 http_msg_rpline_eol:
1109 /* We have seen the end of line. Note that we do not
1110 * necessarily have the \n yet, but at least we know that we
1111 * have EITHER \r OR \n, otherwise the response would not be
1112 * complete. We can then record the response length and return
1113 * to the caller which will be able to register it.
1114 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001115 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 return ptr;
1117
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001119#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001120 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1121 exit(1);
1122#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001123 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 }
1125
1126 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001127 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 if (ret_state)
1129 *ret_state = state;
1130 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001131 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001132 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133}
1134
Willy Tarreau8973c702007-01-21 23:58:29 +01001135/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 * This function parses a request line between <ptr> and <end>, starting with
1137 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1138 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1139 * will give undefined results.
1140 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1141 * and that msg->sol points to the beginning of the request.
1142 * If a complete line is found (which implies that at least one CR or LF is
1143 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1144 * returned indicating an incomplete line (which does not mean that parts have
1145 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1146 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1147 * upon next call.
1148 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001149 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001150 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1151 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001152 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001154const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001155 enum ht_state state, const char *ptr, const char *end,
1156 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001157{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001158 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001159
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001160 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001162 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 if (likely(HTTP_IS_TOKEN(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001167 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1169 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 if (likely(HTTP_IS_CRLF(*ptr))) {
1172 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001173 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001177 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001179 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 msg->sl.rq.v_l = 0;
1181 goto http_msg_rqline_eol;
1182 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001183 state = HTTP_MSG_ERROR;
1184 break;
1185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001187 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001189 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190 goto http_msg_rquri;
1191 }
1192 if (likely(HTTP_IS_SPHT(*ptr)))
1193 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1194 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1195 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001198 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001199 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001203 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001206
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001207 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001208 /* non-ASCII chars are forbidden unless option
1209 * accept-invalid-http-request is enabled in the frontend.
1210 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001211 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001212 if (msg->err_pos < -1)
1213 goto invalid_char;
1214 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001215 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001216 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1217 }
1218
1219 if (likely(HTTP_IS_CRLF(*ptr))) {
1220 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1221 goto http_msg_req09_uri_e;
1222 }
1223
1224 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001225 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001226 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001227 state = HTTP_MSG_ERROR;
1228 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001231 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001232 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001233 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 goto http_msg_rqver;
1235 }
1236 if (likely(HTTP_IS_SPHT(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1238 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1239 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001242 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001243 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001245
1246 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001248 http_msg_rqline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the request would not be
1252 * complete. We can then record the request length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001256 return ptr;
1257 }
1258
1259 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001260 state = HTTP_MSG_ERROR;
1261 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001262
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001264#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1266 exit(1);
1267#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001268 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001272 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 if (ret_state)
1274 *ret_state = state;
1275 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001276 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001280/*
1281 * Returns the data from Authorization header. Function may be called more
1282 * than once so data is stored in txn->auth_data. When no header is found
1283 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1284 * searching again for something we are unable to find anyway.
1285 */
1286
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001287char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001288
1289int
1290get_http_auth(struct session *s)
1291{
1292
1293 struct http_txn *txn = &s->txn;
1294 struct chunk auth_method;
1295 struct hdr_ctx ctx;
1296 char *h, *p;
1297 int len;
1298
1299#ifdef DEBUG_AUTH
1300 printf("Auth for session %p: %d\n", s, txn->auth.method);
1301#endif
1302
1303 if (txn->auth.method == HTTP_AUTH_WRONG)
1304 return 0;
1305
1306 if (txn->auth.method)
1307 return 1;
1308
1309 txn->auth.method = HTTP_AUTH_WRONG;
1310
1311 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001312
1313 if (txn->flags & TX_USE_PX_CONN) {
1314 h = "Proxy-Authorization";
1315 len = strlen(h);
1316 } else {
1317 h = "Authorization";
1318 len = strlen(h);
1319 }
1320
Willy Tarreau9b28e032012-10-12 23:49:43 +02001321 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001322 return 0;
1323
1324 h = ctx.line + ctx.val;
1325
1326 p = memchr(h, ' ', ctx.vlen);
1327 if (!p || p == h)
1328 return 0;
1329
1330 chunk_initlen(&auth_method, h, 0, p-h);
1331 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1332
1333 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1334
1335 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001336 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001337
1338 if (len < 0)
1339 return 0;
1340
1341
1342 get_http_auth_buff[len] = '\0';
1343
1344 p = strchr(get_http_auth_buff, ':');
1345
1346 if (!p)
1347 return 0;
1348
1349 txn->auth.user = get_http_auth_buff;
1350 *p = '\0';
1351 txn->auth.pass = p+1;
1352
1353 txn->auth.method = HTTP_AUTH_BASIC;
1354 return 1;
1355 }
1356
1357 return 0;
1358}
1359
Willy Tarreau58f10d72006-12-04 02:26:12 +01001360
Willy Tarreau8973c702007-01-21 23:58:29 +01001361/*
1362 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001363 * depending on the initial msg->msg_state. The caller is responsible for
1364 * ensuring that the message does not wrap. The function can be preempted
1365 * everywhere when data are missing and recalled at the exact same location
1366 * with no information loss. The message may even be realigned between two
1367 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001368 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001369 * fields. Note that msg->sol will be initialized after completing the first
1370 * state, so that none of the msg pointers has to be initialized prior to the
1371 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001373void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374{
Willy Tarreau3770f232013-12-07 00:01:53 +01001375 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001379 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001380 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001381 ptr = buf->p + msg->next;
1382 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (unlikely(ptr >= end))
1385 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001388 /*
1389 * First, states that are specific to the response only.
1390 * We check them first so that request and headers are
1391 * closer to each other (accessed more often).
1392 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001394 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001396 /* we have a start of message, but we have to check
1397 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001398 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001399 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001400 if (unlikely(ptr != buf->p)) {
1401 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001402 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001403 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001404 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001405 }
Willy Tarreau26927362012-05-18 23:22:52 +02001406 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001407 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 hdr_idx_init(idx);
1409 state = HTTP_MSG_RPVER;
1410 goto http_msg_rpver;
1411 }
1412
1413 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1414 goto http_msg_invalid;
1415
1416 if (unlikely(*ptr == '\n'))
1417 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1419 /* stop here */
1420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001423 EXPECT_LF_HERE(ptr, http_msg_invalid);
1424 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1425 /* stop here */
1426
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001428 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 case HTTP_MSG_RPVER_SP:
1430 case HTTP_MSG_RPCODE:
1431 case HTTP_MSG_RPCODE_SP:
1432 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001433 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001434 state, ptr, end,
1435 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 if (unlikely(!ptr))
1437 return;
1438
1439 /* we have a full response and we know that we have either a CR
1440 * or an LF at <ptr>.
1441 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1443
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001444 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001445 if (likely(*ptr == '\r'))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1447 goto http_msg_rpline_end;
1448
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001450 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 /* msg->sol must point to the first of CR or LF. */
1452 EXPECT_LF_HERE(ptr, http_msg_invalid);
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1454 /* stop here */
1455
1456 /*
1457 * Second, states that are specific to the request only
1458 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001460 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001462 /* we have a start of message, but we have to check
1463 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001464 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001465 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 if (likely(ptr != buf->p)) {
1467 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001468 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001469 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 }
Willy Tarreau26927362012-05-18 23:22:52 +02001472 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001473 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001474 state = HTTP_MSG_RQMETH;
1475 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1479 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 if (unlikely(*ptr == '\n'))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1483 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 case HTTP_MSG_RQMETH_SP:
1495 case HTTP_MSG_RQURI:
1496 case HTTP_MSG_RQURI_SP:
1497 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001498 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001499 state, ptr, end,
1500 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 if (unlikely(!ptr))
1502 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 /* we have a full request and we know that we have either a CR
1505 * or an LF at <ptr>.
1506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001509 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 if (likely(*ptr == '\r'))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001515 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 /* check for HTTP/0.9 request : no version information available.
1517 * msg->sol must point to the first of CR or LF.
1518 */
1519 if (unlikely(msg->sl.rq.v_l == 0))
1520 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8973c702007-01-21 23:58:29 +01001526 /*
1527 * Common states below
1528 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001530 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001531 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (likely(!HTTP_IS_CRLF(*ptr))) {
1533 goto http_msg_hdr_name;
1534 }
1535
1536 if (likely(*ptr == '\r'))
1537 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1538 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001541 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 /* assumes msg->sol points to the first char */
1543 if (likely(HTTP_IS_TOKEN(*ptr)))
1544 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001546 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001549 if (likely(msg->err_pos < -1) || *ptr == '\n')
1550 goto http_msg_invalid;
1551
1552 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001553 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001554
1555 /* and we still accept this non-token character */
1556 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001559 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001560 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(HTTP_IS_SPHT(*ptr)))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001565 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (likely(!HTTP_IS_CRLF(*ptr))) {
1568 goto http_msg_hdr_val;
1569 }
1570
1571 if (likely(*ptr == '\r'))
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1573 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001581 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 if (likely(HTTP_IS_SPHT(*ptr))) {
1583 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 for (; buf->p + msg->sov < ptr; msg->sov++)
1585 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 goto http_msg_hdr_l1_sp;
1587 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001588 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001589 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_complete_header;
1591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001594 /* assumes msg->sol points to the first char, and msg->sov
1595 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 */
1597 if (likely(!HTTP_IS_CRLF(*ptr)))
1598 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001600 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 /* Note: we could also copy eol into ->eoh so that we have the
1602 * real header end in case it ends with lots of LWS, but is this
1603 * really needed ?
1604 */
1605 if (likely(*ptr == '\r'))
1606 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1607 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001610 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001615 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1617 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001618 for (; buf->p + msg->eol < ptr; msg->eol++)
1619 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 goto http_msg_hdr_val;
1621 }
1622 http_msg_complete_header:
1623 /*
1624 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001625 * Assumes msg->sol points to the first char, msg->sov points
1626 * to the first character of the value and msg->eol to the
1627 * first CR or LF so we know how the line ends. We insert last
1628 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 idx, idx->tail) < 0))
1632 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001633
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001634 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 if (likely(!HTTP_IS_CRLF(*ptr))) {
1636 goto http_msg_hdr_name;
1637 }
1638
1639 if (likely(*ptr == '\r'))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1641 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001644 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 /* Assumes msg->sol points to the first of either CR or LF */
1646 EXPECT_LF_HERE(ptr, http_msg_invalid);
1647 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001648 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001649 msg->eoh = msg->sol;
1650 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001651 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001653
1654 case HTTP_MSG_ERROR:
1655 /* this may only happen if we call http_msg_analyser() twice with an error */
1656 break;
1657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001659#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1661 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001662#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001663 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 }
1665 http_msg_ood:
1666 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001668 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 http_msg_invalid:
1672 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001673 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001674 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 return;
1676}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001677
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001678/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1679 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1680 * nothing is done and 1 is returned.
1681 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001682static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001683{
1684 int delta;
1685 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001686 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001687
1688 if (msg->sl.rq.v_l != 0)
1689 return 1;
1690
Willy Tarreau9b28e032012-10-12 23:49:43 +02001691 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001692 delta = 0;
1693
1694 if (msg->sl.rq.u_l == 0) {
1695 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001696 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001697 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001698 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001699 }
1700 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001701 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001702 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001703 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001704 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001705 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001706 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001707 NULL, NULL);
1708 if (unlikely(!cur_end))
1709 return 0;
1710
1711 /* we have a full HTTP/1.0 request now and we know that
1712 * we have either a CR or an LF at <ptr>.
1713 */
1714 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1715 return 1;
1716}
1717
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001719 * and "keep-alive" values. If we already know that some headers may safely
1720 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001721 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1722 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001723 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001724 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1725 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1726 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001727 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001728 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001729void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001730{
Willy Tarreau5b154472009-12-21 20:11:07 +01001731 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001732 const char *hdr_val = "Connection";
1733 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001734
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001735 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001736 return;
1737
Willy Tarreau88d349d2010-01-25 12:15:43 +01001738 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1739 hdr_val = "Proxy-Connection";
1740 hdr_len = 16;
1741 }
1742
Willy Tarreau5b154472009-12-21 20:11:07 +01001743 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001744 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001745 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001746 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1747 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001748 if (to_del & 2)
1749 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001750 else
1751 txn->flags |= TX_CON_KAL_SET;
1752 }
1753 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1754 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001755 if (to_del & 1)
1756 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001757 else
1758 txn->flags |= TX_CON_CLO_SET;
1759 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001760 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1761 txn->flags |= TX_HDR_CONN_UPG;
1762 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001763 }
1764
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001765 txn->flags |= TX_HDR_CONN_PRS;
1766 return;
1767}
Willy Tarreau5b154472009-12-21 20:11:07 +01001768
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001769/* Apply desired changes on the Connection: header. Values may be removed and/or
1770 * added depending on the <wanted> flags, which are exclusively composed of
1771 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1772 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1773 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001774void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001775{
1776 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001777 const char *hdr_val = "Connection";
1778 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001779
1780 ctx.idx = 0;
1781
Willy Tarreau88d349d2010-01-25 12:15:43 +01001782
1783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1784 hdr_val = "Proxy-Connection";
1785 hdr_len = 16;
1786 }
1787
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001788 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001789 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001790 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1791 if (wanted & TX_CON_KAL_SET)
1792 txn->flags |= TX_CON_KAL_SET;
1793 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001794 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001795 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001796 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1797 if (wanted & TX_CON_CLO_SET)
1798 txn->flags |= TX_CON_CLO_SET;
1799 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001801 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001802 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803
1804 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1805 return;
1806
1807 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1808 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001809 hdr_val = "Connection: close";
1810 hdr_len = 17;
1811 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1812 hdr_val = "Proxy-Connection: close";
1813 hdr_len = 23;
1814 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001815 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001816 }
1817
1818 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1819 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001820 hdr_val = "Connection: keep-alive";
1821 hdr_len = 22;
1822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection: keep-alive";
1824 hdr_len = 28;
1825 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001826 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001827 }
1828 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001829}
1830
Willy Tarreaua458b672012-03-05 11:17:50 +01001831/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001833 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001834 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001836 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001837static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001838{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001839 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001841 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001842 const char *end = buf->data + buf->size;
1843 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001844 unsigned int chunk = 0;
1845
1846 /* The chunk size is in the following form, though we are only
1847 * interested in the size and CRLF :
1848 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1849 */
1850 while (1) {
1851 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001852 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 if (c < 0) /* not a hex digit anymore */
1856 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001857 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001858 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001859 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001860 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001861 chunk = (chunk << 4) + c;
1862 }
1863
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001865 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001866 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867
1868 while (http_is_spht[(unsigned char)*ptr]) {
1869 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001870 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001871 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001872 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001873 }
1874
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 /* Up to there, we know that at least one byte is present at *ptr. Check
1876 * for the end of chunk size.
1877 */
1878 while (1) {
1879 if (likely(HTTP_IS_CRLF(*ptr))) {
1880 /* we now have a CR or an LF at ptr */
1881 if (likely(*ptr == '\r')) {
1882 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001883 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001884 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 return 0;
1886 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001887
Willy Tarreaud98cf932009-12-27 22:54:55 +01001888 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001889 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001890 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001891 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001892 /* done */
1893 break;
1894 }
1895 else if (*ptr == ';') {
1896 /* chunk extension, ends at next CRLF */
1897 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001899 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001900 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901
1902 while (!HTTP_IS_CRLF(*ptr)) {
1903 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001905 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 return 0;
1907 }
1908 /* we have a CRLF now, loop above */
1909 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001910 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001912 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 }
1914
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001916 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001919 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001920 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001921 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001923 msg->chunk_len = chunk;
1924 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001927 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001929 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001930}
1931
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001932/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001933 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 * the trailers is found, it is automatically scheduled to be forwarded,
1935 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1936 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001937 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001938 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001939 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001940 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1941 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001942 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001943 * matches the length of trailers already parsed and not forwarded. It is also
1944 * important to note that this function is designed to be able to parse wrapped
1945 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001947static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001949 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001950
Willy Tarreaua458b672012-03-05 11:17:50 +01001951 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001953 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 const char *ptr = b_ptr(buf, msg->next);
1955 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001956 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957
1958 /* scan current line and stop at LF or CRLF */
1959 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001960 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 return 0;
1962
1963 if (*ptr == '\n') {
1964 if (!p1)
1965 p1 = ptr;
1966 p2 = ptr;
1967 break;
1968 }
1969
1970 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001971 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001972 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001973 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001974 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 p1 = ptr;
1976 }
1977
1978 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001979 if (ptr >= buf->data + buf->size)
1980 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981 }
1982
1983 /* after LF; point to beginning of next line */
1984 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 if (p2 >= buf->data + buf->size)
1986 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001989 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001991
1992 /* schedule this line for forwarding */
1993 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001994 if (msg->sov >= buf->size)
1995 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001996
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001997 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001998 /* LF/CRLF at beginning of line => end of trailers at p2.
1999 * Everything was scheduled for forwarding, there's nothing
2000 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002001 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003 msg->msg_state = HTTP_MSG_DONE;
2004 return 1;
2005 }
2006 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002007 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 }
2009}
2010
Willy Tarreau54d23df2012-10-25 19:04:45 +02002011/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002013 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002014 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002015 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2016 * not enough data are available, the function does not change anything and
2017 * returns zero. If a parse error is encountered, the function returns < 0 and
2018 * does not change anything. Note: this function is designed to parse wrapped
2019 * CRLF at the end of the buffer.
2020 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002021static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002022{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002023 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002024 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 int bytes;
2026
2027 /* NB: we'll check data availabilty at the end. It's not a
2028 * problem because whatever we match first will be checked
2029 * against the correct length.
2030 */
2031 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002032 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 if (*ptr == '\r') {
2034 bytes++;
2035 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002036 if (ptr >= buf->data + buf->size)
2037 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 }
2039
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041 return 0;
2042
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002043 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002046 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047
2048 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002049 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002050 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002051 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2052 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002053 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002054 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2055 return 1;
2056}
Willy Tarreau5b154472009-12-21 20:11:07 +01002057
William Lallemand82fe75c2012-10-23 10:25:10 +02002058
2059/*
2060 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002061 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002062int select_compression_request_header(struct session *s, struct buffer *req)
2063{
2064 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002065 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002066 struct hdr_ctx ctx;
2067 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002068 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002069
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002070 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2071 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002072 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2073 */
2074 ctx.idx = 0;
2075 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2076 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002077 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2078 (ctx.vlen < 31 ||
2079 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2080 ctx.line[ctx.val + 30] < '6' ||
2081 (ctx.line[ctx.val + 30] == '6' &&
2082 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2083 s->comp_algo = NULL;
2084 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002085 }
2086
William Lallemand82fe75c2012-10-23 10:25:10 +02002087 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002088 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002089 ctx.idx = 0;
2090 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002091 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002092 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2093 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002094
2095 /* remove all occurrences of the header when "compression offload" is set */
2096
2097 if ((s->be->comp && s->be->comp->offload) ||
2098 (s->fe->comp && s->fe->comp->offload)) {
2099 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2100 ctx.idx = 0;
2101 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2102 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2103 }
2104 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002105 return 1;
2106 }
2107 }
2108 }
2109 }
2110
2111 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002112 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2113 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002114 if (comp_algo->add_data == identity_add_data) {
2115 s->comp_algo = comp_algo;
2116 return 1;
2117 }
2118 }
2119 }
2120
2121 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002122 return 0;
2123}
2124
2125/*
2126 * Selects a comression algorithm depending of the server response.
2127 */
2128int select_compression_response_header(struct session *s, struct buffer *res)
2129{
2130 struct http_txn *txn = &s->txn;
2131 struct http_msg *msg = &txn->rsp;
2132 struct hdr_ctx ctx;
2133 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002134
2135 /* no common compression algorithm was found in request header */
2136 if (s->comp_algo == NULL)
2137 goto fail;
2138
2139 /* HTTP < 1.1 should not be compressed */
2140 if (!(msg->flags & HTTP_MSGF_VER_11))
2141 goto fail;
2142
William Lallemandd3002612012-11-26 14:34:47 +01002143 /* 200 only */
2144 if (txn->status != 200)
2145 goto fail;
2146
William Lallemand82fe75c2012-10-23 10:25:10 +02002147 /* Content-Length is null */
2148 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2149 goto fail;
2150
Willy Tarreau667c2a32013-04-09 08:13:58 +02002151 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2152 if (msg->flags & HTTP_MSGF_TE_CHNK)
2153 goto fail;
2154
William Lallemand82fe75c2012-10-23 10:25:10 +02002155 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002156 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002157 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2158 goto fail;
2159
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002160 /* no compression when Cache-Control: no-transform is present in the message */
2161 ctx.idx = 0;
2162 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2163 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2164 goto fail;
2165 }
2166
William Lallemand82fe75c2012-10-23 10:25:10 +02002167 comp_type = NULL;
2168
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002169 /* we don't want to compress multipart content-types, nor content-types that are
2170 * not listed in the "compression type" directive if any. If no content-type was
2171 * found but configuration requires one, we don't compress either. Backend has
2172 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002173 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002174 ctx.idx = 0;
2175 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2176 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2177 goto fail;
2178
2179 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2180 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002181 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002182 if (ctx.vlen >= comp_type->name_len &&
2183 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002184 /* this Content-Type should be compressed */
2185 break;
2186 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002187 /* this Content-Type should not be compressed */
2188 if (comp_type == NULL)
2189 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002190 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002191 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002192 else { /* no content-type header */
2193 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2194 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002195 }
2196
William Lallemandd85f9172012-11-09 17:05:39 +01002197 /* limit compression rate */
2198 if (global.comp_rate_lim > 0)
2199 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2200 goto fail;
2201
William Lallemand072a2bf2012-11-20 17:01:01 +01002202 /* limit cpu usage */
2203 if (idle_pct < compress_min_idle)
2204 goto fail;
2205
William Lallemand4c49fae2012-11-07 15:00:23 +01002206 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002207 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002208 goto fail;
2209
William Lallemandec3e3892012-11-12 17:02:18 +01002210 s->flags |= SN_COMP_READY;
2211
William Lallemand82fe75c2012-10-23 10:25:10 +02002212 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002213 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002214 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2215 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2216
2217 /* add Transfer-Encoding header */
2218 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2219 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2220
2221 /*
2222 * Add Content-Encoding header when it's not identity encoding.
2223 * RFC 2616 : Identity encoding: This content-coding is used only in the
2224 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2225 * header.
2226 */
2227 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002228 trash.len = 18;
2229 memcpy(trash.str, "Content-Encoding: ", trash.len);
2230 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2231 trash.len += s->comp_algo->name_len;
2232 trash.str[trash.len] = '\0';
2233 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002234 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002235 return 1;
2236
2237fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002238 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002239 return 0;
2240}
2241
2242
Willy Tarreaud787e662009-07-07 10:14:51 +02002243/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2244 * processing can continue on next analysers, or zero if it either needs more
2245 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2246 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2247 * when it has nothing left to do, and may remove any analyser when it wants to
2248 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002249 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002250int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002251{
Willy Tarreau59234e92008-11-30 23:51:27 +01002252 /*
2253 * We will parse the partial (or complete) lines.
2254 * We will check the request syntax, and also join multi-line
2255 * headers. An index of all the lines will be elaborated while
2256 * parsing.
2257 *
2258 * For the parsing, we use a 28 states FSM.
2259 *
2260 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002261 * req->buf->p = beginning of request
2262 * req->buf->p + msg->eoh = end of processed headers / start of current one
2263 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002264 * msg->eol = end of current header or line (LF or CRLF)
2265 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002266 *
2267 * At end of parsing, we may perform a capture of the error (if any), and
2268 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2269 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2270 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002271 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002274 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 struct http_txn *txn = &s->txn;
2276 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002277 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002278
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002279 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 +01002280 now_ms, __FUNCTION__,
2281 s,
2282 req,
2283 req->rex, req->wex,
2284 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002285 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002286 req->analysers);
2287
Willy Tarreau52a0c602009-08-16 22:45:38 +02002288 /* we're speaking HTTP here, so let's speak HTTP to the client */
2289 s->srv_error = http_return_srv_error;
2290
Willy Tarreau83e3af02009-12-28 17:39:57 +01002291 /* There's a protected area at the end of the buffer for rewriting
2292 * purposes. We don't want to start to parse the request if the
2293 * protected area is affected, because we may have to move processed
2294 * data later, which is much more complicated.
2295 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002296 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002297 if (txn->flags & TX_NOT_FIRST) {
2298 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002299 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002300 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002301 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002302 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002303 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002304 return 0;
2305 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002306 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2307 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2308 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002309 }
2310
Willy Tarreau065e8332010-01-08 00:30:20 +01002311 /* Note that we have the same problem with the response ; we
2312 * may want to send a redirect, error or anything which requires
2313 * some spare space. So we'll ensure that we have at least
2314 * maxrewrite bytes available in the response buffer before
2315 * processing that one. This will only affect pipelined
2316 * keep-alive requests.
2317 */
2318 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002319 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002320 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2321 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2322 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002323 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002324 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002325 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002326 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002327 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002328 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002329 return 0;
2330 }
2331 }
2332
Willy Tarreau9b28e032012-10-12 23:49:43 +02002333 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002334 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002335 }
2336
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 /* 1: we might have to print this header in debug mode */
2338 if (unlikely((global.mode & MODE_DEBUG) &&
2339 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002340 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342
Willy Tarreau9b28e032012-10-12 23:49:43 +02002343 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002344 /* this is a bit complex : in case of error on the request line,
2345 * we know that rq.l is still zero, so we display only the part
2346 * up to the end of the line (truncated by debug_hdr).
2347 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002348 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 sol += hdr_idx_first_pos(&txn->hdr_idx);
2352 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002353
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 while (cur_idx) {
2355 eol = sol + txn->hdr_idx.v[cur_idx].len;
2356 debug_hdr("clihdr", s, sol, eol);
2357 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2358 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 }
2361
Willy Tarreau58f10d72006-12-04 02:26:12 +01002362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 /*
2364 * Now we quickly check if we have found a full valid request.
2365 * If not so, we check the FD and buffer states before leaving.
2366 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002367 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002368 * requests are checked first. When waiting for a second request
2369 * on a keep-alive session, if we encounter and error, close, t/o,
2370 * we note the error in the session flags but don't set any state.
2371 * Since the error will be noted there, it will not be counted by
2372 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002373 * Last, we may increase some tracked counters' http request errors on
2374 * the cases that are deliberately the client's fault. For instance,
2375 * a timeout or connection reset is not counted as an error. However
2376 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002378
Willy Tarreau655dce92009-11-08 13:10:58 +01002379 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002380 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002382 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002383 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002384 session_inc_http_req_ctr(s);
2385 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002386 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002388 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* 1: Since we are in header mode, if there's no space
2391 * left for headers, we won't be able to free more
2392 * later, so the session will never terminate. We
2393 * must terminate it now.
2394 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002395 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 /* FIXME: check if URI is set and return Status
2397 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002398 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002399 session_inc_http_req_ctr(s);
2400 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002401 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002402 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002403 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 goto return_bad_req;
2405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002408 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002409 if (!(s->flags & SN_ERR_MASK))
2410 s->flags |= SN_ERR_CLICL;
2411
Willy Tarreaufcffa692010-01-10 14:21:19 +01002412 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002413 goto failed_keep_alive;
2414
Willy Tarreau59234e92008-11-30 23:51:27 +01002415 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002416 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002417 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002418 session_inc_http_err_ctr(s);
2419 }
2420
Willy Tarreaudc979f22012-12-04 10:39:01 +01002421 txn->status = 400;
2422 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 msg->msg_state = HTTP_MSG_ERROR;
2424 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002425
Willy Tarreauda7ff642010-06-23 11:44:09 +02002426 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002427 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002428 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002429 if (s->listener->counters)
2430 s->listener->counters->failed_req++;
2431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 if (!(s->flags & SN_FINST_MASK))
2433 s->flags |= SN_FINST_R;
2434 return 0;
2435 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002438 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002439 if (!(s->flags & SN_ERR_MASK))
2440 s->flags |= SN_ERR_CLITO;
2441
Willy Tarreaufcffa692010-01-10 14:21:19 +01002442 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002443 goto failed_keep_alive;
2444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002446 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002447 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002448 session_inc_http_err_ctr(s);
2449 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002450 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002451 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 msg->msg_state = HTTP_MSG_ERROR;
2453 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002454
Willy Tarreauda7ff642010-06-23 11:44:09 +02002455 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002456 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002457 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002458 if (s->listener->counters)
2459 s->listener->counters->failed_req++;
2460
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 if (!(s->flags & SN_FINST_MASK))
2462 s->flags |= SN_FINST_R;
2463 return 0;
2464 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002465
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002467 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002468 if (!(s->flags & SN_ERR_MASK))
2469 s->flags |= SN_ERR_CLICL;
2470
Willy Tarreaufcffa692010-01-10 14:21:19 +01002471 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002472 goto failed_keep_alive;
2473
Willy Tarreau4076a152009-04-02 15:18:36 +02002474 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002475 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002477 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 msg->msg_state = HTTP_MSG_ERROR;
2479 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480
Willy Tarreauda7ff642010-06-23 11:44:09 +02002481 session_inc_http_err_ctr(s);
2482 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002483 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002484 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002485 if (s->listener->counters)
2486 s->listener->counters->failed_req++;
2487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 if (!(s->flags & SN_FINST_MASK))
2489 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002490 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002491 }
2492
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002493 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002494 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2495 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002496#ifdef TCP_QUICKACK
Willy Tarreauf79c8172013-10-21 16:30:56 +02002497 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && (__objt_conn(s->req->prod->end)->flags & CO_FL_CTRL_READY)) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002498 /* We need more data, we have to re-enable quick-ack in case we
2499 * previously disabled it, otherwise we might cause the client
2500 * to delay next data.
2501 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002502 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002503 }
2504#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002505
Willy Tarreaufcffa692010-01-10 14:21:19 +01002506 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2507 /* If the client starts to talk, let's fall back to
2508 * request timeout processing.
2509 */
2510 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002511 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002512 }
2513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002515 if (!tick_isset(req->analyse_exp)) {
2516 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2517 (txn->flags & TX_WAIT_NEXT_RQ) &&
2518 tick_isset(s->be->timeout.httpka))
2519 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2520 else
2521 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* we're not ready yet */
2525 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002526
2527 failed_keep_alive:
2528 /* Here we process low-level errors for keep-alive requests. In
2529 * short, if the request is not the first one and it experiences
2530 * a timeout, read error or shutdown, we just silently close so
2531 * that the client can try again.
2532 */
2533 txn->status = 0;
2534 msg->msg_state = HTTP_MSG_RQBEFORE;
2535 req->analysers = 0;
2536 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002537 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002538 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002539 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002540 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002542
Willy Tarreaud787e662009-07-07 10:14:51 +02002543 /* OK now we have a complete HTTP request with indexed headers. Let's
2544 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002545 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002546 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002547 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002548 * byte after the last LF. msg->sov points to the first byte of data.
2549 * msg->eol cannot be trusted because it may have been left uninitialized
2550 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002551 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002552
Willy Tarreauda7ff642010-06-23 11:44:09 +02002553 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002554 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2555
Willy Tarreaub16a5742010-01-10 14:46:16 +01002556 if (txn->flags & TX_WAIT_NEXT_RQ) {
2557 /* kill the pending keep-alive timeout */
2558 txn->flags &= ~TX_WAIT_NEXT_RQ;
2559 req->analyse_exp = TICK_ETERNITY;
2560 }
2561
2562
Willy Tarreaud787e662009-07-07 10:14:51 +02002563 /* Maybe we found in invalid header name while we were configured not
2564 * to block on that, so we have to capture it now.
2565 */
2566 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002567 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 /*
2570 * 1: identify the method
2571 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002572 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002573
2574 /* we can make use of server redirect on GET and HEAD */
2575 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2576 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002577
Willy Tarreau59234e92008-11-30 23:51:27 +01002578 /*
2579 * 2: check if the URI matches the monitor_uri.
2580 * We have to do this for every request which gets in, because
2581 * the monitor-uri is defined by the frontend.
2582 */
2583 if (unlikely((s->fe->monitor_uri_len != 0) &&
2584 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002585 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 s->fe->monitor_uri,
2587 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002588 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002590 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002594 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002595
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002597 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002598 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 ret = acl_pass(ret);
2601 if (cond->pol == ACL_COND_UNLESS)
2602 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 if (ret) {
2605 /* we fail this request, let's return 503 service unavail */
2606 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002607 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002608 if (!(s->flags & SN_ERR_MASK))
2609 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002611 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002612 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002613
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 /* nothing to fail, let's reply normaly */
2615 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002616 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002617 if (!(s->flags & SN_ERR_MASK))
2618 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 goto return_prx_cond;
2620 }
2621
2622 /*
2623 * 3: Maybe we have to copy the original REQURI for the logs ?
2624 * Note: we cannot log anymore if the request has been
2625 * classified as invalid.
2626 */
2627 if (unlikely(s->logs.logwait & LW_REQ)) {
2628 /* we have a complete HTTP request that we must log */
2629 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2630 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 if (urilen >= REQURI_LEN)
2633 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002634 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002636
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002637 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 s->do_log(s);
2639 } else {
2640 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002641 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 }
Willy Tarreau06619262006-12-17 08:37:22 +01002643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002645 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002646 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647
Willy Tarreau5b154472009-12-21 20:11:07 +01002648 /* ... and check if the request is HTTP/1.1 or above */
2649 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002650 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2651 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2652 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002653 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002654
2655 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002656 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 +01002657
Willy Tarreau88d349d2010-01-25 12:15:43 +01002658 /* if the frontend has "option http-use-proxy-header", we'll check if
2659 * we have what looks like a proxied connection instead of a connection,
2660 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2661 * Note that this is *not* RFC-compliant, however browsers and proxies
2662 * happen to do that despite being non-standard :-(
2663 * We consider that a request not beginning with either '/' or '*' is
2664 * a proxied connection, which covers both "scheme://location" and
2665 * CONNECT ip:port.
2666 */
2667 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002668 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002669 txn->flags |= TX_USE_PX_CONN;
2670
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002671 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002672 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002673
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002675 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002678
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002679 /* 6: determine the transfer-length.
2680 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2681 * the presence of a message-body in a REQUEST and its transfer length
2682 * must be determined that way (in order of precedence) :
2683 * 1. The presence of a message-body in a request is signaled by the
2684 * inclusion of a Content-Length or Transfer-Encoding header field
2685 * in the request's header fields. When a request message contains
2686 * both a message-body of non-zero length and a method that does
2687 * not define any semantics for that request message-body, then an
2688 * origin server SHOULD either ignore the message-body or respond
2689 * with an appropriate error message (e.g., 413). A proxy or
2690 * gateway, when presented the same request, SHOULD either forward
2691 * the request inbound with the message- body or ignore the
2692 * message-body when determining a response.
2693 *
2694 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2695 * and the "chunked" transfer-coding (Section 6.2) is used, the
2696 * transfer-length is defined by the use of this transfer-coding.
2697 * If a Transfer-Encoding header field is present and the "chunked"
2698 * transfer-coding is not present, the transfer-length is defined
2699 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002700 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002701 * 3. If a Content-Length header field is present, its decimal value in
2702 * OCTETs represents both the entity-length and the transfer-length.
2703 * If a message is received with both a Transfer-Encoding header
2704 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002705 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002706 * 4. By the server closing the connection. (Closing the connection
2707 * cannot be used to indicate the end of a request body, since that
2708 * would leave no possibility for the server to send back a response.)
2709 *
2710 * Whenever a transfer-coding is applied to a message-body, the set of
2711 * transfer-codings MUST include "chunked", unless the message indicates
2712 * it is terminated by closing the connection. When the "chunked"
2713 * transfer-coding is used, it MUST be the last transfer-coding applied
2714 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002715 */
2716
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002717 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002718 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002719 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002720 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002721 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002722 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002723 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2724 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002725 /* bad transfer-encoding (chunked followed by something else) */
2726 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002727 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002728 break;
2729 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002730 }
2731
Willy Tarreau32b47f42009-10-18 20:55:02 +02002732 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002733 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002734 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002735 signed long long cl;
2736
Willy Tarreauad14f752011-09-02 20:33:27 +02002737 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002738 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002739 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002740 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002741
Willy Tarreauad14f752011-09-02 20:33:27 +02002742 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002743 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002744 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002745 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746
Willy Tarreauad14f752011-09-02 20:33:27 +02002747 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002748 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002749 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002750 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002752 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002753 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002754 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002755 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002756
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002757 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002758 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002759 }
2760
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002761 /* bodyless requests have a known length */
2762 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002763 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764
Willy Tarreaud787e662009-07-07 10:14:51 +02002765 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002766 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002767 req->analyse_exp = TICK_ETERNITY;
2768 return 1;
2769
2770 return_bad_req:
2771 /* We centralize bad requests processing here */
2772 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2773 /* we detected a parsing error. We want to archive this request
2774 * in the dedicated proxy area for later troubleshooting.
2775 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002776 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 }
2778
2779 txn->req.msg_state = HTTP_MSG_ERROR;
2780 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002781 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002782
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002783 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002784 if (s->listener->counters)
2785 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002786
2787 return_prx_cond:
2788 if (!(s->flags & SN_ERR_MASK))
2789 s->flags |= SN_ERR_PRXCOND;
2790 if (!(s->flags & SN_FINST_MASK))
2791 s->flags |= SN_FINST_R;
2792
2793 req->analysers = 0;
2794 req->analyse_exp = TICK_ETERNITY;
2795 return 0;
2796}
2797
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002798
Willy Tarreau347a35d2013-11-22 17:51:09 +01002799/* This function prepares an applet to handle the stats. It can deal with the
2800 * "100-continue" expectation, check that admin rules are met for POST requests,
2801 * and program a response message if something was unexpected. It cannot fail
2802 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002803 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002804 * s->target which is supposed to already point to the stats applet. The caller
2805 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002806 */
2807int http_handle_stats(struct session *s, struct channel *req)
2808{
2809 struct stats_admin_rule *stats_admin_rule;
2810 struct stream_interface *si = s->rep->prod;
2811 struct http_txn *txn = &s->txn;
2812 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002813 struct uri_auth *uri_auth = s->be->uri_auth;
2814 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002815 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002817 appctx = si_appctx(si);
2818 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2819 appctx->st1 = appctx->st2 = 0;
2820 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2821 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002822
2823 uri = msg->chn->buf->p + msg->sl.rq.u;
2824 lookup = uri + uri_auth->uri_len;
2825
2826 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2827 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002828 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002829 break;
2830 }
2831 }
2832
2833 if (uri_auth->refresh) {
2834 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2835 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002836 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002837 break;
2838 }
2839 }
2840 }
2841
2842 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2843 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002844 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002845 break;
2846 }
2847 }
2848
2849 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2850 if (memcmp(h, ";st=", 4) == 0) {
2851 int i;
2852 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002853 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002854 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2855 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002857 break;
2858 }
2859 }
2860 break;
2861 }
2862 }
2863
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002864 appctx->ctx.stats.scope_str = 0;
2865 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002866 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2867 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2868 int itx = 0;
2869 const char *h2;
2870 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2871 const char *err;
2872
2873 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2874 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002875 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002876 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2877 itx++;
2878 h++;
2879 }
2880
2881 if (itx > STAT_SCOPE_TXT_MAXLEN)
2882 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002883 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002884
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002885 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002886 memcpy(scope_txt, h2, itx);
2887 scope_txt[itx] = '\0';
2888 err = invalid_char(scope_txt);
2889 if (err) {
2890 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 appctx->ctx.stats.scope_str = 0;
2892 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002893 }
2894 break;
2895 }
2896 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002897
2898 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002899 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002900 int ret = 1;
2901
2902 if (stats_admin_rule->cond) {
2903 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2904 ret = acl_pass(ret);
2905 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2906 ret = !ret;
2907 }
2908
2909 if (ret) {
2910 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002911 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002912 break;
2913 }
2914 }
2915
2916 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002917 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002918 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002919 if (msg->msg_state < HTTP_MSG_100_SENT) {
2920 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2921 * send an HTTP/1.1 100 Continue intermediate response.
2922 */
2923 if (msg->flags & HTTP_MSGF_VER_11) {
2924 struct hdr_ctx ctx;
2925 ctx.idx = 0;
2926 /* Expect is allowed in 1.1, look for it */
2927 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2928 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2929 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2930 }
2931 }
2932 msg->msg_state = HTTP_MSG_100_SENT;
2933 s->logs.tv_request = now; /* update the request timer to reflect full request */
2934 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002935 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002936 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002937 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002938 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2939 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002940 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002941 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002942 else {
2943 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002945 }
2946
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002947 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002948 return 1;
2949}
2950
Lukas Tribus67db8df2013-06-23 17:37:13 +02002951/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2952 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2953 */
2954static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
2955{
2956#ifdef IP_TOS
2957 if (from.ss_family == AF_INET)
2958 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2959#endif
2960#ifdef IPV6_TCLASS
2961 if (from.ss_family == AF_INET6) {
2962 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
2963 /* v4-mapped addresses need IP_TOS */
2964 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2965 else
2966 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2967 }
2968#endif
2969}
2970
Willy Tarreau20b0de52012-12-24 15:45:22 +01002971/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01002972 * transaction <txn>. Returns the first rule that prevents further processing
2973 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
2974 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
2975 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002976 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01002977static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01002978http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002979{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002980 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01002981 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002982 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002983
Willy Tarreauff011f22011-01-06 17:51:27 +01002984 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002985 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002986 continue;
2987
Willy Tarreau96257ec2012-12-27 10:46:37 +01002988 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002990 int ret;
2991
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002992 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002993 ret = acl_pass(ret);
2994
Willy Tarreauff011f22011-01-06 17:51:27 +01002995 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002996 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002997
2998 if (!ret) /* condition not matched */
2999 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003000 }
3001
Willy Tarreau20b0de52012-12-24 15:45:22 +01003002
Willy Tarreau96257ec2012-12-27 10:46:37 +01003003 switch (rule->action) {
3004 case HTTP_REQ_ACT_ALLOW:
3005 return NULL; /* "allow" rules are OK */
3006
3007 case HTTP_REQ_ACT_DENY:
3008 txn->flags |= TX_CLDENY;
3009 return rule;
3010
Willy Tarreauccbcc372012-12-27 12:37:57 +01003011 case HTTP_REQ_ACT_TARPIT:
3012 txn->flags |= TX_CLTARPIT;
3013 return rule;
3014
Willy Tarreau96257ec2012-12-27 10:46:37 +01003015 case HTTP_REQ_ACT_AUTH:
3016 return rule;
3017
Willy Tarreau81499eb2012-12-27 12:19:02 +01003018 case HTTP_REQ_ACT_REDIR:
3019 return rule;
3020
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003021 case HTTP_REQ_ACT_SET_NICE:
3022 s->task->nice = rule->arg.nice;
3023 break;
3024
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003025 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003026 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003027 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003028 break;
3029
Willy Tarreau51347ed2013-06-11 19:34:13 +02003030 case HTTP_REQ_ACT_SET_MARK:
3031#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003032 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003033 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003034#endif
3035 break;
3036
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003037 case HTTP_REQ_ACT_SET_LOGL:
3038 s->logs.level = rule->arg.loglevel;
3039 break;
3040
Willy Tarreau96257ec2012-12-27 10:46:37 +01003041 case HTTP_REQ_ACT_SET_HDR:
3042 ctx.idx = 0;
3043 /* remove all occurrences of the header */
3044 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3045 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3046 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003047 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003048 /* now fall through to header addition */
3049
3050 case HTTP_REQ_ACT_ADD_HDR:
3051 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3052 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3053 trash.len = rule->arg.hdr_add.name_len;
3054 trash.str[trash.len++] = ':';
3055 trash.str[trash.len++] = ' ';
3056 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3057 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3058 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003059 }
3060 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003061
3062 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003063 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003064}
3065
Willy Tarreau71241ab2012-12-27 11:30:54 +01003066
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003067/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3068 * transaction <txn>. Returns the first rule that prevents further processing
3069 * of the response (deny, ...) or NULL if it executed all rules or stopped
3070 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3071 * rule.
3072 */
3073static struct http_res_rule *
3074http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3075{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003076 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003077 struct http_res_rule *rule;
3078 struct hdr_ctx ctx;
3079
3080 list_for_each_entry(rule, rules, list) {
3081 if (rule->action >= HTTP_RES_ACT_MAX)
3082 continue;
3083
3084 /* check optional condition */
3085 if (rule->cond) {
3086 int ret;
3087
3088 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3089 ret = acl_pass(ret);
3090
3091 if (rule->cond->pol == ACL_COND_UNLESS)
3092 ret = !ret;
3093
3094 if (!ret) /* condition not matched */
3095 continue;
3096 }
3097
3098
3099 switch (rule->action) {
3100 case HTTP_RES_ACT_ALLOW:
3101 return NULL; /* "allow" rules are OK */
3102
3103 case HTTP_RES_ACT_DENY:
3104 txn->flags |= TX_SVDENY;
3105 return rule;
3106
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003107 case HTTP_RES_ACT_SET_NICE:
3108 s->task->nice = rule->arg.nice;
3109 break;
3110
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003111 case HTTP_RES_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003112 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003113 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003114 break;
3115
Willy Tarreau51347ed2013-06-11 19:34:13 +02003116 case HTTP_RES_ACT_SET_MARK:
3117#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003118 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003119 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003120#endif
3121 break;
3122
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003123 case HTTP_RES_ACT_SET_LOGL:
3124 s->logs.level = rule->arg.loglevel;
3125 break;
3126
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003127 case HTTP_RES_ACT_SET_HDR:
3128 ctx.idx = 0;
3129 /* remove all occurrences of the header */
3130 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3131 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3132 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3133 }
3134 /* now fall through to header addition */
3135
3136 case HTTP_RES_ACT_ADD_HDR:
3137 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3138 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3139 trash.len = rule->arg.hdr_add.name_len;
3140 trash.str[trash.len++] = ':';
3141 trash.str[trash.len++] = ' ';
3142 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3143 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3144 break;
3145 }
3146 }
3147
3148 /* we reached the end of the rules, nothing to report */
3149 return NULL;
3150}
3151
3152
Willy Tarreau71241ab2012-12-27 11:30:54 +01003153/* Perform an HTTP redirect based on the information in <rule>. The function
3154 * returns non-zero on success, or zero in case of a, irrecoverable error such
3155 * as too large a request to build a valid response.
3156 */
3157static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3158{
3159 struct http_msg *msg = &txn->req;
3160 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003161 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003162
3163 /* build redirect message */
3164 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003165 case 308:
3166 msg_fmt = HTTP_308;
3167 break;
3168 case 307:
3169 msg_fmt = HTTP_307;
3170 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003171 case 303:
3172 msg_fmt = HTTP_303;
3173 break;
3174 case 301:
3175 msg_fmt = HTTP_301;
3176 break;
3177 case 302:
3178 default:
3179 msg_fmt = HTTP_302;
3180 break;
3181 }
3182
3183 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3184 return 0;
3185
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003186 location = trash.str + trash.len;
3187
Willy Tarreau71241ab2012-12-27 11:30:54 +01003188 switch(rule->type) {
3189 case REDIRECT_TYPE_SCHEME: {
3190 const char *path;
3191 const char *host;
3192 struct hdr_ctx ctx;
3193 int pathlen;
3194 int hostlen;
3195
3196 host = "";
3197 hostlen = 0;
3198 ctx.idx = 0;
3199 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3200 host = ctx.line + ctx.val;
3201 hostlen = ctx.vlen;
3202 }
3203
3204 path = http_get_path(txn);
3205 /* build message using path */
3206 if (path) {
3207 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3208 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3209 int qs = 0;
3210 while (qs < pathlen) {
3211 if (path[qs] == '?') {
3212 pathlen = qs;
3213 break;
3214 }
3215 qs++;
3216 }
3217 }
3218 } else {
3219 path = "/";
3220 pathlen = 1;
3221 }
3222
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003223 if (rule->rdr_str) { /* this is an old "redirect" rule */
3224 /* check if we can add scheme + "://" + host + path */
3225 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3226 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003228 /* add scheme */
3229 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3230 trash.len += rule->rdr_len;
3231 }
3232 else {
3233 /* add scheme with executing log format */
3234 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003236 /* check if we can add scheme + "://" + host + path */
3237 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3238 return 0;
3239 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003240 /* add "://" */
3241 memcpy(trash.str + trash.len, "://", 3);
3242 trash.len += 3;
3243
3244 /* add host */
3245 memcpy(trash.str + trash.len, host, hostlen);
3246 trash.len += hostlen;
3247
3248 /* add path */
3249 memcpy(trash.str + trash.len, path, pathlen);
3250 trash.len += pathlen;
3251
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003252 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003253 if (trash.len && trash.str[trash.len - 1] != '/' &&
3254 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3255 if (trash.len > trash.size - 5)
3256 return 0;
3257 trash.str[trash.len] = '/';
3258 trash.len++;
3259 }
3260
3261 break;
3262 }
3263 case REDIRECT_TYPE_PREFIX: {
3264 const char *path;
3265 int pathlen;
3266
3267 path = http_get_path(txn);
3268 /* build message using path */
3269 if (path) {
3270 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3271 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3272 int qs = 0;
3273 while (qs < pathlen) {
3274 if (path[qs] == '?') {
3275 pathlen = qs;
3276 break;
3277 }
3278 qs++;
3279 }
3280 }
3281 } else {
3282 path = "/";
3283 pathlen = 1;
3284 }
3285
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003286 if (rule->rdr_str) { /* this is an old "redirect" rule */
3287 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3288 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003289
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003290 /* add prefix. Note that if prefix == "/", we don't want to
3291 * add anything, otherwise it makes it hard for the user to
3292 * configure a self-redirection.
3293 */
3294 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3295 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3296 trash.len += rule->rdr_len;
3297 }
3298 }
3299 else {
3300 /* add prefix with executing log format */
3301 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3302
3303 /* Check length */
3304 if (trash.len + pathlen > trash.size - 4)
3305 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003306 }
3307
3308 /* add path */
3309 memcpy(trash.str + trash.len, path, pathlen);
3310 trash.len += pathlen;
3311
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003312 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003313 if (trash.len && trash.str[trash.len - 1] != '/' &&
3314 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3315 if (trash.len > trash.size - 5)
3316 return 0;
3317 trash.str[trash.len] = '/';
3318 trash.len++;
3319 }
3320
3321 break;
3322 }
3323 case REDIRECT_TYPE_LOCATION:
3324 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003325 if (rule->rdr_str) { /* this is an old "redirect" rule */
3326 if (trash.len + rule->rdr_len > trash.size - 4)
3327 return 0;
3328
3329 /* add location */
3330 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3331 trash.len += rule->rdr_len;
3332 }
3333 else {
3334 /* add location with executing log format */
3335 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003337 /* Check left length */
3338 if (trash.len > trash.size - 4)
3339 return 0;
3340 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003341 break;
3342 }
3343
3344 if (rule->cookie_len) {
3345 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3346 trash.len += 14;
3347 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3348 trash.len += rule->cookie_len;
3349 memcpy(trash.str + trash.len, "\r\n", 2);
3350 trash.len += 2;
3351 }
3352
3353 /* add end of headers and the keep-alive/close status.
3354 * We may choose to set keep-alive if the Location begins
3355 * with a slash, because the client will come back to the
3356 * same server.
3357 */
3358 txn->status = rule->code;
3359 /* let's log the request time */
3360 s->logs.tv_request = now;
3361
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003362 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003363 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3364 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3365 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3366 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3367 /* keep-alive possible */
3368 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3369 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3370 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3371 trash.len += 30;
3372 } else {
3373 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3374 trash.len += 24;
3375 }
3376 }
3377 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3378 trash.len += 4;
3379 bo_inject(txn->rsp.chn, trash.str, trash.len);
3380 /* "eat" the request */
3381 bi_fast_delete(txn->req.chn->buf, msg->sov);
3382 msg->sov = 0;
3383 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3384 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3385 txn->req.msg_state = HTTP_MSG_CLOSED;
3386 txn->rsp.msg_state = HTTP_MSG_DONE;
3387 } else {
3388 /* keep-alive not possible */
3389 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3390 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3391 trash.len += 29;
3392 } else {
3393 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3394 trash.len += 23;
3395 }
3396 stream_int_retnclose(txn->req.chn->prod, &trash);
3397 txn->req.chn->analysers = 0;
3398 }
3399
3400 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003401 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003402 if (!(s->flags & SN_FINST_MASK))
3403 s->flags |= SN_FINST_R;
3404
3405 return 1;
3406}
3407
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003408/* This stream analyser runs all HTTP request processing which is common to
3409 * frontends and backends, which means blocking ACLs, filters, connection-close,
3410 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003411 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003412 * either needs more data or wants to immediately abort the request (eg: deny,
3413 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003414 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003415int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003416{
Willy Tarreaud787e662009-07-07 10:14:51 +02003417 struct http_txn *txn = &s->txn;
3418 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003419 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003420 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003421 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003422 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003423
Willy Tarreau655dce92009-11-08 13:10:58 +01003424 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003425 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003426 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003427 return 0;
3428 }
3429
Willy Tarreau3a816292009-07-07 10:55:49 +02003430 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003431 req->analyse_exp = TICK_ETERNITY;
3432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003433 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 +02003434 now_ms, __FUNCTION__,
3435 s,
3436 req,
3437 req->rex, req->wex,
3438 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003439 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003440 req->analysers);
3441
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003442 /* first check whether we have some ACLs set to block this request */
3443 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003444 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003445
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003446 ret = acl_pass(ret);
3447 if (cond->pol == ACL_COND_UNLESS)
3448 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 if (ret) {
3451 txn->status = 403;
3452 /* let's log the request time */
3453 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003454 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003455 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003457 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003458 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003459
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003460 /* just in case we have some per-backend tracking */
3461 session_inc_be_http_req_ctr(s);
3462
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003463 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003464 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003465
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003466 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003467 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003468 if (stats_check_uri(s->rep->prod, txn, px)) {
3469 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003470 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3471 txn->status = 500;
3472 s->logs.tv_request = now;
3473 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003474
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003475 if (!(s->flags & SN_ERR_MASK))
3476 s->flags |= SN_ERR_RESOURCE;
3477 goto return_prx_cond;
3478 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003479 /* parse the whole stats request and extract the relevant information */
3480 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003481 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 +01003482 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003483 }
3484
Willy Tarreau3b44e722013-11-16 10:28:23 +01003485 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3486 * blocked by an http-request rule.
3487 */
3488 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003489 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003490 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003491 }
Willy Tarreau06619262006-12-17 08:37:22 +01003492
Willy Tarreau3b44e722013-11-16 10:28:23 +01003493 /* return a 403 if either rule has blocked */
3494 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003495 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003496 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003497 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003498 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003499 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003500 s->fe->fe_counters.denied_req++;
3501 if (s->fe != s->be)
3502 s->be->be_counters.denied_req++;
3503 if (s->listener->counters)
3504 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003505 goto return_prx_cond;
3506 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003507
3508 /* When a connection is tarpitted, we use the tarpit timeout,
3509 * which may be the same as the connect timeout if unspecified.
3510 * If unset, then set it to zero because we really want it to
3511 * eventually expire. We build the tarpit as an analyser.
3512 */
3513 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003514 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003515 /* wipe the request out so that we can drop the connection early
3516 * if the client closes first.
3517 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003518 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003519 req->analysers = 0; /* remove switching rules etc... */
3520 req->analysers |= AN_REQ_HTTP_TARPIT;
3521 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3522 if (!req->analyse_exp)
3523 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003524 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003525 s->fe->fe_counters.denied_req++;
3526 if (s->fe != s->be)
3527 s->be->be_counters.denied_req++;
3528 if (s->listener->counters)
3529 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003530 return 1;
3531 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003532 }
Willy Tarreau06619262006-12-17 08:37:22 +01003533
Willy Tarreau5b154472009-12-21 20:11:07 +01003534 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3535 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003536 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3537 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003538 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3539 * avoid to redo the same work if FE and BE have the same settings (common).
3540 * The method consists in checking if options changed between the two calls
3541 * (implying that either one is non-null, or one of them is non-null and we
3542 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003543 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003544
Willy Tarreaudc008c52010-02-01 16:20:08 +01003545 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3546 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3547 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3548 (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01003549 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003550
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003551 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3552 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003553 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003554 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3555 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003556 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003557 tmp = TX_CON_WANT_CLO;
3558
Willy Tarreau5b154472009-12-21 20:11:07 +01003559 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3560 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003561
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003562 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3563 /* parse the Connection header and possibly clean it */
3564 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003565 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003566 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3567 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003568 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003569 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003570 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003571 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003572 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003573
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003574 /* check if client or config asks for explicit close in KAL/SCL */
3575 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3576 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3577 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003578 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003579 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003580 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003581 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003582 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3583 }
Willy Tarreau78599912009-10-17 20:12:21 +02003584
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003585 /* we can be blocked here because the request needs to be authenticated,
3586 * either to pass or to access stats.
3587 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003588 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003589 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003590
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003591 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003592 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003593
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003594 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003595 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003596 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003597 /* on 401 we still count one error, because normal browsing
3598 * won't significantly increase the counter but brute force
3599 * attempts will.
3600 */
3601 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003602 goto return_prx_cond;
3603 }
3604
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003605 /* add request headers from the rule sets in the same order */
3606 list_for_each_entry(wl, &px->req_add, list) {
3607 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003608 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003609 ret = acl_pass(ret);
3610 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3611 ret = !ret;
3612 if (!ret)
3613 continue;
3614 }
3615
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003616 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003618 }
3619
3620 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3621 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3622 goto return_bad_req;
3623 req->analyse_exp = TICK_ETERNITY;
3624 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003625 }
3626
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003627 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003628 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003629 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3630 s->fe->fe_counters.intercepted_req++;
3631
3632 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3633 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3634 if (!(s->flags & SN_FINST_MASK))
3635 s->flags |= SN_FINST_R;
3636
3637 req->analyse_exp = TICK_ETERNITY;
3638 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003639 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003640 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003641
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003642 /* check whether we have some ACLs set to redirect this request */
3643 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003644 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003645 int ret;
3646
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003647 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003648 ret = acl_pass(ret);
3649 if (rule->cond->pol == ACL_COND_UNLESS)
3650 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003651 if (!ret)
3652 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003653 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003654 if (!http_apply_redirect_rule(rule, s, txn))
3655 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003656
Willy Tarreau71241ab2012-12-27 11:30:54 +01003657 req->analyse_exp = TICK_ETERNITY;
3658 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003659 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003660
Willy Tarreau2be39392010-01-03 17:24:51 +01003661 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3662 * If this happens, then the data will not come immediately, so we must
3663 * send all what we have without waiting. Note that due to the small gain
3664 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003665 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003666 * itself once used.
3667 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003668 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003669
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 /* that's OK for us now, let's move on to next analysers */
3671 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003672
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003673 return_bad_req:
3674 /* We centralize bad requests processing here */
3675 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3676 /* we detected a parsing error. We want to archive this request
3677 * in the dedicated proxy area for later troubleshooting.
3678 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003679 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003680 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003681
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003682 txn->req.msg_state = HTTP_MSG_ERROR;
3683 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003684 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003685
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003686 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003687 if (s->listener->counters)
3688 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003689
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003690 return_prx_cond:
3691 if (!(s->flags & SN_ERR_MASK))
3692 s->flags |= SN_ERR_PRXCOND;
3693 if (!(s->flags & SN_FINST_MASK))
3694 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003695
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003696 req->analysers = 0;
3697 req->analyse_exp = TICK_ETERNITY;
3698 return 0;
3699}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003700
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003701/* This function performs all the processing enabled for the current request.
3702 * It returns 1 if the processing can continue on next analysers, or zero if it
3703 * needs more data, encounters an error, or wants to immediately abort the
3704 * request. It relies on buffers flags, and updates s->req->analysers.
3705 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003706int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003707{
3708 struct http_txn *txn = &s->txn;
3709 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003710 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003711
Willy Tarreau655dce92009-11-08 13:10:58 +01003712 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003713 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003714 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003715 return 0;
3716 }
3717
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003718 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 +02003719 now_ms, __FUNCTION__,
3720 s,
3721 req,
3722 req->rex, req->wex,
3723 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003724 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003725 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003726
William Lallemand82fe75c2012-10-23 10:25:10 +02003727 if (s->fe->comp || s->be->comp)
3728 select_compression_request_header(s, req->buf);
3729
Willy Tarreau59234e92008-11-30 23:51:27 +01003730 /*
3731 * Right now, we know that we have processed the entire headers
3732 * and that unwanted requests have been filtered out. We can do
3733 * whatever we want with the remaining request. Also, now we
3734 * may have separate values for ->fe, ->be.
3735 */
Willy Tarreau06619262006-12-17 08:37:22 +01003736
Willy Tarreau59234e92008-11-30 23:51:27 +01003737 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003738 * If HTTP PROXY is set we simply get remote server address parsing
3739 * incoming request. Note that this requires that a connection is
3740 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003741 */
3742 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003743 struct connection *conn;
3744
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003745 /* Note that for now we don't reuse existing proxy connections */
3746 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003747 txn->req.msg_state = HTTP_MSG_ERROR;
3748 txn->status = 500;
3749 req->analysers = 0;
3750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3751
3752 if (!(s->flags & SN_ERR_MASK))
3753 s->flags |= SN_ERR_RESOURCE;
3754 if (!(s->flags & SN_FINST_MASK))
3755 s->flags |= SN_FINST_R;
3756
3757 return 0;
3758 }
3759 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003760 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003761
Willy Tarreau59234e92008-11-30 23:51:27 +01003762 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003763 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003764 * Note that doing so might move headers in the request, but
3765 * the fields will stay coherent and the URI will not move.
3766 * This should only be performed in the backend.
3767 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003768 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3770 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003771
Willy Tarreau59234e92008-11-30 23:51:27 +01003772 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003773 * 8: the appsession cookie was looked up very early in 1.2,
3774 * so let's do the same now.
3775 */
3776
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003777 /* It needs to look into the URI unless persistence must be ignored */
3778 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003779 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 +01003780 }
3781
William Lallemanda73203e2012-03-12 12:48:57 +01003782 /* add unique-id if "header-unique-id" is specified */
3783
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003784 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3785 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3786 goto return_bad_req;
3787 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003788 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003789 }
William Lallemanda73203e2012-03-12 12:48:57 +01003790
3791 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003792 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3793 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003794 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003795 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003796 goto return_bad_req;
3797 }
3798
Cyril Bontéb21570a2009-11-29 20:04:48 +01003799 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003800 * 9: add X-Forwarded-For if either the frontend or the backend
3801 * asks for it.
3802 */
3803 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003804 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003805 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003806 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3807 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003808 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003809 /* The header is set to be added only if none is present
3810 * and we found it, so don't do anything.
3811 */
3812 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003813 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003814 /* Add an X-Forwarded-For header unless the source IP is
3815 * in the 'except' network range.
3816 */
3817 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003818 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003819 != s->fe->except_net.s_addr) &&
3820 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003821 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003822 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003823 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003824 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003825 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003826
3827 /* Note: we rely on the backend to get the header name to be used for
3828 * x-forwarded-for, because the header is really meant for the backends.
3829 * However, if the backend did not specify any option, we have to rely
3830 * on the frontend's header name.
3831 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003832 if (s->be->fwdfor_hdr_len) {
3833 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003834 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003835 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003836 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003837 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003838 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003839 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003840
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003841 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003842 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003843 }
3844 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003845 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003846 /* FIXME: for the sake of completeness, we should also support
3847 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003848 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003849 int len;
3850 char pn[INET6_ADDRSTRLEN];
3851 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003852 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003853 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003854
Willy Tarreau59234e92008-11-30 23:51:27 +01003855 /* Note: we rely on the backend to get the header name to be used for
3856 * x-forwarded-for, because the header is really meant for the backends.
3857 * However, if the backend did not specify any option, we have to rely
3858 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003859 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003860 if (s->be->fwdfor_hdr_len) {
3861 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003862 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003863 } else {
3864 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003865 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003866 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003867 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003869 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003870 goto return_bad_req;
3871 }
3872 }
3873
3874 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003875 * 10: add X-Original-To if either the frontend or the backend
3876 * asks for it.
3877 */
3878 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3879
3880 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003881 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003882 /* Add an X-Original-To header unless the destination IP is
3883 * in the 'except' network range.
3884 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003885 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003886
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003887 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003888 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003889 (((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 +02003890 != s->fe->except_to.s_addr) &&
3891 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003892 (((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 +02003893 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003894 int len;
3895 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003896 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003897
3898 /* Note: we rely on the backend to get the header name to be used for
3899 * x-original-to, because the header is really meant for the backends.
3900 * However, if the backend did not specify any option, we have to rely
3901 * on the frontend's header name.
3902 */
3903 if (s->be->orgto_hdr_len) {
3904 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003905 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003906 } else {
3907 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003908 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003909 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003910 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003911
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003912 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003913 goto return_bad_req;
3914 }
3915 }
3916 }
3917
Willy Tarreau50fc7772012-11-11 22:19:57 +01003918 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3919 * If an "Upgrade" token is found, the header is left untouched in order not to have
3920 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3921 * "Upgrade" is present in the Connection header.
3922 */
3923 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3924 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3925 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003926 unsigned int want_flags = 0;
3927
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003928 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003929 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3930 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3931 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003932 want_flags |= TX_CON_CLO_SET;
3933 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003934 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3935 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3936 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003937 want_flags |= TX_CON_KAL_SET;
3938 }
3939
3940 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003941 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003942 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003943
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003944
Willy Tarreau522d6c02009-12-06 18:49:18 +01003945 /* If we have no server assigned yet and we're balancing on url_param
3946 * with a POST request, we may be interested in checking the body for
3947 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003948 */
3949 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3950 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003951 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003952 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003953 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003954 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003955 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003956
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003957 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003958 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003959#ifdef TCP_QUICKACK
3960 /* We expect some data from the client. Unless we know for sure
3961 * we already have a full request, we have to re-enable quick-ack
3962 * in case we previously disabled it, otherwise we might cause
3963 * the client to delay further data.
3964 */
3965 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreauf79c8172013-10-21 16:30:56 +02003966 cli_conn && (cli_conn->flags & CO_FL_CTRL_READY) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003967 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003968 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003969 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003970#endif
3971 }
Willy Tarreau03945942009-12-22 16:50:27 +01003972
Willy Tarreau59234e92008-11-30 23:51:27 +01003973 /*************************************************************
3974 * OK, that's finished for the headers. We have done what we *
3975 * could. Let's switch to the DATA state. *
3976 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003977 req->analyse_exp = TICK_ETERNITY;
3978 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003979
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003980 /* if the server closes the connection, we want to immediately react
3981 * and close the socket to save packets and syscalls.
3982 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003983 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3984 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003985
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003987 /* OK let's go on with the BODY now */
3988 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003989
Willy Tarreau59234e92008-11-30 23:51:27 +01003990 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003991 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003992 /* we detected a parsing error. We want to archive this request
3993 * in the dedicated proxy area for later troubleshooting.
3994 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003995 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003996 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003997
Willy Tarreau59234e92008-11-30 23:51:27 +01003998 txn->req.msg_state = HTTP_MSG_ERROR;
3999 txn->status = 400;
4000 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004001 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004002
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004003 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004004 if (s->listener->counters)
4005 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004006
Willy Tarreau59234e92008-11-30 23:51:27 +01004007 if (!(s->flags & SN_ERR_MASK))
4008 s->flags |= SN_ERR_PRXCOND;
4009 if (!(s->flags & SN_FINST_MASK))
4010 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004011 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004012}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004013
Willy Tarreau60b85b02008-11-30 23:28:40 +01004014/* This function is an analyser which processes the HTTP tarpit. It always
4015 * returns zero, at the beginning because it prevents any other processing
4016 * from occurring, and at the end because it terminates the request.
4017 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004018int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004019{
4020 struct http_txn *txn = &s->txn;
4021
4022 /* This connection is being tarpitted. The CLIENT side has
4023 * already set the connect expiration date to the right
4024 * timeout. We just have to check that the client is still
4025 * there and that the timeout has not expired.
4026 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004027 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004028 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004029 !tick_is_expired(req->analyse_exp, now_ms))
4030 return 0;
4031
4032 /* We will set the queue timer to the time spent, just for
4033 * logging purposes. We fake a 500 server error, so that the
4034 * attacker will not suspect his connection has been tarpitted.
4035 * It will not cause trouble to the logs because we can exclude
4036 * the tarpitted connections by filtering on the 'PT' status flags.
4037 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004038 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4039
4040 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004041 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004042 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004043
4044 req->analysers = 0;
4045 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004046
Willy Tarreau60b85b02008-11-30 23:28:40 +01004047 if (!(s->flags & SN_ERR_MASK))
4048 s->flags |= SN_ERR_PRXCOND;
4049 if (!(s->flags & SN_FINST_MASK))
4050 s->flags |= SN_FINST_T;
4051 return 0;
4052}
4053
Willy Tarreaud34af782008-11-30 23:36:37 +01004054/* This function is an analyser which processes the HTTP request body. It looks
4055 * for parameters to be used for the load balancing algorithm (url_param). It
4056 * must only be called after the standard HTTP request processing has occurred,
4057 * because it expects the request to be parsed. It returns zero if it needs to
4058 * read more data, or 1 once it has completed its analysis.
4059 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004060int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004061{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004062 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004063 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004064 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004065
4066 /* We have to parse the HTTP request body to find any required data.
4067 * "balance url_param check_post" should have been the only way to get
4068 * into this. We were brought here after HTTP header analysis, so all
4069 * related structures are ready.
4070 */
4071
Willy Tarreau522d6c02009-12-06 18:49:18 +01004072 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4073 goto missing_data;
4074
4075 if (msg->msg_state < HTTP_MSG_100_SENT) {
4076 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4077 * send an HTTP/1.1 100 Continue intermediate response.
4078 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004079 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004080 struct hdr_ctx ctx;
4081 ctx.idx = 0;
4082 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004083 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004084 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004085 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004086 }
4087 }
4088 msg->msg_state = HTTP_MSG_100_SENT;
4089 }
4090
4091 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004092 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004093 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004094 * is still null. We must save the body in msg->next because it
4095 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004096 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004097 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004098
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004099 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004100 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4101 else
4102 msg->msg_state = HTTP_MSG_DATA;
4103 }
4104
4105 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004106 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004107 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004108 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004109 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004110 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004111
Willy Tarreau115acb92009-12-26 13:56:06 +01004112 if (!ret)
4113 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004114 else if (ret < 0) {
4115 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004116 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004117 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004118 }
4119
Willy Tarreaud98cf932009-12-27 22:54:55 +01004120 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004121 * We have the first data byte is in msg->sov. We're waiting for at
4122 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004123 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004124
Willy Tarreau124d9912011-03-01 20:30:48 +01004125 if (msg->body_len < limit)
4126 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004127
Willy Tarreau9b28e032012-10-12 23:49:43 +02004128 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004129 goto http_end;
4130
4131 missing_data:
4132 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004133 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004134 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004135 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004136 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004137
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004138 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004139 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004140 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004141
4142 if (!(s->flags & SN_ERR_MASK))
4143 s->flags |= SN_ERR_CLITO;
4144 if (!(s->flags & SN_FINST_MASK))
4145 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004146 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004147 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004148
4149 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004150 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004151 /* Not enough data. We'll re-use the http-request
4152 * timeout here. Ideally, we should set the timeout
4153 * relative to the accept() date. We just set the
4154 * request timeout once at the beginning of the
4155 * request.
4156 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004157 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004158 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004159 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004160 return 0;
4161 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004162
4163 http_end:
4164 /* The situation will not evolve, so let's give up on the analysis. */
4165 s->logs.tv_request = now; /* update the request timer to reflect full request */
4166 req->analysers &= ~an_bit;
4167 req->analyse_exp = TICK_ETERNITY;
4168 return 1;
4169
4170 return_bad_req: /* let's centralize all bad requests */
4171 txn->req.msg_state = HTTP_MSG_ERROR;
4172 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004173 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004174
Willy Tarreau79ebac62010-06-07 13:47:49 +02004175 if (!(s->flags & SN_ERR_MASK))
4176 s->flags |= SN_ERR_PRXCOND;
4177 if (!(s->flags & SN_FINST_MASK))
4178 s->flags |= SN_FINST_R;
4179
Willy Tarreau522d6c02009-12-06 18:49:18 +01004180 return_err_msg:
4181 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004182 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004183 if (s->listener->counters)
4184 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004185 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004186}
4187
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004188/* send a server's name with an outgoing request over an established connection.
4189 * Note: this function is designed to be called once the request has been scheduled
4190 * for being forwarded. This is the reason why it rewinds the buffer before
4191 * proceeding.
4192 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004193int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004194
4195 struct hdr_ctx ctx;
4196
Mark Lamourinec2247f02012-01-04 13:02:01 -05004197 char *hdr_name = be->server_id_hdr_name;
4198 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004199 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004200 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004201 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004202
William Lallemandd9e90662012-01-30 17:27:17 +01004203 ctx.idx = 0;
4204
Willy Tarreau9b28e032012-10-12 23:49:43 +02004205 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004206 if (old_o) {
4207 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004208 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004209 }
4210
Willy Tarreau9b28e032012-10-12 23:49:43 +02004211 old_i = chn->buf->i;
4212 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 -05004213 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004214 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004215 }
4216
4217 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004218 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004219 memcpy(hdr_val, hdr_name, hdr_name_len);
4220 hdr_val += hdr_name_len;
4221 *hdr_val++ = ':';
4222 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004223 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4224 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004225
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004226 if (old_o) {
4227 /* If this was a forwarded request, we must readjust the amount of
4228 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004229 * variations. Note that if the request was already scheduled for
4230 * forwarding, it had its req->sol pointing to the body, which
4231 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004232 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004233 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004234 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004235 }
4236
Mark Lamourinec2247f02012-01-04 13:02:01 -05004237 return 0;
4238}
4239
Willy Tarreau610ecce2010-01-04 21:15:02 +01004240/* Terminate current transaction and prepare a new one. This is very tricky
4241 * right now but it works.
4242 */
4243void http_end_txn_clean_session(struct session *s)
4244{
4245 /* FIXME: We need a more portable way of releasing a backend's and a
4246 * server's connections. We need a safer way to reinitialize buffer
4247 * flags. We also need a more accurate method for computing per-request
4248 * data.
4249 */
4250 http_silent_debug(__LINE__, s);
4251
Willy Tarreau4213a112013-12-15 10:25:42 +01004252 /* unless we're doing keep-alive, we want to quickly close the connection
4253 * to the server.
4254 */
4255 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4256 !si_conn_ready(s->req->cons)) {
4257 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4258 si_shutr(s->req->cons);
4259 si_shutw(s->req->cons);
4260 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004261
4262 http_silent_debug(__LINE__, s);
4263
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004264 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004265 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004266 if (unlikely(s->srv_conn))
4267 sess_change_server(s, NULL);
4268 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004269
4270 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4271 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004272 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273
4274 if (s->txn.status) {
4275 int n;
4276
4277 n = s->txn.status / 100;
4278 if (n < 1 || n > 5)
4279 n = 0;
4280
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004281 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004282 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004283 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004284 s->fe->fe_counters.p.http.comp_rsp++;
4285 }
Willy Tarreau24657792010-02-26 10:30:28 +01004286 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004287 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004288 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004289 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004290 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004291 s->be->be_counters.p.http.comp_rsp++;
4292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 }
4294
4295 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004296 s->logs.bytes_in -= s->req->buf->i;
4297 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298
4299 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004300 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 !(s->flags & SN_MONITOR) &&
4302 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4303 s->do_log(s);
4304 }
4305
4306 s->logs.accept_date = date; /* user-visible date for logging */
4307 s->logs.tv_accept = now; /* corrected date for internal use */
4308 tv_zero(&s->logs.tv_request);
4309 s->logs.t_queue = -1;
4310 s->logs.t_connect = -1;
4311 s->logs.t_data = -1;
4312 s->logs.t_close = 0;
4313 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4314 s->logs.srv_queue_size = 0; /* we will get this number soon */
4315
Willy Tarreau9b28e032012-10-12 23:49:43 +02004316 s->logs.bytes_in = s->req->total = s->req->buf->i;
4317 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004318
4319 if (s->pend_pos)
4320 pendconn_free(s->pend_pos);
4321
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004322 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004323 if (s->flags & SN_CURR_SESS) {
4324 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004325 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004326 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004327 if (may_dequeue_tasks(objt_server(s->target), s->be))
4328 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 }
4330
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004331 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332
Willy Tarreau4213a112013-12-15 10:25:42 +01004333 /* only release our endpoint if we don't intend to reuse the
4334 * connection.
4335 */
4336 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4337 !si_conn_ready(s->req->cons)) {
4338 si_release_endpoint(s->req->cons);
4339 }
4340
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004343 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004344 s->req->cons->exp = TICK_ETERNITY;
4345 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004346 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4347 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 +02004348 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004350
4351 if (s->flags & SN_COMP_READY)
4352 s->comp_algo->end(&s->comp_ctx);
4353 s->comp_algo = NULL;
4354 s->flags &= ~SN_COMP_READY;
4355
Willy Tarreau610ecce2010-01-04 21:15:02 +01004356 s->txn.meth = 0;
4357 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004358 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004359 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360 s->req->cons->flags |= SI_FL_INDEP_STR;
4361
Willy Tarreau96e31212011-05-30 18:10:30 +02004362 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004363 s->req->flags |= CF_NEVER_WAIT;
4364 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004365 }
4366
Willy Tarreau610ecce2010-01-04 21:15:02 +01004367 /* if the request buffer is not empty, it means we're
4368 * about to process another request, so send pending
4369 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004370 * Just don't do this if the buffer is close to be full,
4371 * because the request will wait for it to flush a little
4372 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004374 if (s->req->buf->i) {
4375 if (s->rep->buf->o &&
4376 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4377 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004378 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004379 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004380
4381 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004382 channel_auto_read(s->req);
4383 channel_auto_close(s->req);
4384 channel_auto_read(s->rep);
4385 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004386
Willy Tarreau342b11c2010-11-24 16:22:09 +01004387 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004388 s->rep->analysers = 0;
4389
4390 http_silent_debug(__LINE__, s);
4391}
4392
4393
4394/* This function updates the request state machine according to the response
4395 * state machine and buffer flags. It returns 1 if it changes anything (flag
4396 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4397 * it is only used to find when a request/response couple is complete. Both
4398 * this function and its equivalent should loop until both return zero. It
4399 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4400 */
4401int http_sync_req_state(struct session *s)
4402{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004403 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004405 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004406 unsigned int old_state = txn->req.msg_state;
4407
4408 http_silent_debug(__LINE__, s);
4409 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4410 return 0;
4411
4412 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004413 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004414 * We can shut the read side unless we want to abort_on_close,
4415 * or we have a POST request. The issue with POST requests is
4416 * that some browsers still send a CRLF after the request, and
4417 * this CRLF must be read so that it does not remain in the kernel
4418 * buffers, otherwise a close could cause an RST on some systems
4419 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004420 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004421 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004422 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004423
Willy Tarreau40f151a2012-12-20 12:10:09 +01004424 /* if the server closes the connection, we want to immediately react
4425 * and close the socket to save packets and syscalls.
4426 */
4427 chn->cons->flags |= SI_FL_NOHALF;
4428
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4430 goto wait_other_side;
4431
4432 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4433 /* The server has not finished to respond, so we
4434 * don't want to move in order not to upset it.
4435 */
4436 goto wait_other_side;
4437 }
4438
4439 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4440 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004441 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004443 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 goto wait_other_side;
4445 }
4446
4447 /* When we get here, it means that both the request and the
4448 * response have finished receiving. Depending on the connection
4449 * mode, we'll have to wait for the last bytes to leave in either
4450 * direction, and sometimes for a close to be effective.
4451 */
4452
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004453 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4454 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004455 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4456 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004457 }
4458 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4459 /* Option forceclose is set, or either side wants to close,
4460 * let's enforce it now that we're not expecting any new
4461 * data to come. The caller knows the session is complete
4462 * once both states are CLOSED.
4463 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004464 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4465 channel_shutr_now(chn);
4466 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004468 }
4469 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004470 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4471 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004472 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004473 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4474 channel_auto_read(chn);
4475 txn->req.msg_state = HTTP_MSG_TUNNEL;
4476 chn->flags |= CF_NEVER_WAIT;
4477 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004478 }
4479
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004480 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004482 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004483
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004484 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485 txn->req.msg_state = HTTP_MSG_CLOSING;
4486 goto http_msg_closing;
4487 }
4488 else {
4489 txn->req.msg_state = HTTP_MSG_CLOSED;
4490 goto http_msg_closed;
4491 }
4492 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004493 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 }
4495
4496 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4497 http_msg_closing:
4498 /* nothing else to forward, just waiting for the output buffer
4499 * to be empty and for the shutw_now to take effect.
4500 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004501 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004502 txn->req.msg_state = HTTP_MSG_CLOSED;
4503 goto http_msg_closed;
4504 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004505 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 txn->req.msg_state = HTTP_MSG_ERROR;
4507 goto wait_other_side;
4508 }
4509 }
4510
4511 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4512 http_msg_closed:
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004513 if (!(s->be->options & PR_O_ABRT_CLOSE))
4514 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 goto wait_other_side;
4516 }
4517
4518 wait_other_side:
4519 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004520 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004521}
4522
4523
4524/* This function updates the response state machine according to the request
4525 * state machine and buffer flags. It returns 1 if it changes anything (flag
4526 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4527 * it is only used to find when a request/response couple is complete. Both
4528 * this function and its equivalent should loop until both return zero. It
4529 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4530 */
4531int http_sync_res_state(struct session *s)
4532{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004533 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004534 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004535 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536 unsigned int old_state = txn->rsp.msg_state;
4537
4538 http_silent_debug(__LINE__, s);
4539 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4540 return 0;
4541
4542 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4543 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004544 * still monitor the server connection for a possible close
4545 * while the request is being uploaded, so we don't disable
4546 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004548 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004549
4550 if (txn->req.msg_state == HTTP_MSG_ERROR)
4551 goto wait_other_side;
4552
4553 if (txn->req.msg_state < HTTP_MSG_DONE) {
4554 /* The client seems to still be sending data, probably
4555 * because we got an error response during an upload.
4556 * We have the choice of either breaking the connection
4557 * or letting it pass through. Let's do the later.
4558 */
4559 goto wait_other_side;
4560 }
4561
4562 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4563 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004564 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004566 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 goto wait_other_side;
4568 }
4569
4570 /* When we get here, it means that both the request and the
4571 * response have finished receiving. Depending on the connection
4572 * mode, we'll have to wait for the last bytes to leave in either
4573 * direction, and sometimes for a close to be effective.
4574 */
4575
4576 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4577 /* Server-close mode : shut read and wait for the request
4578 * side to close its output buffer. The caller will detect
4579 * when we're in DONE and the other is in CLOSED and will
4580 * catch that for the final cleanup.
4581 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004582 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4583 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004584 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004585 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4586 /* Option forceclose is set, or either side wants to close,
4587 * let's enforce it now that we're not expecting any new
4588 * data to come. The caller knows the session is complete
4589 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004591 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4592 channel_shutr_now(chn);
4593 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004594 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 }
4596 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004597 /* The last possible modes are keep-alive and tunnel. Tunnel will
4598 * need to forward remaining data. Keep-alive will need to monitor
4599 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004600 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004602 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004603 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4604 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605 }
4606
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004607 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004608 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004609 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004610 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4611 goto http_msg_closing;
4612 }
4613 else {
4614 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4615 goto http_msg_closed;
4616 }
4617 }
4618 goto wait_other_side;
4619 }
4620
4621 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4622 http_msg_closing:
4623 /* nothing else to forward, just waiting for the output buffer
4624 * to be empty and for the shutw_now to take effect.
4625 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004626 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4628 goto http_msg_closed;
4629 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004630 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004631 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004632 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004633 if (objt_server(s->target))
4634 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 goto wait_other_side;
4636 }
4637 }
4638
4639 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4640 http_msg_closed:
4641 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004642 bi_erase(chn);
4643 channel_auto_close(chn);
4644 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645 goto wait_other_side;
4646 }
4647
4648 wait_other_side:
4649 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004650 /* We force the response to leave immediately if we're waiting for the
4651 * other side, since there is no pending shutdown to push it out.
4652 */
4653 if (!channel_is_empty(chn))
4654 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004655 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656}
4657
4658
4659/* Resync the request and response state machines. Return 1 if either state
4660 * changes.
4661 */
4662int http_resync_states(struct session *s)
4663{
4664 struct http_txn *txn = &s->txn;
4665 int old_req_state = txn->req.msg_state;
4666 int old_res_state = txn->rsp.msg_state;
4667
4668 http_silent_debug(__LINE__, s);
4669 http_sync_req_state(s);
4670 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004671 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004672 if (!http_sync_res_state(s))
4673 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004674 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004675 if (!http_sync_req_state(s))
4676 break;
4677 }
4678 http_silent_debug(__LINE__, s);
4679 /* OK, both state machines agree on a compatible state.
4680 * There are a few cases we're interested in :
4681 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4682 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4683 * directions, so let's simply disable both analysers.
4684 * - HTTP_MSG_CLOSED on the response only means we must abort the
4685 * request.
4686 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4687 * with server-close mode means we've completed one request and we
4688 * must re-initialize the server connection.
4689 */
4690
4691 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4692 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4693 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4694 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4695 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004696 channel_auto_close(s->req);
4697 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004698 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004699 channel_auto_close(s->rep);
4700 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004701 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004702 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4703 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004704 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004705 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004706 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004707 channel_auto_close(s->rep);
4708 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004709 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004710 channel_abort(s->req);
4711 channel_auto_close(s->req);
4712 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004713 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004715 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4716 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004717 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004718 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4719 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4720 /* server-close/keep-alive: terminate this transaction,
4721 * possibly killing the server connection and reinitialize
4722 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004723 */
4724 http_end_txn_clean_session(s);
4725 }
4726
4727 http_silent_debug(__LINE__, s);
4728 return txn->req.msg_state != old_req_state ||
4729 txn->rsp.msg_state != old_res_state;
4730}
4731
Willy Tarreaud98cf932009-12-27 22:54:55 +01004732/* This function is an analyser which forwards request body (including chunk
4733 * sizes if any). It is called as soon as we must forward, even if we forward
4734 * zero byte. The only situation where it must not be called is when we're in
4735 * tunnel mode and we want to forward till the close. It's used both to forward
4736 * remaining data and to resync after end of body. It expects the msg_state to
4737 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4738 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004739 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004740 * bytes of pending data + the headers if not already done (between sol and sov).
4741 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004742 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004743int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004744{
4745 struct http_txn *txn = &s->txn;
4746 struct http_msg *msg = &s->txn.req;
4747
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004748 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4749 return 0;
4750
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004751 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004752 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004753 /* Output closed while we were sending data. We must abort and
4754 * wake the other side up.
4755 */
4756 msg->msg_state = HTTP_MSG_ERROR;
4757 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004758 return 1;
4759 }
4760
Willy Tarreau4fe41902010-06-07 22:27:41 +02004761 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004762 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763
4764 /* Note that we don't have to send 100-continue back because we don't
4765 * need the data to complete our job, and it's up to the server to
4766 * decide whether to return 100, 417 or anything else in return of
4767 * an "Expect: 100-continue" header.
4768 */
4769
4770 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004771 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004772 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004773 * is still null. We must save the body in msg->next because it
4774 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004775 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004776 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004777
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004778 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004779 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004780 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004781 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004782 }
4783
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004785 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004786
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004788 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004789 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004790 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004791 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004792 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004793 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004794 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004795 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004796
Willy Tarreaucaabe412010-01-03 23:08:28 +01004797 if (msg->msg_state == HTTP_MSG_DATA) {
4798 /* must still forward */
4799 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004800 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004801
4802 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004803 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004804 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004805 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004806 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004807 }
4808 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004809 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004810 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004811 * TRAILERS state.
4812 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004813 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004814
Willy Tarreau54d23df2012-10-25 19:04:45 +02004815 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004817 else if (ret < 0) {
4818 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004819 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004820 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004822 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004823 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004824 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004825 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004826 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004827 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004828
4829 if (ret == 0)
4830 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004831 else if (ret < 0) {
4832 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004833 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004834 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004835 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004836 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004837 /* we're in MSG_CHUNK_SIZE now */
4838 }
4839 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004840 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004841
4842 if (ret == 0)
4843 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004844 else if (ret < 0) {
4845 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004846 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004847 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004848 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004849 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004850 /* we're in HTTP_MSG_DONE now */
4851 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004852 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004853 int old_state = msg->msg_state;
4854
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004856 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004857 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4858 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004859 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004860 if (http_resync_states(s)) {
4861 /* some state changes occurred, maybe the analyser
4862 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004863 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004864 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004865 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004866 /* request errors are most likely due to
4867 * the server aborting the transfer.
4868 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004869 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004870 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004871 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004872 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004873 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004874 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004876 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004877
4878 /* If "option abortonclose" is set on the backend, we
4879 * want to monitor the client's connection and forward
4880 * any shutdown notification to the server, which will
4881 * decide whether to close or to go on processing the
4882 * request.
4883 */
4884 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004885 channel_auto_read(req);
4886 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004887 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004888 else if (s->txn.meth == HTTP_METH_POST) {
4889 /* POST requests may require to read extra CRLF
4890 * sent by broken browsers and which could cause
4891 * an RST to be sent upon close on some systems
4892 * (eg: Linux).
4893 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004894 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004895 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004896
Willy Tarreau610ecce2010-01-04 21:15:02 +01004897 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004898 }
4899 }
4900
Willy Tarreaud98cf932009-12-27 22:54:55 +01004901 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004902 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004903 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004904 if (!(s->flags & SN_ERR_MASK))
4905 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004906 if (!(s->flags & SN_FINST_MASK)) {
4907 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4908 s->flags |= SN_FINST_H;
4909 else
4910 s->flags |= SN_FINST_D;
4911 }
4912
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004913 s->fe->fe_counters.cli_aborts++;
4914 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004915 if (objt_server(s->target))
4916 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004917
4918 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004919 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004920
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004921 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004922 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004923 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004924
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004925 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004926 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004927 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004928 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004929 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004930
Willy Tarreau5c620922011-05-11 19:56:11 +02004931 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004932 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004933 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004934 * modes are already handled by the stream sock layer. We must not do
4935 * this in content-length mode because it could present the MSG_MORE
4936 * flag with the last block of forwarded data, which would cause an
4937 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004938 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004939 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004940 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004941
Willy Tarreau610ecce2010-01-04 21:15:02 +01004942 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004943 return 0;
4944
Willy Tarreaud98cf932009-12-27 22:54:55 +01004945 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004946 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004947 if (s->listener->counters)
4948 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004949 return_bad_req_stats_ok:
4950 txn->req.msg_state = HTTP_MSG_ERROR;
4951 if (txn->status) {
4952 /* Note: we don't send any error if some data were already sent */
4953 stream_int_retnclose(req->prod, NULL);
4954 } else {
4955 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004956 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004957 }
4958 req->analysers = 0;
4959 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004960
4961 if (!(s->flags & SN_ERR_MASK))
4962 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963 if (!(s->flags & SN_FINST_MASK)) {
4964 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4965 s->flags |= SN_FINST_H;
4966 else
4967 s->flags |= SN_FINST_D;
4968 }
4969 return 0;
4970
4971 aborted_xfer:
4972 txn->req.msg_state = HTTP_MSG_ERROR;
4973 if (txn->status) {
4974 /* Note: we don't send any error if some data were already sent */
4975 stream_int_retnclose(req->prod, NULL);
4976 } else {
4977 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004978 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004979 }
4980 req->analysers = 0;
4981 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4982
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004983 s->fe->fe_counters.srv_aborts++;
4984 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004985 if (objt_server(s->target))
4986 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004987
4988 if (!(s->flags & SN_ERR_MASK))
4989 s->flags |= SN_ERR_SRVCL;
4990 if (!(s->flags & SN_FINST_MASK)) {
4991 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4992 s->flags |= SN_FINST_H;
4993 else
4994 s->flags |= SN_FINST_D;
4995 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004996 return 0;
4997}
4998
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5000 * processing can continue on next analysers, or zero if it either needs more
5001 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5002 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5003 * when it has nothing left to do, and may remove any analyser when it wants to
5004 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005005 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005006int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005007{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005008 struct http_txn *txn = &s->txn;
5009 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005010 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005011 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005012 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005013 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005014
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005015 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 +02005016 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005017 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005018 rep,
5019 rep->rex, rep->wex,
5020 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005021 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005022 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005023
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005024 /*
5025 * Now parse the partial (or complete) lines.
5026 * We will check the response syntax, and also join multi-line
5027 * headers. An index of all the lines will be elaborated while
5028 * parsing.
5029 *
5030 * For the parsing, we use a 28 states FSM.
5031 *
5032 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005033 * rep->buf->p = beginning of response
5034 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5035 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005036 * msg->eol = end of current header or line (LF or CRLF)
5037 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005038 */
5039
Willy Tarreau83e3af02009-12-28 17:39:57 +01005040 /* There's a protected area at the end of the buffer for rewriting
5041 * purposes. We don't want to start to parse the request if the
5042 * protected area is affected, because we may have to move processed
5043 * data later, which is much more complicated.
5044 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005045 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005046 if (unlikely(!channel_reserved(rep))) {
5047 /* some data has still not left the buffer, wake us once that's done */
5048 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5049 goto abort_response;
5050 channel_dont_close(rep);
5051 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5052 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005053 }
5054
Willy Tarreau379357a2013-06-08 12:55:46 +02005055 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5056 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5057 buffer_slow_realign(rep->buf);
5058
Willy Tarreau9b28e032012-10-12 23:49:43 +02005059 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005060 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005061 }
5062
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005063 /* 1: we might have to print this header in debug mode */
5064 if (unlikely((global.mode & MODE_DEBUG) &&
5065 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005066 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005067 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005068
Willy Tarreau9b28e032012-10-12 23:49:43 +02005069 sol = rep->buf->p;
5070 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005071 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005072
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073 sol += hdr_idx_first_pos(&txn->hdr_idx);
5074 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005075
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005076 while (cur_idx) {
5077 eol = sol + txn->hdr_idx.v[cur_idx].len;
5078 debug_hdr("srvhdr", s, sol, eol);
5079 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5080 cur_idx = txn->hdr_idx.v[cur_idx].next;
5081 }
5082 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005083
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005084 /*
5085 * Now we quickly check if we have found a full valid response.
5086 * If not so, we check the FD and buffer states before leaving.
5087 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005088 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089 * responses are checked first.
5090 *
5091 * Depending on whether the client is still there or not, we
5092 * may send an error response back or not. Note that normally
5093 * we should only check for HTTP status there, and check I/O
5094 * errors somewhere else.
5095 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005096
Willy Tarreau655dce92009-11-08 13:10:58 +01005097 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005098 /* Invalid response */
5099 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5100 /* we detected a parsing error. We want to archive this response
5101 * in the dedicated proxy area for later troubleshooting.
5102 */
5103 hdr_response_bad:
5104 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005105 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005106
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005107 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005108 if (objt_server(s->target)) {
5109 objt_server(s->target)->counters.failed_resp++;
5110 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005111 }
Willy Tarreau64648412010-03-05 10:41:54 +01005112 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005113 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005114 rep->analysers = 0;
5115 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005116 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005117 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005118 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005119
5120 if (!(s->flags & SN_ERR_MASK))
5121 s->flags |= SN_ERR_PRXCOND;
5122 if (!(s->flags & SN_FINST_MASK))
5123 s->flags |= SN_FINST_H;
5124
5125 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005126 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005127
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005128 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005129 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005130 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005131 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 goto hdr_response_bad;
5133 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005134
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005135 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005136 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005137 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005138 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005139
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005140 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005141 if (objt_server(s->target)) {
5142 objt_server(s->target)->counters.failed_resp++;
5143 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005144 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005145
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005146 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147 rep->analysers = 0;
5148 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005149 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005150 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005151 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005153 if (!(s->flags & SN_ERR_MASK))
5154 s->flags |= SN_ERR_SRVCL;
5155 if (!(s->flags & SN_FINST_MASK))
5156 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005157 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005158 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005159
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005160 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005161 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005162 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005163 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005164
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005165 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005166 if (objt_server(s->target)) {
5167 objt_server(s->target)->counters.failed_resp++;
5168 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005169 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005170
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005171 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005172 rep->analysers = 0;
5173 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005174 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005175 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005176 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005177
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 if (!(s->flags & SN_ERR_MASK))
5179 s->flags |= SN_ERR_SRVTO;
5180 if (!(s->flags & SN_FINST_MASK))
5181 s->flags |= SN_FINST_H;
5182 return 0;
5183 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005184
Willy Tarreauf003d372012-11-26 13:35:37 +01005185 /* client abort with an abortonclose */
5186 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5187 s->fe->fe_counters.cli_aborts++;
5188 s->be->be_counters.cli_aborts++;
5189 if (objt_server(s->target))
5190 objt_server(s->target)->counters.cli_aborts++;
5191
5192 rep->analysers = 0;
5193 channel_auto_close(rep);
5194
5195 txn->status = 400;
5196 bi_erase(rep);
5197 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5198
5199 if (!(s->flags & SN_ERR_MASK))
5200 s->flags |= SN_ERR_CLICL;
5201 if (!(s->flags & SN_FINST_MASK))
5202 s->flags |= SN_FINST_H;
5203
5204 /* process_session() will take care of the error */
5205 return 0;
5206 }
5207
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005208 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005209 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005210 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005211 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005212
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005213 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005214 if (objt_server(s->target)) {
5215 objt_server(s->target)->counters.failed_resp++;
5216 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005217 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005218
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005219 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220 rep->analysers = 0;
5221 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005222 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005223 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005224 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005225
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005226 if (!(s->flags & SN_ERR_MASK))
5227 s->flags |= SN_ERR_SRVCL;
5228 if (!(s->flags & SN_FINST_MASK))
5229 s->flags |= SN_FINST_H;
5230 return 0;
5231 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005232
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005234 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005235 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005236 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005237
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005238 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005239 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005240 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005241
5242 if (!(s->flags & SN_ERR_MASK))
5243 s->flags |= SN_ERR_CLICL;
5244 if (!(s->flags & SN_FINST_MASK))
5245 s->flags |= SN_FINST_H;
5246
5247 /* process_session() will take care of the error */
5248 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005249 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005250
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005251 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005252 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253 return 0;
5254 }
5255
5256 /* More interesting part now : we know that we have a complete
5257 * response which at least looks like HTTP. We have an indicator
5258 * of each header's length, so we can parse them quickly.
5259 */
5260
5261 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005262 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 /*
5265 * 1: get the status code
5266 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005267 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268 if (n < 1 || n > 5)
5269 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005270 /* when the client triggers a 4xx from the server, it's most often due
5271 * to a missing object or permission. These events should be tracked
5272 * because if they happen often, it may indicate a brute force or a
5273 * vulnerability scan.
5274 */
5275 if (n == 4)
5276 session_inc_http_err_ctr(s);
5277
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005278 if (objt_server(s->target))
5279 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005280
Willy Tarreau5b154472009-12-21 20:11:07 +01005281 /* check if the response is HTTP/1.1 or above */
5282 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005283 ((rep->buf->p[5] > '1') ||
5284 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005285 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005286
5287 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005288 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 +01005289
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005290 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005291 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005292
Willy Tarreau9b28e032012-10-12 23:49:43 +02005293 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005294
Willy Tarreau39650402010-03-15 19:44:39 +01005295 /* Adjust server's health based on status code. Note: status codes 501
5296 * and 505 are triggered on demand by client request, so we must not
5297 * count them as server failures.
5298 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005299 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005300 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005301 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005302 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005303 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005304 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005305
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005306 /*
5307 * 2: check for cacheability.
5308 */
5309
5310 switch (txn->status) {
5311 case 200:
5312 case 203:
5313 case 206:
5314 case 300:
5315 case 301:
5316 case 410:
5317 /* RFC2616 @13.4:
5318 * "A response received with a status code of
5319 * 200, 203, 206, 300, 301 or 410 MAY be stored
5320 * by a cache (...) unless a cache-control
5321 * directive prohibits caching."
5322 *
5323 * RFC2616 @9.5: POST method :
5324 * "Responses to this method are not cacheable,
5325 * unless the response includes appropriate
5326 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005327 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005329 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5331 break;
5332 default:
5333 break;
5334 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005335
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005336 /*
5337 * 3: we may need to capture headers
5338 */
5339 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005340 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005341 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342 txn->rsp.cap, s->fe->rsp_cap);
5343
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005344 /* 4: determine the transfer-length.
5345 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5346 * the presence of a message-body in a RESPONSE and its transfer length
5347 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005348 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005349 * All responses to the HEAD request method MUST NOT include a
5350 * message-body, even though the presence of entity-header fields
5351 * might lead one to believe they do. All 1xx (informational), 204
5352 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5353 * message-body. All other responses do include a message-body,
5354 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005355 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005356 * 1. Any response which "MUST NOT" include a message-body (such as the
5357 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5358 * always terminated by the first empty line after the header fields,
5359 * regardless of the entity-header fields present in the message.
5360 *
5361 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5362 * the "chunked" transfer-coding (Section 6.2) is used, the
5363 * transfer-length is defined by the use of this transfer-coding.
5364 * If a Transfer-Encoding header field is present and the "chunked"
5365 * transfer-coding is not present, the transfer-length is defined by
5366 * the sender closing the connection.
5367 *
5368 * 3. If a Content-Length header field is present, its decimal value in
5369 * OCTETs represents both the entity-length and the transfer-length.
5370 * If a message is received with both a Transfer-Encoding header
5371 * field and a Content-Length header field, the latter MUST be ignored.
5372 *
5373 * 4. If the message uses the media type "multipart/byteranges", and
5374 * the transfer-length is not otherwise specified, then this self-
5375 * delimiting media type defines the transfer-length. This media
5376 * type MUST NOT be used unless the sender knows that the recipient
5377 * can parse it; the presence in a request of a Range header with
5378 * multiple byte-range specifiers from a 1.1 client implies that the
5379 * client can parse multipart/byteranges responses.
5380 *
5381 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005382 */
5383
5384 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005385 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005386 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005387 * FIXME: should we parse anyway and return an error on chunked encoding ?
5388 */
5389 if (txn->meth == HTTP_METH_HEAD ||
5390 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005391 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005392 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005393 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005394 goto skip_content_length;
5395 }
5396
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005397 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005398 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005399 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005400 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005401 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005402 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5403 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005404 /* bad transfer-encoding (chunked followed by something else) */
5405 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005406 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005407 break;
5408 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005409 }
5410
5411 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5412 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005413 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005414 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005415 signed long long cl;
5416
Willy Tarreauad14f752011-09-02 20:33:27 +02005417 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005418 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005419 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005420 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005421
Willy Tarreauad14f752011-09-02 20:33:27 +02005422 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005423 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005424 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005425 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005426
Willy Tarreauad14f752011-09-02 20:33:27 +02005427 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005428 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005429 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005430 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005431
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005432 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005433 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005434 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005435 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005436
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005437 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005438 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005439 }
5440
William Lallemand82fe75c2012-10-23 10:25:10 +02005441 if (s->fe->comp || s->be->comp)
5442 select_compression_response_header(s, rep->buf);
5443
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005444 /* FIXME: we should also implement the multipart/byterange method.
5445 * For now on, we resort to close mode in this case (unknown length).
5446 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005447skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005448
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005449 /* end of job, return OK */
5450 rep->analysers &= ~an_bit;
5451 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005452 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005453 return 1;
5454}
5455
5456/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005457 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5458 * and updates t->rep->analysers. It might make sense to explode it into several
5459 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005460 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005461int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005462{
5463 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005464 struct http_msg *msg = &txn->rsp;
5465 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005466 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005467 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005468
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005469 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 +02005470 now_ms, __FUNCTION__,
5471 t,
5472 rep,
5473 rep->rex, rep->wex,
5474 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005475 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005476 rep->analysers);
5477
Willy Tarreau655dce92009-11-08 13:10:58 +01005478 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005479 return 0;
5480
5481 rep->analysers &= ~an_bit;
5482 rep->analyse_exp = TICK_ETERNITY;
5483
Willy Tarreau5b154472009-12-21 20:11:07 +01005484 /* Now we have to check if we need to modify the Connection header.
5485 * This is more difficult on the response than it is on the request,
5486 * because we can have two different HTTP versions and we don't know
5487 * how the client will interprete a response. For instance, let's say
5488 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5489 * HTTP/1.1 response without any header. Maybe it will bound itself to
5490 * HTTP/1.0 because it only knows about it, and will consider the lack
5491 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5492 * the lack of header as a keep-alive. Thus we will use two flags
5493 * indicating how a request MAY be understood by the client. In case
5494 * of multiple possibilities, we'll fix the header to be explicit. If
5495 * ambiguous cases such as both close and keepalive are seen, then we
5496 * will fall back to explicit close. Note that we won't take risks with
5497 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005498 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005499 */
5500
Willy Tarreaudc008c52010-02-01 16:20:08 +01005501 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5502 txn->status == 101)) {
5503 /* Either we've established an explicit tunnel, or we're
5504 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005505 * to understand the next protocols. We have to switch to tunnel
5506 * mode, so that we transfer the request and responses then let
5507 * this protocol pass unmodified. When we later implement specific
5508 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005509 * header which contains information about that protocol for
5510 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005511 */
5512 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5513 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005514 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5515 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5516 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005517 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005518
Willy Tarreau60466522010-01-18 19:08:45 +01005519 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005520 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005521 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5522 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005523
Willy Tarreau60466522010-01-18 19:08:45 +01005524 /* now adjust header transformations depending on current state */
5525 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5526 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5527 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005528 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005529 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005530 }
Willy Tarreau60466522010-01-18 19:08:45 +01005531 else { /* SCL / KAL */
5532 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005533 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005534 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005535 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005536
Willy Tarreau60466522010-01-18 19:08:45 +01005537 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005538 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005539
Willy Tarreau60466522010-01-18 19:08:45 +01005540 /* Some keep-alive responses are converted to Server-close if
5541 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005542 */
Willy Tarreau60466522010-01-18 19:08:45 +01005543 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5544 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005545 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005546 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005547 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005548 }
5549
Willy Tarreau7959a552013-09-23 16:44:27 +02005550 /* we want to have the response time before we start processing it */
5551 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5552
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005553 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005554 /*
5555 * 3: we will have to evaluate the filters.
5556 * As opposed to version 1.2, now they will be evaluated in the
5557 * filters order and not in the header order. This means that
5558 * each filter has to be validated among all headers.
5559 *
5560 * Filters are tried with ->be first, then with ->fe if it is
5561 * different from ->be.
5562 */
5563
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005564 cur_proxy = t->be;
5565 while (1) {
5566 struct proxy *rule_set = cur_proxy;
5567
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005568 /* evaluate http-response rules */
5569 if (!http_res_last_rule)
5570 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5571
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005572 /* try headers filters */
5573 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005574 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005575 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005576 if (objt_server(t->target)) {
5577 objt_server(t->target)->counters.failed_resp++;
5578 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005579 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005580 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005581 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005582 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005583 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005584 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005585 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005586 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005587 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005588 if (!(t->flags & SN_ERR_MASK))
5589 t->flags |= SN_ERR_PRXCOND;
5590 if (!(t->flags & SN_FINST_MASK))
5591 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005592 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005593 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005594 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005595
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005596 /* has the response been denied ? */
5597 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005598 if (objt_server(t->target))
5599 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005600
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005601 t->be->be_counters.denied_resp++;
5602 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005603 if (t->listener->counters)
5604 t->listener->counters->denied_resp++;
5605
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005606 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005607 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005608
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005609 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005610 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005611 if (txn->status < 200)
5612 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005613 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005614 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005615 ret = acl_pass(ret);
5616 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5617 ret = !ret;
5618 if (!ret)
5619 continue;
5620 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005621 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005622 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005623 }
5624
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005625 /* check whether we're already working on the frontend */
5626 if (cur_proxy == t->fe)
5627 break;
5628 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005629 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005630
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005631 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005632 * We may be facing a 100-continue response, in which case this
5633 * is not the right response, and we're waiting for the next one.
5634 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005635 * next one.
5636 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005637 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005638 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005639 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005640 msg->msg_state = HTTP_MSG_RPBEFORE;
5641 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005642 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005643 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5644 return 1;
5645 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005646 else if (unlikely(txn->status < 200))
5647 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005648
5649 /* we don't have any 1xx status code now */
5650
5651 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005652 * 4: check for server cookie.
5653 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005654 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5655 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005656 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005657
Willy Tarreaubaaee002006-06-26 02:48:02 +02005658
Willy Tarreaua15645d2007-03-18 16:22:39 +01005659 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005660 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005661 */
Willy Tarreau67402132012-05-31 20:40:20 +02005662 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005663 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005664
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005665 /*
5666 * 6: add server cookie in the response if needed
5667 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005668 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005669 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005670 (!(t->flags & SN_DIRECT) ||
5671 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5672 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5673 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5674 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005675 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005676 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005677 /* the server is known, it's not the one the client requested, or the
5678 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005679 * insert a set-cookie here, except if we want to insert only on POST
5680 * requests and this one isn't. Note that servers which don't have cookies
5681 * (eg: some backup servers) will return a full cookie removal request.
5682 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005683 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005684 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005685 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5686 t->be->cookie_name);
5687 }
5688 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005689 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005690
5691 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5692 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005693 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5694 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5695 trash.len += 5;
5696
Willy Tarreauef4f3912010-10-07 21:00:29 +02005697 if (t->be->cookie_maxlife) {
5698 /* emit first_date, which is either the original one or
5699 * the current date.
5700 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005701 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005702 s30tob64(txn->cookie_first_date ?
5703 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005704 (date.tv_sec+3) >> 2, trash.str + trash.len);
5705 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005706 }
5707 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005708 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005709 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005710
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005711 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005712 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005713
Willy Tarreau4992dd22012-05-31 21:02:17 +02005714 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005715 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005716
5717 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005718 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005719
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005720 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005721 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005722
Willy Tarreauf1348312010-10-07 15:54:11 +02005723 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005724 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005725 /* the server did not change, only the date was updated */
5726 txn->flags |= TX_SCK_UPDATED;
5727 else
5728 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005729
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005730 /* Here, we will tell an eventual cache on the client side that we don't
5731 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5732 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5733 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5734 */
Willy Tarreau67402132012-05-31 20:40:20 +02005735 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005736
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005737 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5738
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005739 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005740 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005741 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005742 }
5743 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005744
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005745 /*
5746 * 7: check if result will be cacheable with a cookie.
5747 * We'll block the response if security checks have caught
5748 * nasty things such as a cacheable cookie.
5749 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005750 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5751 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005752 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005753
5754 /* we're in presence of a cacheable response containing
5755 * a set-cookie header. We'll block it as requested by
5756 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005757 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005758 if (objt_server(t->target))
5759 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005760
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005761 t->be->be_counters.denied_resp++;
5762 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005763 if (t->listener->counters)
5764 t->listener->counters->denied_resp++;
5765
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005766 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005767 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005768 send_log(t->be, LOG_ALERT,
5769 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005770 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005771 goto return_srv_prx_502;
5772 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005773
5774 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005775 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005776 * If an "Upgrade" token is found, the header is left untouched in order
5777 * not to have to deal with some client bugs : some of them fail an upgrade
5778 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005779 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005780 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5781 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5782 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005783 unsigned int want_flags = 0;
5784
5785 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5786 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5787 /* we want a keep-alive response here. Keep-alive header
5788 * required if either side is not 1.1.
5789 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005790 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005791 want_flags |= TX_CON_KAL_SET;
5792 }
5793 else {
5794 /* we want a close response here. Close header required if
5795 * the server is 1.1, regardless of the client.
5796 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005797 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005798 want_flags |= TX_CON_CLO_SET;
5799 }
5800
5801 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005802 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005803 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005804
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005805 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005806 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005807 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005808 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005809
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005810 /*************************************************************
5811 * OK, that's finished for the headers. We have done what we *
5812 * could. Let's switch to the DATA state. *
5813 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005814
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005815 /* if the user wants to log as soon as possible, without counting
5816 * bytes from the server, then this is the right moment. We have
5817 * to temporarily assign bytes_out to log what we currently have.
5818 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005819 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005820 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5821 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005822 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005823 t->logs.bytes_out = 0;
5824 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005825
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005826 /* Note: we must not try to cheat by jumping directly to DATA,
5827 * otherwise we would not let the client side wake up.
5828 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005829
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005830 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005831 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005832 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005833}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005834
Willy Tarreaud98cf932009-12-27 22:54:55 +01005835/* This function is an analyser which forwards response body (including chunk
5836 * sizes if any). It is called as soon as we must forward, even if we forward
5837 * zero byte. The only situation where it must not be called is when we're in
5838 * tunnel mode and we want to forward till the close. It's used both to forward
5839 * remaining data and to resync after end of body. It expects the msg_state to
5840 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5841 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005842 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005843 * bytes of pending data + the headers if not already done (between sol and sov).
5844 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005845 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005846int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005847{
5848 struct http_txn *txn = &s->txn;
5849 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005850 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005851 static struct buffer *tmpbuf = NULL;
5852 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005853 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005854 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005855
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005856 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5857 return 0;
5858
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005859 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005860 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005861 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005862 /* Output closed while we were sending data. We must abort and
5863 * wake the other side up.
5864 */
5865 msg->msg_state = HTTP_MSG_ERROR;
5866 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005867 return 1;
5868 }
5869
Willy Tarreau4fe41902010-06-07 22:27:41 +02005870 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005871 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005872
William Lallemand82fe75c2012-10-23 10:25:10 +02005873 /* this is the first time we need the compression buffer */
5874 if (s->comp_algo != NULL && tmpbuf == NULL) {
5875 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5876 goto aborted_xfer; /* no memory */
5877 }
5878
Willy Tarreaud98cf932009-12-27 22:54:55 +01005879 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005880 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005881 * rep->buf.p still points to the beginning of the message and msg->sol
5882 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005883 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005884 channel_forward(res, msg->sov);
5885 msg->next = 0;
5886 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005887
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005888 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005889 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005890 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005891 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005892 }
5893
William Lallemand82fe75c2012-10-23 10:25:10 +02005894 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005895 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005896 if (ret < 0)
5897 goto missing_data; /* not enough spaces in buffers */
5898 compressing = 1;
5899 }
5900
Willy Tarreaud98cf932009-12-27 22:54:55 +01005901 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005902 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005903 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005904 if (s->comp_algo == NULL) {
5905 bytes = msg->sov - msg->sol;
5906 if (msg->chunk_len || bytes) {
5907 msg->sol = msg->sov;
5908 msg->next -= bytes; /* will be forwarded */
5909 msg->chunk_len += bytes;
5910 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5911 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005912 }
5913
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005914 switch (msg->msg_state - HTTP_MSG_DATA) {
5915 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005916 if (compressing) {
5917 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5918 if (ret < 0)
5919 goto aborted_xfer;
5920 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005921
5922 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005923 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005924
5925 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005926 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005927 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005928 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005929 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005930 if (compressing && consumed_data) {
5931 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5932 compressing = 0;
5933 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005934 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005935 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005936 /* fall through for HTTP_MSG_CHUNK_CRLF */
5937
5938 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5939 /* we want the CRLF after the data */
5940
5941 ret = http_skip_chunk_crlf(msg);
5942 if (ret == 0)
5943 goto missing_data;
5944 else if (ret < 0) {
5945 if (msg->err_pos >= 0)
5946 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5947 goto return_bad_res;
5948 }
5949 /* skipping data in buffer for compression */
5950 if (compressing) {
5951 b_adv(res->buf, msg->next);
5952 msg->next = 0;
5953 msg->sov = 0;
5954 msg->sol = 0;
5955 }
5956 /* we're in MSG_CHUNK_SIZE now, fall through */
5957
5958 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005959 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005960 * set ->sov and ->next to point to the body and switch to DATA or
5961 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005962 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005963
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005964 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005965 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005966 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005967 else if (ret < 0) {
5968 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005969 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005970 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005971 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005972 if (compressing) {
5973 if (likely(msg->chunk_len > 0)) {
5974 /* skipping data if we are in compression mode */
5975 b_adv(res->buf, msg->next);
5976 msg->next = 0;
5977 msg->sov = 0;
5978 msg->sol = 0;
5979 } else {
5980 if (consumed_data) {
5981 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5982 compressing = 0;
5983 }
5984 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005985 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005986 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005987 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005988
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005989 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5990 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005991 if (ret == 0)
5992 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005993 else if (ret < 0) {
5994 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005995 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005996 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005997 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005998 if (s->comp_algo != NULL) {
5999 /* forwarding trailers */
6000 channel_forward(res, msg->next);
6001 msg->next = 0;
6002 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006003 /* we're in HTTP_MSG_DONE now, but we might still have
6004 * some data pending, so let's loop over once.
6005 */
6006 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006007
6008 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006009 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006010
6011 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006012 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006013 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6014 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006015 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006016 if (http_resync_states(s)) {
6017 http_silent_debug(__LINE__, s);
6018 /* some state changes occurred, maybe the analyser
6019 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006020 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006021 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006022 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006023 /* response errors are most likely due to
6024 * the client aborting the transfer.
6025 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006026 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006027 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006028 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006029 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006030 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006031 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006032 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006033 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006034 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006035 }
6036 }
6037
Willy Tarreaud98cf932009-12-27 22:54:55 +01006038 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006039 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006040 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6041 compressing = 0;
6042 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006043
6044 if (res->flags & CF_SHUTW)
6045 goto aborted_xfer;
6046
6047 /* stop waiting for data if the input is closed before the end. If the
6048 * client side was already closed, it means that the client has aborted,
6049 * so we don't want to count this as a server abort. Otherwise it's a
6050 * server abort.
6051 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006052 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006053 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6054 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006055 if (!(s->flags & SN_ERR_MASK))
6056 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006057 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006058 if (objt_server(s->target))
6059 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006060 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006061 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006062
Willy Tarreau40dba092010-03-04 18:14:51 +01006063 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006064 if (!s->req->analysers)
6065 goto return_bad_res;
6066
Willy Tarreauea953162012-05-18 23:41:28 +02006067 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006068 if (s->comp_algo == NULL) {
6069 bytes = msg->sov - msg->sol;
6070 if (msg->chunk_len || bytes) {
6071 msg->sol = msg->sov;
6072 msg->next -= bytes; /* will be forwarded */
6073 msg->chunk_len += bytes;
6074 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6075 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006076 }
6077
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006078 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006079 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006080 * Similarly, with keep-alive on the client side, we don't want to forward a
6081 * close.
6082 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006083 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006084 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6085 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006086 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006087
Willy Tarreau5c620922011-05-11 19:56:11 +02006088 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006089 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006090 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006091 * modes are already handled by the stream sock layer. We must not do
6092 * this in content-length mode because it could present the MSG_MORE
6093 * flag with the last block of forwarded data, which would cause an
6094 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006095 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006096 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006097 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006098
Willy Tarreaud98cf932009-12-27 22:54:55 +01006099 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006100 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006101 return 0;
6102
Willy Tarreau40dba092010-03-04 18:14:51 +01006103 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006104 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006105 if (objt_server(s->target))
6106 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006107
6108 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006109 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006110 /* don't send any error message as we're in the body */
6111 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006112 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006113 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006114 if (objt_server(s->target))
6115 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006116
6117 if (!(s->flags & SN_ERR_MASK))
6118 s->flags |= SN_ERR_PRXCOND;
6119 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006120 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006121 return 0;
6122
6123 aborted_xfer:
6124 txn->rsp.msg_state = HTTP_MSG_ERROR;
6125 /* don't send any error message as we're in the body */
6126 stream_int_retnclose(res->cons, NULL);
6127 res->analysers = 0;
6128 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6129
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006130 s->fe->fe_counters.cli_aborts++;
6131 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006132 if (objt_server(s->target))
6133 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006134
6135 if (!(s->flags & SN_ERR_MASK))
6136 s->flags |= SN_ERR_CLICL;
6137 if (!(s->flags & SN_FINST_MASK))
6138 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006139 return 0;
6140}
6141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006142/* Iterate the same filter through all request headers.
6143 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006144 * Since it can manage the switch to another backend, it updates the per-proxy
6145 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006146 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006147int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006148{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006149 char term;
6150 char *cur_ptr, *cur_end, *cur_next;
6151 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006152 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006153 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006154 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006155
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006156 last_hdr = 0;
6157
Willy Tarreau9b28e032012-10-12 23:49:43 +02006158 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006159 old_idx = 0;
6160
6161 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006162 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006163 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006164 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006165 (exp->action == ACT_ALLOW ||
6166 exp->action == ACT_DENY ||
6167 exp->action == ACT_TARPIT))
6168 return 0;
6169
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006170 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006171 if (!cur_idx)
6172 break;
6173
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006174 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006175 cur_ptr = cur_next;
6176 cur_end = cur_ptr + cur_hdr->len;
6177 cur_next = cur_end + cur_hdr->cr + 1;
6178
6179 /* Now we have one header between cur_ptr and cur_end,
6180 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006181 */
6182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006183 /* The annoying part is that pattern matching needs
6184 * that we modify the contents to null-terminate all
6185 * strings before testing them.
6186 */
6187
6188 term = *cur_end;
6189 *cur_end = '\0';
6190
6191 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6192 switch (exp->action) {
6193 case ACT_SETBE:
6194 /* It is not possible to jump a second time.
6195 * FIXME: should we return an HTTP/500 here so that
6196 * the admin knows there's a problem ?
6197 */
6198 if (t->be != t->fe)
6199 break;
6200
6201 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006202 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006203 last_hdr = 1;
6204 break;
6205
6206 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006207 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006208 last_hdr = 1;
6209 break;
6210
6211 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006212 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006213 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006214 break;
6215
6216 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006217 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006218 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006219 break;
6220
6221 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006222 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6223 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006224 /* FIXME: if the user adds a newline in the replacement, the
6225 * index will not be recalculated for now, and the new line
6226 * will not be counted as a new header.
6227 */
6228
6229 cur_end += delta;
6230 cur_next += delta;
6231 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006232 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006233 break;
6234
6235 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006236 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006237 cur_next += delta;
6238
Willy Tarreaufa355d42009-11-29 18:12:29 +01006239 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006240 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6241 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006242 cur_hdr->len = 0;
6243 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006244 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006245 break;
6246
6247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006248 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006249 if (cur_end)
6250 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006251
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006252 /* keep the link from this header to next one in case of later
6253 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006255 old_idx = cur_idx;
6256 }
6257 return 0;
6258}
6259
6260
6261/* Apply the filter to the request line.
6262 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6263 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006264 * Since it can manage the switch to another backend, it updates the per-proxy
6265 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006266 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006267int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006268{
6269 char term;
6270 char *cur_ptr, *cur_end;
6271 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006272 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006273 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274
Willy Tarreau3d300592007-03-18 18:34:41 +01006275 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006276 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006277 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006278 (exp->action == ACT_ALLOW ||
6279 exp->action == ACT_DENY ||
6280 exp->action == ACT_TARPIT))
6281 return 0;
6282 else if (exp->action == ACT_REMOVE)
6283 return 0;
6284
6285 done = 0;
6286
Willy Tarreau9b28e032012-10-12 23:49:43 +02006287 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006288 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006289
6290 /* Now we have the request line between cur_ptr and cur_end */
6291
6292 /* The annoying part is that pattern matching needs
6293 * that we modify the contents to null-terminate all
6294 * strings before testing them.
6295 */
6296
6297 term = *cur_end;
6298 *cur_end = '\0';
6299
6300 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6301 switch (exp->action) {
6302 case ACT_SETBE:
6303 /* It is not possible to jump a second time.
6304 * FIXME: should we return an HTTP/500 here so that
6305 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006306 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006307 if (t->be != t->fe)
6308 break;
6309
6310 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006311 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006312 done = 1;
6313 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006315 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006316 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 done = 1;
6318 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006320 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006321 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006322 done = 1;
6323 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006325 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006326 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006327 done = 1;
6328 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006330 case ACT_REPLACE:
6331 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006332 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6333 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006334 /* FIXME: if the user adds a newline in the replacement, the
6335 * index will not be recalculated for now, and the new line
6336 * will not be counted as a new header.
6337 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006338
Willy Tarreaufa355d42009-11-29 18:12:29 +01006339 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006340 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006341 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006342 HTTP_MSG_RQMETH,
6343 cur_ptr, cur_end + 1,
6344 NULL, NULL);
6345 if (unlikely(!cur_end))
6346 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 /* we have a full request and we know that we have either a CR
6349 * or an LF at <ptr>.
6350 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006351 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6352 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353 /* there is no point trying this regex on headers */
6354 return 1;
6355 }
6356 }
6357 *cur_end = term; /* restore the string terminator */
6358 return done;
6359}
Willy Tarreau97de6242006-12-27 17:18:38 +01006360
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361
Willy Tarreau58f10d72006-12-04 02:26:12 +01006362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006363/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006364 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006365 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366 * unparsable request. Since it can manage the switch to another backend, it
6367 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006368 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006369int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006371 struct http_txn *txn = &s->txn;
6372 struct hdr_exp *exp;
6373
6374 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006375 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006377 /*
6378 * The interleaving of transformations and verdicts
6379 * makes it difficult to decide to continue or stop
6380 * the evaluation.
6381 */
6382
Willy Tarreau6c123b12010-01-28 20:22:06 +01006383 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6384 break;
6385
Willy Tarreau3d300592007-03-18 18:34:41 +01006386 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006387 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006388 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006390
6391 /* if this filter had a condition, evaluate it now and skip to
6392 * next filter if the condition does not match.
6393 */
6394 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006395 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006396 ret = acl_pass(ret);
6397 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6398 ret = !ret;
6399
6400 if (!ret)
6401 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 }
6403
6404 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006405 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006406 if (unlikely(ret < 0))
6407 return -1;
6408
6409 if (likely(ret == 0)) {
6410 /* The filter did not match the request, it can be
6411 * iterated through all headers.
6412 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006413 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006414 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006415 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006416 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006417}
6418
6419
Willy Tarreaua15645d2007-03-18 16:22:39 +01006420
Willy Tarreau58f10d72006-12-04 02:26:12 +01006421/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006422 * Try to retrieve the server associated to the appsession.
6423 * If the server is found, it's assigned to the session.
6424 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006425void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006426 struct http_txn *txn = &t->txn;
6427 appsess *asession = NULL;
6428 char *sessid_temp = NULL;
6429
Cyril Bontéb21570a2009-11-29 20:04:48 +01006430 if (len > t->be->appsession_len) {
6431 len = t->be->appsession_len;
6432 }
6433
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006434 if (t->be->options2 & PR_O2_AS_REQL) {
6435 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006436 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006437 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006438 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006439 }
6440
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006441 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006442 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6443 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6444 return;
6445 }
6446
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006447 memcpy(txn->sessid, buf, len);
6448 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006449 }
6450
6451 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6452 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6453 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6454 return;
6455 }
6456
Cyril Bontéb21570a2009-11-29 20:04:48 +01006457 memcpy(sessid_temp, buf, len);
6458 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006459
6460 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6461 /* free previously allocated memory */
6462 pool_free2(apools.sessid, sessid_temp);
6463
6464 if (asession != NULL) {
6465 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6466 if (!(t->be->options2 & PR_O2_AS_REQL))
6467 asession->request_count++;
6468
6469 if (asession->serverid != NULL) {
6470 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006471
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006472 while (srv) {
6473 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006474 if ((srv->state & SRV_RUNNING) ||
6475 (t->be->options & PR_O_PERSIST) ||
6476 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006477 /* we found the server and it's usable */
6478 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006479 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006480 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006481 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006482
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006483 break;
6484 } else {
6485 txn->flags &= ~TX_CK_MASK;
6486 txn->flags |= TX_CK_DOWN;
6487 }
6488 }
6489 srv = srv->next;
6490 }
6491 }
6492 }
6493}
6494
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006495/* Find the end of a cookie value contained between <s> and <e>. It works the
6496 * same way as with headers above except that the semi-colon also ends a token.
6497 * See RFC2965 for more information. Note that it requires a valid header to
6498 * return a valid result.
6499 */
6500char *find_cookie_value_end(char *s, const char *e)
6501{
6502 int quoted, qdpair;
6503
6504 quoted = qdpair = 0;
6505 for (; s < e; s++) {
6506 if (qdpair) qdpair = 0;
6507 else if (quoted) {
6508 if (*s == '\\') qdpair = 1;
6509 else if (*s == '"') quoted = 0;
6510 }
6511 else if (*s == '"') quoted = 1;
6512 else if (*s == ',' || *s == ';') return s;
6513 }
6514 return s;
6515}
6516
6517/* Delete a value in a header between delimiters <from> and <next> in buffer
6518 * <buf>. The number of characters displaced is returned, and the pointer to
6519 * the first delimiter is updated if required. The function tries as much as
6520 * possible to respect the following principles :
6521 * - replace <from> delimiter by the <next> one unless <from> points to a
6522 * colon, in which case <next> is simply removed
6523 * - set exactly one space character after the new first delimiter, unless
6524 * there are not enough characters in the block being moved to do so.
6525 * - remove unneeded spaces before the previous delimiter and after the new
6526 * one.
6527 *
6528 * It is the caller's responsibility to ensure that :
6529 * - <from> points to a valid delimiter or the colon ;
6530 * - <next> points to a valid delimiter or the final CR/LF ;
6531 * - there are non-space chars before <from> ;
6532 * - there is a CR/LF at or after <next>.
6533 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006534int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535{
6536 char *prev = *from;
6537
6538 if (*prev == ':') {
6539 /* We're removing the first value, preserve the colon and add a
6540 * space if possible.
6541 */
6542 if (!http_is_crlf[(unsigned char)*next])
6543 next++;
6544 prev++;
6545 if (prev < next)
6546 *prev++ = ' ';
6547
6548 while (http_is_spht[(unsigned char)*next])
6549 next++;
6550 } else {
6551 /* Remove useless spaces before the old delimiter. */
6552 while (http_is_spht[(unsigned char)*(prev-1)])
6553 prev--;
6554 *from = prev;
6555
6556 /* copy the delimiter and if possible a space if we're
6557 * not at the end of the line.
6558 */
6559 if (!http_is_crlf[(unsigned char)*next]) {
6560 *prev++ = *next++;
6561 if (prev + 1 < next)
6562 *prev++ = ' ';
6563 while (http_is_spht[(unsigned char)*next])
6564 next++;
6565 }
6566 }
6567 return buffer_replace2(buf, prev, next, NULL, 0);
6568}
6569
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006570/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006571 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006572 * desirable to call it only when needed. This code is quite complex because
6573 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6574 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006576void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006577{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006578 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006581 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6582 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006583
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006584 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006585 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006586 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006587
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006588 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006589 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006590 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006591
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006592 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006593 hdr_beg = hdr_next;
6594 hdr_end = hdr_beg + cur_hdr->len;
6595 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006596
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 /* We have one full header between hdr_beg and hdr_end, and the
6598 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006599 * "Cookie:" headers.
6600 */
6601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006603 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006604 old_idx = cur_idx;
6605 continue;
6606 }
6607
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006608 del_from = NULL; /* nothing to be deleted */
6609 preserve_hdr = 0; /* assume we may kill the whole header */
6610
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611 /* Now look for cookies. Conforming to RFC2109, we have to support
6612 * attributes whose name begin with a '$', and associate them with
6613 * the right cookie, if we want to delete this cookie.
6614 * So there are 3 cases for each cookie read :
6615 * 1) it's a special attribute, beginning with a '$' : ignore it.
6616 * 2) it's a server id cookie that we *MAY* want to delete : save
6617 * some pointers on it (last semi-colon, beginning of cookie...)
6618 * 3) it's an application cookie : we *MAY* have to delete a previous
6619 * "special" cookie.
6620 * At the end of loop, if a "special" cookie remains, we may have to
6621 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006622 * *MUST* delete it.
6623 *
6624 * Note: RFC2965 is unclear about the processing of spaces around
6625 * the equal sign in the ATTR=VALUE form. A careful inspection of
6626 * the RFC explicitly allows spaces before it, and not within the
6627 * tokens (attrs or values). An inspection of RFC2109 allows that
6628 * too but section 10.1.3 lets one think that spaces may be allowed
6629 * after the equal sign too, resulting in some (rare) buggy
6630 * implementations trying to do that. So let's do what servers do.
6631 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6632 * allowed quoted strings in values, with any possible character
6633 * after a backslash, including control chars and delimitors, which
6634 * causes parsing to become ambiguous. Browsers also allow spaces
6635 * within values even without quotes.
6636 *
6637 * We have to keep multiple pointers in order to support cookie
6638 * removal at the beginning, middle or end of header without
6639 * corrupting the header. All of these headers are valid :
6640 *
6641 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6642 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6643 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6644 * | | | | | | | | |
6645 * | | | | | | | | hdr_end <--+
6646 * | | | | | | | +--> next
6647 * | | | | | | +----> val_end
6648 * | | | | | +-----------> val_beg
6649 * | | | | +--------------> equal
6650 * | | | +----------------> att_end
6651 * | | +---------------------> att_beg
6652 * | +--------------------------> prev
6653 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654 */
6655
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006656 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6657 /* Iterate through all cookies on this line */
6658
6659 /* find att_beg */
6660 att_beg = prev + 1;
6661 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6662 att_beg++;
6663
6664 /* find att_end : this is the first character after the last non
6665 * space before the equal. It may be equal to hdr_end.
6666 */
6667 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006668
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006669 while (equal < hdr_end) {
6670 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006671 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006672 if (http_is_spht[(unsigned char)*equal++])
6673 continue;
6674 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006675 }
6676
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006677 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6678 * is between <att_beg> and <equal>, both may be identical.
6679 */
6680
6681 /* look for end of cookie if there is an equal sign */
6682 if (equal < hdr_end && *equal == '=') {
6683 /* look for the beginning of the value */
6684 val_beg = equal + 1;
6685 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6686 val_beg++;
6687
6688 /* find the end of the value, respecting quotes */
6689 next = find_cookie_value_end(val_beg, hdr_end);
6690
6691 /* make val_end point to the first white space or delimitor after the value */
6692 val_end = next;
6693 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6694 val_end--;
6695 } else {
6696 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006699 /* We have nothing to do with attributes beginning with '$'. However,
6700 * they will automatically be removed if a header before them is removed,
6701 * since they're supposed to be linked together.
6702 */
6703 if (*att_beg == '$')
6704 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006705
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006706 /* Ignore cookies with no equal sign */
6707 if (equal == next) {
6708 /* This is not our cookie, so we must preserve it. But if we already
6709 * scheduled another cookie for removal, we cannot remove the
6710 * complete header, but we can remove the previous block itself.
6711 */
6712 preserve_hdr = 1;
6713 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006714 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 val_end += delta;
6716 next += delta;
6717 hdr_end += delta;
6718 hdr_next += delta;
6719 cur_hdr->len += delta;
6720 http_msg_move_end(&txn->req, delta);
6721 prev = del_from;
6722 del_from = NULL;
6723 }
6724 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006726
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006727 /* if there are spaces around the equal sign, we need to
6728 * strip them otherwise we'll get trouble for cookie captures,
6729 * or even for rewrites. Since this happens extremely rarely,
6730 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006731 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6733 int stripped_before = 0;
6734 int stripped_after = 0;
6735
6736 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006737 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006738 equal += stripped_before;
6739 val_beg += stripped_before;
6740 }
6741
6742 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006743 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006744 val_beg += stripped_after;
6745 stripped_before += stripped_after;
6746 }
6747
6748 val_end += stripped_before;
6749 next += stripped_before;
6750 hdr_end += stripped_before;
6751 hdr_next += stripped_before;
6752 cur_hdr->len += stripped_before;
6753 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757 /* First, let's see if we want to capture this cookie. We check
6758 * that we don't already have a client side cookie, because we
6759 * can only capture one. Also as an optimisation, we ignore
6760 * cookies shorter than the declared name.
6761 */
6762 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6763 (val_end - att_beg >= t->fe->capture_namelen) &&
6764 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6765 int log_len = val_end - att_beg;
6766
6767 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6768 Alert("HTTP logging : out of memory.\n");
6769 } else {
6770 if (log_len > t->fe->capture_len)
6771 log_len = t->fe->capture_len;
6772 memcpy(txn->cli_cookie, att_beg, log_len);
6773 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006774 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006775 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006776
Willy Tarreaubca99692010-10-06 19:25:55 +02006777 /* Persistence cookies in passive, rewrite or insert mode have the
6778 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006779 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006780 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006781 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006782 * For cookies in prefix mode, the form is :
6783 *
6784 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006785 */
6786 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6787 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6788 struct server *srv = t->be->srv;
6789 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006790
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6792 * have the server ID between val_beg and delim, and the original cookie between
6793 * delim+1 and val_end. Otherwise, delim==val_end :
6794 *
6795 * Cookie: NAME=SRV; # in all but prefix modes
6796 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6797 * | || || | |+-> next
6798 * | || || | +--> val_end
6799 * | || || +---------> delim
6800 * | || |+------------> val_beg
6801 * | || +-------------> att_end = equal
6802 * | |+-----------------> att_beg
6803 * | +------------------> prev
6804 * +-------------------------> hdr_beg
6805 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006806
Willy Tarreau67402132012-05-31 20:40:20 +02006807 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006808 for (delim = val_beg; delim < val_end; delim++)
6809 if (*delim == COOKIE_DELIM)
6810 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006811 } else {
6812 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006813 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006814 /* Now check if the cookie contains a date field, which would
6815 * appear after a vertical bar ('|') just after the server name
6816 * and before the delimiter.
6817 */
6818 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6819 if (vbar1) {
6820 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006821 * right is the last seen date. It is a base64 encoded
6822 * 30-bit value representing the UNIX date since the
6823 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006824 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006825 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006826 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006827 if (val_end - vbar1 >= 5) {
6828 val = b64tos30(vbar1);
6829 if (val > 0)
6830 txn->cookie_last_date = val << 2;
6831 }
6832 /* look for a second vertical bar */
6833 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6834 if (vbar1 && (val_end - vbar1 > 5)) {
6835 val = b64tos30(vbar1 + 1);
6836 if (val > 0)
6837 txn->cookie_first_date = val << 2;
6838 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006839 }
6840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006841
Willy Tarreauf64d1412010-10-07 20:06:11 +02006842 /* if the cookie has an expiration date and the proxy wants to check
6843 * it, then we do that now. We first check if the cookie is too old,
6844 * then only if it has expired. We detect strict overflow because the
6845 * time resolution here is not great (4 seconds). Cookies with dates
6846 * in the future are ignored if their offset is beyond one day. This
6847 * allows an admin to fix timezone issues without expiring everyone
6848 * and at the same time avoids keeping unwanted side effects for too
6849 * long.
6850 */
6851 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006852 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6853 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006854 txn->flags &= ~TX_CK_MASK;
6855 txn->flags |= TX_CK_OLD;
6856 delim = val_beg; // let's pretend we have not found the cookie
6857 txn->cookie_first_date = 0;
6858 txn->cookie_last_date = 0;
6859 }
6860 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006861 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6862 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006863 txn->flags &= ~TX_CK_MASK;
6864 txn->flags |= TX_CK_EXPIRED;
6865 delim = val_beg; // let's pretend we have not found the cookie
6866 txn->cookie_first_date = 0;
6867 txn->cookie_last_date = 0;
6868 }
6869
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 /* Here, we'll look for the first running server which supports the cookie.
6871 * This allows to share a same cookie between several servers, for example
6872 * to dedicate backup servers to specific servers only.
6873 * However, to prevent clients from sticking to cookie-less backup server
6874 * when they have incidentely learned an empty cookie, we simply ignore
6875 * empty cookies and mark them as invalid.
6876 * The same behaviour is applied when persistence must be ignored.
6877 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006878 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006879 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006881 while (srv) {
6882 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6883 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6884 if ((srv->state & SRV_RUNNING) ||
6885 (t->be->options & PR_O_PERSIST) ||
6886 (t->flags & SN_FORCE_PRST)) {
6887 /* we found the server and we can use it */
6888 txn->flags &= ~TX_CK_MASK;
6889 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6890 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006891 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006892 break;
6893 } else {
6894 /* we found a server, but it's down,
6895 * mark it as such and go on in case
6896 * another one is available.
6897 */
6898 txn->flags &= ~TX_CK_MASK;
6899 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006901 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006902 srv = srv->next;
6903 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006904
Willy Tarreauf64d1412010-10-07 20:06:11 +02006905 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006906 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006908 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6909 txn->flags |= TX_CK_UNUSED;
6910 else
6911 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006912 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006913
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 /* depending on the cookie mode, we may have to either :
6915 * - delete the complete cookie if we're in insert+indirect mode, so that
6916 * the server never sees it ;
6917 * - remove the server id from the cookie value, and tag the cookie as an
6918 * application cookie so that it does not get accidentely removed later,
6919 * if we're in cookie prefix mode
6920 */
Willy Tarreau67402132012-05-31 20:40:20 +02006921 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006922 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923
Willy Tarreau9b28e032012-10-12 23:49:43 +02006924 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006925 val_end += delta;
6926 next += delta;
6927 hdr_end += delta;
6928 hdr_next += delta;
6929 cur_hdr->len += delta;
6930 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006931
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006932 del_from = NULL;
6933 preserve_hdr = 1; /* we want to keep this cookie */
6934 }
6935 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006936 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 del_from = prev;
6938 }
6939 } else {
6940 /* This is not our cookie, so we must preserve it. But if we already
6941 * scheduled another cookie for removal, we cannot remove the
6942 * complete header, but we can remove the previous block itself.
6943 */
6944 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006947 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006948 if (att_beg >= del_from)
6949 att_beg += delta;
6950 if (att_end >= del_from)
6951 att_end += delta;
6952 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 val_end += delta;
6954 next += delta;
6955 hdr_end += delta;
6956 hdr_next += delta;
6957 cur_hdr->len += delta;
6958 http_msg_move_end(&txn->req, delta);
6959 prev = del_from;
6960 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006963
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006964 /* Look for the appsession cookie unless persistence must be ignored */
6965 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6966 int cmp_len, value_len;
6967 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 if (t->be->options2 & PR_O2_AS_PFX) {
6970 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6971 value_begin = att_beg + t->be->appsession_name_len;
6972 value_len = val_end - att_beg - t->be->appsession_name_len;
6973 } else {
6974 cmp_len = att_end - att_beg;
6975 value_begin = val_beg;
6976 value_len = val_end - val_beg;
6977 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006978
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 /* let's see if the cookie is our appcookie */
6980 if (cmp_len == t->be->appsession_name_len &&
6981 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6982 manage_client_side_appsession(t, value_begin, value_len);
6983 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006984 }
6985
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006986 /* continue with next cookie on this header line */
6987 att_beg = next;
6988 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006990 /* There are no more cookies on this line.
6991 * We may still have one (or several) marked for deletion at the
6992 * end of the line. We must do this now in two ways :
6993 * - if some cookies must be preserved, we only delete from the
6994 * mark to the end of line ;
6995 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006997 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006998 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007000 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007001 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007002 cur_hdr->len += delta;
7003 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007004 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007005
7006 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007007 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7008 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007009 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007010 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007011 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007012 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007013 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007014 }
7015
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007016 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007017 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007018 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019}
7020
7021
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022/* Iterate the same filter through all response headers contained in <rtr>.
7023 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7024 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007025int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026{
7027 char term;
7028 char *cur_ptr, *cur_end, *cur_next;
7029 int cur_idx, old_idx, last_hdr;
7030 struct http_txn *txn = &t->txn;
7031 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007032 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007033
7034 last_hdr = 0;
7035
Willy Tarreau9b28e032012-10-12 23:49:43 +02007036 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037 old_idx = 0;
7038
7039 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007040 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007041 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007042 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007043 (exp->action == ACT_ALLOW ||
7044 exp->action == ACT_DENY))
7045 return 0;
7046
7047 cur_idx = txn->hdr_idx.v[old_idx].next;
7048 if (!cur_idx)
7049 break;
7050
7051 cur_hdr = &txn->hdr_idx.v[cur_idx];
7052 cur_ptr = cur_next;
7053 cur_end = cur_ptr + cur_hdr->len;
7054 cur_next = cur_end + cur_hdr->cr + 1;
7055
7056 /* Now we have one header between cur_ptr and cur_end,
7057 * and the next header starts at cur_next.
7058 */
7059
7060 /* The annoying part is that pattern matching needs
7061 * that we modify the contents to null-terminate all
7062 * strings before testing them.
7063 */
7064
7065 term = *cur_end;
7066 *cur_end = '\0';
7067
7068 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7069 switch (exp->action) {
7070 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007071 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007072 last_hdr = 1;
7073 break;
7074
7075 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007076 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007077 last_hdr = 1;
7078 break;
7079
7080 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007081 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7082 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083 /* FIXME: if the user adds a newline in the replacement, the
7084 * index will not be recalculated for now, and the new line
7085 * will not be counted as a new header.
7086 */
7087
7088 cur_end += delta;
7089 cur_next += delta;
7090 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007091 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092 break;
7093
7094 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007095 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007096 cur_next += delta;
7097
Willy Tarreaufa355d42009-11-29 18:12:29 +01007098 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007099 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7100 txn->hdr_idx.used--;
7101 cur_hdr->len = 0;
7102 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007103 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007104 break;
7105
7106 }
7107 }
7108 if (cur_end)
7109 *cur_end = term; /* restore the string terminator */
7110
7111 /* keep the link from this header to next one in case of later
7112 * removal of next header.
7113 */
7114 old_idx = cur_idx;
7115 }
7116 return 0;
7117}
7118
7119
7120/* Apply the filter to the status line in the response buffer <rtr>.
7121 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7122 * or -1 if a replacement resulted in an invalid status line.
7123 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007124int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125{
7126 char term;
7127 char *cur_ptr, *cur_end;
7128 int done;
7129 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007130 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131
7132
Willy Tarreau3d300592007-03-18 18:34:41 +01007133 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007135 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 (exp->action == ACT_ALLOW ||
7137 exp->action == ACT_DENY))
7138 return 0;
7139 else if (exp->action == ACT_REMOVE)
7140 return 0;
7141
7142 done = 0;
7143
Willy Tarreau9b28e032012-10-12 23:49:43 +02007144 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007145 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146
7147 /* Now we have the status line between cur_ptr and cur_end */
7148
7149 /* The annoying part is that pattern matching needs
7150 * that we modify the contents to null-terminate all
7151 * strings before testing them.
7152 */
7153
7154 term = *cur_end;
7155 *cur_end = '\0';
7156
7157 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7158 switch (exp->action) {
7159 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007160 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161 done = 1;
7162 break;
7163
7164 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007165 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166 done = 1;
7167 break;
7168
7169 case ACT_REPLACE:
7170 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007171 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7172 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173 /* FIXME: if the user adds a newline in the replacement, the
7174 * index will not be recalculated for now, and the new line
7175 * will not be counted as a new header.
7176 */
7177
Willy Tarreaufa355d42009-11-29 18:12:29 +01007178 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007180 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007181 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182 cur_ptr, cur_end + 1,
7183 NULL, NULL);
7184 if (unlikely(!cur_end))
7185 return -1;
7186
7187 /* we have a full respnse and we know that we have either a CR
7188 * or an LF at <ptr>.
7189 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007190 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007191 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192 /* there is no point trying this regex on headers */
7193 return 1;
7194 }
7195 }
7196 *cur_end = term; /* restore the string terminator */
7197 return done;
7198}
7199
7200
7201
7202/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007203 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7205 * unparsable response.
7206 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007207int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007208{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007209 struct http_txn *txn = &s->txn;
7210 struct hdr_exp *exp;
7211
7212 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213 int ret;
7214
7215 /*
7216 * The interleaving of transformations and verdicts
7217 * makes it difficult to decide to continue or stop
7218 * the evaluation.
7219 */
7220
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007221 if (txn->flags & TX_SVDENY)
7222 break;
7223
Willy Tarreau3d300592007-03-18 18:34:41 +01007224 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7226 exp->action == ACT_PASS)) {
7227 exp = exp->next;
7228 continue;
7229 }
7230
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007231 /* if this filter had a condition, evaluate it now and skip to
7232 * next filter if the condition does not match.
7233 */
7234 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007235 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007236 ret = acl_pass(ret);
7237 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7238 ret = !ret;
7239 if (!ret)
7240 continue;
7241 }
7242
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007244 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 if (unlikely(ret < 0))
7246 return -1;
7247
7248 if (likely(ret == 0)) {
7249 /* The filter did not match the response, it can be
7250 * iterated through all headers.
7251 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007252 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007253 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 }
7255 return 0;
7256}
7257
7258
Willy Tarreaua15645d2007-03-18 16:22:39 +01007259/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007260 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007261 * desirable to call it only when needed. This function is also used when we
7262 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007264void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265{
7266 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007267 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007268 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007270 char *hdr_beg, *hdr_end, *hdr_next;
7271 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 /* Iterate through the headers.
7274 * we start with the start line.
7275 */
7276 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007277 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007278
7279 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7280 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007281 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007282
7283 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007284 hdr_beg = hdr_next;
7285 hdr_end = hdr_beg + cur_hdr->len;
7286 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287
Willy Tarreau24581ba2010-08-31 22:39:35 +02007288 /* We have one full header between hdr_beg and hdr_end, and the
7289 * next header starts at hdr_next. We're only interested in
7290 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 */
7292
Willy Tarreau24581ba2010-08-31 22:39:35 +02007293 is_cookie2 = 0;
7294 prev = hdr_beg + 10;
7295 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007296 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007297 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7298 if (!val) {
7299 old_idx = cur_idx;
7300 continue;
7301 }
7302 is_cookie2 = 1;
7303 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304 }
7305
Willy Tarreau24581ba2010-08-31 22:39:35 +02007306 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7307 * <prev> points to the colon.
7308 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007309 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310
Willy Tarreau24581ba2010-08-31 22:39:35 +02007311 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7312 * check-cache is enabled) and we are not interested in checking
7313 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007314 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007315 if (t->be->cookie_name == NULL &&
7316 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007317 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318 return;
7319
Willy Tarreau24581ba2010-08-31 22:39:35 +02007320 /* OK so now we know we have to process this response cookie.
7321 * The format of the Set-Cookie header is slightly different
7322 * from the format of the Cookie header in that it does not
7323 * support the comma as a cookie delimiter (thus the header
7324 * cannot be folded) because the Expires attribute described in
7325 * the original Netscape's spec may contain an unquoted date
7326 * with a comma inside. We have to live with this because
7327 * many browsers don't support Max-Age and some browsers don't
7328 * support quoted strings. However the Set-Cookie2 header is
7329 * clean.
7330 *
7331 * We have to keep multiple pointers in order to support cookie
7332 * removal at the beginning, middle or end of header without
7333 * corrupting the header (in case of set-cookie2). A special
7334 * pointer, <scav> points to the beginning of the set-cookie-av
7335 * fields after the first semi-colon. The <next> pointer points
7336 * either to the end of line (set-cookie) or next unquoted comma
7337 * (set-cookie2). All of these headers are valid :
7338 *
7339 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7340 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7341 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7342 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7343 * | | | | | | | | | |
7344 * | | | | | | | | +-> next hdr_end <--+
7345 * | | | | | | | +------------> scav
7346 * | | | | | | +--------------> val_end
7347 * | | | | | +--------------------> val_beg
7348 * | | | | +----------------------> equal
7349 * | | | +------------------------> att_end
7350 * | | +----------------------------> att_beg
7351 * | +------------------------------> prev
7352 * +-----------------------------------------> hdr_beg
7353 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354
Willy Tarreau24581ba2010-08-31 22:39:35 +02007355 for (; prev < hdr_end; prev = next) {
7356 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007357
Willy Tarreau24581ba2010-08-31 22:39:35 +02007358 /* find att_beg */
7359 att_beg = prev + 1;
7360 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7361 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362
Willy Tarreau24581ba2010-08-31 22:39:35 +02007363 /* find att_end : this is the first character after the last non
7364 * space before the equal. It may be equal to hdr_end.
7365 */
7366 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367
Willy Tarreau24581ba2010-08-31 22:39:35 +02007368 while (equal < hdr_end) {
7369 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7370 break;
7371 if (http_is_spht[(unsigned char)*equal++])
7372 continue;
7373 att_end = equal;
7374 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375
Willy Tarreau24581ba2010-08-31 22:39:35 +02007376 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7377 * is between <att_beg> and <equal>, both may be identical.
7378 */
7379
7380 /* look for end of cookie if there is an equal sign */
7381 if (equal < hdr_end && *equal == '=') {
7382 /* look for the beginning of the value */
7383 val_beg = equal + 1;
7384 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7385 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007386
Willy Tarreau24581ba2010-08-31 22:39:35 +02007387 /* find the end of the value, respecting quotes */
7388 next = find_cookie_value_end(val_beg, hdr_end);
7389
7390 /* make val_end point to the first white space or delimitor after the value */
7391 val_end = next;
7392 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7393 val_end--;
7394 } else {
7395 /* <equal> points to next comma, semi-colon or EOL */
7396 val_beg = val_end = next = equal;
7397 }
7398
7399 if (next < hdr_end) {
7400 /* Set-Cookie2 supports multiple cookies, and <next> points to
7401 * a colon or semi-colon before the end. So skip all attr-value
7402 * pairs and look for the next comma. For Set-Cookie, since
7403 * commas are permitted in values, skip to the end.
7404 */
7405 if (is_cookie2)
7406 next = find_hdr_value_end(next, hdr_end);
7407 else
7408 next = hdr_end;
7409 }
7410
7411 /* Now everything is as on the diagram above */
7412
7413 /* Ignore cookies with no equal sign */
7414 if (equal == val_end)
7415 continue;
7416
7417 /* If there are spaces around the equal sign, we need to
7418 * strip them otherwise we'll get trouble for cookie captures,
7419 * or even for rewrites. Since this happens extremely rarely,
7420 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007422 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7423 int stripped_before = 0;
7424 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007425
Willy Tarreau24581ba2010-08-31 22:39:35 +02007426 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007427 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007428 equal += stripped_before;
7429 val_beg += stripped_before;
7430 }
7431
7432 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007433 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007434 val_beg += stripped_after;
7435 stripped_before += stripped_after;
7436 }
7437
7438 val_end += stripped_before;
7439 next += stripped_before;
7440 hdr_end += stripped_before;
7441 hdr_next += stripped_before;
7442 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007443 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007444 }
7445
7446 /* First, let's see if we want to capture this cookie. We check
7447 * that we don't already have a server side cookie, because we
7448 * can only capture one. Also as an optimisation, we ignore
7449 * cookies shorter than the declared name.
7450 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007451 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007452 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007453 (val_end - att_beg >= t->fe->capture_namelen) &&
7454 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7455 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007456 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007457 Alert("HTTP logging : out of memory.\n");
7458 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007459 else {
7460 if (log_len > t->fe->capture_len)
7461 log_len = t->fe->capture_len;
7462 memcpy(txn->srv_cookie, att_beg, log_len);
7463 txn->srv_cookie[log_len] = 0;
7464 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465 }
7466
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007467 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007468 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007469 if (!(t->flags & SN_IGNORE_PRST) &&
7470 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7471 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007472 /* assume passive cookie by default */
7473 txn->flags &= ~TX_SCK_MASK;
7474 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475
7476 /* If the cookie is in insert mode on a known server, we'll delete
7477 * this occurrence because we'll insert another one later.
7478 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007479 * a direct access.
7480 */
Willy Tarreau67402132012-05-31 20:40:20 +02007481 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007482 /* The "preserve" flag was set, we don't want to touch the
7483 * server's cookie.
7484 */
7485 }
Willy Tarreau67402132012-05-31 20:40:20 +02007486 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7487 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007488 /* this cookie must be deleted */
7489 if (*prev == ':' && next == hdr_end) {
7490 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007491 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7493 txn->hdr_idx.used--;
7494 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007495 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 hdr_next += delta;
7497 http_msg_move_end(&txn->rsp, delta);
7498 /* note: while both invalid now, <next> and <hdr_end>
7499 * are still equal, so the for() will stop as expected.
7500 */
7501 } else {
7502 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007503 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007504 next = prev;
7505 hdr_end += delta;
7506 hdr_next += delta;
7507 cur_hdr->len += delta;
7508 http_msg_move_end(&txn->rsp, delta);
7509 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007510 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007511 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007512 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007513 }
Willy Tarreau67402132012-05-31 20:40:20 +02007514 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007516 * with this server since we know it.
7517 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007518 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007519 next += delta;
7520 hdr_end += delta;
7521 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007522 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007523 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007524
Willy Tarreauf1348312010-10-07 15:54:11 +02007525 txn->flags &= ~TX_SCK_MASK;
7526 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007527 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007528 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007529 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007530 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007531 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007532 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007533 next += delta;
7534 hdr_end += delta;
7535 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007536 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007537 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007538
Willy Tarreau827aee92011-03-10 16:55:02 +01007539 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007540 txn->flags &= ~TX_SCK_MASK;
7541 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007542 }
7543 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007544 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7545 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007546 int cmp_len, value_len;
7547 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007548
Cyril Bontéb21570a2009-11-29 20:04:48 +01007549 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007550 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7551 value_begin = att_beg + t->be->appsession_name_len;
7552 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007553 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007554 cmp_len = att_end - att_beg;
7555 value_begin = val_beg;
7556 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007557 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007558
Cyril Bonté17530c32010-04-06 21:11:10 +02007559 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7561 /* free a possibly previously allocated memory */
7562 pool_free2(apools.sessid, txn->sessid);
7563
Cyril Bontéb21570a2009-11-29 20:04:48 +01007564 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007565 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007566 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7567 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7568 return;
7569 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007570 memcpy(txn->sessid, value_begin, value_len);
7571 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007572 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007573 }
7574 /* that's done for this cookie, check the next one on the same
7575 * line when next != hdr_end (only if is_cookie2).
7576 */
7577 }
7578 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007579 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007580 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007581
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007582 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007583 appsess *asession = NULL;
7584 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007585 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007586 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007587 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007588 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7589 Alert("Not enough Memory process_srv():asession:calloc().\n");
7590 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7591 return;
7592 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007593 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7594
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007595 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7596 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7597 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007598 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007599 return;
7600 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007601 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007602 asession->sessid[t->be->appsession_len] = 0;
7603
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007604 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007605 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007606 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007607 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007608 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007609 return;
7610 }
7611 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007612 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007613
7614 asession->request_count = 0;
7615 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7616 }
7617
7618 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7619 asession->request_count++;
7620 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007621}
7622
7623
Willy Tarreaua15645d2007-03-18 16:22:39 +01007624/*
7625 * Check if response is cacheable or not. Updates t->flags.
7626 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007627void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628{
7629 struct http_txn *txn = &t->txn;
7630 char *p1, *p2;
7631
7632 char *cur_ptr, *cur_end, *cur_next;
7633 int cur_idx;
7634
Willy Tarreau5df51872007-11-25 16:20:08 +01007635 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007636 return;
7637
7638 /* Iterate through the headers.
7639 * we start with the start line.
7640 */
7641 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007642 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007643
7644 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7645 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007646 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647
7648 cur_hdr = &txn->hdr_idx.v[cur_idx];
7649 cur_ptr = cur_next;
7650 cur_end = cur_ptr + cur_hdr->len;
7651 cur_next = cur_end + cur_hdr->cr + 1;
7652
7653 /* We have one full header between cur_ptr and cur_end, and the
7654 * next header starts at cur_next. We're only interested in
7655 * "Cookie:" headers.
7656 */
7657
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007658 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7659 if (val) {
7660 if ((cur_end - (cur_ptr + val) >= 8) &&
7661 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7662 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7663 return;
7664 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 }
7666
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007667 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7668 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007669 continue;
7670
7671 /* OK, right now we know we have a cache-control header at cur_ptr */
7672
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007673 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674
7675 if (p1 >= cur_end) /* no more info */
7676 continue;
7677
7678 /* p1 is at the beginning of the value */
7679 p2 = p1;
7680
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007681 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682 p2++;
7683
7684 /* we have a complete value between p1 and p2 */
7685 if (p2 < cur_end && *p2 == '=') {
7686 /* we have something of the form no-cache="set-cookie" */
7687 if ((cur_end - p1 >= 21) &&
7688 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7689 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007690 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007691 continue;
7692 }
7693
7694 /* OK, so we know that either p2 points to the end of string or to a comma */
7695 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007696 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007697 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7698 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7699 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007700 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701 return;
7702 }
7703
7704 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007705 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007706 continue;
7707 }
7708 }
7709}
7710
7711
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712/*
7713 * Try to retrieve a known appsession in the URI, then the associated server.
7714 * If the server is found, it's assigned to the session.
7715 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007716void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007718 char *end_params, *first_param, *cur_param, *next_param;
7719 char separator;
7720 int value_len;
7721
7722 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007724 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007725 (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 +01007726 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007727 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728
Cyril Bontéb21570a2009-11-29 20:04:48 +01007729 first_param = NULL;
7730 switch (mode) {
7731 case PR_O2_AS_M_PP:
7732 first_param = memchr(begin, ';', len);
7733 break;
7734 case PR_O2_AS_M_QS:
7735 first_param = memchr(begin, '?', len);
7736 break;
7737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007738
Cyril Bontéb21570a2009-11-29 20:04:48 +01007739 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007740 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007741 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007742
Cyril Bontéb21570a2009-11-29 20:04:48 +01007743 switch (mode) {
7744 case PR_O2_AS_M_PP:
7745 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7746 end_params = (char *) begin + len;
7747 }
7748 separator = ';';
7749 break;
7750 case PR_O2_AS_M_QS:
7751 end_params = (char *) begin + len;
7752 separator = '&';
7753 break;
7754 default:
7755 /* unknown mode, shouldn't happen */
7756 return;
7757 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758
Cyril Bontéb21570a2009-11-29 20:04:48 +01007759 cur_param = next_param = end_params;
7760 while (cur_param > first_param) {
7761 cur_param--;
7762 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7763 /* let's see if this is the appsession parameter */
7764 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7765 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7766 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7767 /* Cool... it's the right one */
7768 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7769 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7770 if (value_len > 0) {
7771 manage_client_side_appsession(t, cur_param, value_len);
7772 }
7773 break;
7774 }
7775 next_param = cur_param;
7776 }
7777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007778#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007779 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007780 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007782}
7783
Willy Tarreaub2513902006-12-17 14:52:38 +01007784/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007785 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007786 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007787 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007788 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007789 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007790 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007791 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007792 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007793int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007794{
7795 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007796 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007797 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007798
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007799 if (!uri_auth)
7800 return 0;
7801
Cyril Bonté70be45d2010-10-12 00:14:35 +02007802 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007803 return 0;
7804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007805 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007806 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007807 return 0;
7808
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007809 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007810 return 0;
7811
Willy Tarreaub2513902006-12-17 14:52:38 +01007812 return 1;
7813}
7814
Willy Tarreau4076a152009-04-02 15:18:36 +02007815/*
7816 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007817 * By default it tries to report the error position as msg->err_pos. However if
7818 * this one is not set, it will then report msg->next, which is the last known
7819 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007820 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007821 */
7822void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007823 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007824 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007825{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007826 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007827 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007828
Willy Tarreau9b28e032012-10-12 23:49:43 +02007829 es->len = MIN(chn->buf->i, sizeof(es->buf));
7830 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007831 len1 = MIN(len1, es->len);
7832 len2 = es->len - len1; /* remaining data if buffer wraps */
7833
Willy Tarreau9b28e032012-10-12 23:49:43 +02007834 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007835 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007836 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007837
Willy Tarreau4076a152009-04-02 15:18:36 +02007838 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007839 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007840 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007841 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007842
Willy Tarreau4076a152009-04-02 15:18:36 +02007843 es->when = date; // user-visible date
7844 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007845 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007846 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007847 if (objt_conn(s->req->prod->end))
7848 es->src = __objt_conn(s->req->prod->end)->addr.from;
7849 else
7850 memset(&es->src, 0, sizeof(es->src));
7851
Willy Tarreau078272e2010-12-12 12:46:33 +01007852 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007853 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007854 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007855 es->s_flags = s->flags;
7856 es->t_flags = s->txn.flags;
7857 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007858 es->b_out = chn->buf->o;
7859 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007860 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007861 es->m_clen = msg->chunk_len;
7862 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007863}
Willy Tarreaub2513902006-12-17 14:52:38 +01007864
Willy Tarreau294c4732011-12-16 21:35:50 +01007865/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7866 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7867 * performed over the whole headers. Otherwise it must contain a valid header
7868 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7869 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7870 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7871 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007872 * -1. The value fetch stops at commas, so this function is suited for use with
7873 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007874 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007875 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007876unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007877 struct hdr_idx *idx, int occ,
7878 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007879{
Willy Tarreau294c4732011-12-16 21:35:50 +01007880 struct hdr_ctx local_ctx;
7881 char *ptr_hist[MAX_HDR_HISTORY];
7882 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007883 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007884 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007885
Willy Tarreau294c4732011-12-16 21:35:50 +01007886 if (!ctx) {
7887 local_ctx.idx = 0;
7888 ctx = &local_ctx;
7889 }
7890
Willy Tarreaubce70882009-09-07 11:51:47 +02007891 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007892 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007893 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007894 occ--;
7895 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007896 *vptr = ctx->line + ctx->val;
7897 *vlen = ctx->vlen;
7898 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007899 }
7900 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007901 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007902 }
7903
7904 /* negative occurrence, we scan all the list then walk back */
7905 if (-occ > MAX_HDR_HISTORY)
7906 return 0;
7907
Willy Tarreau294c4732011-12-16 21:35:50 +01007908 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007909 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007910 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7911 len_hist[hist_ptr] = ctx->vlen;
7912 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007913 hist_ptr = 0;
7914 found++;
7915 }
7916 if (-occ > found)
7917 return 0;
7918 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007919 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7920 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7921 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007922 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007923 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007924 if (hist_ptr >= MAX_HDR_HISTORY)
7925 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007926 *vptr = ptr_hist[hist_ptr];
7927 *vlen = len_hist[hist_ptr];
7928 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007929}
7930
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007931/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7932 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7933 * performed over the whole headers. Otherwise it must contain a valid header
7934 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7935 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7936 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7937 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7938 * -1. This function differs from http_get_hdr() in that it only returns full
7939 * line header values and does not stop at commas.
7940 * The return value is 0 if nothing was found, or non-zero otherwise.
7941 */
7942unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7943 struct hdr_idx *idx, int occ,
7944 struct hdr_ctx *ctx, char **vptr, int *vlen)
7945{
7946 struct hdr_ctx local_ctx;
7947 char *ptr_hist[MAX_HDR_HISTORY];
7948 int len_hist[MAX_HDR_HISTORY];
7949 unsigned int hist_ptr;
7950 int found;
7951
7952 if (!ctx) {
7953 local_ctx.idx = 0;
7954 ctx = &local_ctx;
7955 }
7956
7957 if (occ >= 0) {
7958 /* search from the beginning */
7959 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7960 occ--;
7961 if (occ <= 0) {
7962 *vptr = ctx->line + ctx->val;
7963 *vlen = ctx->vlen;
7964 return 1;
7965 }
7966 }
7967 return 0;
7968 }
7969
7970 /* negative occurrence, we scan all the list then walk back */
7971 if (-occ > MAX_HDR_HISTORY)
7972 return 0;
7973
7974 found = hist_ptr = 0;
7975 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7976 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7977 len_hist[hist_ptr] = ctx->vlen;
7978 if (++hist_ptr >= MAX_HDR_HISTORY)
7979 hist_ptr = 0;
7980 found++;
7981 }
7982 if (-occ > found)
7983 return 0;
7984 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7985 * find occurrence -occ, so we have to check [hist_ptr+occ].
7986 */
7987 hist_ptr += occ;
7988 if (hist_ptr >= MAX_HDR_HISTORY)
7989 hist_ptr -= MAX_HDR_HISTORY;
7990 *vptr = ptr_hist[hist_ptr];
7991 *vlen = len_hist[hist_ptr];
7992 return 1;
7993}
7994
Willy Tarreaubaaee002006-06-26 02:48:02 +02007995/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007996 * Print a debug line with a header. Always stop at the first CR or LF char,
7997 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7998 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007999 */
8000void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8001{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008002 int max;
8003 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008004 dir,
8005 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8006 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 +02008007
8008 for (max = 0; start + max < end; max++)
8009 if (start[max] == '\r' || start[max] == '\n')
8010 break;
8011
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008012 UBOUND(max, trash.size - trash.len - 3);
8013 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8014 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008015 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008016}
8017
Willy Tarreau0937bc42009-12-22 15:03:09 +01008018/*
8019 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8020 * the required fields are properly allocated and that we only need to (re)init
8021 * them. This should be used before processing any new request.
8022 */
8023void http_init_txn(struct session *s)
8024{
8025 struct http_txn *txn = &s->txn;
8026 struct proxy *fe = s->fe;
8027
8028 txn->flags = 0;
8029 txn->status = -1;
8030
Willy Tarreauf64d1412010-10-07 20:06:11 +02008031 txn->cookie_first_date = 0;
8032 txn->cookie_last_date = 0;
8033
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008034 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008035 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008036 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008037 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008038 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008039 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008040 txn->req.chunk_len = 0LL;
8041 txn->req.body_len = 0LL;
8042 txn->rsp.chunk_len = 0LL;
8043 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008044 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8045 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008046 txn->req.chn = s->req;
8047 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008048
8049 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008050
8051 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8052 if (fe->options2 & PR_O2_REQBUG_OK)
8053 txn->req.err_pos = -1; /* let buggy requests pass */
8054
Willy Tarreau46023632010-01-07 22:51:47 +01008055 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008056 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8057
Willy Tarreau46023632010-01-07 22:51:47 +01008058 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008059 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8060
8061 if (txn->hdr_idx.v)
8062 hdr_idx_init(&txn->hdr_idx);
8063}
8064
8065/* to be used at the end of a transaction */
8066void http_end_txn(struct session *s)
8067{
8068 struct http_txn *txn = &s->txn;
8069
8070 /* these ones will have been dynamically allocated */
8071 pool_free2(pool2_requri, txn->uri);
8072 pool_free2(pool2_capture, txn->cli_cookie);
8073 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008074 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008075 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008076
William Lallemanda73203e2012-03-12 12:48:57 +01008077 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008078 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008079 txn->uri = NULL;
8080 txn->srv_cookie = NULL;
8081 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008082
8083 if (txn->req.cap) {
8084 struct cap_hdr *h;
8085 for (h = s->fe->req_cap; h; h = h->next)
8086 pool_free2(h->pool, txn->req.cap[h->index]);
8087 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8088 }
8089
8090 if (txn->rsp.cap) {
8091 struct cap_hdr *h;
8092 for (h = s->fe->rsp_cap; h; h = h->next)
8093 pool_free2(h->pool, txn->rsp.cap[h->index]);
8094 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8095 }
8096
Willy Tarreau0937bc42009-12-22 15:03:09 +01008097}
8098
8099/* to be used at the end of a transaction to prepare a new one */
8100void http_reset_txn(struct session *s)
8101{
8102 http_end_txn(s);
8103 http_init_txn(s);
8104
8105 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008106 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008107 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008108 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008109 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008110 /* re-init store persistence */
8111 s->store_count = 0;
8112
Willy Tarreau0937bc42009-12-22 15:03:09 +01008113 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008114
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008115 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008116
Willy Tarreau739cfba2010-01-25 23:11:14 +01008117 /* We must trim any excess data from the response buffer, because we
8118 * may have blocked an invalid response from a server that we don't
8119 * want to accidentely forward once we disable the analysers, nor do
8120 * we want those data to come along with next response. A typical
8121 * example of such data would be from a buggy server responding to
8122 * a HEAD with some data, or sending more than the advertised
8123 * content-length.
8124 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008125 if (unlikely(s->rep->buf->i))
8126 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008127
Willy Tarreau0937bc42009-12-22 15:03:09 +01008128 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008129 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008130
Willy Tarreaud04e8582010-05-31 12:31:35 +02008131 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008132 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008133
8134 s->req->rex = TICK_ETERNITY;
8135 s->req->wex = TICK_ETERNITY;
8136 s->req->analyse_exp = TICK_ETERNITY;
8137 s->rep->rex = TICK_ETERNITY;
8138 s->rep->wex = TICK_ETERNITY;
8139 s->rep->analyse_exp = TICK_ETERNITY;
8140}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008141
Willy Tarreauff011f22011-01-06 17:51:27 +01008142void free_http_req_rules(struct list *r) {
8143 struct http_req_rule *tr, *pr;
8144
8145 list_for_each_entry_safe(pr, tr, r, list) {
8146 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008147 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008148 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008149
8150 free(pr);
8151 }
8152}
8153
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008154/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008155struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8156{
8157 struct http_req_rule *rule;
8158 int cur_arg;
8159
8160 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8161 if (!rule) {
8162 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008163 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008164 }
8165
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008166 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008167 rule->action = HTTP_REQ_ACT_ALLOW;
8168 cur_arg = 1;
8169 } else if (!strcmp(args[0], "deny")) {
8170 rule->action = HTTP_REQ_ACT_DENY;
8171 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008172 } else if (!strcmp(args[0], "tarpit")) {
8173 rule->action = HTTP_REQ_ACT_TARPIT;
8174 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008175 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008176 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008177 cur_arg = 1;
8178
8179 while(*args[cur_arg]) {
8180 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008181 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008182 cur_arg+=2;
8183 continue;
8184 } else
8185 break;
8186 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008187 } else if (!strcmp(args[0], "set-nice")) {
8188 rule->action = HTTP_REQ_ACT_SET_NICE;
8189 cur_arg = 1;
8190
8191 if (!*args[cur_arg] ||
8192 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8193 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8194 file, linenum, args[0]);
8195 goto out_err;
8196 }
8197 rule->arg.nice = atoi(args[cur_arg]);
8198 if (rule->arg.nice < -1024)
8199 rule->arg.nice = -1024;
8200 else if (rule->arg.nice > 1024)
8201 rule->arg.nice = 1024;
8202 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008203 } else if (!strcmp(args[0], "set-tos")) {
8204#ifdef IP_TOS
8205 char *err;
8206 rule->action = HTTP_REQ_ACT_SET_TOS;
8207 cur_arg = 1;
8208
8209 if (!*args[cur_arg] ||
8210 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8211 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8212 file, linenum, args[0]);
8213 goto out_err;
8214 }
8215
8216 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8217 if (err && *err != '\0') {
8218 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8219 file, linenum, err, args[0]);
8220 goto out_err;
8221 }
8222 cur_arg++;
8223#else
8224 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8225 goto out_err;
8226#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008227 } else if (!strcmp(args[0], "set-mark")) {
8228#ifdef SO_MARK
8229 char *err;
8230 rule->action = HTTP_REQ_ACT_SET_MARK;
8231 cur_arg = 1;
8232
8233 if (!*args[cur_arg] ||
8234 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8235 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8236 file, linenum, args[0]);
8237 goto out_err;
8238 }
8239
8240 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8241 if (err && *err != '\0') {
8242 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8243 file, linenum, err, args[0]);
8244 goto out_err;
8245 }
8246 cur_arg++;
8247 global.last_checks |= LSTCHK_NETADM;
8248#else
8249 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8250 goto out_err;
8251#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008252 } else if (!strcmp(args[0], "set-log-level")) {
8253 rule->action = HTTP_REQ_ACT_SET_LOGL;
8254 cur_arg = 1;
8255
8256 if (!*args[cur_arg] ||
8257 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8258 bad_log_level:
8259 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8260 file, linenum, args[0]);
8261 goto out_err;
8262 }
8263 if (strcmp(args[cur_arg], "silent") == 0)
8264 rule->arg.loglevel = -1;
8265 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8266 goto bad_log_level;
8267 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008268 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8269 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8270 cur_arg = 1;
8271
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008272 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8273 (*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 +01008274 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8275 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008276 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008277 }
8278
8279 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8280 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8281 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008282
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008283 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008284 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8285 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008286 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008287 } else if (strcmp(args[0], "redirect") == 0) {
8288 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008289 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008290
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008291 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008292 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8293 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8294 goto out_err;
8295 }
8296
8297 /* this redirect rule might already contain a parsed condition which
8298 * we'll pass to the http-request rule.
8299 */
8300 rule->action = HTTP_REQ_ACT_REDIR;
8301 rule->arg.redir = redir;
8302 rule->cond = redir->cond;
8303 redir->cond = NULL;
8304 cur_arg = 2;
8305 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008306 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008307 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 +01008308 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008309 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008310 }
8311
8312 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8313 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008314 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008315
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008316 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8317 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8318 file, linenum, args[0], errmsg);
8319 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008320 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008321 }
8322 rule->cond = cond;
8323 }
8324 else if (*args[cur_arg]) {
8325 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8326 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8327 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008328 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008329 }
8330
8331 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008332 out_err:
8333 free(rule);
8334 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008335}
8336
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008337/* parse an "http-respose" rule */
8338struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8339{
8340 struct http_res_rule *rule;
8341 int cur_arg;
8342
8343 rule = calloc(1, sizeof(*rule));
8344 if (!rule) {
8345 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8346 goto out_err;
8347 }
8348
8349 if (!strcmp(args[0], "allow")) {
8350 rule->action = HTTP_RES_ACT_ALLOW;
8351 cur_arg = 1;
8352 } else if (!strcmp(args[0], "deny")) {
8353 rule->action = HTTP_RES_ACT_DENY;
8354 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008355 } else if (!strcmp(args[0], "set-nice")) {
8356 rule->action = HTTP_RES_ACT_SET_NICE;
8357 cur_arg = 1;
8358
8359 if (!*args[cur_arg] ||
8360 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8361 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8362 file, linenum, args[0]);
8363 goto out_err;
8364 }
8365 rule->arg.nice = atoi(args[cur_arg]);
8366 if (rule->arg.nice < -1024)
8367 rule->arg.nice = -1024;
8368 else if (rule->arg.nice > 1024)
8369 rule->arg.nice = 1024;
8370 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008371 } else if (!strcmp(args[0], "set-tos")) {
8372#ifdef IP_TOS
8373 char *err;
8374 rule->action = HTTP_RES_ACT_SET_TOS;
8375 cur_arg = 1;
8376
8377 if (!*args[cur_arg] ||
8378 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8379 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8380 file, linenum, args[0]);
8381 goto out_err;
8382 }
8383
8384 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8385 if (err && *err != '\0') {
8386 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8387 file, linenum, err, args[0]);
8388 goto out_err;
8389 }
8390 cur_arg++;
8391#else
8392 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8393 goto out_err;
8394#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008395 } else if (!strcmp(args[0], "set-mark")) {
8396#ifdef SO_MARK
8397 char *err;
8398 rule->action = HTTP_RES_ACT_SET_MARK;
8399 cur_arg = 1;
8400
8401 if (!*args[cur_arg] ||
8402 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8403 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8404 file, linenum, args[0]);
8405 goto out_err;
8406 }
8407
8408 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8409 if (err && *err != '\0') {
8410 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8411 file, linenum, err, args[0]);
8412 goto out_err;
8413 }
8414 cur_arg++;
8415 global.last_checks |= LSTCHK_NETADM;
8416#else
8417 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8418 goto out_err;
8419#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008420 } else if (!strcmp(args[0], "set-log-level")) {
8421 rule->action = HTTP_RES_ACT_SET_LOGL;
8422 cur_arg = 1;
8423
8424 if (!*args[cur_arg] ||
8425 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8426 bad_log_level:
8427 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8428 file, linenum, args[0]);
8429 goto out_err;
8430 }
8431 if (strcmp(args[cur_arg], "silent") == 0)
8432 rule->arg.loglevel = -1;
8433 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8434 goto bad_log_level;
8435 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008436 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8437 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8438 cur_arg = 1;
8439
8440 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8441 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8442 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8443 file, linenum, args[0]);
8444 goto out_err;
8445 }
8446
8447 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8448 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8449 LIST_INIT(&rule->arg.hdr_add.fmt);
8450
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008451 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008452 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8453 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8454 cur_arg += 2;
8455 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008456 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 +02008457 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8458 goto out_err;
8459 }
8460
8461 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8462 struct acl_cond *cond;
8463 char *errmsg = NULL;
8464
8465 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8466 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8467 file, linenum, args[0], errmsg);
8468 free(errmsg);
8469 goto out_err;
8470 }
8471 rule->cond = cond;
8472 }
8473 else if (*args[cur_arg]) {
8474 Alert("parsing [%s:%d]: 'http-response %s' expects"
8475 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8476 file, linenum, args[0], args[cur_arg]);
8477 goto out_err;
8478 }
8479
8480 return rule;
8481 out_err:
8482 free(rule);
8483 return NULL;
8484}
8485
Willy Tarreau4baae242012-12-27 12:00:31 +01008486/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008487 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8488 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008489 */
8490struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008491 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008492{
8493 struct redirect_rule *rule;
8494 int cur_arg;
8495 int type = REDIRECT_TYPE_NONE;
8496 int code = 302;
8497 const char *destination = NULL;
8498 const char *cookie = NULL;
8499 int cookie_set = 0;
8500 unsigned int flags = REDIRECT_FLAG_NONE;
8501 struct acl_cond *cond = NULL;
8502
8503 cur_arg = 0;
8504 while (*(args[cur_arg])) {
8505 if (strcmp(args[cur_arg], "location") == 0) {
8506 if (!*args[cur_arg + 1])
8507 goto missing_arg;
8508
8509 type = REDIRECT_TYPE_LOCATION;
8510 cur_arg++;
8511 destination = args[cur_arg];
8512 }
8513 else if (strcmp(args[cur_arg], "prefix") == 0) {
8514 if (!*args[cur_arg + 1])
8515 goto missing_arg;
8516
8517 type = REDIRECT_TYPE_PREFIX;
8518 cur_arg++;
8519 destination = args[cur_arg];
8520 }
8521 else if (strcmp(args[cur_arg], "scheme") == 0) {
8522 if (!*args[cur_arg + 1])
8523 goto missing_arg;
8524
8525 type = REDIRECT_TYPE_SCHEME;
8526 cur_arg++;
8527 destination = args[cur_arg];
8528 }
8529 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8530 if (!*args[cur_arg + 1])
8531 goto missing_arg;
8532
8533 cur_arg++;
8534 cookie = args[cur_arg];
8535 cookie_set = 1;
8536 }
8537 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8538 if (!*args[cur_arg + 1])
8539 goto missing_arg;
8540
8541 cur_arg++;
8542 cookie = args[cur_arg];
8543 cookie_set = 0;
8544 }
8545 else if (strcmp(args[cur_arg], "code") == 0) {
8546 if (!*args[cur_arg + 1])
8547 goto missing_arg;
8548
8549 cur_arg++;
8550 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008551 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008552 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008553 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008554 args[cur_arg - 1], args[cur_arg]);
8555 return NULL;
8556 }
8557 }
8558 else if (!strcmp(args[cur_arg],"drop-query")) {
8559 flags |= REDIRECT_FLAG_DROP_QS;
8560 }
8561 else if (!strcmp(args[cur_arg],"append-slash")) {
8562 flags |= REDIRECT_FLAG_APPEND_SLASH;
8563 }
8564 else if (strcmp(args[cur_arg], "if") == 0 ||
8565 strcmp(args[cur_arg], "unless") == 0) {
8566 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8567 if (!cond) {
8568 memprintf(errmsg, "error in condition: %s", *errmsg);
8569 return NULL;
8570 }
8571 break;
8572 }
8573 else {
8574 memprintf(errmsg,
8575 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8576 args[cur_arg]);
8577 return NULL;
8578 }
8579 cur_arg++;
8580 }
8581
8582 if (type == REDIRECT_TYPE_NONE) {
8583 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8584 return NULL;
8585 }
8586
8587 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8588 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008589 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008590
8591 if (!use_fmt) {
8592 /* old-style static redirect rule */
8593 rule->rdr_str = strdup(destination);
8594 rule->rdr_len = strlen(destination);
8595 }
8596 else {
8597 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008598
8599 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8600 * if prefix == "/", we don't want to add anything, otherwise it
8601 * makes it hard for the user to configure a self-redirection.
8602 */
8603 proxy->conf.args.ctx = ARGC_RDR;
8604 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8605 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8606 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8607 }
8608 }
8609
Willy Tarreau4baae242012-12-27 12:00:31 +01008610 if (cookie) {
8611 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8612 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8613 */
8614 rule->cookie_len = strlen(cookie);
8615 if (cookie_set) {
8616 rule->cookie_str = malloc(rule->cookie_len + 10);
8617 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8618 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8619 rule->cookie_len += 9;
8620 } else {
8621 rule->cookie_str = malloc(rule->cookie_len + 21);
8622 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8623 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8624 rule->cookie_len += 20;
8625 }
8626 }
8627 rule->type = type;
8628 rule->code = code;
8629 rule->flags = flags;
8630 LIST_INIT(&rule->list);
8631 return rule;
8632
8633 missing_arg:
8634 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8635 return NULL;
8636}
8637
Willy Tarreau8797c062007-05-07 00:55:35 +02008638/************************************************************************/
8639/* The code below is dedicated to ACL parsing and matching */
8640/************************************************************************/
8641
8642
Willy Tarreau14174bc2012-04-16 14:34:04 +02008643/* This function ensures that the prerequisites for an L7 fetch are ready,
8644 * which means that a request or response is ready. If some data is missing,
8645 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008646 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8647 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008648 *
8649 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008650 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8651 * decide whether or not an HTTP message is present ;
8652 * 0 if the requested data cannot be fetched or if it is certain that
8653 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008654 * 1 if an HTTP message is ready
8655 */
8656static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008657smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008658 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008659{
8660 struct http_txn *txn = l7;
8661 struct http_msg *msg = &txn->req;
8662
8663 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8664 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8665 */
8666
8667 if (unlikely(!s || !txn))
8668 return 0;
8669
8670 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008671 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008672
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008673 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008674 if (unlikely(!s->req))
8675 return 0;
8676
Willy Tarreauaae75e32013-03-29 12:31:49 +01008677 /* If the buffer does not leave enough free space at the end,
8678 * we must first realign it.
8679 */
8680 if (s->req->buf->p > s->req->buf->data &&
8681 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8682 buffer_slow_realign(s->req->buf);
8683
Willy Tarreau14174bc2012-04-16 14:34:04 +02008684 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008685 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008686 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008687
8688 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008689 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008690 http_msg_analyzer(msg, &txn->hdr_idx);
8691
8692 /* Still no valid request ? */
8693 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008694 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008695 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008696 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008697 }
8698 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008699 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008700 return 0;
8701 }
8702
8703 /* OK we just got a valid HTTP request. We have some minor
8704 * preparation to perform so that further checks can rely
8705 * on HTTP tests.
8706 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008707
8708 /* If the request was parsed but was too large, we must absolutely
8709 * return an error so that it is not processed. At the moment this
8710 * cannot happen, but if the parsers are to change in the future,
8711 * we want this check to be maintained.
8712 */
8713 if (unlikely(s->req->buf->i + s->req->buf->p >
8714 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8715 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008716 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008717 return 1;
8718 }
8719
Willy Tarreau9b28e032012-10-12 23:49:43 +02008720 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008721 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8722 s->flags |= SN_REDIRECTABLE;
8723
Willy Tarreau506d0502013-07-06 13:29:24 +02008724 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8725 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008726 }
8727
Willy Tarreau506d0502013-07-06 13:29:24 +02008728 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008729 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008730 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008731
8732 /* otherwise everything's ready for the request */
8733 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008734 else {
8735 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008736 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8737 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008738 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008739 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008740 }
8741
8742 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008743 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008744 return 1;
8745}
Willy Tarreau8797c062007-05-07 00:55:35 +02008746
Willy Tarreauc0239e02012-04-16 14:42:55 +02008747#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008748 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 +02008749
Willy Tarreau24e32d82012-04-23 23:55:44 +02008750#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008751 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 +02008752
Willy Tarreau8797c062007-05-07 00:55:35 +02008753
8754/* 1. Check on METHOD
8755 * We use the pre-parsed method if it is known, and store its number as an
8756 * integer. If it is unknown, we use the pointer and the length.
8757 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008758static int pat_parse_meth(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008759{
8760 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008761 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008762
Willy Tarreauae8b7962007-06-09 23:10:04 +02008763 len = strlen(*text);
8764 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008765
8766 pattern->val.i = meth;
8767 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008768 if (usage == PAT_U_COMPILE) {
8769 pattern->ptr.str = strdup(*text);
8770 if (!pattern->ptr.str) {
8771 memprintf(err, "out of memory while loading pattern");
8772 return 0;
8773 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008774 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008775 else {
8776 trash = get_trash_chunk();
8777 if (trash->size < len) {
8778 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8779 len, trash->size);
8780 return 0;
8781 }
8782 pattern->ptr.str = trash->str;
8783 }
8784 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008785 pattern->len = len;
8786 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008787 else
8788 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008789 return 1;
8790}
8791
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008792/* This function fetches the method of current HTTP request and stores
8793 * it in the global pattern struct as a chunk. There are two possibilities :
8794 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8795 * in <len> and <ptr> is NULL ;
8796 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8797 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008798 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008799 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008800static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008801smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008802 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008803{
8804 int meth;
8805 struct http_txn *txn = l7;
8806
Willy Tarreau24e32d82012-04-23 23:55:44 +02008807 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008808
Willy Tarreau8797c062007-05-07 00:55:35 +02008809 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008810 smp->type = SMP_T_UINT;
8811 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008812 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008813 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8814 /* ensure the indexes are not affected */
8815 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008816 smp->type = SMP_T_CSTR;
8817 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008818 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008819 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008820 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008821 return 1;
8822}
8823
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008824/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008825static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008826{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008827 int icase;
8828
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008829
Willy Tarreauf853c462012-04-23 18:53:56 +02008830 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008831 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008832 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008833 return PAT_MATCH;
8834 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008835 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008836
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008837 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8838 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008839 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008840
8841 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008842 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008843 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008844
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008845 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008846 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8847 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008848 return PAT_NOMATCH;
8849 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008850}
8851
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008852static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008853smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008854 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008855{
8856 struct http_txn *txn = l7;
8857 char *ptr;
8858 int len;
8859
Willy Tarreauc0239e02012-04-16 14:42:55 +02008860 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008861
Willy Tarreau8797c062007-05-07 00:55:35 +02008862 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008863 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008864
8865 while ((len-- > 0) && (*ptr++ != '/'));
8866 if (len <= 0)
8867 return 0;
8868
Willy Tarreauf853c462012-04-23 18:53:56 +02008869 smp->type = SMP_T_CSTR;
8870 smp->data.str.str = ptr;
8871 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008872
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008873 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008874 return 1;
8875}
8876
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008877static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008878smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008879 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008880{
8881 struct http_txn *txn = l7;
8882 char *ptr;
8883 int len;
8884
Willy Tarreauc0239e02012-04-16 14:42:55 +02008885 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008886
Willy Tarreauf26b2522012-12-14 08:33:14 +01008887 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8888 return 0;
8889
Willy Tarreau8797c062007-05-07 00:55:35 +02008890 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008891 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008892
8893 while ((len-- > 0) && (*ptr++ != '/'));
8894 if (len <= 0)
8895 return 0;
8896
Willy Tarreauf853c462012-04-23 18:53:56 +02008897 smp->type = SMP_T_CSTR;
8898 smp->data.str.str = ptr;
8899 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008900
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008901 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008902 return 1;
8903}
8904
8905/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008906static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008907smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008908 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008909{
8910 struct http_txn *txn = l7;
8911 char *ptr;
8912 int len;
8913
Willy Tarreauc0239e02012-04-16 14:42:55 +02008914 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008915
Willy Tarreauf26b2522012-12-14 08:33:14 +01008916 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8917 return 0;
8918
Willy Tarreau8797c062007-05-07 00:55:35 +02008919 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008920 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008921
Willy Tarreauf853c462012-04-23 18:53:56 +02008922 smp->type = SMP_T_UINT;
8923 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008924 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008925 return 1;
8926}
8927
8928/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008929static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008930smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008931 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008932{
8933 struct http_txn *txn = l7;
8934
Willy Tarreauc0239e02012-04-16 14:42:55 +02008935 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008936
Willy Tarreauf853c462012-04-23 18:53:56 +02008937 smp->type = SMP_T_CSTR;
8938 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008939 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008940 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008941 return 1;
8942}
8943
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008944static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008945smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008946 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008947{
8948 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008949 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008950
Willy Tarreauc0239e02012-04-16 14:42:55 +02008951 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008952
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008953 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8954 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008955 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008956
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008957 smp->type = SMP_T_IPV4;
8958 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02008959 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008960 return 1;
8961}
8962
8963static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008964smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008965 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008966{
8967 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008968 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008969
Willy Tarreauc0239e02012-04-16 14:42:55 +02008970 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008971
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008972 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8973 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
8974 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008975
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008976 smp->type = SMP_T_UINT;
8977 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008978 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008979 return 1;
8980}
8981
Willy Tarreau185b5c42012-04-26 15:11:51 +02008982/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8983 * Accepts an optional argument of type string containing the header field name,
8984 * and an optional argument of type signed or unsigned integer to request an
8985 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008986 * headers are considered from the first one. It does not stop on commas and
8987 * returns full lines instead (useful for User-Agent or Date for example).
8988 */
8989static int
8990smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008991 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008992{
8993 struct http_txn *txn = l7;
8994 struct hdr_idx *idx = &txn->hdr_idx;
8995 struct hdr_ctx *ctx = smp->ctx.a[0];
8996 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
8997 int occ = 0;
8998 const char *name_str = NULL;
8999 int name_len = 0;
9000
9001 if (!ctx) {
9002 /* first call */
9003 ctx = &static_hdr_ctx;
9004 ctx->idx = 0;
9005 smp->ctx.a[0] = ctx;
9006 }
9007
9008 if (args) {
9009 if (args[0].type != ARGT_STR)
9010 return 0;
9011 name_str = args[0].data.str.str;
9012 name_len = args[0].data.str.len;
9013
9014 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9015 occ = args[1].data.uint;
9016 }
9017
9018 CHECK_HTTP_MESSAGE_FIRST();
9019
9020 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9021 /* search for header from the beginning */
9022 ctx->idx = 0;
9023
9024 if (!occ && !(opt & SMP_OPT_ITERATE))
9025 /* no explicit occurrence and single fetch => last header by default */
9026 occ = -1;
9027
9028 if (!occ)
9029 /* prepare to report multiple occurrences for ACL fetches */
9030 smp->flags |= SMP_F_NOT_LAST;
9031
9032 smp->type = SMP_T_CSTR;
9033 smp->flags |= SMP_F_VOL_HDR;
9034 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9035 return 1;
9036
9037 smp->flags &= ~SMP_F_NOT_LAST;
9038 return 0;
9039}
9040
9041/* 6. Check on HTTP header count. The number of occurrences is returned.
9042 * Accepts exactly 1 argument of type string. It does not stop on commas and
9043 * returns full lines instead (useful for User-Agent or Date for example).
9044 */
9045static int
9046smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009047 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009048{
9049 struct http_txn *txn = l7;
9050 struct hdr_idx *idx = &txn->hdr_idx;
9051 struct hdr_ctx ctx;
9052 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9053 int cnt;
9054
9055 if (!args || args->type != ARGT_STR)
9056 return 0;
9057
9058 CHECK_HTTP_MESSAGE_FIRST();
9059
9060 ctx.idx = 0;
9061 cnt = 0;
9062 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9063 cnt++;
9064
9065 smp->type = SMP_T_UINT;
9066 smp->data.uint = cnt;
9067 smp->flags = SMP_F_VOL_HDR;
9068 return 1;
9069}
9070
9071/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9072 * Accepts an optional argument of type string containing the header field name,
9073 * and an optional argument of type signed or unsigned integer to request an
9074 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009075 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009076 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009077static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009078smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009079 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009080{
9081 struct http_txn *txn = l7;
9082 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009083 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009084 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009085 int occ = 0;
9086 const char *name_str = NULL;
9087 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009088
Willy Tarreaua890d072013-04-02 12:01:06 +02009089 if (!ctx) {
9090 /* first call */
9091 ctx = &static_hdr_ctx;
9092 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009093 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009094 }
9095
Willy Tarreau185b5c42012-04-26 15:11:51 +02009096 if (args) {
9097 if (args[0].type != ARGT_STR)
9098 return 0;
9099 name_str = args[0].data.str.str;
9100 name_len = args[0].data.str.len;
9101
9102 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9103 occ = args[1].data.uint;
9104 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009105
Willy Tarreaue333ec92012-04-16 16:26:40 +02009106 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009107
Willy Tarreau185b5c42012-04-26 15:11:51 +02009108 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009109 /* search for header from the beginning */
9110 ctx->idx = 0;
9111
Willy Tarreau185b5c42012-04-26 15:11:51 +02009112 if (!occ && !(opt & SMP_OPT_ITERATE))
9113 /* no explicit occurrence and single fetch => last header by default */
9114 occ = -1;
9115
9116 if (!occ)
9117 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009118 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009119
Willy Tarreau185b5c42012-04-26 15:11:51 +02009120 smp->type = SMP_T_CSTR;
9121 smp->flags |= SMP_F_VOL_HDR;
9122 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 +02009123 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009124
Willy Tarreau37406352012-04-23 16:16:37 +02009125 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009126 return 0;
9127}
9128
Willy Tarreauc11416f2007-06-17 16:58:38 +02009129/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009130 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009131 */
9132static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009133smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009134 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009135{
9136 struct http_txn *txn = l7;
9137 struct hdr_idx *idx = &txn->hdr_idx;
9138 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009139 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009140 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009141
Willy Tarreau24e32d82012-04-23 23:55:44 +02009142 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009143 return 0;
9144
Willy Tarreaue333ec92012-04-16 16:26:40 +02009145 CHECK_HTTP_MESSAGE_FIRST();
9146
Willy Tarreau33a7e692007-06-10 19:45:56 +02009147 ctx.idx = 0;
9148 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009149 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 +02009150 cnt++;
9151
Willy Tarreauf853c462012-04-23 18:53:56 +02009152 smp->type = SMP_T_UINT;
9153 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009154 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009155 return 1;
9156}
9157
Willy Tarreau185b5c42012-04-26 15:11:51 +02009158/* Fetch an HTTP header's integer value. The integer value is returned. It
9159 * takes a mandatory argument of type string and an optional one of type int
9160 * to designate a specific occurrence. It returns an unsigned integer, which
9161 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009162 */
9163static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009164smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009165 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009166{
Willy Tarreauef38c392013-07-22 16:29:32 +02009167 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009168
Willy Tarreauf853c462012-04-23 18:53:56 +02009169 if (ret > 0) {
9170 smp->type = SMP_T_UINT;
9171 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9172 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009173
Willy Tarreaud53e2422012-04-16 17:21:11 +02009174 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009175}
9176
Cyril Bonté69fa9922012-10-25 00:01:06 +02009177/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9178 * and an optional one of type int to designate a specific occurrence.
9179 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009180 */
9181static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009182smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009183 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009184{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009185 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009186
Willy Tarreauef38c392013-07-22 16:29:32 +02009187 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009188 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9189 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009190 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009191 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009192 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009193 if (smp->data.str.len < temp->size - 1) {
9194 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9195 temp->str[smp->data.str.len] = '\0';
9196 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9197 smp->type = SMP_T_IPV6;
9198 break;
9199 }
9200 }
9201 }
9202
Willy Tarreaud53e2422012-04-16 17:21:11 +02009203 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009204 if (!(smp->flags & SMP_F_NOT_LAST))
9205 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009206 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009207 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009208}
9209
Willy Tarreau737b0c12007-06-10 21:28:46 +02009210/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9211 * the first '/' after the possible hostname, and ends before the possible '?'.
9212 */
9213static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009214smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009215 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009216{
9217 struct http_txn *txn = l7;
9218 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009219
Willy Tarreauc0239e02012-04-16 14:42:55 +02009220 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009221
Willy Tarreau9b28e032012-10-12 23:49:43 +02009222 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009223 ptr = http_get_path(txn);
9224 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009225 return 0;
9226
9227 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009228 smp->type = SMP_T_CSTR;
9229 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009230
9231 while (ptr < end && *ptr != '?')
9232 ptr++;
9233
Willy Tarreauf853c462012-04-23 18:53:56 +02009234 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009235 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009236 return 1;
9237}
9238
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009239/* This produces a concatenation of the first occurrence of the Host header
9240 * followed by the path component if it begins with a slash ('/'). This means
9241 * that '*' will not be added, resulting in exactly the first Host entry.
9242 * If no Host header is found, then the path is returned as-is. The returned
9243 * value is stored in the trash so it does not need to be marked constant.
9244 */
9245static int
9246smp_fetch_base(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 Tarreaua7ad50c2012-04-29 15:39:40 +02009248{
9249 struct http_txn *txn = l7;
9250 char *ptr, *end, *beg;
9251 struct hdr_ctx ctx;
9252
9253 CHECK_HTTP_MESSAGE_FIRST();
9254
9255 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009256 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 +02009257 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009258 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009259
9260 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009261 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009262 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009263 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009264 smp->data.str.len = ctx.vlen;
9265
9266 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009267 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 +02009268 beg = http_get_path(txn);
9269 if (!beg)
9270 beg = end;
9271
9272 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9273
9274 if (beg < ptr && *beg == '/') {
9275 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9276 smp->data.str.len += ptr - beg;
9277 }
9278
9279 smp->flags = SMP_F_VOL_1ST;
9280 return 1;
9281}
9282
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009283/* This produces a 32-bit hash of the concatenation of the first occurrence of
9284 * the Host header followed by the path component if it begins with a slash ('/').
9285 * This means that '*' will not be added, resulting in exactly the first Host
9286 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009287 * is hashed using the path hash followed by a full avalanche hash and provides a
9288 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009289 * high-traffic sites without having to store whole paths.
9290 */
9291static int
9292smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009293 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009294{
9295 struct http_txn *txn = l7;
9296 struct hdr_ctx ctx;
9297 unsigned int hash = 0;
9298 char *ptr, *beg, *end;
9299 int len;
9300
9301 CHECK_HTTP_MESSAGE_FIRST();
9302
9303 ctx.idx = 0;
9304 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9305 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9306 ptr = ctx.line + ctx.val;
9307 len = ctx.vlen;
9308 while (len--)
9309 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9310 }
9311
9312 /* now retrieve the path */
9313 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9314 beg = http_get_path(txn);
9315 if (!beg)
9316 beg = end;
9317
9318 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9319
9320 if (beg < ptr && *beg == '/') {
9321 while (beg < ptr)
9322 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9323 }
9324 hash = full_hash(hash);
9325
9326 smp->type = SMP_T_UINT;
9327 smp->data.uint = hash;
9328 smp->flags = SMP_F_VOL_1ST;
9329 return 1;
9330}
9331
Willy Tarreau4a550602012-12-09 14:53:32 +01009332/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009333 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9334 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9335 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009336 * that in environments where IPv6 is insignificant, truncating the output to
9337 * 8 bytes would still work.
9338 */
9339static int
9340smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009341 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009342{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009343 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009344 struct connection *cli_conn = objt_conn(l4->si[0].end);
9345
9346 if (!cli_conn)
9347 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009348
Willy Tarreauef38c392013-07-22 16:29:32 +02009349 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009350 return 0;
9351
Willy Tarreau47ca5452012-12-23 20:22:19 +01009352 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009353 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9354 temp->len += sizeof(smp->data.uint);
9355
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009356 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009357 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009358 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009359 temp->len += 4;
9360 break;
9361 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009362 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009363 temp->len += 16;
9364 break;
9365 default:
9366 return 0;
9367 }
9368
9369 smp->data.str = *temp;
9370 smp->type = SMP_T_BIN;
9371 return 1;
9372}
9373
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009374static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009375smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009376 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009377{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009378 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9379 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9380 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009381
Willy Tarreau24e32d82012-04-23 23:55:44 +02009382 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009383
Willy Tarreauf853c462012-04-23 18:53:56 +02009384 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009385 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009386 return 1;
9387}
9388
Willy Tarreau7f18e522010-10-22 20:04:13 +02009389/* return a valid test if the current request is the first one on the connection */
9390static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009391smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009392 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009393{
9394 if (!s)
9395 return 0;
9396
Willy Tarreauf853c462012-04-23 18:53:56 +02009397 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009398 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009399 return 1;
9400}
9401
Willy Tarreau34db1082012-04-19 17:16:54 +02009402/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009403static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009404smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009405 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009406{
9407
Willy Tarreau24e32d82012-04-23 23:55:44 +02009408 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009409 return 0;
9410
Willy Tarreauc0239e02012-04-16 14:42:55 +02009411 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009412
Willy Tarreauc0239e02012-04-16 14:42:55 +02009413 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009414 return 0;
9415
Willy Tarreauf853c462012-04-23 18:53:56 +02009416 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009417 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009418 return 1;
9419}
Willy Tarreau8797c062007-05-07 00:55:35 +02009420
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009421/* Accepts exactly 1 argument of type userlist */
9422static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009423smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009424 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009425{
9426
9427 if (!args || args->type != ARGT_USR)
9428 return 0;
9429
9430 CHECK_HTTP_MESSAGE_FIRST();
9431
9432 if (!get_http_auth(l4))
9433 return 0;
9434
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009435 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009436 smp->ctx.a[0] = args->data.usr; /* user list */
9437 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9438 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9439
9440 /* if the user does not belong to the userlist or has a wrong password,
9441 * report that it unconditionally does not match. Otherwise we return
9442 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009443 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009444 */
9445 smp->type = SMP_T_BOOL;
9446 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9447 if (smp->data.uint)
9448 smp->type = SMP_T_UINT;
9449
9450 return 1;
9451}
9452
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009453/* Try to find the next occurrence of a cookie name in a cookie header value.
9454 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9455 * the cookie value is returned into *value and *value_l, and the function
9456 * returns a pointer to the next pointer to search from if the value was found.
9457 * Otherwise if the cookie was not found, NULL is returned and neither value
9458 * nor value_l are touched. The input <hdr> string should first point to the
9459 * header's value, and the <hdr_end> pointer must point to the first character
9460 * not part of the value. <list> must be non-zero if value may represent a list
9461 * of values (cookie headers). This makes it faster to abort parsing when no
9462 * list is expected.
9463 */
9464static char *
9465extract_cookie_value(char *hdr, const char *hdr_end,
9466 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009467 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009468{
9469 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9470 char *next;
9471
9472 /* we search at least a cookie name followed by an equal, and more
9473 * generally something like this :
9474 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9475 */
9476 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9477 /* Iterate through all cookies on this line */
9478
9479 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9480 att_beg++;
9481
9482 /* find att_end : this is the first character after the last non
9483 * space before the equal. It may be equal to hdr_end.
9484 */
9485 equal = att_end = att_beg;
9486
9487 while (equal < hdr_end) {
9488 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9489 break;
9490 if (http_is_spht[(unsigned char)*equal++])
9491 continue;
9492 att_end = equal;
9493 }
9494
9495 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9496 * is between <att_beg> and <equal>, both may be identical.
9497 */
9498
9499 /* look for end of cookie if there is an equal sign */
9500 if (equal < hdr_end && *equal == '=') {
9501 /* look for the beginning of the value */
9502 val_beg = equal + 1;
9503 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9504 val_beg++;
9505
9506 /* find the end of the value, respecting quotes */
9507 next = find_cookie_value_end(val_beg, hdr_end);
9508
9509 /* make val_end point to the first white space or delimitor after the value */
9510 val_end = next;
9511 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9512 val_end--;
9513 } else {
9514 val_beg = val_end = next = equal;
9515 }
9516
9517 /* We have nothing to do with attributes beginning with '$'. However,
9518 * they will automatically be removed if a header before them is removed,
9519 * since they're supposed to be linked together.
9520 */
9521 if (*att_beg == '$')
9522 continue;
9523
9524 /* Ignore cookies with no equal sign */
9525 if (equal == next)
9526 continue;
9527
9528 /* Now we have the cookie name between att_beg and att_end, and
9529 * its value between val_beg and val_end.
9530 */
9531
9532 if (att_end - att_beg == cookie_name_l &&
9533 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9534 /* let's return this value and indicate where to go on from */
9535 *value = val_beg;
9536 *value_l = val_end - val_beg;
9537 return next + 1;
9538 }
9539
9540 /* Set-Cookie headers only have the name in the first attr=value part */
9541 if (!list)
9542 break;
9543 }
9544
9545 return NULL;
9546}
9547
Willy Tarreaue333ec92012-04-16 16:26:40 +02009548/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009549 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009550 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009551 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009552 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009553 * Accepts exactly 1 argument of type string. If the input options indicate
9554 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009555 */
9556static int
Willy Tarreau51539362012-05-08 12:46:28 +02009557smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009558 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009559{
9560 struct http_txn *txn = l7;
9561 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009562 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009563 const struct http_msg *msg;
9564 const char *hdr_name;
9565 int hdr_name_len;
9566 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009567 int occ = 0;
9568 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009569
Willy Tarreau24e32d82012-04-23 23:55:44 +02009570 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009571 return 0;
9572
Willy Tarreaua890d072013-04-02 12:01:06 +02009573 if (!ctx) {
9574 /* first call */
9575 ctx = &static_hdr_ctx;
9576 ctx->idx = 0;
9577 smp->ctx.a[2] = ctx;
9578 }
9579
Willy Tarreaue333ec92012-04-16 16:26:40 +02009580 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009581
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009582 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009583 msg = &txn->req;
9584 hdr_name = "Cookie";
9585 hdr_name_len = 6;
9586 } else {
9587 msg = &txn->rsp;
9588 hdr_name = "Set-Cookie";
9589 hdr_name_len = 10;
9590 }
9591
Willy Tarreau28376d62012-04-26 21:26:10 +02009592 if (!occ && !(opt & SMP_OPT_ITERATE))
9593 /* no explicit occurrence and single fetch => last cookie by default */
9594 occ = -1;
9595
9596 /* OK so basically here, either we want only one value and it's the
9597 * last one, or we want to iterate over all of them and we fetch the
9598 * next one.
9599 */
9600
Willy Tarreau9b28e032012-10-12 23:49:43 +02009601 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009602 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009603 /* search for the header from the beginning, we must first initialize
9604 * the search parameters.
9605 */
Willy Tarreau37406352012-04-23 16:16:37 +02009606 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009607 ctx->idx = 0;
9608 }
9609
Willy Tarreau28376d62012-04-26 21:26:10 +02009610 smp->flags |= SMP_F_VOL_HDR;
9611
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009612 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009613 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9614 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009615 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9616 goto out;
9617
Willy Tarreau24e32d82012-04-23 23:55:44 +02009618 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009619 continue;
9620
Willy Tarreau37406352012-04-23 16:16:37 +02009621 smp->ctx.a[0] = ctx->line + ctx->val;
9622 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009623 }
9624
Willy Tarreauf853c462012-04-23 18:53:56 +02009625 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009626 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009627 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009628 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009629 &smp->data.str.str,
9630 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009631 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009632 found = 1;
9633 if (occ >= 0) {
9634 /* one value was returned into smp->data.str.{str,len} */
9635 smp->flags |= SMP_F_NOT_LAST;
9636 return 1;
9637 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009638 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009639 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009640 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009641 /* all cookie headers and values were scanned. If we're looking for the
9642 * last occurrence, we may return it now.
9643 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009644 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009645 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009646 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009647}
9648
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009649/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009650 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009651 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009652 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009653 */
9654static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009655smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009656 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009657{
9658 struct http_txn *txn = l7;
9659 struct hdr_idx *idx = &txn->hdr_idx;
9660 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009661 const struct http_msg *msg;
9662 const char *hdr_name;
9663 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009664 int cnt;
9665 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009666 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009667
Willy Tarreau24e32d82012-04-23 23:55:44 +02009668 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009669 return 0;
9670
Willy Tarreaue333ec92012-04-16 16:26:40 +02009671 CHECK_HTTP_MESSAGE_FIRST();
9672
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009673 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009674 msg = &txn->req;
9675 hdr_name = "Cookie";
9676 hdr_name_len = 6;
9677 } else {
9678 msg = &txn->rsp;
9679 hdr_name = "Set-Cookie";
9680 hdr_name_len = 10;
9681 }
9682
Willy Tarreau9b28e032012-10-12 23:49:43 +02009683 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009684 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009685 ctx.idx = 0;
9686 cnt = 0;
9687
9688 while (1) {
9689 /* Note: val_beg == NULL every time we need to fetch a new header */
9690 if (!val_beg) {
9691 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9692 break;
9693
Willy Tarreau24e32d82012-04-23 23:55:44 +02009694 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009695 continue;
9696
9697 val_beg = ctx.line + ctx.val;
9698 val_end = val_beg + ctx.vlen;
9699 }
9700
Willy Tarreauf853c462012-04-23 18:53:56 +02009701 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009702 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009703 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009704 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009705 &smp->data.str.str,
9706 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009707 cnt++;
9708 }
9709 }
9710
Willy Tarreauf853c462012-04-23 18:53:56 +02009711 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009712 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009713 return 1;
9714}
9715
Willy Tarreau51539362012-05-08 12:46:28 +02009716/* Fetch an cookie's integer value. The integer value is returned. It
9717 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9718 */
9719static int
9720smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009721 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009722{
Willy Tarreauef38c392013-07-22 16:29:32 +02009723 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009724
9725 if (ret > 0) {
9726 smp->type = SMP_T_UINT;
9727 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9728 }
9729
9730 return ret;
9731}
9732
Willy Tarreau8797c062007-05-07 00:55:35 +02009733/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009734/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009735/************************************************************************/
9736
David Cournapeau16023ee2010-12-23 20:55:41 +09009737/*
9738 * Given a path string and its length, find the position of beginning of the
9739 * query string. Returns NULL if no query string is found in the path.
9740 *
9741 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9742 *
9743 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9744 */
bedis4c75cca2012-10-05 08:38:24 +02009745static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009746{
9747 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009748
bedis4c75cca2012-10-05 08:38:24 +02009749 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009750 return p ? p + 1 : NULL;
9751}
9752
bedis4c75cca2012-10-05 08:38:24 +02009753static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009754{
bedis4c75cca2012-10-05 08:38:24 +02009755 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009756}
9757
9758/*
9759 * Given a url parameter, find the starting position of the first occurence,
9760 * or NULL if the parameter is not found.
9761 *
9762 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9763 * the function will return query_string+8.
9764 */
9765static char*
9766find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009767 char* url_param_name, size_t url_param_name_l,
9768 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009769{
9770 char *pos, *last;
9771
9772 pos = query_string;
9773 last = query_string + query_string_l - url_param_name_l - 1;
9774
9775 while (pos <= last) {
9776 if (pos[url_param_name_l] == '=') {
9777 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9778 return pos;
9779 pos += url_param_name_l + 1;
9780 }
bedis4c75cca2012-10-05 08:38:24 +02009781 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009782 pos++;
9783 pos++;
9784 }
9785 return NULL;
9786}
9787
9788/*
9789 * Given a url parameter name, returns its value and size into *value and
9790 * *value_l respectively, and returns non-zero. If the parameter is not found,
9791 * zero is returned and value/value_l are not touched.
9792 */
9793static int
9794find_url_param_value(char* path, size_t path_l,
9795 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009796 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009797{
9798 char *query_string, *qs_end;
9799 char *arg_start;
9800 char *value_start, *value_end;
9801
bedis4c75cca2012-10-05 08:38:24 +02009802 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009803 if (!query_string)
9804 return 0;
9805
9806 qs_end = path + path_l;
9807 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009808 url_param_name, url_param_name_l,
9809 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009810 if (!arg_start)
9811 return 0;
9812
9813 value_start = arg_start + url_param_name_l + 1;
9814 value_end = value_start;
9815
bedis4c75cca2012-10-05 08:38:24 +02009816 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009817 value_end++;
9818
9819 *value = value_start;
9820 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009821 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009822}
9823
9824static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009825smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009826 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009827{
bedis4c75cca2012-10-05 08:38:24 +02009828 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009829 struct http_txn *txn = l7;
9830 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009831
bedis4c75cca2012-10-05 08:38:24 +02009832 if (!args || args[0].type != ARGT_STR ||
9833 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009834 return 0;
9835
9836 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009837
bedis4c75cca2012-10-05 08:38:24 +02009838 if (args[1].type)
9839 delim = *args[1].data.str.str;
9840
Willy Tarreau9b28e032012-10-12 23:49:43 +02009841 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009842 args->data.str.str, args->data.str.len,
9843 &smp->data.str.str, &smp->data.str.len,
9844 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009845 return 0;
9846
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009847 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009848 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009849 return 1;
9850}
9851
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009852/* Return the signed integer value for the specified url parameter (see url_param
9853 * above).
9854 */
9855static int
9856smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009857 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009858{
Willy Tarreauef38c392013-07-22 16:29:32 +02009859 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009860
9861 if (ret > 0) {
9862 smp->type = SMP_T_UINT;
9863 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9864 }
9865
9866 return ret;
9867}
9868
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009869/* This produces a 32-bit hash of the concatenation of the first occurrence of
9870 * the Host header followed by the path component if it begins with a slash ('/').
9871 * This means that '*' will not be added, resulting in exactly the first Host
9872 * entry. If no Host header is found, then the path is used. The resulting value
9873 * is hashed using the url hash followed by a full avalanche hash and provides a
9874 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
9875 * high-traffic sites without having to store whole paths.
9876 * this differs from the base32 functions in that it includes the url parameters
9877 * as well as the path
9878 */
9879static int
9880smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009881 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009882{
9883 struct http_txn *txn = l7;
9884 struct hdr_ctx ctx;
9885 unsigned int hash = 0;
9886 char *ptr, *beg, *end;
9887 int len;
9888
9889 CHECK_HTTP_MESSAGE_FIRST();
9890
9891 ctx.idx = 0;
9892 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9893 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9894 ptr = ctx.line + ctx.val;
9895 len = ctx.vlen;
9896 while (len--)
9897 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9898 }
9899
9900 /* now retrieve the path */
9901 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9902 beg = http_get_path(txn);
9903 if (!beg)
9904 beg = end;
9905
9906 for (ptr = beg; ptr < end ; ptr++);
9907
9908 if (beg < ptr && *beg == '/') {
9909 while (beg < ptr)
9910 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9911 }
9912 hash = full_hash(hash);
9913
9914 smp->type = SMP_T_UINT;
9915 smp->data.uint = hash;
9916 smp->flags = SMP_F_VOL_1ST;
9917 return 1;
9918}
9919
9920/* This concatenates the source address with the 32-bit hash of the Host and
9921 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
9922 * per-url counters. The result is a binary block from 8 to 20 bytes depending
9923 * on the source address length. The URL hash is stored before the address so
9924 * that in environments where IPv6 is insignificant, truncating the output to
9925 * 8 bytes would still work.
9926 */
9927static int
9928smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009929 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009930{
9931 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009932 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009933
Willy Tarreaue155ec22013-11-18 18:33:22 +01009934 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009935 return 0;
9936
9937 temp = get_trash_chunk();
9938 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9939 temp->len += sizeof(smp->data.uint);
9940
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009941 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009942 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009943 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009944 temp->len += 4;
9945 break;
9946 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009947 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009948 temp->len += 16;
9949 break;
9950 default:
9951 return 0;
9952 }
9953
9954 smp->data.str = *temp;
9955 smp->type = SMP_T_BIN;
9956 return 1;
9957}
9958
Willy Tarreau185b5c42012-04-26 15:11:51 +02009959/* This function is used to validate the arguments passed to any "hdr" fetch
9960 * keyword. These keywords support an optional positive or negative occurrence
9961 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9962 * is assumed that the types are already the correct ones. Returns 0 on error,
9963 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9964 * error message in case of error, that the caller is responsible for freeing.
9965 * The initial location must either be freeable or NULL.
9966 */
9967static int val_hdr(struct arg *arg, char **err_msg)
9968{
9969 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009970 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009971 return 0;
9972 }
9973 return 1;
9974}
9975
Willy Tarreau276fae92013-07-25 14:36:01 +02009976/* takes an UINT value on input supposed to represent the time since EPOCH,
9977 * adds an optional offset found in args[0] and emits a string representing
9978 * the date in RFC-1123/5322 format.
9979 */
9980static int sample_conv_http_date(const struct arg *args, struct sample *smp)
9981{
9982 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
9983 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
9984 struct chunk *temp;
9985 struct tm *tm;
9986 time_t curr_date = smp->data.uint;
9987
9988 /* add offset */
9989 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
9990 curr_date += args[0].data.sint;
9991
9992 tm = gmtime(&curr_date);
9993
9994 temp = get_trash_chunk();
9995 temp->len = snprintf(temp->str, temp->size - temp->len,
9996 "%s, %02d %s %04d %02d:%02d:%02d GMT",
9997 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
9998 tm->tm_hour, tm->tm_min, tm->tm_sec);
9999
10000 smp->data.str = *temp;
10001 smp->type = SMP_T_STR;
10002 return 1;
10003}
10004
Willy Tarreau4a568972010-05-12 08:08:50 +020010005/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010006/* All supported ACL keywords must be declared here. */
10007/************************************************************************/
10008
10009/* Note: must not be declared <const> as its list will be overwritten.
10010 * Please take care of keeping this list alphabetically sorted.
10011 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010012static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010013 { "base", "base", pat_parse_str, pat_match_str },
10014 { "base_beg", "base", pat_parse_str, pat_match_beg },
10015 { "base_dir", "base", pat_parse_str, pat_match_dir },
10016 { "base_dom", "base", pat_parse_str, pat_match_dom },
10017 { "base_end", "base", pat_parse_str, pat_match_end },
10018 { "base_len", "base", pat_parse_int, pat_match_len },
10019 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10020 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010021
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010022 { "cook", "req.cook", pat_parse_str, pat_match_str },
10023 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10024 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10025 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10026 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10027 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10028 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10029 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010030
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010031 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10032 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10033 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10034 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10035 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10036 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10037 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10038 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010039
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010040 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010041
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010042 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010043
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010044 { "path", "path", pat_parse_str, pat_match_str },
10045 { "path_beg", "path", pat_parse_str, pat_match_beg },
10046 { "path_dir", "path", pat_parse_str, pat_match_dir },
10047 { "path_dom", "path", pat_parse_str, pat_match_dom },
10048 { "path_end", "path", pat_parse_str, pat_match_end },
10049 { "path_len", "path", pat_parse_int, pat_match_len },
10050 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10051 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010052
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010053 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10054 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010055
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010056 { "scook", "res.cook", pat_parse_str, pat_match_str },
10057 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10058 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10059 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10060 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10061 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10062 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10063 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010064
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010065 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10066 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10067 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10068 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10069 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10070 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10071 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10072 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010073
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010074 { "url", "url", pat_parse_str, pat_match_str },
10075 { "url_beg", "url", pat_parse_str, pat_match_beg },
10076 { "url_dir", "url", pat_parse_str, pat_match_dir },
10077 { "url_dom", "url", pat_parse_str, pat_match_dom },
10078 { "url_end", "url", pat_parse_str, pat_match_end },
10079 { "url_len", "url", pat_parse_int, pat_match_len },
10080 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10081 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010082
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010083 { "urlp", "urlp", pat_parse_str, pat_match_str },
10084 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10085 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10086 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10087 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10088 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10089 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10090 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010091
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010092 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010093}};
10094
10095/************************************************************************/
10096/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010097/************************************************************************/
10098/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010099static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010100 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10101 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10102 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10103
10104 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10105 * are only here to match the ACL's name, are request-only and are used
10106 * for ACL compatibility only.
10107 */
10108 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10109 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10110 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10111 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10112
10113 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10114 * only here to match the ACL's name, are request-only and are used for
10115 * ACL compatibility only.
10116 */
10117 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10118 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10119 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10120 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10121
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010122 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10123 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010124 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10125 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10126 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010127
10128 /* HTTP protocol on the request path */
10129 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010130 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010131
10132 /* HTTP version on the request path */
10133 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010134 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010135
10136 /* HTTP version on the response path */
10137 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010138 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10139
Willy Tarreau18ed2562013-01-14 15:56:36 +010010140 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10141 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10142 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10143 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10144
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010145 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10146 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010147 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10148 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10149 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10150 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10151
10152 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10153 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10154 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10155 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10156
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010157 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10158 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010159 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10160 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10161 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10162 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10163
Willy Tarreau409bcde2013-01-08 00:31:00 +010010164 /* scook is valid only on the response and is used for ACL compatibility */
10165 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10166 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10167 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10168 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10169
10170 /* shdr is valid only on the response and is used for ACL compatibility */
10171 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10172 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10173 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10174 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10175
10176 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10177 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010178 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10179 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010180 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10181 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10182 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10183 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10184 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10185 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010186}};
10187
Willy Tarreau8797c062007-05-07 00:55:35 +020010188
Willy Tarreau276fae92013-07-25 14:36:01 +020010189/* Note: must not be declared <const> as its list will be overwritten */
10190static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10191 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10192 { NULL, NULL, 0, 0, 0 },
10193}};
10194
Willy Tarreau8797c062007-05-07 00:55:35 +020010195__attribute__((constructor))
10196static void __http_protocol_init(void)
10197{
10198 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010199 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010200 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010201}
10202
10203
Willy Tarreau58f10d72006-12-04 02:26:12 +010010204/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010205 * Local variables:
10206 * c-indent-level: 8
10207 * c-basic-offset: 8
10208 * End:
10209 */