blob: c91b109142b914d1d54d94f99ab3f5cc54f4e8c4 [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 {
297 http_meth_t meth;
298 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 */
770static http_meth_t find_http_meth(const char *str, const int len)
771{
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;
899 si->err_loc = NULL;
900 si->state = SI_ST_CLO;
901
902 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200903 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100904
905 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100906 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100907}
908
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100909/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100910 * that the server side is closed. Note that err_type is actually a
911 * bitmask, where almost only aborts may be cumulated with other
912 * values. We consider that aborted operations are more important
913 * than timeouts or errors due to the fact that nobody else in the
914 * logs might explain incomplete retries. All others should avoid
915 * being cumulated. It should normally not be possible to have multiple
916 * aborts at once, but just in case, the first one in sequence is reported.
917 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100918void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100920 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100921
922 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100923 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200924 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100926 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200927 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100929 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200930 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100931 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100932 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200933 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100935 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200936 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100938 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200939 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100940 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100941 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200942 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100943}
944
Willy Tarreau42250582007-04-01 01:30:43 +0200945extern const char sess_term_cond[8];
946extern const char sess_fin_state[8];
947extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200948struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100949struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100950struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100951
Willy Tarreau117f59e2007-03-04 18:17:17 +0100952/*
953 * Capture headers from message starting at <som> according to header list
954 * <cap_hdr>, and fill the <idx> structure appropriately.
955 */
956void capture_headers(char *som, struct hdr_idx *idx,
957 char **cap, struct cap_hdr *cap_hdr)
958{
959 char *eol, *sol, *col, *sov;
960 int cur_idx;
961 struct cap_hdr *h;
962 int len;
963
964 sol = som + hdr_idx_first_pos(idx);
965 cur_idx = hdr_idx_first_idx(idx);
966
967 while (cur_idx) {
968 eol = sol + idx->v[cur_idx].len;
969
970 col = sol;
971 while (col < eol && *col != ':')
972 col++;
973
974 sov = col + 1;
975 while (sov < eol && http_is_lws[(unsigned char)*sov])
976 sov++;
977
978 for (h = cap_hdr; h; h = h->next) {
979 if ((h->namelen == col - sol) &&
980 (strncasecmp(sol, h->name, h->namelen) == 0)) {
981 if (cap[h->index] == NULL)
982 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200983 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100984
985 if (cap[h->index] == NULL) {
986 Alert("HTTP capture : out of memory.\n");
987 continue;
988 }
989
990 len = eol - sov;
991 if (len > h->len)
992 len = h->len;
993
994 memcpy(cap[h->index], sov, len);
995 cap[h->index][len]=0;
996 }
997 }
998 sol = eol + idx->v[cur_idx].cr + 1;
999 cur_idx = idx->v[cur_idx].next;
1000 }
1001}
1002
1003
Willy Tarreau42250582007-04-01 01:30:43 +02001004/* either we find an LF at <ptr> or we jump to <bad>.
1005 */
1006#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1007
1008/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1009 * otherwise to <http_msg_ood> with <state> set to <st>.
1010 */
1011#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1012 ptr++; \
1013 if (likely(ptr < end)) \
1014 goto good; \
1015 else { \
1016 state = (st); \
1017 goto http_msg_ood; \
1018 } \
1019 } while (0)
1020
1021
Willy Tarreaubaaee002006-06-26 02:48:02 +02001022/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001023 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1025 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1026 * will give undefined results.
1027 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1028 * and that msg->sol points to the beginning of the response.
1029 * If a complete line is found (which implies that at least one CR or LF is
1030 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1031 * returned indicating an incomplete line (which does not mean that parts have
1032 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1033 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1034 * upon next call.
1035 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001036 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001037 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1038 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001039 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001041const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001042 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001043 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001044{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001045 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001046
Willy Tarreau8973c702007-01-21 23:58:29 +01001047 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001048 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001049 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001050 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001051 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1052
1053 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001054 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001055 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1056 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001057 state = HTTP_MSG_ERROR;
1058 break;
1059
Willy Tarreau8973c702007-01-21 23:58:29 +01001060 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001061 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001062 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001063 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 goto http_msg_rpcode;
1065 }
1066 if (likely(HTTP_IS_SPHT(*ptr)))
1067 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1068 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001069 state = HTTP_MSG_ERROR;
1070 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001071
Willy Tarreau8973c702007-01-21 23:58:29 +01001072 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001073 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001074 if (likely(!HTTP_IS_LWS(*ptr)))
1075 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1076
1077 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001078 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001079 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1080 }
1081
1082 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001083 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001084 http_msg_rsp_reason:
1085 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001087 msg->sl.st.r_l = 0;
1088 goto http_msg_rpline_eol;
1089
Willy Tarreau8973c702007-01-21 23:58:29 +01001090 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001091 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001092 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001093 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001094 goto http_msg_rpreason;
1095 }
1096 if (likely(HTTP_IS_SPHT(*ptr)))
1097 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1098 /* so it's a CR/LF, so there is no reason phrase */
1099 goto http_msg_rsp_reason;
1100
Willy Tarreau8973c702007-01-21 23:58:29 +01001101 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001102 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001103 if (likely(!HTTP_IS_CRLF(*ptr)))
1104 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001105 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001106 http_msg_rpline_eol:
1107 /* We have seen the end of line. Note that we do not
1108 * necessarily have the \n yet, but at least we know that we
1109 * have EITHER \r OR \n, otherwise the response would not be
1110 * complete. We can then record the response length and return
1111 * to the caller which will be able to register it.
1112 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001113 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001114 return ptr;
1115
1116#ifdef DEBUG_FULL
1117 default:
1118 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1119 exit(1);
1120#endif
1121 }
1122
1123 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001124 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 if (ret_state)
1126 *ret_state = state;
1127 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001128 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001130}
1131
Willy Tarreau8973c702007-01-21 23:58:29 +01001132/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001133 * This function parses a request line between <ptr> and <end>, starting with
1134 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1135 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1136 * will give undefined results.
1137 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1138 * and that msg->sol points to the beginning of the request.
1139 * If a complete line is found (which implies that at least one CR or LF is
1140 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1141 * returned indicating an incomplete line (which does not mean that parts have
1142 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1143 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1144 * upon next call.
1145 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001146 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1148 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001149 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001151const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001152 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001153 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001154{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001155 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001159 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001160 if (likely(HTTP_IS_TOKEN(*ptr)))
1161 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001162
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1166 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 if (likely(HTTP_IS_CRLF(*ptr))) {
1169 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001170 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001172 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001173 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001174 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001176 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 msg->sl.rq.v_l = 0;
1178 goto http_msg_rqline_eol;
1179 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001180 state = HTTP_MSG_ERROR;
1181 break;
1182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001184 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001186 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 goto http_msg_rquri;
1188 }
1189 if (likely(HTTP_IS_SPHT(*ptr)))
1190 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1191 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1192 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001193
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001195 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001196 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001198
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001200 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001201 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1202 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001203
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001204 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001205 /* non-ASCII chars are forbidden unless option
1206 * accept-invalid-http-request is enabled in the frontend.
1207 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001208 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001209 if (msg->err_pos < -1)
1210 goto invalid_char;
1211 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001212 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001213 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1214 }
1215
1216 if (likely(HTTP_IS_CRLF(*ptr))) {
1217 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1218 goto http_msg_req09_uri_e;
1219 }
1220
1221 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001222 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001223 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001224 state = HTTP_MSG_ERROR;
1225 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001228 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001230 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 goto http_msg_rqver;
1232 }
1233 if (likely(HTTP_IS_SPHT(*ptr)))
1234 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1235 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1236 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001237
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001239 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001240 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001242
1243 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001244 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001245 http_msg_rqline_eol:
1246 /* We have seen the end of line. Note that we do not
1247 * necessarily have the \n yet, but at least we know that we
1248 * have EITHER \r OR \n, otherwise the request would not be
1249 * complete. We can then record the request length and return
1250 * to the caller which will be able to register it.
1251 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001252 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001253 return ptr;
1254 }
1255
1256 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001257 state = HTTP_MSG_ERROR;
1258 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001260#ifdef DEBUG_FULL
1261 default:
1262 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1263 exit(1);
1264#endif
1265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001268 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 if (ret_state)
1270 *ret_state = state;
1271 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001272 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001276/*
1277 * Returns the data from Authorization header. Function may be called more
1278 * than once so data is stored in txn->auth_data. When no header is found
1279 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1280 * searching again for something we are unable to find anyway.
1281 */
1282
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001283char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001284
1285int
1286get_http_auth(struct session *s)
1287{
1288
1289 struct http_txn *txn = &s->txn;
1290 struct chunk auth_method;
1291 struct hdr_ctx ctx;
1292 char *h, *p;
1293 int len;
1294
1295#ifdef DEBUG_AUTH
1296 printf("Auth for session %p: %d\n", s, txn->auth.method);
1297#endif
1298
1299 if (txn->auth.method == HTTP_AUTH_WRONG)
1300 return 0;
1301
1302 if (txn->auth.method)
1303 return 1;
1304
1305 txn->auth.method = HTTP_AUTH_WRONG;
1306
1307 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001308
1309 if (txn->flags & TX_USE_PX_CONN) {
1310 h = "Proxy-Authorization";
1311 len = strlen(h);
1312 } else {
1313 h = "Authorization";
1314 len = strlen(h);
1315 }
1316
Willy Tarreau9b28e032012-10-12 23:49:43 +02001317 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001318 return 0;
1319
1320 h = ctx.line + ctx.val;
1321
1322 p = memchr(h, ' ', ctx.vlen);
1323 if (!p || p == h)
1324 return 0;
1325
1326 chunk_initlen(&auth_method, h, 0, p-h);
1327 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1328
1329 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1330
1331 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001332 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001333
1334 if (len < 0)
1335 return 0;
1336
1337
1338 get_http_auth_buff[len] = '\0';
1339
1340 p = strchr(get_http_auth_buff, ':');
1341
1342 if (!p)
1343 return 0;
1344
1345 txn->auth.user = get_http_auth_buff;
1346 *p = '\0';
1347 txn->auth.pass = p+1;
1348
1349 txn->auth.method = HTTP_AUTH_BASIC;
1350 return 1;
1351 }
1352
1353 return 0;
1354}
1355
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8973c702007-01-21 23:58:29 +01001357/*
1358 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001359 * depending on the initial msg->msg_state. The caller is responsible for
1360 * ensuring that the message does not wrap. The function can be preempted
1361 * everywhere when data are missing and recalled at the exact same location
1362 * with no information loss. The message may even be realigned between two
1363 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001364 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001365 * fields. Note that msg->sol will be initialized after completing the first
1366 * state, so that none of the msg pointers has to be initialized prior to the
1367 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001369void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001371 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001373 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001374
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001375 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001376 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 ptr = buf->p + msg->next;
1378 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 if (unlikely(ptr >= end))
1381 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001382
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001384 /*
1385 * First, states that are specific to the response only.
1386 * We check them first so that request and headers are
1387 * closer to each other (accessed more often).
1388 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001390 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001392 /* we have a start of message, but we have to check
1393 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001394 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001395 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001396 if (unlikely(ptr != buf->p)) {
1397 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001398 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001399 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001400 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 }
Willy Tarreau26927362012-05-18 23:22:52 +02001402 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001403 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001404 hdr_idx_init(idx);
1405 state = HTTP_MSG_RPVER;
1406 goto http_msg_rpver;
1407 }
1408
1409 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1410 goto http_msg_invalid;
1411
1412 if (unlikely(*ptr == '\n'))
1413 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1414 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1415 /* stop here */
1416
Willy Tarreau8973c702007-01-21 23:58:29 +01001417 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001418 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 EXPECT_LF_HERE(ptr, http_msg_invalid);
1420 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1421 /* stop here */
1422
Willy Tarreau8973c702007-01-21 23:58:29 +01001423 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001424 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001425 case HTTP_MSG_RPVER_SP:
1426 case HTTP_MSG_RPCODE:
1427 case HTTP_MSG_RPCODE_SP:
1428 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001429 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001430 state, ptr, end,
1431 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001432 if (unlikely(!ptr))
1433 return;
1434
1435 /* we have a full response and we know that we have either a CR
1436 * or an LF at <ptr>.
1437 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1439
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001440 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 if (likely(*ptr == '\r'))
1442 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1443 goto http_msg_rpline_end;
1444
Willy Tarreau8973c702007-01-21 23:58:29 +01001445 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001446 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 /* msg->sol must point to the first of CR or LF. */
1448 EXPECT_LF_HERE(ptr, http_msg_invalid);
1449 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1450 /* stop here */
1451
1452 /*
1453 * Second, states that are specific to the request only
1454 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001458 /* we have a start of message, but we have to check
1459 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001460 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001461 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001462 if (likely(ptr != buf->p)) {
1463 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001464 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001465 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 }
Willy Tarreau26927362012-05-18 23:22:52 +02001468 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001469 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 state = HTTP_MSG_RQMETH;
1471 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1475 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (unlikely(*ptr == '\n'))
1478 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1479 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001480 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001489 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_RQMETH_SP:
1491 case HTTP_MSG_RQURI:
1492 case HTTP_MSG_RQURI_SP:
1493 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001494 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001495 state, ptr, end,
1496 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (unlikely(!ptr))
1498 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 /* we have a full request and we know that we have either a CR
1501 * or an LF at <ptr>.
1502 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001504
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001505 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 if (likely(*ptr == '\r'))
1507 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001511 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 /* check for HTTP/0.9 request : no version information available.
1513 * msg->sol must point to the first of CR or LF.
1514 */
1515 if (unlikely(msg->sl.rq.v_l == 0))
1516 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 EXPECT_LF_HERE(ptr, http_msg_invalid);
1519 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8973c702007-01-21 23:58:29 +01001522 /*
1523 * Common states below
1524 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001527 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (likely(!HTTP_IS_CRLF(*ptr))) {
1529 goto http_msg_hdr_name;
1530 }
1531
1532 if (likely(*ptr == '\r'))
1533 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1534 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001537 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 /* assumes msg->sol points to the first char */
1539 if (likely(HTTP_IS_TOKEN(*ptr)))
1540 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001541
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001542 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001545 if (likely(msg->err_pos < -1) || *ptr == '\n')
1546 goto http_msg_invalid;
1547
1548 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001549 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001550
1551 /* and we still accept this non-token character */
1552 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001555 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001556 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 if (likely(HTTP_IS_SPHT(*ptr)))
1558 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001561 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 if (likely(!HTTP_IS_CRLF(*ptr))) {
1564 goto http_msg_hdr_val;
1565 }
1566
1567 if (likely(*ptr == '\r'))
1568 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1569 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001572 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 EXPECT_LF_HERE(ptr, http_msg_invalid);
1574 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001577 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 if (likely(HTTP_IS_SPHT(*ptr))) {
1579 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001580 for (; buf->p + msg->sov < ptr; msg->sov++)
1581 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 goto http_msg_hdr_l1_sp;
1583 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001584 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001585 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 goto http_msg_complete_header;
1587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001589 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001590 /* assumes msg->sol points to the first char, and msg->sov
1591 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 */
1593 if (likely(!HTTP_IS_CRLF(*ptr)))
1594 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001595
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001596 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 /* Note: we could also copy eol into ->eoh so that we have the
1598 * real header end in case it ends with lots of LWS, but is this
1599 * really needed ?
1600 */
1601 if (likely(*ptr == '\r'))
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1603 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001606 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 EXPECT_LF_HERE(ptr, http_msg_invalid);
1608 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001611 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1613 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001614 for (; buf->p + msg->eol < ptr; msg->eol++)
1615 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 goto http_msg_hdr_val;
1617 }
1618 http_msg_complete_header:
1619 /*
1620 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001621 * Assumes msg->sol points to the first char, msg->sov points
1622 * to the first character of the value and msg->eol to the
1623 * first CR or LF so we know how the line ends. We insert last
1624 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001626 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 idx, idx->tail) < 0))
1628 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001629
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 if (likely(!HTTP_IS_CRLF(*ptr))) {
1632 goto http_msg_hdr_name;
1633 }
1634
1635 if (likely(*ptr == '\r'))
1636 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1637 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001640 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 /* Assumes msg->sol points to the first of either CR or LF */
1642 EXPECT_LF_HERE(ptr, http_msg_invalid);
1643 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001644 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001645 msg->eoh = msg->sol;
1646 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001647 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001649
1650 case HTTP_MSG_ERROR:
1651 /* this may only happen if we call http_msg_analyser() twice with an error */
1652 break;
1653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654#ifdef DEBUG_FULL
1655 default:
1656 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1657 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001658#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 }
1660 http_msg_ood:
1661 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001662 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001663 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 http_msg_invalid:
1667 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001668 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001669 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 return;
1671}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001672
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001673/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1674 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1675 * nothing is done and 1 is returned.
1676 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001677static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001678{
1679 int delta;
1680 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001681 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001682
1683 if (msg->sl.rq.v_l != 0)
1684 return 1;
1685
Willy Tarreau9b28e032012-10-12 23:49:43 +02001686 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001687 delta = 0;
1688
1689 if (msg->sl.rq.u_l == 0) {
1690 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001691 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001692 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001693 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001694 }
1695 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001696 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001697 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001698 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001699 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001700 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001701 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001702 NULL, NULL);
1703 if (unlikely(!cur_end))
1704 return 0;
1705
1706 /* we have a full HTTP/1.0 request now and we know that
1707 * we have either a CR or an LF at <ptr>.
1708 */
1709 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1710 return 1;
1711}
1712
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001714 * and "keep-alive" values. If we already know that some headers may safely
1715 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001716 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1717 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001718 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001719 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1720 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1721 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001722 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001723 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001724void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001725{
Willy Tarreau5b154472009-12-21 20:11:07 +01001726 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001727 const char *hdr_val = "Connection";
1728 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001729
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001730 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001731 return;
1732
Willy Tarreau88d349d2010-01-25 12:15:43 +01001733 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1734 hdr_val = "Proxy-Connection";
1735 hdr_len = 16;
1736 }
1737
Willy Tarreau5b154472009-12-21 20:11:07 +01001738 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001739 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001740 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001741 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1742 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001743 if (to_del & 2)
1744 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001745 else
1746 txn->flags |= TX_CON_KAL_SET;
1747 }
1748 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1749 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001750 if (to_del & 1)
1751 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001752 else
1753 txn->flags |= TX_CON_CLO_SET;
1754 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001755 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1756 txn->flags |= TX_HDR_CONN_UPG;
1757 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001758 }
1759
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001760 txn->flags |= TX_HDR_CONN_PRS;
1761 return;
1762}
Willy Tarreau5b154472009-12-21 20:11:07 +01001763
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001764/* Apply desired changes on the Connection: header. Values may be removed and/or
1765 * added depending on the <wanted> flags, which are exclusively composed of
1766 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1767 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1768 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001769void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001770{
1771 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001772 const char *hdr_val = "Connection";
1773 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001774
1775 ctx.idx = 0;
1776
Willy Tarreau88d349d2010-01-25 12:15:43 +01001777
1778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1779 hdr_val = "Proxy-Connection";
1780 hdr_len = 16;
1781 }
1782
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001783 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001784 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001785 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1786 if (wanted & TX_CON_KAL_SET)
1787 txn->flags |= TX_CON_KAL_SET;
1788 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001789 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001790 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001791 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1792 if (wanted & TX_CON_CLO_SET)
1793 txn->flags |= TX_CON_CLO_SET;
1794 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001795 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001796 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001797 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001798
1799 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1800 return;
1801
1802 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1803 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001804 hdr_val = "Connection: close";
1805 hdr_len = 17;
1806 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1807 hdr_val = "Proxy-Connection: close";
1808 hdr_len = 23;
1809 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001810 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001811 }
1812
1813 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1814 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001815 hdr_val = "Connection: keep-alive";
1816 hdr_len = 22;
1817 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1818 hdr_val = "Proxy-Connection: keep-alive";
1819 hdr_len = 28;
1820 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001821 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001822 }
1823 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001824}
1825
Willy Tarreaua458b672012-03-05 11:17:50 +01001826/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001828 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001830 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001831 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001832static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001833{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001834 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001835 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001836 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001837 const char *end = buf->data + buf->size;
1838 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001839 unsigned int chunk = 0;
1840
1841 /* The chunk size is in the following form, though we are only
1842 * interested in the size and CRLF :
1843 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1844 */
1845 while (1) {
1846 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001847 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001848 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001849 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001850 if (c < 0) /* not a hex digit anymore */
1851 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001852 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001853 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001854 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001855 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001856 chunk = (chunk << 4) + c;
1857 }
1858
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001860 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001861 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862
1863 while (http_is_spht[(unsigned char)*ptr]) {
1864 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001865 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001866 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001868 }
1869
Willy Tarreaud98cf932009-12-27 22:54:55 +01001870 /* Up to there, we know that at least one byte is present at *ptr. Check
1871 * for the end of chunk size.
1872 */
1873 while (1) {
1874 if (likely(HTTP_IS_CRLF(*ptr))) {
1875 /* we now have a CR or an LF at ptr */
1876 if (likely(*ptr == '\r')) {
1877 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001878 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001879 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001880 return 0;
1881 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001882
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001884 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001886 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001887 /* done */
1888 break;
1889 }
1890 else if (*ptr == ';') {
1891 /* chunk extension, ends at next CRLF */
1892 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001893 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001894 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001895 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896
1897 while (!HTTP_IS_CRLF(*ptr)) {
1898 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001899 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001900 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901 return 0;
1902 }
1903 /* we have a CRLF now, loop above */
1904 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001905 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001907 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001908 }
1909
Willy Tarreaud98cf932009-12-27 22:54:55 +01001910 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001911 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001914 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001916 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001917 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001918 msg->chunk_len = chunk;
1919 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001922 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001923 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001924 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001925}
1926
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001928 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929 * the trailers is found, it is automatically scheduled to be forwarded,
1930 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1931 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001932 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001933 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001934 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001935 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1936 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001937 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001938 * matches the length of trailers already parsed and not forwarded. It is also
1939 * important to note that this function is designed to be able to parse wrapped
1940 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001942static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001944 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001945
Willy Tarreaua458b672012-03-05 11:17:50 +01001946 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001948 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001949 const char *ptr = b_ptr(buf, msg->next);
1950 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001951 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952
1953 /* scan current line and stop at LF or CRLF */
1954 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001955 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956 return 0;
1957
1958 if (*ptr == '\n') {
1959 if (!p1)
1960 p1 = ptr;
1961 p2 = ptr;
1962 break;
1963 }
1964
1965 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001966 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001967 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001968 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001969 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 p1 = ptr;
1971 }
1972
1973 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001974 if (ptr >= buf->data + buf->size)
1975 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 }
1977
1978 /* after LF; point to beginning of next line */
1979 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001980 if (p2 >= buf->data + buf->size)
1981 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001982
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001983 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001984 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001986
1987 /* schedule this line for forwarding */
1988 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001989 if (msg->sov >= buf->size)
1990 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001991
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001993 /* LF/CRLF at beginning of line => end of trailers at p2.
1994 * Everything was scheduled for forwarding, there's nothing
1995 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001996 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001997 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 msg->msg_state = HTTP_MSG_DONE;
1999 return 1;
2000 }
2001 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003 }
2004}
2005
Willy Tarreau54d23df2012-10-25 19:04:45 +02002006/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002007 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002008 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002009 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002010 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2011 * not enough data are available, the function does not change anything and
2012 * returns zero. If a parse error is encountered, the function returns < 0 and
2013 * does not change anything. Note: this function is designed to parse wrapped
2014 * CRLF at the end of the buffer.
2015 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002016static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002018 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002019 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 int bytes;
2021
2022 /* NB: we'll check data availabilty at the end. It's not a
2023 * problem because whatever we match first will be checked
2024 * against the correct length.
2025 */
2026 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002027 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002028 if (*ptr == '\r') {
2029 bytes++;
2030 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002031 if (ptr >= buf->data + buf->size)
2032 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 }
2034
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002035 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002036 return 0;
2037
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002038 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002041 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002044 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002045 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002046 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2047 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002048 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002049 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2050 return 1;
2051}
Willy Tarreau5b154472009-12-21 20:11:07 +01002052
William Lallemand82fe75c2012-10-23 10:25:10 +02002053
2054/*
2055 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002056 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002057int select_compression_request_header(struct session *s, struct buffer *req)
2058{
2059 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002060 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002061 struct hdr_ctx ctx;
2062 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002063 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002064
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002065 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2066 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002067 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2068 */
2069 ctx.idx = 0;
2070 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2071 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002072 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2073 (ctx.vlen < 31 ||
2074 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2075 ctx.line[ctx.val + 30] < '6' ||
2076 (ctx.line[ctx.val + 30] == '6' &&
2077 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2078 s->comp_algo = NULL;
2079 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002080 }
2081
William Lallemand82fe75c2012-10-23 10:25:10 +02002082 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002083 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 +02002084 ctx.idx = 0;
2085 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002086 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002087 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2088 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002089
2090 /* remove all occurrences of the header when "compression offload" is set */
2091
2092 if ((s->be->comp && s->be->comp->offload) ||
2093 (s->fe->comp && s->fe->comp->offload)) {
2094 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2095 ctx.idx = 0;
2096 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2097 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2098 }
2099 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002100 return 1;
2101 }
2102 }
2103 }
2104 }
2105
2106 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002107 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2108 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002109 if (comp_algo->add_data == identity_add_data) {
2110 s->comp_algo = comp_algo;
2111 return 1;
2112 }
2113 }
2114 }
2115
2116 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002117 return 0;
2118}
2119
2120/*
2121 * Selects a comression algorithm depending of the server response.
2122 */
2123int select_compression_response_header(struct session *s, struct buffer *res)
2124{
2125 struct http_txn *txn = &s->txn;
2126 struct http_msg *msg = &txn->rsp;
2127 struct hdr_ctx ctx;
2128 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002129
2130 /* no common compression algorithm was found in request header */
2131 if (s->comp_algo == NULL)
2132 goto fail;
2133
2134 /* HTTP < 1.1 should not be compressed */
2135 if (!(msg->flags & HTTP_MSGF_VER_11))
2136 goto fail;
2137
William Lallemandd3002612012-11-26 14:34:47 +01002138 /* 200 only */
2139 if (txn->status != 200)
2140 goto fail;
2141
William Lallemand82fe75c2012-10-23 10:25:10 +02002142 /* Content-Length is null */
2143 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2144 goto fail;
2145
Willy Tarreau667c2a32013-04-09 08:13:58 +02002146 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2147 if (msg->flags & HTTP_MSGF_TE_CHNK)
2148 goto fail;
2149
William Lallemand82fe75c2012-10-23 10:25:10 +02002150 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002151 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002152 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2153 goto fail;
2154
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002155 /* no compression when Cache-Control: no-transform is present in the message */
2156 ctx.idx = 0;
2157 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2158 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2159 goto fail;
2160 }
2161
William Lallemand82fe75c2012-10-23 10:25:10 +02002162 comp_type = NULL;
2163
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002164 /* we don't want to compress multipart content-types, nor content-types that are
2165 * not listed in the "compression type" directive if any. If no content-type was
2166 * found but configuration requires one, we don't compress either. Backend has
2167 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002168 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002169 ctx.idx = 0;
2170 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2171 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2172 goto fail;
2173
2174 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2175 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002176 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002177 if (ctx.vlen >= comp_type->name_len &&
2178 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 /* this Content-Type should be compressed */
2180 break;
2181 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002182 /* this Content-Type should not be compressed */
2183 if (comp_type == NULL)
2184 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002185 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002186 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002187 else { /* no content-type header */
2188 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2189 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002190 }
2191
William Lallemandd85f9172012-11-09 17:05:39 +01002192 /* limit compression rate */
2193 if (global.comp_rate_lim > 0)
2194 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2195 goto fail;
2196
William Lallemand072a2bf2012-11-20 17:01:01 +01002197 /* limit cpu usage */
2198 if (idle_pct < compress_min_idle)
2199 goto fail;
2200
William Lallemand4c49fae2012-11-07 15:00:23 +01002201 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002202 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002203 goto fail;
2204
William Lallemandec3e3892012-11-12 17:02:18 +01002205 s->flags |= SN_COMP_READY;
2206
William Lallemand82fe75c2012-10-23 10:25:10 +02002207 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002208 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002209 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2210 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2211
2212 /* add Transfer-Encoding header */
2213 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2214 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2215
2216 /*
2217 * Add Content-Encoding header when it's not identity encoding.
2218 * RFC 2616 : Identity encoding: This content-coding is used only in the
2219 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2220 * header.
2221 */
2222 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002223 trash.len = 18;
2224 memcpy(trash.str, "Content-Encoding: ", trash.len);
2225 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2226 trash.len += s->comp_algo->name_len;
2227 trash.str[trash.len] = '\0';
2228 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002230 return 1;
2231
2232fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002233 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002234 return 0;
2235}
2236
2237
Willy Tarreaud787e662009-07-07 10:14:51 +02002238/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2239 * processing can continue on next analysers, or zero if it either needs more
2240 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2241 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2242 * when it has nothing left to do, and may remove any analyser when it wants to
2243 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002244 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002245int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002246{
Willy Tarreau59234e92008-11-30 23:51:27 +01002247 /*
2248 * We will parse the partial (or complete) lines.
2249 * We will check the request syntax, and also join multi-line
2250 * headers. An index of all the lines will be elaborated while
2251 * parsing.
2252 *
2253 * For the parsing, we use a 28 states FSM.
2254 *
2255 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002256 * req->buf->p = beginning of request
2257 * req->buf->p + msg->eoh = end of processed headers / start of current one
2258 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002259 * msg->eol = end of current header or line (LF or CRLF)
2260 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002261 *
2262 * At end of parsing, we may perform a capture of the error (if any), and
2263 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2264 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2265 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002266 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002267
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002269 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002270 struct http_txn *txn = &s->txn;
2271 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002272 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002273
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002274 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 +01002275 now_ms, __FUNCTION__,
2276 s,
2277 req,
2278 req->rex, req->wex,
2279 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002280 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002281 req->analysers);
2282
Willy Tarreau52a0c602009-08-16 22:45:38 +02002283 /* we're speaking HTTP here, so let's speak HTTP to the client */
2284 s->srv_error = http_return_srv_error;
2285
Willy Tarreau83e3af02009-12-28 17:39:57 +01002286 /* There's a protected area at the end of the buffer for rewriting
2287 * purposes. We don't want to start to parse the request if the
2288 * protected area is affected, because we may have to move processed
2289 * data later, which is much more complicated.
2290 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002291 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002292 if (txn->flags & TX_NOT_FIRST) {
2293 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002294 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002295 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002296 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002297 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002298 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002299 return 0;
2300 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002301 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2302 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2303 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002304 }
2305
Willy Tarreau065e8332010-01-08 00:30:20 +01002306 /* Note that we have the same problem with the response ; we
2307 * may want to send a redirect, error or anything which requires
2308 * some spare space. So we'll ensure that we have at least
2309 * maxrewrite bytes available in the response buffer before
2310 * processing that one. This will only affect pipelined
2311 * keep-alive requests.
2312 */
2313 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002314 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002315 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2316 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2317 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002318 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002319 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002320 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002321 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002322 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002323 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002324 return 0;
2325 }
2326 }
2327
Willy Tarreau9b28e032012-10-12 23:49:43 +02002328 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002329 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002330 }
2331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /* 1: we might have to print this header in debug mode */
2333 if (unlikely((global.mode & MODE_DEBUG) &&
2334 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002335 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002337
Willy Tarreau9b28e032012-10-12 23:49:43 +02002338 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002339 /* this is a bit complex : in case of error on the request line,
2340 * we know that rq.l is still zero, so we display only the part
2341 * up to the end of the line (truncated by debug_hdr).
2342 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002343 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 sol += hdr_idx_first_pos(&txn->hdr_idx);
2347 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002348
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 while (cur_idx) {
2350 eol = sol + txn->hdr_idx.v[cur_idx].len;
2351 debug_hdr("clihdr", s, sol, eol);
2352 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2353 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002354 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 }
2356
Willy Tarreau58f10d72006-12-04 02:26:12 +01002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 /*
2359 * Now we quickly check if we have found a full valid request.
2360 * If not so, we check the FD and buffer states before leaving.
2361 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002362 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002363 * requests are checked first. When waiting for a second request
2364 * on a keep-alive session, if we encounter and error, close, t/o,
2365 * we note the error in the session flags but don't set any state.
2366 * Since the error will be noted there, it will not be counted by
2367 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002368 * Last, we may increase some tracked counters' http request errors on
2369 * the cases that are deliberately the client's fault. For instance,
2370 * a timeout or connection reset is not counted as an error. However
2371 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002372 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002373
Willy Tarreau655dce92009-11-08 13:10:58 +01002374 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002375 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002377 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002378 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002379 session_inc_http_req_ctr(s);
2380 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002381 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002383 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 /* 1: Since we are in header mode, if there's no space
2386 * left for headers, we won't be able to free more
2387 * later, so the session will never terminate. We
2388 * must terminate it now.
2389 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002390 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 /* FIXME: check if URI is set and return Status
2392 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002393 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002394 session_inc_http_req_ctr(s);
2395 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002396 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002397 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002398 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002399 goto return_bad_req;
2400 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002401
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002403 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002404 if (!(s->flags & SN_ERR_MASK))
2405 s->flags |= SN_ERR_CLICL;
2406
Willy Tarreaufcffa692010-01-10 14:21:19 +01002407 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002408 goto failed_keep_alive;
2409
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002411 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002412 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002413 session_inc_http_err_ctr(s);
2414 }
2415
Willy Tarreaudc979f22012-12-04 10:39:01 +01002416 txn->status = 400;
2417 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002418 msg->msg_state = HTTP_MSG_ERROR;
2419 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002420
Willy Tarreauda7ff642010-06-23 11:44:09 +02002421 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002422 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002423 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002424 if (s->listener->counters)
2425 s->listener->counters->failed_req++;
2426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 if (!(s->flags & SN_FINST_MASK))
2428 s->flags |= SN_FINST_R;
2429 return 0;
2430 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002433 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002434 if (!(s->flags & SN_ERR_MASK))
2435 s->flags |= SN_ERR_CLITO;
2436
Willy Tarreaufcffa692010-01-10 14:21:19 +01002437 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002438 goto failed_keep_alive;
2439
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002441 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002442 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002443 session_inc_http_err_ctr(s);
2444 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002446 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002447 msg->msg_state = HTTP_MSG_ERROR;
2448 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002449
Willy Tarreauda7ff642010-06-23 11:44:09 +02002450 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002451 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002452 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002453 if (s->listener->counters)
2454 s->listener->counters->failed_req++;
2455
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 if (!(s->flags & SN_FINST_MASK))
2457 s->flags |= SN_FINST_R;
2458 return 0;
2459 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002460
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002462 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002463 if (!(s->flags & SN_ERR_MASK))
2464 s->flags |= SN_ERR_CLICL;
2465
Willy Tarreaufcffa692010-01-10 14:21:19 +01002466 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002467 goto failed_keep_alive;
2468
Willy Tarreau4076a152009-04-02 15:18:36 +02002469 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002470 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002472 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 msg->msg_state = HTTP_MSG_ERROR;
2474 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002475
Willy Tarreauda7ff642010-06-23 11:44:09 +02002476 session_inc_http_err_ctr(s);
2477 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002478 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002479 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480 if (s->listener->counters)
2481 s->listener->counters->failed_req++;
2482
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 if (!(s->flags & SN_FINST_MASK))
2484 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002485 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002486 }
2487
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002488 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002489 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2490 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002491#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002493 /* We need more data, we have to re-enable quick-ack in case we
2494 * previously disabled it, otherwise we might cause the client
2495 * to delay next data.
2496 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002497 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002498 }
2499#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002500
Willy Tarreaufcffa692010-01-10 14:21:19 +01002501 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2502 /* If the client starts to talk, let's fall back to
2503 * request timeout processing.
2504 */
2505 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002506 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002507 }
2508
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002510 if (!tick_isset(req->analyse_exp)) {
2511 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2512 (txn->flags & TX_WAIT_NEXT_RQ) &&
2513 tick_isset(s->be->timeout.httpka))
2514 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2515 else
2516 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2517 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* we're not ready yet */
2520 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002521
2522 failed_keep_alive:
2523 /* Here we process low-level errors for keep-alive requests. In
2524 * short, if the request is not the first one and it experiences
2525 * a timeout, read error or shutdown, we just silently close so
2526 * that the client can try again.
2527 */
2528 txn->status = 0;
2529 msg->msg_state = HTTP_MSG_RQBEFORE;
2530 req->analysers = 0;
2531 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002532 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002533 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002534 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002535 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002537
Willy Tarreaud787e662009-07-07 10:14:51 +02002538 /* OK now we have a complete HTTP request with indexed headers. Let's
2539 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002540 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002541 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002542 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002543 * byte after the last LF. msg->sov points to the first byte of data.
2544 * msg->eol cannot be trusted because it may have been left uninitialized
2545 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002546 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002547
Willy Tarreauda7ff642010-06-23 11:44:09 +02002548 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002549 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2550
Willy Tarreaub16a5742010-01-10 14:46:16 +01002551 if (txn->flags & TX_WAIT_NEXT_RQ) {
2552 /* kill the pending keep-alive timeout */
2553 txn->flags &= ~TX_WAIT_NEXT_RQ;
2554 req->analyse_exp = TICK_ETERNITY;
2555 }
2556
2557
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 /* Maybe we found in invalid header name while we were configured not
2559 * to block on that, so we have to capture it now.
2560 */
2561 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002562 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002563
Willy Tarreau59234e92008-11-30 23:51:27 +01002564 /*
2565 * 1: identify the method
2566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002567 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002568
2569 /* we can make use of server redirect on GET and HEAD */
2570 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2571 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002572
Willy Tarreau59234e92008-11-30 23:51:27 +01002573 /*
2574 * 2: check if the URI matches the monitor_uri.
2575 * We have to do this for every request which gets in, because
2576 * the monitor-uri is defined by the frontend.
2577 */
2578 if (unlikely((s->fe->monitor_uri_len != 0) &&
2579 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 s->fe->monitor_uri,
2582 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002583 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002585 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002589 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002590
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002592 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002593 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002594
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 ret = acl_pass(ret);
2596 if (cond->pol == ACL_COND_UNLESS)
2597 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 if (ret) {
2600 /* we fail this request, let's return 503 service unavail */
2601 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002602 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002603 if (!(s->flags & SN_ERR_MASK))
2604 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002606 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002607 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 /* nothing to fail, let's reply normaly */
2610 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002611 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002612 if (!(s->flags & SN_ERR_MASK))
2613 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 goto return_prx_cond;
2615 }
2616
2617 /*
2618 * 3: Maybe we have to copy the original REQURI for the logs ?
2619 * Note: we cannot log anymore if the request has been
2620 * classified as invalid.
2621 */
2622 if (unlikely(s->logs.logwait & LW_REQ)) {
2623 /* we have a complete HTTP request that we must log */
2624 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2625 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 if (urilen >= REQURI_LEN)
2628 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002629 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002632 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 s->do_log(s);
2634 } else {
2635 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002636 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002637 }
Willy Tarreau06619262006-12-17 08:37:22 +01002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002640 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002641 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002642
Willy Tarreau5b154472009-12-21 20:11:07 +01002643 /* ... and check if the request is HTTP/1.1 or above */
2644 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002645 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2646 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2647 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002648 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002649
2650 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002651 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 +01002652
Willy Tarreau88d349d2010-01-25 12:15:43 +01002653 /* if the frontend has "option http-use-proxy-header", we'll check if
2654 * we have what looks like a proxied connection instead of a connection,
2655 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2656 * Note that this is *not* RFC-compliant, however browsers and proxies
2657 * happen to do that despite being non-standard :-(
2658 * We consider that a request not beginning with either '/' or '*' is
2659 * a proxied connection, which covers both "scheme://location" and
2660 * CONNECT ip:port.
2661 */
2662 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002663 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002664 txn->flags |= TX_USE_PX_CONN;
2665
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002666 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002667 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002668
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002670 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002671 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002673
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002674 /* 6: determine the transfer-length.
2675 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2676 * the presence of a message-body in a REQUEST and its transfer length
2677 * must be determined that way (in order of precedence) :
2678 * 1. The presence of a message-body in a request is signaled by the
2679 * inclusion of a Content-Length or Transfer-Encoding header field
2680 * in the request's header fields. When a request message contains
2681 * both a message-body of non-zero length and a method that does
2682 * not define any semantics for that request message-body, then an
2683 * origin server SHOULD either ignore the message-body or respond
2684 * with an appropriate error message (e.g., 413). A proxy or
2685 * gateway, when presented the same request, SHOULD either forward
2686 * the request inbound with the message- body or ignore the
2687 * message-body when determining a response.
2688 *
2689 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2690 * and the "chunked" transfer-coding (Section 6.2) is used, the
2691 * transfer-length is defined by the use of this transfer-coding.
2692 * If a Transfer-Encoding header field is present and the "chunked"
2693 * transfer-coding is not present, the transfer-length is defined
2694 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002695 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002696 * 3. If a Content-Length header field is present, its decimal value in
2697 * OCTETs represents both the entity-length and the transfer-length.
2698 * If a message is received with both a Transfer-Encoding header
2699 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002700 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002701 * 4. By the server closing the connection. (Closing the connection
2702 * cannot be used to indicate the end of a request body, since that
2703 * would leave no possibility for the server to send back a response.)
2704 *
2705 * Whenever a transfer-coding is applied to a message-body, the set of
2706 * transfer-codings MUST include "chunked", unless the message indicates
2707 * it is terminated by closing the connection. When the "chunked"
2708 * transfer-coding is used, it MUST be the last transfer-coding applied
2709 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002710 */
2711
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002712 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002713 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002714 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002715 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002716 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002717 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002718 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2719 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002720 /* bad transfer-encoding (chunked followed by something else) */
2721 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002722 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002723 break;
2724 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002725 }
2726
Willy Tarreau32b47f42009-10-18 20:55:02 +02002727 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002728 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002729 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002730 signed long long cl;
2731
Willy Tarreauad14f752011-09-02 20:33:27 +02002732 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002733 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002734 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002735 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002736
Willy Tarreauad14f752011-09-02 20:33:27 +02002737 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
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; /* parse failure */
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 (cl < 0) {
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;
Willy Tarreauad14f752011-09-02 20:33:27 +02002745 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002747 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
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; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002750 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002752 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002753 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002754 }
2755
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002756 /* bodyless requests have a known length */
2757 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002758 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002759
Willy Tarreaud787e662009-07-07 10:14:51 +02002760 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002761 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002762 req->analyse_exp = TICK_ETERNITY;
2763 return 1;
2764
2765 return_bad_req:
2766 /* We centralize bad requests processing here */
2767 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2768 /* we detected a parsing error. We want to archive this request
2769 * in the dedicated proxy area for later troubleshooting.
2770 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002771 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002772 }
2773
2774 txn->req.msg_state = HTTP_MSG_ERROR;
2775 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002776 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002777
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002778 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002779 if (s->listener->counters)
2780 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002781
2782 return_prx_cond:
2783 if (!(s->flags & SN_ERR_MASK))
2784 s->flags |= SN_ERR_PRXCOND;
2785 if (!(s->flags & SN_FINST_MASK))
2786 s->flags |= SN_FINST_R;
2787
2788 req->analysers = 0;
2789 req->analyse_exp = TICK_ETERNITY;
2790 return 0;
2791}
2792
Cyril Bonté70be45d2010-10-12 00:14:35 +02002793/* We reached the stats page through a POST request.
2794 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002795 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002796 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002797int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002798{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002799 struct proxy *px = NULL;
2800 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002801
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002802 char key[LINESIZE];
2803 int action = ST_ADM_ACTION_NONE;
2804 int reprocess = 0;
2805
2806 int total_servers = 0;
2807 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002808
2809 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002810 char *st_cur_param = NULL;
2811 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002812
Willy Tarreau9b28e032012-10-12 23:49:43 +02002813 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002814 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002815
2816 cur_param = next_param = end_params;
2817
Willy Tarreau9b28e032012-10-12 23:49:43 +02002818 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002819 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002820 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002821 return 1;
2822 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002823 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002824 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002825 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002826 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002827 }
2828
2829 *end_params = '\0';
2830
Willy Tarreau295a8372011-03-10 11:25:07 +01002831 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002832
2833 /*
2834 * Parse the parameters in reverse order to only store the last value.
2835 * From the html form, the backend and the action are at the end.
2836 */
2837 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002838 char *value;
2839 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002840
2841 cur_param--;
2842 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002843 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002844 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002845 poffset = (cur_param != first_param ? 1 : 0);
2846 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2847 if ((plen > 0) && (plen <= sizeof(key))) {
2848 strncpy(key, cur_param + poffset, plen);
2849 key[plen - 1] = '\0';
2850 } else {
2851 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2852 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002853 }
2854
2855 /* Parse the value */
2856 value = key;
2857 while (*value != '\0' && *value != '=') {
2858 value++;
2859 }
2860 if (*value == '=') {
2861 /* Ok, a value is found, we can mark the end of the key */
2862 *value++ = '\0';
2863 }
2864
Thierry FOURNIER5068d962013-10-04 16:27:27 +02002865 if (url_decode(key) < 0 || url_decode(value) < 0)
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002866 break;
2867
Cyril Bonté70be45d2010-10-12 00:14:35 +02002868 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002869 if (!px && (strcmp(key, "b") == 0)) {
2870 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2871 /* the backend name is unknown or ambiguous (duplicate names) */
2872 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2873 goto out;
2874 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002875 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002876 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002878 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002879 }
2880 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002881 action = ST_ADM_ACTION_ENABLE;
2882 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002883 else if (strcmp(value, "stop") == 0) {
2884 action = ST_ADM_ACTION_STOP;
2885 }
2886 else if (strcmp(value, "start") == 0) {
2887 action = ST_ADM_ACTION_START;
2888 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002889 else if (strcmp(value, "shutdown") == 0) {
2890 action = ST_ADM_ACTION_SHUTDOWN;
2891 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002892 else {
2893 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2894 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002895 }
2896 }
2897 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002898 if (!(px && action)) {
2899 /*
2900 * Indicates that we'll need to reprocess the parameters
2901 * as soon as backend and action are known
2902 */
2903 if (!reprocess) {
2904 st_cur_param = cur_param;
2905 st_next_param = next_param;
2906 }
2907 reprocess = 1;
2908 }
2909 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002910 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002911 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002912 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002913 /* Not already in maintenance, we can change the server state */
2914 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09002915 set_server_down(&sv->check);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002916 altered_servers++;
2917 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002918 }
2919 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002920 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002921 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002922 /* Already in maintenance, we can change the server state */
Simon Horman4a741432013-02-23 15:35:38 +09002923 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09002924 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002925 altered_servers++;
2926 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002927 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002928 break;
2929 case ST_ADM_ACTION_STOP:
2930 case ST_ADM_ACTION_START:
2931 if (action == ST_ADM_ACTION_START)
2932 sv->uweight = sv->iweight;
2933 else
2934 sv->uweight = 0;
2935
Willy Tarreau004e0452013-11-21 11:22:01 +01002936 server_recalc_eweight(sv);
Willy Tarreaud7282242012-06-04 00:22:44 +02002937
Willy Tarreaud7282242012-06-04 00:22:44 +02002938 altered_servers++;
2939 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002940 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002941 case ST_ADM_ACTION_SHUTDOWN:
2942 if (px->state != PR_STSTOPPED) {
2943 struct session *sess, *sess_bck;
2944
2945 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2946 if (sess->srv_conn == sv)
2947 session_shutdown(sess, SN_ERR_KILLED);
2948
2949 altered_servers++;
2950 total_servers++;
2951 }
2952 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002953 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002954 } else {
2955 /* the server name is unknown or ambiguous (duplicate names) */
2956 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002957 }
2958 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002959 if (reprocess && px && action) {
2960 /* Now, we know the backend and the action chosen by the user.
2961 * We can safely restart from the first server parameter
2962 * to reprocess them
2963 */
2964 cur_param = st_cur_param;
2965 next_param = st_next_param;
2966 reprocess = 0;
2967 goto reprocess_servers;
2968 }
2969
Cyril Bonté70be45d2010-10-12 00:14:35 +02002970 next_param = cur_param;
2971 }
2972 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002973
2974 if (total_servers == 0) {
2975 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2976 }
2977 else if (altered_servers == 0) {
2978 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2979 }
2980 else if (altered_servers == total_servers) {
2981 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2982 }
2983 else {
2984 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2985 }
2986 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002987 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002988}
2989
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002990/* This function checks whether we need to enable a POST analyser to parse a
2991 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002992 * if it needs to come back again, otherwise non-zero if it finishes. In the
2993 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002994 */
2995int http_handle_stats(struct session *s, struct channel *req)
2996{
2997 struct stats_admin_rule *stats_admin_rule;
2998 struct stream_interface *si = s->rep->prod;
2999 struct http_txn *txn = &s->txn;
3000 struct http_msg *msg = &txn->req;
3001 struct uri_auth *uri = s->be->uri_auth;
3002
3003 /* now check whether we have some admin rules for this request */
3004 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3005 int ret = 1;
3006
3007 if (stats_admin_rule->cond) {
3008 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3009 ret = acl_pass(ret);
3010 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3011 ret = !ret;
3012 }
3013
3014 if (ret) {
3015 /* no rule, or the rule matches */
3016 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
3017 break;
3018 }
3019 }
3020
3021 /* Was the status page requested with a POST ? */
3022 if (unlikely(txn->meth == HTTP_METH_POST)) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003023 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3024
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003025 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
3026 if (msg->msg_state < HTTP_MSG_100_SENT) {
3027 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3028 * send an HTTP/1.1 100 Continue intermediate response.
3029 */
3030 if (msg->flags & HTTP_MSGF_VER_11) {
3031 struct hdr_ctx ctx;
3032 ctx.idx = 0;
3033 /* Expect is allowed in 1.1, look for it */
3034 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3035 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3036 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3037 }
3038 }
3039 msg->msg_state = HTTP_MSG_100_SENT;
3040 s->logs.tv_request = now; /* update the request timer to reflect full request */
3041 }
3042 if (!http_process_req_stat_post(si, txn, req))
3043 return 0; /* we need more data */
3044 }
3045 else
3046 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003047 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3048 scope_txt[0] = 0;
3049 if (si->applet.ctx.stats.scope_len) {
3050 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3051 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(req->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3052 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3053 }
3054
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003055
3056 /* We don't want to land on the posted stats page because a refresh will
3057 * repost the data. We don't want this to happen on accident so we redirect
3058 * the browse to the stats page with a GET.
3059 */
3060 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04003061 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003062 "Cache-Control: no-cache\r\n"
3063 "Content-Type: text/plain\r\n"
3064 "Connection: close\r\n"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003065 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003066 "\r\n",
3067 uri->uri_prefix,
3068 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3069 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3070 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3071 stat_status_codes[si->applet.ctx.stats.st_code] :
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003072 stat_status_codes[STAT_STATUS_UNKN],
3073 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3074 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3075 scope_txt);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003076
3077 s->txn.status = 303;
3078 s->logs.tv_request = now;
3079 stream_int_retnclose(req->prod, &trash);
3080 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3081
3082 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3083 s->fe->fe_counters.intercepted_req++;
3084
3085 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003086 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003087 if (!(s->flags & SN_FINST_MASK))
3088 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003089 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 return 1;
3091 }
3092
3093 /* OK, let's go on now */
3094
3095 chunk_printf(&trash,
3096 "HTTP/1.0 200 OK\r\n"
3097 "Cache-Control: no-cache\r\n"
3098 "Connection: close\r\n"
3099 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003100 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003101
3102 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3103 chunk_appendf(&trash, "Refresh: %d\r\n",
3104 uri->refresh);
3105
3106 chunk_appendf(&trash, "\r\n");
3107
3108 s->txn.status = 200;
3109 s->logs.tv_request = now;
3110
3111 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3112 s->fe->fe_counters.intercepted_req++;
3113
3114 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003115 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003116 if (!(s->flags & SN_FINST_MASK))
3117 s->flags |= SN_FINST_R;
3118
3119 if (s->txn.meth == HTTP_METH_HEAD) {
3120 /* that's all we return in case of HEAD request, so let's immediately close. */
3121 stream_int_retnclose(req->prod, &trash);
3122 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003123 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003124 return 1;
3125 }
3126
3127 /* OK, push the response and hand over to the stats I/O handler */
3128 bi_putchk(s->rep, &trash);
3129
3130 s->task->nice = -32; /* small boost for HTTP statistics */
3131 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3132 s->target = s->rep->prod->conn->target; // for logging only
3133 s->rep->prod->conn->xprt_ctx = s;
3134 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3135 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003136 return 1;
3137}
3138
Lukas Tribus67db8df2013-06-23 17:37:13 +02003139/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3140 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3141 */
3142static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3143{
3144#ifdef IP_TOS
3145 if (from.ss_family == AF_INET)
3146 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3147#endif
3148#ifdef IPV6_TCLASS
3149 if (from.ss_family == AF_INET6) {
3150 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3151 /* v4-mapped addresses need IP_TOS */
3152 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3153 else
3154 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3155 }
3156#endif
3157}
3158
Willy Tarreau20b0de52012-12-24 15:45:22 +01003159/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003160 * transaction <txn>. Returns the first rule that prevents further processing
3161 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3162 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3163 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003164 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003165static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003166http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003167{
Willy Tarreauff011f22011-01-06 17:51:27 +01003168 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003169 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003170
Willy Tarreauff011f22011-01-06 17:51:27 +01003171 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003172 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003173 continue;
3174
Willy Tarreau96257ec2012-12-27 10:46:37 +01003175 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003176 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003177 int ret;
3178
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003179 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003180 ret = acl_pass(ret);
3181
Willy Tarreauff011f22011-01-06 17:51:27 +01003182 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003183 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003184
3185 if (!ret) /* condition not matched */
3186 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003187 }
3188
Willy Tarreau20b0de52012-12-24 15:45:22 +01003189
Willy Tarreau96257ec2012-12-27 10:46:37 +01003190 switch (rule->action) {
3191 case HTTP_REQ_ACT_ALLOW:
3192 return NULL; /* "allow" rules are OK */
3193
3194 case HTTP_REQ_ACT_DENY:
3195 txn->flags |= TX_CLDENY;
3196 return rule;
3197
Willy Tarreauccbcc372012-12-27 12:37:57 +01003198 case HTTP_REQ_ACT_TARPIT:
3199 txn->flags |= TX_CLTARPIT;
3200 return rule;
3201
Willy Tarreau96257ec2012-12-27 10:46:37 +01003202 case HTTP_REQ_ACT_AUTH:
3203 return rule;
3204
Willy Tarreau81499eb2012-12-27 12:19:02 +01003205 case HTTP_REQ_ACT_REDIR:
3206 return rule;
3207
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003208 case HTTP_REQ_ACT_SET_NICE:
3209 s->task->nice = rule->arg.nice;
3210 break;
3211
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003212 case HTTP_REQ_ACT_SET_TOS:
Lukas Tribus67db8df2013-06-23 17:37:13 +02003213 inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003214 break;
3215
Willy Tarreau51347ed2013-06-11 19:34:13 +02003216 case HTTP_REQ_ACT_SET_MARK:
3217#ifdef SO_MARK
3218 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3219#endif
3220 break;
3221
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003222 case HTTP_REQ_ACT_SET_LOGL:
3223 s->logs.level = rule->arg.loglevel;
3224 break;
3225
Willy Tarreau96257ec2012-12-27 10:46:37 +01003226 case HTTP_REQ_ACT_SET_HDR:
3227 ctx.idx = 0;
3228 /* remove all occurrences of the header */
3229 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3230 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3231 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003232 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003233 /* now fall through to header addition */
3234
3235 case HTTP_REQ_ACT_ADD_HDR:
3236 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3237 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3238 trash.len = rule->arg.hdr_add.name_len;
3239 trash.str[trash.len++] = ':';
3240 trash.str[trash.len++] = ' ';
3241 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3242 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3243 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003244 }
3245 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003246
3247 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003248 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003249}
3250
Willy Tarreau71241ab2012-12-27 11:30:54 +01003251
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003252/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3253 * transaction <txn>. Returns the first rule that prevents further processing
3254 * of the response (deny, ...) or NULL if it executed all rules or stopped
3255 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3256 * rule.
3257 */
3258static struct http_res_rule *
3259http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3260{
3261 struct http_res_rule *rule;
3262 struct hdr_ctx ctx;
3263
3264 list_for_each_entry(rule, rules, list) {
3265 if (rule->action >= HTTP_RES_ACT_MAX)
3266 continue;
3267
3268 /* check optional condition */
3269 if (rule->cond) {
3270 int ret;
3271
3272 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3273 ret = acl_pass(ret);
3274
3275 if (rule->cond->pol == ACL_COND_UNLESS)
3276 ret = !ret;
3277
3278 if (!ret) /* condition not matched */
3279 continue;
3280 }
3281
3282
3283 switch (rule->action) {
3284 case HTTP_RES_ACT_ALLOW:
3285 return NULL; /* "allow" rules are OK */
3286
3287 case HTTP_RES_ACT_DENY:
3288 txn->flags |= TX_SVDENY;
3289 return rule;
3290
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003291 case HTTP_RES_ACT_SET_NICE:
3292 s->task->nice = rule->arg.nice;
3293 break;
3294
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003295 case HTTP_RES_ACT_SET_TOS:
Lukas Tribus67db8df2013-06-23 17:37:13 +02003296 inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003297 break;
3298
Willy Tarreau51347ed2013-06-11 19:34:13 +02003299 case HTTP_RES_ACT_SET_MARK:
3300#ifdef SO_MARK
3301 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3302#endif
3303 break;
3304
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003305 case HTTP_RES_ACT_SET_LOGL:
3306 s->logs.level = rule->arg.loglevel;
3307 break;
3308
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003309 case HTTP_RES_ACT_SET_HDR:
3310 ctx.idx = 0;
3311 /* remove all occurrences of the header */
3312 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3313 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3314 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3315 }
3316 /* now fall through to header addition */
3317
3318 case HTTP_RES_ACT_ADD_HDR:
3319 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3320 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3321 trash.len = rule->arg.hdr_add.name_len;
3322 trash.str[trash.len++] = ':';
3323 trash.str[trash.len++] = ' ';
3324 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3325 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3326 break;
3327 }
3328 }
3329
3330 /* we reached the end of the rules, nothing to report */
3331 return NULL;
3332}
3333
3334
Willy Tarreau71241ab2012-12-27 11:30:54 +01003335/* Perform an HTTP redirect based on the information in <rule>. The function
3336 * returns non-zero on success, or zero in case of a, irrecoverable error such
3337 * as too large a request to build a valid response.
3338 */
3339static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3340{
3341 struct http_msg *msg = &txn->req;
3342 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003343 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003344
3345 /* build redirect message */
3346 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003347 case 308:
3348 msg_fmt = HTTP_308;
3349 break;
3350 case 307:
3351 msg_fmt = HTTP_307;
3352 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003353 case 303:
3354 msg_fmt = HTTP_303;
3355 break;
3356 case 301:
3357 msg_fmt = HTTP_301;
3358 break;
3359 case 302:
3360 default:
3361 msg_fmt = HTTP_302;
3362 break;
3363 }
3364
3365 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3366 return 0;
3367
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003368 location = trash.str + trash.len;
3369
Willy Tarreau71241ab2012-12-27 11:30:54 +01003370 switch(rule->type) {
3371 case REDIRECT_TYPE_SCHEME: {
3372 const char *path;
3373 const char *host;
3374 struct hdr_ctx ctx;
3375 int pathlen;
3376 int hostlen;
3377
3378 host = "";
3379 hostlen = 0;
3380 ctx.idx = 0;
3381 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3382 host = ctx.line + ctx.val;
3383 hostlen = ctx.vlen;
3384 }
3385
3386 path = http_get_path(txn);
3387 /* build message using path */
3388 if (path) {
3389 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3390 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3391 int qs = 0;
3392 while (qs < pathlen) {
3393 if (path[qs] == '?') {
3394 pathlen = qs;
3395 break;
3396 }
3397 qs++;
3398 }
3399 }
3400 } else {
3401 path = "/";
3402 pathlen = 1;
3403 }
3404
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003405 if (rule->rdr_str) { /* this is an old "redirect" rule */
3406 /* check if we can add scheme + "://" + host + path */
3407 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3408 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003409
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003410 /* add scheme */
3411 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3412 trash.len += rule->rdr_len;
3413 }
3414 else {
3415 /* add scheme with executing log format */
3416 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003417
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003418 /* check if we can add scheme + "://" + host + path */
3419 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3420 return 0;
3421 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003422 /* add "://" */
3423 memcpy(trash.str + trash.len, "://", 3);
3424 trash.len += 3;
3425
3426 /* add host */
3427 memcpy(trash.str + trash.len, host, hostlen);
3428 trash.len += hostlen;
3429
3430 /* add path */
3431 memcpy(trash.str + trash.len, path, pathlen);
3432 trash.len += pathlen;
3433
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003434 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003435 if (trash.len && trash.str[trash.len - 1] != '/' &&
3436 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3437 if (trash.len > trash.size - 5)
3438 return 0;
3439 trash.str[trash.len] = '/';
3440 trash.len++;
3441 }
3442
3443 break;
3444 }
3445 case REDIRECT_TYPE_PREFIX: {
3446 const char *path;
3447 int pathlen;
3448
3449 path = http_get_path(txn);
3450 /* build message using path */
3451 if (path) {
3452 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3453 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3454 int qs = 0;
3455 while (qs < pathlen) {
3456 if (path[qs] == '?') {
3457 pathlen = qs;
3458 break;
3459 }
3460 qs++;
3461 }
3462 }
3463 } else {
3464 path = "/";
3465 pathlen = 1;
3466 }
3467
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003468 if (rule->rdr_str) { /* this is an old "redirect" rule */
3469 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3470 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003471
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003472 /* add prefix. Note that if prefix == "/", we don't want to
3473 * add anything, otherwise it makes it hard for the user to
3474 * configure a self-redirection.
3475 */
3476 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3477 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3478 trash.len += rule->rdr_len;
3479 }
3480 }
3481 else {
3482 /* add prefix with executing log format */
3483 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3484
3485 /* Check length */
3486 if (trash.len + pathlen > trash.size - 4)
3487 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003488 }
3489
3490 /* add path */
3491 memcpy(trash.str + trash.len, path, pathlen);
3492 trash.len += pathlen;
3493
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003494 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003495 if (trash.len && trash.str[trash.len - 1] != '/' &&
3496 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3497 if (trash.len > trash.size - 5)
3498 return 0;
3499 trash.str[trash.len] = '/';
3500 trash.len++;
3501 }
3502
3503 break;
3504 }
3505 case REDIRECT_TYPE_LOCATION:
3506 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003507 if (rule->rdr_str) { /* this is an old "redirect" rule */
3508 if (trash.len + rule->rdr_len > trash.size - 4)
3509 return 0;
3510
3511 /* add location */
3512 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3513 trash.len += rule->rdr_len;
3514 }
3515 else {
3516 /* add location with executing log format */
3517 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003518
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003519 /* Check left length */
3520 if (trash.len > trash.size - 4)
3521 return 0;
3522 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003523 break;
3524 }
3525
3526 if (rule->cookie_len) {
3527 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3528 trash.len += 14;
3529 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3530 trash.len += rule->cookie_len;
3531 memcpy(trash.str + trash.len, "\r\n", 2);
3532 trash.len += 2;
3533 }
3534
3535 /* add end of headers and the keep-alive/close status.
3536 * We may choose to set keep-alive if the Location begins
3537 * with a slash, because the client will come back to the
3538 * same server.
3539 */
3540 txn->status = rule->code;
3541 /* let's log the request time */
3542 s->logs.tv_request = now;
3543
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003544 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003545 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3546 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3547 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3548 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3549 /* keep-alive possible */
3550 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3551 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3552 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3553 trash.len += 30;
3554 } else {
3555 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3556 trash.len += 24;
3557 }
3558 }
3559 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3560 trash.len += 4;
3561 bo_inject(txn->rsp.chn, trash.str, trash.len);
3562 /* "eat" the request */
3563 bi_fast_delete(txn->req.chn->buf, msg->sov);
3564 msg->sov = 0;
3565 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3566 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3567 txn->req.msg_state = HTTP_MSG_CLOSED;
3568 txn->rsp.msg_state = HTTP_MSG_DONE;
3569 } else {
3570 /* keep-alive not possible */
3571 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3572 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3573 trash.len += 29;
3574 } else {
3575 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3576 trash.len += 23;
3577 }
3578 stream_int_retnclose(txn->req.chn->prod, &trash);
3579 txn->req.chn->analysers = 0;
3580 }
3581
3582 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003583 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003584 if (!(s->flags & SN_FINST_MASK))
3585 s->flags |= SN_FINST_R;
3586
3587 return 1;
3588}
3589
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003590/* This stream analyser runs all HTTP request processing which is common to
3591 * frontends and backends, which means blocking ACLs, filters, connection-close,
3592 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003593 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003594 * either needs more data or wants to immediately abort the request (eg: deny,
3595 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003596 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003597int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003598{
Willy Tarreaud787e662009-07-07 10:14:51 +02003599 struct http_txn *txn = &s->txn;
3600 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003602 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003603 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003604 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003605 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003606
Willy Tarreau655dce92009-11-08 13:10:58 +01003607 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003608 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003609 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003610 return 0;
3611 }
3612
Willy Tarreau3a816292009-07-07 10:55:49 +02003613 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003614 req->analyse_exp = TICK_ETERNITY;
3615
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003616 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 +02003617 now_ms, __FUNCTION__,
3618 s,
3619 req,
3620 req->rex, req->wex,
3621 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003622 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003623 req->analysers);
3624
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003625 /* first check whether we have some ACLs set to block this request */
3626 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003627 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003628
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003629 ret = acl_pass(ret);
3630 if (cond->pol == ACL_COND_UNLESS)
3631 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003632
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003633 if (ret) {
3634 txn->status = 403;
3635 /* let's log the request time */
3636 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003637 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003638 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003640 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003641 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003642
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003643 /* just in case we have some per-backend tracking */
3644 session_inc_be_http_req_ctr(s);
3645
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003646 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003647 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003648
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003649 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003650 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003651 do_stats = stats_check_uri(s->rep->prod, txn, px);
3652 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003653 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003654 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003655 else
3656 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003657
Willy Tarreau3b44e722013-11-16 10:28:23 +01003658 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3659 * blocked by an http-request rule.
3660 */
3661 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003662 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003663 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003664 }
Willy Tarreau06619262006-12-17 08:37:22 +01003665
Willy Tarreau3b44e722013-11-16 10:28:23 +01003666 /* return a 403 if either rule has blocked */
3667 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003668 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003669 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003670 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003671 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003672 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003673 s->fe->fe_counters.denied_req++;
3674 if (s->fe != s->be)
3675 s->be->be_counters.denied_req++;
3676 if (s->listener->counters)
3677 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003678 goto return_prx_cond;
3679 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003680
3681 /* When a connection is tarpitted, we use the tarpit timeout,
3682 * which may be the same as the connect timeout if unspecified.
3683 * If unset, then set it to zero because we really want it to
3684 * eventually expire. We build the tarpit as an analyser.
3685 */
3686 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003687 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003688 /* wipe the request out so that we can drop the connection early
3689 * if the client closes first.
3690 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003691 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003692 req->analysers = 0; /* remove switching rules etc... */
3693 req->analysers |= AN_REQ_HTTP_TARPIT;
3694 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3695 if (!req->analyse_exp)
3696 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003697 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003698 s->fe->fe_counters.denied_req++;
3699 if (s->fe != s->be)
3700 s->be->be_counters.denied_req++;
3701 if (s->listener->counters)
3702 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003703 return 1;
3704 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003705 }
Willy Tarreau06619262006-12-17 08:37:22 +01003706
Willy Tarreau5b154472009-12-21 20:11:07 +01003707 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3708 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003709 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3710 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003711 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3712 * avoid to redo the same work if FE and BE have the same settings (common).
3713 * The method consists in checking if options changed between the two calls
3714 * (implying that either one is non-null, or one of them is non-null and we
3715 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003716 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003717
Willy Tarreaudc008c52010-02-01 16:20:08 +01003718 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3719 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3720 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3721 (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 +01003722 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003723
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003724 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3725 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003726 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003727 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3728 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003729 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003730 tmp = TX_CON_WANT_CLO;
3731
Willy Tarreau5b154472009-12-21 20:11:07 +01003732 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3733 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003734
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003735 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3736 /* parse the Connection header and possibly clean it */
3737 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003738 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003739 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3740 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003741 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003742 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003743 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003744 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003745 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003746
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003747 /* check if client or config asks for explicit close in KAL/SCL */
3748 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3749 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3750 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003751 (!(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 +01003752 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003753 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003754 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003755 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3756 }
Willy Tarreau78599912009-10-17 20:12:21 +02003757
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003758 /* we can be blocked here because the request needs to be authenticated,
3759 * either to pass or to access stats.
3760 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003761 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003762 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003763
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003764 if (!realm)
3765 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3766
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003767 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003768 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003769 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003770 /* on 401 we still count one error, because normal browsing
3771 * won't significantly increase the counter but brute force
3772 * attempts will.
3773 */
3774 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003775 goto return_prx_cond;
3776 }
3777
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003778 /* add request headers from the rule sets in the same order */
3779 list_for_each_entry(wl, &px->req_add, list) {
3780 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003781 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003782 ret = acl_pass(ret);
3783 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3784 ret = !ret;
3785 if (!ret)
3786 continue;
3787 }
3788
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003789 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003790 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003791 }
3792
3793 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3794 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3795 goto return_bad_req;
3796 req->analyse_exp = TICK_ETERNITY;
3797 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003798 }
3799
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003800 if (unlikely(do_stats)) {
3801 /* process the stats request now */
3802 if (!http_handle_stats(s, req)) {
3803 /* we need more data, let's come back here later */
3804 req->analysers |= an_bit;
3805 channel_dont_connect(req);
Willy Tarreau7fe33002013-04-21 08:04:22 +02003806 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003807 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003808 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003809 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003810
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003811 /* check whether we have some ACLs set to redirect this request */
3812 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003813 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003814 int ret;
3815
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003816 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003817 ret = acl_pass(ret);
3818 if (rule->cond->pol == ACL_COND_UNLESS)
3819 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003820 if (!ret)
3821 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003822 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003823 if (!http_apply_redirect_rule(rule, s, txn))
3824 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003825
Willy Tarreau71241ab2012-12-27 11:30:54 +01003826 req->analyse_exp = TICK_ETERNITY;
3827 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003828 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003829
Willy Tarreau2be39392010-01-03 17:24:51 +01003830 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3831 * If this happens, then the data will not come immediately, so we must
3832 * send all what we have without waiting. Note that due to the small gain
3833 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003834 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003835 * itself once used.
3836 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003837 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003838
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003839 /* that's OK for us now, let's move on to next analysers */
3840 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003841
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003842 return_bad_req:
3843 /* We centralize bad requests processing here */
3844 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3845 /* we detected a parsing error. We want to archive this request
3846 * in the dedicated proxy area for later troubleshooting.
3847 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003848 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003849 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003850
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003851 txn->req.msg_state = HTTP_MSG_ERROR;
3852 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003853 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003854
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003855 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003856 if (s->listener->counters)
3857 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003858
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003859 return_prx_cond:
3860 if (!(s->flags & SN_ERR_MASK))
3861 s->flags |= SN_ERR_PRXCOND;
3862 if (!(s->flags & SN_FINST_MASK))
3863 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003864
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003865 req->analysers = 0;
3866 req->analyse_exp = TICK_ETERNITY;
3867 return 0;
3868}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003869
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003870/* This function performs all the processing enabled for the current request.
3871 * It returns 1 if the processing can continue on next analysers, or zero if it
3872 * needs more data, encounters an error, or wants to immediately abort the
3873 * request. It relies on buffers flags, and updates s->req->analysers.
3874 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003875int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003876{
3877 struct http_txn *txn = &s->txn;
3878 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003879
Willy Tarreau655dce92009-11-08 13:10:58 +01003880 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003881 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003882 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003883 return 0;
3884 }
3885
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003886 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 +02003887 now_ms, __FUNCTION__,
3888 s,
3889 req,
3890 req->rex, req->wex,
3891 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003892 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003893 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003894
William Lallemand82fe75c2012-10-23 10:25:10 +02003895 if (s->fe->comp || s->be->comp)
3896 select_compression_request_header(s, req->buf);
3897
Willy Tarreau59234e92008-11-30 23:51:27 +01003898 /*
3899 * Right now, we know that we have processed the entire headers
3900 * and that unwanted requests have been filtered out. We can do
3901 * whatever we want with the remaining request. Also, now we
3902 * may have separate values for ->fe, ->be.
3903 */
Willy Tarreau06619262006-12-17 08:37:22 +01003904
Willy Tarreau59234e92008-11-30 23:51:27 +01003905 /*
3906 * If HTTP PROXY is set we simply get remote server address
3907 * parsing incoming request.
3908 */
3909 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003910 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003912
Willy Tarreau59234e92008-11-30 23:51:27 +01003913 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003914 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003915 * Note that doing so might move headers in the request, but
3916 * the fields will stay coherent and the URI will not move.
3917 * This should only be performed in the backend.
3918 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003919 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003920 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3921 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003922
Willy Tarreau59234e92008-11-30 23:51:27 +01003923 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003924 * 8: the appsession cookie was looked up very early in 1.2,
3925 * so let's do the same now.
3926 */
3927
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003928 /* It needs to look into the URI unless persistence must be ignored */
3929 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003930 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 +01003931 }
3932
William Lallemanda73203e2012-03-12 12:48:57 +01003933 /* add unique-id if "header-unique-id" is specified */
3934
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003935 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3936 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3937 goto return_bad_req;
3938 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003939 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003940 }
William Lallemanda73203e2012-03-12 12:48:57 +01003941
3942 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003943 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3944 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003945 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003946 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003947 goto return_bad_req;
3948 }
3949
Cyril Bontéb21570a2009-11-29 20:04:48 +01003950 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003951 * 9: add X-Forwarded-For if either the frontend or the backend
3952 * asks for it.
3953 */
3954 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003955 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003956 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003957 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3958 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003959 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003960 /* The header is set to be added only if none is present
3961 * and we found it, so don't do anything.
3962 */
3963 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003964 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 /* Add an X-Forwarded-For header unless the source IP is
3966 * in the 'except' network range.
3967 */
3968 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003969 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003970 != s->fe->except_net.s_addr) &&
3971 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003972 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003973 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003974 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003975 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003976 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003977
3978 /* Note: we rely on the backend to get the header name to be used for
3979 * x-forwarded-for, because the header is really meant for the backends.
3980 * However, if the backend did not specify any option, we have to rely
3981 * on the frontend's header name.
3982 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003983 if (s->be->fwdfor_hdr_len) {
3984 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003985 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003986 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003987 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003989 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003990 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003992 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003993 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003994 }
3995 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003996 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003997 /* FIXME: for the sake of completeness, we should also support
3998 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003999 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004000 int len;
4001 char pn[INET6_ADDRSTRLEN];
4002 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004003 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004004 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004005
Willy Tarreau59234e92008-11-30 23:51:27 +01004006 /* Note: we rely on the backend to get the header name to be used for
4007 * x-forwarded-for, because the header is really meant for the backends.
4008 * However, if the backend did not specify any option, we have to rely
4009 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004010 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004011 if (s->be->fwdfor_hdr_len) {
4012 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004013 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004014 } else {
4015 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004016 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004017 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004018 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004019
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004020 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004021 goto return_bad_req;
4022 }
4023 }
4024
4025 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004026 * 10: add X-Original-To if either the frontend or the backend
4027 * asks for it.
4028 */
4029 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4030
4031 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004032 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004033 /* Add an X-Original-To header unless the destination IP is
4034 * in the 'except' network range.
4035 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004036 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004037
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004038 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004039 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004040 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004041 != s->fe->except_to.s_addr) &&
4042 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004043 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004044 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004045 int len;
4046 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004047 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004048
4049 /* Note: we rely on the backend to get the header name to be used for
4050 * x-original-to, because the header is really meant for the backends.
4051 * However, if the backend did not specify any option, we have to rely
4052 * on the frontend's header name.
4053 */
4054 if (s->be->orgto_hdr_len) {
4055 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004056 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004057 } else {
4058 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004059 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004060 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004061 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004062
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004063 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004064 goto return_bad_req;
4065 }
4066 }
4067 }
4068
Willy Tarreau50fc7772012-11-11 22:19:57 +01004069 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4070 * If an "Upgrade" token is found, the header is left untouched in order not to have
4071 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4072 * "Upgrade" is present in the Connection header.
4073 */
4074 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4075 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4076 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004077 unsigned int want_flags = 0;
4078
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004079 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004080 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
4081 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
4082 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004083 want_flags |= TX_CON_CLO_SET;
4084 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004085 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
4086 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
4087 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004088 want_flags |= TX_CON_KAL_SET;
4089 }
4090
4091 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004092 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004093 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004094
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004095
Willy Tarreau522d6c02009-12-06 18:49:18 +01004096 /* If we have no server assigned yet and we're balancing on url_param
4097 * with a POST request, we may be interested in checking the body for
4098 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004099 */
4100 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4101 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004102 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004103 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004104 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004105 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004106 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004107
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004108 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004109 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004110#ifdef TCP_QUICKACK
4111 /* We expect some data from the client. Unless we know for sure
4112 * we already have a full request, we have to re-enable quick-ack
4113 * in case we previously disabled it, otherwise we might cause
4114 * the client to delay further data.
4115 */
4116 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004117 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004118 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004119 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004120#endif
4121 }
Willy Tarreau03945942009-12-22 16:50:27 +01004122
Willy Tarreau59234e92008-11-30 23:51:27 +01004123 /*************************************************************
4124 * OK, that's finished for the headers. We have done what we *
4125 * could. Let's switch to the DATA state. *
4126 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004127 req->analyse_exp = TICK_ETERNITY;
4128 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004129
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004130 /* if the server closes the connection, we want to immediately react
4131 * and close the socket to save packets and syscalls.
4132 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004133 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4134 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004135
Willy Tarreau59234e92008-11-30 23:51:27 +01004136 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004137 /* OK let's go on with the BODY now */
4138 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004139
Willy Tarreau59234e92008-11-30 23:51:27 +01004140 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004141 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004142 /* we detected a parsing error. We want to archive this request
4143 * in the dedicated proxy area for later troubleshooting.
4144 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004145 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004146 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004147
Willy Tarreau59234e92008-11-30 23:51:27 +01004148 txn->req.msg_state = HTTP_MSG_ERROR;
4149 txn->status = 400;
4150 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004151 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004152
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004153 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004154 if (s->listener->counters)
4155 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004156
Willy Tarreau59234e92008-11-30 23:51:27 +01004157 if (!(s->flags & SN_ERR_MASK))
4158 s->flags |= SN_ERR_PRXCOND;
4159 if (!(s->flags & SN_FINST_MASK))
4160 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004161 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004162}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004163
Willy Tarreau60b85b02008-11-30 23:28:40 +01004164/* This function is an analyser which processes the HTTP tarpit. It always
4165 * returns zero, at the beginning because it prevents any other processing
4166 * from occurring, and at the end because it terminates the request.
4167 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004168int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004169{
4170 struct http_txn *txn = &s->txn;
4171
4172 /* This connection is being tarpitted. The CLIENT side has
4173 * already set the connect expiration date to the right
4174 * timeout. We just have to check that the client is still
4175 * there and that the timeout has not expired.
4176 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004177 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004178 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004179 !tick_is_expired(req->analyse_exp, now_ms))
4180 return 0;
4181
4182 /* We will set the queue timer to the time spent, just for
4183 * logging purposes. We fake a 500 server error, so that the
4184 * attacker will not suspect his connection has been tarpitted.
4185 * It will not cause trouble to the logs because we can exclude
4186 * the tarpitted connections by filtering on the 'PT' status flags.
4187 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004188 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4189
4190 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004191 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004192 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004193
4194 req->analysers = 0;
4195 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004196
Willy Tarreau60b85b02008-11-30 23:28:40 +01004197 if (!(s->flags & SN_ERR_MASK))
4198 s->flags |= SN_ERR_PRXCOND;
4199 if (!(s->flags & SN_FINST_MASK))
4200 s->flags |= SN_FINST_T;
4201 return 0;
4202}
4203
Willy Tarreaud34af782008-11-30 23:36:37 +01004204/* This function is an analyser which processes the HTTP request body. It looks
4205 * for parameters to be used for the load balancing algorithm (url_param). It
4206 * must only be called after the standard HTTP request processing has occurred,
4207 * because it expects the request to be parsed. It returns zero if it needs to
4208 * read more data, or 1 once it has completed its analysis.
4209 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004210int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004211{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004212 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004213 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004214 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004215
4216 /* We have to parse the HTTP request body to find any required data.
4217 * "balance url_param check_post" should have been the only way to get
4218 * into this. We were brought here after HTTP header analysis, so all
4219 * related structures are ready.
4220 */
4221
Willy Tarreau522d6c02009-12-06 18:49:18 +01004222 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4223 goto missing_data;
4224
4225 if (msg->msg_state < HTTP_MSG_100_SENT) {
4226 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4227 * send an HTTP/1.1 100 Continue intermediate response.
4228 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004229 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004230 struct hdr_ctx ctx;
4231 ctx.idx = 0;
4232 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004233 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004234 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004235 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004236 }
4237 }
4238 msg->msg_state = HTTP_MSG_100_SENT;
4239 }
4240
4241 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004242 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004243 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004244 * is still null. We must save the body in msg->next because it
4245 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004247 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004248
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004249 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004250 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4251 else
4252 msg->msg_state = HTTP_MSG_DATA;
4253 }
4254
4255 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004256 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004257 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004258 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004259 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004260 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004261
Willy Tarreau115acb92009-12-26 13:56:06 +01004262 if (!ret)
4263 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004264 else if (ret < 0) {
4265 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004266 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004267 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004268 }
4269
Willy Tarreaud98cf932009-12-27 22:54:55 +01004270 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004271 * We have the first data byte is in msg->sov. We're waiting for at
4272 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004273 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004274
Willy Tarreau124d9912011-03-01 20:30:48 +01004275 if (msg->body_len < limit)
4276 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004277
Willy Tarreau9b28e032012-10-12 23:49:43 +02004278 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004279 goto http_end;
4280
4281 missing_data:
4282 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004283 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004284 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004285 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004286 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004287
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004288 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004289 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004290 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004291
4292 if (!(s->flags & SN_ERR_MASK))
4293 s->flags |= SN_ERR_CLITO;
4294 if (!(s->flags & SN_FINST_MASK))
4295 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004296 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004297 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004298
4299 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004300 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004301 /* Not enough data. We'll re-use the http-request
4302 * timeout here. Ideally, we should set the timeout
4303 * relative to the accept() date. We just set the
4304 * request timeout once at the beginning of the
4305 * request.
4306 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004307 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004308 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004309 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004310 return 0;
4311 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004312
4313 http_end:
4314 /* The situation will not evolve, so let's give up on the analysis. */
4315 s->logs.tv_request = now; /* update the request timer to reflect full request */
4316 req->analysers &= ~an_bit;
4317 req->analyse_exp = TICK_ETERNITY;
4318 return 1;
4319
4320 return_bad_req: /* let's centralize all bad requests */
4321 txn->req.msg_state = HTTP_MSG_ERROR;
4322 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004323 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004324
Willy Tarreau79ebac62010-06-07 13:47:49 +02004325 if (!(s->flags & SN_ERR_MASK))
4326 s->flags |= SN_ERR_PRXCOND;
4327 if (!(s->flags & SN_FINST_MASK))
4328 s->flags |= SN_FINST_R;
4329
Willy Tarreau522d6c02009-12-06 18:49:18 +01004330 return_err_msg:
4331 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004332 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004333 if (s->listener->counters)
4334 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004335 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004336}
4337
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004338/* send a server's name with an outgoing request over an established connection.
4339 * Note: this function is designed to be called once the request has been scheduled
4340 * for being forwarded. This is the reason why it rewinds the buffer before
4341 * proceeding.
4342 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004343int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004344
4345 struct hdr_ctx ctx;
4346
Mark Lamourinec2247f02012-01-04 13:02:01 -05004347 char *hdr_name = be->server_id_hdr_name;
4348 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004349 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004350 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004351 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004352
William Lallemandd9e90662012-01-30 17:27:17 +01004353 ctx.idx = 0;
4354
Willy Tarreau9b28e032012-10-12 23:49:43 +02004355 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004356 if (old_o) {
4357 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004358 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004359 }
4360
Willy Tarreau9b28e032012-10-12 23:49:43 +02004361 old_i = chn->buf->i;
4362 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 -05004363 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004364 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004365 }
4366
4367 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004368 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004369 memcpy(hdr_val, hdr_name, hdr_name_len);
4370 hdr_val += hdr_name_len;
4371 *hdr_val++ = ':';
4372 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004373 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4374 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004375
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004376 if (old_o) {
4377 /* If this was a forwarded request, we must readjust the amount of
4378 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004379 * variations. Note that if the request was already scheduled for
4380 * forwarding, it had its req->sol pointing to the body, which
4381 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004382 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004383 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004384 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004385 }
4386
Mark Lamourinec2247f02012-01-04 13:02:01 -05004387 return 0;
4388}
4389
Willy Tarreau610ecce2010-01-04 21:15:02 +01004390/* Terminate current transaction and prepare a new one. This is very tricky
4391 * right now but it works.
4392 */
4393void http_end_txn_clean_session(struct session *s)
4394{
4395 /* FIXME: We need a more portable way of releasing a backend's and a
4396 * server's connections. We need a safer way to reinitialize buffer
4397 * flags. We also need a more accurate method for computing per-request
4398 * data.
4399 */
4400 http_silent_debug(__LINE__, s);
4401
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004402 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004403 si_shutr(s->req->cons);
4404 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405
4406 http_silent_debug(__LINE__, s);
4407
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004408 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004410 if (unlikely(s->srv_conn))
4411 sess_change_server(s, NULL);
4412 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413
4414 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4415 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004416 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417
4418 if (s->txn.status) {
4419 int n;
4420
4421 n = s->txn.status / 100;
4422 if (n < 1 || n > 5)
4423 n = 0;
4424
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004425 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004426 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004427 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004428 s->fe->fe_counters.p.http.comp_rsp++;
4429 }
Willy Tarreau24657792010-02-26 10:30:28 +01004430 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004431 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004432 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004433 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004434 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004435 s->be->be_counters.p.http.comp_rsp++;
4436 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004437 }
4438
4439 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004440 s->logs.bytes_in -= s->req->buf->i;
4441 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442
4443 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004444 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004445 !(s->flags & SN_MONITOR) &&
4446 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4447 s->do_log(s);
4448 }
4449
4450 s->logs.accept_date = date; /* user-visible date for logging */
4451 s->logs.tv_accept = now; /* corrected date for internal use */
4452 tv_zero(&s->logs.tv_request);
4453 s->logs.t_queue = -1;
4454 s->logs.t_connect = -1;
4455 s->logs.t_data = -1;
4456 s->logs.t_close = 0;
4457 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4458 s->logs.srv_queue_size = 0; /* we will get this number soon */
4459
Willy Tarreau9b28e032012-10-12 23:49:43 +02004460 s->logs.bytes_in = s->req->total = s->req->buf->i;
4461 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462
4463 if (s->pend_pos)
4464 pendconn_free(s->pend_pos);
4465
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004466 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 if (s->flags & SN_CURR_SESS) {
4468 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004469 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004470 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004471 if (may_dequeue_tasks(objt_server(s->target), s->be))
4472 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004473 }
4474
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004475 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004476
4477 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004478 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4479 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004480 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004482 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004483 s->req->cons->err_loc = NULL;
4484 s->req->cons->exp = TICK_ETERNITY;
4485 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004486 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4487 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 +02004488 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 +01004489 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004490
4491 if (s->flags & SN_COMP_READY)
4492 s->comp_algo->end(&s->comp_ctx);
4493 s->comp_algo = NULL;
4494 s->flags &= ~SN_COMP_READY;
4495
Willy Tarreau610ecce2010-01-04 21:15:02 +01004496 s->txn.meth = 0;
4497 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004498 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004499 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 s->req->cons->flags |= SI_FL_INDEP_STR;
4501
Willy Tarreau96e31212011-05-30 18:10:30 +02004502 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004503 s->req->flags |= CF_NEVER_WAIT;
4504 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004505 }
4506
Willy Tarreau610ecce2010-01-04 21:15:02 +01004507 /* if the request buffer is not empty, it means we're
4508 * about to process another request, so send pending
4509 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004510 * Just don't do this if the buffer is close to be full,
4511 * because the request will wait for it to flush a little
4512 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004514 if (s->req->buf->i) {
4515 if (s->rep->buf->o &&
4516 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4517 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004518 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004519 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004520
4521 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004522 channel_auto_read(s->req);
4523 channel_auto_close(s->req);
4524 channel_auto_read(s->rep);
4525 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004526
Willy Tarreau342b11c2010-11-24 16:22:09 +01004527 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004528 s->rep->analysers = 0;
4529
4530 http_silent_debug(__LINE__, s);
4531}
4532
4533
4534/* This function updates the request state machine according to the response
4535 * state machine and buffer flags. It returns 1 if it changes anything (flag
4536 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4537 * it is only used to find when a request/response couple is complete. Both
4538 * this function and its equivalent should loop until both return zero. It
4539 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4540 */
4541int http_sync_req_state(struct session *s)
4542{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004543 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004545 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004546 unsigned int old_state = txn->req.msg_state;
4547
4548 http_silent_debug(__LINE__, s);
4549 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4550 return 0;
4551
4552 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004553 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004554 * We can shut the read side unless we want to abort_on_close,
4555 * or we have a POST request. The issue with POST requests is
4556 * that some browsers still send a CRLF after the request, and
4557 * this CRLF must be read so that it does not remain in the kernel
4558 * buffers, otherwise a close could cause an RST on some systems
4559 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004560 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004561 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004562 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563
Willy Tarreau40f151a2012-12-20 12:10:09 +01004564 /* if the server closes the connection, we want to immediately react
4565 * and close the socket to save packets and syscalls.
4566 */
4567 chn->cons->flags |= SI_FL_NOHALF;
4568
Willy Tarreau610ecce2010-01-04 21:15:02 +01004569 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4570 goto wait_other_side;
4571
4572 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4573 /* The server has not finished to respond, so we
4574 * don't want to move in order not to upset it.
4575 */
4576 goto wait_other_side;
4577 }
4578
4579 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4580 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004581 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004582 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004583 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004584 goto wait_other_side;
4585 }
4586
4587 /* When we get here, it means that both the request and the
4588 * response have finished receiving. Depending on the connection
4589 * mode, we'll have to wait for the last bytes to leave in either
4590 * direction, and sometimes for a close to be effective.
4591 */
4592
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004593 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4594 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004595 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4596 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004597 }
4598 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4599 /* Option forceclose is set, or either side wants to close,
4600 * let's enforce it now that we're not expecting any new
4601 * data to come. The caller knows the session is complete
4602 * once both states are CLOSED.
4603 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004604 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4605 channel_shutr_now(chn);
4606 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004607 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004608 }
4609 else {
4610 /* The last possible modes are keep-alive and tunnel. Since tunnel
4611 * mode does not set the body analyser, we can't reach this place
4612 * in tunnel mode, so we're left with keep-alive only.
4613 * This mode is currently not implemented, we switch to tunnel mode.
4614 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004615 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004616 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004617 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 }
4619
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004620 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004622 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004623
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004624 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004625 txn->req.msg_state = HTTP_MSG_CLOSING;
4626 goto http_msg_closing;
4627 }
4628 else {
4629 txn->req.msg_state = HTTP_MSG_CLOSED;
4630 goto http_msg_closed;
4631 }
4632 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004633 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004634 }
4635
4636 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4637 http_msg_closing:
4638 /* nothing else to forward, just waiting for the output buffer
4639 * to be empty and for the shutw_now to take effect.
4640 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004641 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004642 txn->req.msg_state = HTTP_MSG_CLOSED;
4643 goto http_msg_closed;
4644 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004645 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004646 txn->req.msg_state = HTTP_MSG_ERROR;
4647 goto wait_other_side;
4648 }
4649 }
4650
4651 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4652 http_msg_closed:
4653 goto wait_other_side;
4654 }
4655
4656 wait_other_side:
4657 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004658 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659}
4660
4661
4662/* This function updates the response state machine according to the request
4663 * state machine and buffer flags. It returns 1 if it changes anything (flag
4664 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4665 * it is only used to find when a request/response couple is complete. Both
4666 * this function and its equivalent should loop until both return zero. It
4667 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4668 */
4669int http_sync_res_state(struct session *s)
4670{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004671 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004672 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004673 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674 unsigned int old_state = txn->rsp.msg_state;
4675
4676 http_silent_debug(__LINE__, s);
4677 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4678 return 0;
4679
4680 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4681 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004682 * still monitor the server connection for a possible close
4683 * while the request is being uploaded, so we don't disable
4684 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004686 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687
4688 if (txn->req.msg_state == HTTP_MSG_ERROR)
4689 goto wait_other_side;
4690
4691 if (txn->req.msg_state < HTTP_MSG_DONE) {
4692 /* The client seems to still be sending data, probably
4693 * because we got an error response during an upload.
4694 * We have the choice of either breaking the connection
4695 * or letting it pass through. Let's do the later.
4696 */
4697 goto wait_other_side;
4698 }
4699
4700 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4701 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004702 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004703 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004704 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004705 goto wait_other_side;
4706 }
4707
4708 /* When we get here, it means that both the request and the
4709 * response have finished receiving. Depending on the connection
4710 * mode, we'll have to wait for the last bytes to leave in either
4711 * direction, and sometimes for a close to be effective.
4712 */
4713
4714 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4715 /* Server-close mode : shut read and wait for the request
4716 * side to close its output buffer. The caller will detect
4717 * when we're in DONE and the other is in CLOSED and will
4718 * catch that for the final cleanup.
4719 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004720 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4721 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004722 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004723 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4724 /* Option forceclose is set, or either side wants to close,
4725 * let's enforce it now that we're not expecting any new
4726 * data to come. The caller knows the session is complete
4727 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004728 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004729 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4730 channel_shutr_now(chn);
4731 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004732 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004733 }
4734 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004735 /* The last possible modes are keep-alive and tunnel. Since tunnel
4736 * mode does not set the body analyser, we can't reach this place
4737 * in tunnel mode, so we're left with keep-alive only.
4738 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004740 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004742 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743 }
4744
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004745 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004746 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004747 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4749 goto http_msg_closing;
4750 }
4751 else {
4752 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4753 goto http_msg_closed;
4754 }
4755 }
4756 goto wait_other_side;
4757 }
4758
4759 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4760 http_msg_closing:
4761 /* nothing else to forward, just waiting for the output buffer
4762 * to be empty and for the shutw_now to take effect.
4763 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004764 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004765 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4766 goto http_msg_closed;
4767 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004768 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004770 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004771 if (objt_server(s->target))
4772 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773 goto wait_other_side;
4774 }
4775 }
4776
4777 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4778 http_msg_closed:
4779 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004780 bi_erase(chn);
4781 channel_auto_close(chn);
4782 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004783 goto wait_other_side;
4784 }
4785
4786 wait_other_side:
4787 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004788 /* We force the response to leave immediately if we're waiting for the
4789 * other side, since there is no pending shutdown to push it out.
4790 */
4791 if (!channel_is_empty(chn))
4792 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004793 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004794}
4795
4796
4797/* Resync the request and response state machines. Return 1 if either state
4798 * changes.
4799 */
4800int http_resync_states(struct session *s)
4801{
4802 struct http_txn *txn = &s->txn;
4803 int old_req_state = txn->req.msg_state;
4804 int old_res_state = txn->rsp.msg_state;
4805
4806 http_silent_debug(__LINE__, s);
4807 http_sync_req_state(s);
4808 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004809 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810 if (!http_sync_res_state(s))
4811 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004812 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813 if (!http_sync_req_state(s))
4814 break;
4815 }
4816 http_silent_debug(__LINE__, s);
4817 /* OK, both state machines agree on a compatible state.
4818 * There are a few cases we're interested in :
4819 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4820 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4821 * directions, so let's simply disable both analysers.
4822 * - HTTP_MSG_CLOSED on the response only means we must abort the
4823 * request.
4824 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4825 * with server-close mode means we've completed one request and we
4826 * must re-initialize the server connection.
4827 */
4828
4829 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4830 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4831 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4832 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4833 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004834 channel_auto_close(s->req);
4835 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004836 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004837 channel_auto_close(s->rep);
4838 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004839 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004840 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4841 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004842 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004843 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004844 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004845 channel_auto_close(s->rep);
4846 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004847 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004848 channel_abort(s->req);
4849 channel_auto_close(s->req);
4850 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004851 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004852 }
4853 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4854 txn->rsp.msg_state == HTTP_MSG_DONE &&
4855 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4856 /* server-close: terminate this server connection and
4857 * reinitialize a fresh-new transaction.
4858 */
4859 http_end_txn_clean_session(s);
4860 }
4861
4862 http_silent_debug(__LINE__, s);
4863 return txn->req.msg_state != old_req_state ||
4864 txn->rsp.msg_state != old_res_state;
4865}
4866
Willy Tarreaud98cf932009-12-27 22:54:55 +01004867/* This function is an analyser which forwards request body (including chunk
4868 * sizes if any). It is called as soon as we must forward, even if we forward
4869 * zero byte. The only situation where it must not be called is when we're in
4870 * tunnel mode and we want to forward till the close. It's used both to forward
4871 * remaining data and to resync after end of body. It expects the msg_state to
4872 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4873 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004874 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004875 * bytes of pending data + the headers if not already done (between sol and sov).
4876 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004877 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004878int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004879{
4880 struct http_txn *txn = &s->txn;
4881 struct http_msg *msg = &s->txn.req;
4882
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004883 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4884 return 0;
4885
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004886 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004887 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004888 /* Output closed while we were sending data. We must abort and
4889 * wake the other side up.
4890 */
4891 msg->msg_state = HTTP_MSG_ERROR;
4892 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004893 return 1;
4894 }
4895
Willy Tarreau4fe41902010-06-07 22:27:41 +02004896 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004897 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004898
4899 /* Note that we don't have to send 100-continue back because we don't
4900 * need the data to complete our job, and it's up to the server to
4901 * decide whether to return 100, 417 or anything else in return of
4902 * an "Expect: 100-continue" header.
4903 */
4904
4905 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004906 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004907 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004908 * is still null. We must save the body in msg->next because it
4909 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004910 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004911 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004912
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004913 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004914 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004915 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004916 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004917 }
4918
Willy Tarreaud98cf932009-12-27 22:54:55 +01004919 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004920 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004921
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004923 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004924 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004925 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004926 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004927 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004928 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004929 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004930 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004931
Willy Tarreaucaabe412010-01-03 23:08:28 +01004932 if (msg->msg_state == HTTP_MSG_DATA) {
4933 /* must still forward */
4934 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004935 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004936
4937 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004938 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004939 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004940 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004941 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004942 }
4943 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004944 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004945 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004946 * TRAILERS state.
4947 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004948 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004949
Willy Tarreau54d23df2012-10-25 19:04:45 +02004950 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004952 else if (ret < 0) {
4953 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004954 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004955 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004956 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004957 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004958 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004959 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004960 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004961 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004962 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004963
4964 if (ret == 0)
4965 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004966 else if (ret < 0) {
4967 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004968 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004969 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004970 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004971 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004972 /* we're in MSG_CHUNK_SIZE now */
4973 }
4974 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004975 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004976
4977 if (ret == 0)
4978 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004979 else if (ret < 0) {
4980 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004981 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004982 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004983 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004984 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004985 /* we're in HTTP_MSG_DONE now */
4986 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004987 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004988 int old_state = msg->msg_state;
4989
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004991 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004992 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4993 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004994 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995 if (http_resync_states(s)) {
4996 /* some state changes occurred, maybe the analyser
4997 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004998 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004999 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005000 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005001 /* request errors are most likely due to
5002 * the server aborting the transfer.
5003 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005004 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005005 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005006 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005007 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005008 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005009 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005010 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005011 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005012
5013 /* If "option abortonclose" is set on the backend, we
5014 * want to monitor the client's connection and forward
5015 * any shutdown notification to the server, which will
5016 * decide whether to close or to go on processing the
5017 * request.
5018 */
5019 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005020 channel_auto_read(req);
5021 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005022 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005023 else if (s->txn.meth == HTTP_METH_POST) {
5024 /* POST requests may require to read extra CRLF
5025 * sent by broken browsers and which could cause
5026 * an RST to be sent upon close on some systems
5027 * (eg: Linux).
5028 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005029 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005030 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005031
Willy Tarreau610ecce2010-01-04 21:15:02 +01005032 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005033 }
5034 }
5035
Willy Tarreaud98cf932009-12-27 22:54:55 +01005036 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005037 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005038 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005039 if (!(s->flags & SN_ERR_MASK))
5040 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005041 if (!(s->flags & SN_FINST_MASK)) {
5042 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5043 s->flags |= SN_FINST_H;
5044 else
5045 s->flags |= SN_FINST_D;
5046 }
5047
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005048 s->fe->fe_counters.cli_aborts++;
5049 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005050 if (objt_server(s->target))
5051 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005052
5053 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005054 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005055
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005056 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005057 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005058 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005060 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005061 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005062 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005063 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005064 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005065
Willy Tarreau5c620922011-05-11 19:56:11 +02005066 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005067 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005068 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005069 * modes are already handled by the stream sock layer. We must not do
5070 * this in content-length mode because it could present the MSG_MORE
5071 * flag with the last block of forwarded data, which would cause an
5072 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005073 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005074 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005075 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005076
Willy Tarreau610ecce2010-01-04 21:15:02 +01005077 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005078 return 0;
5079
Willy Tarreaud98cf932009-12-27 22:54:55 +01005080 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005081 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005082 if (s->listener->counters)
5083 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005084 return_bad_req_stats_ok:
5085 txn->req.msg_state = HTTP_MSG_ERROR;
5086 if (txn->status) {
5087 /* Note: we don't send any error if some data were already sent */
5088 stream_int_retnclose(req->prod, NULL);
5089 } else {
5090 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005091 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005092 }
5093 req->analysers = 0;
5094 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005095
5096 if (!(s->flags & SN_ERR_MASK))
5097 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005098 if (!(s->flags & SN_FINST_MASK)) {
5099 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5100 s->flags |= SN_FINST_H;
5101 else
5102 s->flags |= SN_FINST_D;
5103 }
5104 return 0;
5105
5106 aborted_xfer:
5107 txn->req.msg_state = HTTP_MSG_ERROR;
5108 if (txn->status) {
5109 /* Note: we don't send any error if some data were already sent */
5110 stream_int_retnclose(req->prod, NULL);
5111 } else {
5112 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005113 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005114 }
5115 req->analysers = 0;
5116 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5117
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005118 s->fe->fe_counters.srv_aborts++;
5119 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005120 if (objt_server(s->target))
5121 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005122
5123 if (!(s->flags & SN_ERR_MASK))
5124 s->flags |= SN_ERR_SRVCL;
5125 if (!(s->flags & SN_FINST_MASK)) {
5126 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5127 s->flags |= SN_FINST_H;
5128 else
5129 s->flags |= SN_FINST_D;
5130 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005131 return 0;
5132}
5133
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5135 * processing can continue on next analysers, or zero if it either needs more
5136 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5137 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5138 * when it has nothing left to do, and may remove any analyser when it wants to
5139 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005140 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005141int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005142{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 struct http_txn *txn = &s->txn;
5144 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005145 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005146 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005148 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005149
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005150 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 +02005151 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005152 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005153 rep,
5154 rep->rex, rep->wex,
5155 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005156 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005157 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005158
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005159 /*
5160 * Now parse the partial (or complete) lines.
5161 * We will check the response syntax, and also join multi-line
5162 * headers. An index of all the lines will be elaborated while
5163 * parsing.
5164 *
5165 * For the parsing, we use a 28 states FSM.
5166 *
5167 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005168 * rep->buf->p = beginning of response
5169 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5170 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005171 * msg->eol = end of current header or line (LF or CRLF)
5172 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005173 */
5174
Willy Tarreau83e3af02009-12-28 17:39:57 +01005175 /* There's a protected area at the end of the buffer for rewriting
5176 * purposes. We don't want to start to parse the request if the
5177 * protected area is affected, because we may have to move processed
5178 * data later, which is much more complicated.
5179 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005180 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005181 if (unlikely(!channel_reserved(rep))) {
5182 /* some data has still not left the buffer, wake us once that's done */
5183 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5184 goto abort_response;
5185 channel_dont_close(rep);
5186 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5187 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005188 }
5189
Willy Tarreau379357a2013-06-08 12:55:46 +02005190 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5191 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5192 buffer_slow_realign(rep->buf);
5193
Willy Tarreau9b28e032012-10-12 23:49:43 +02005194 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005195 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005196 }
5197
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005198 /* 1: we might have to print this header in debug mode */
5199 if (unlikely((global.mode & MODE_DEBUG) &&
5200 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005201 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203
Willy Tarreau9b28e032012-10-12 23:49:43 +02005204 sol = rep->buf->p;
5205 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005206 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005208 sol += hdr_idx_first_pos(&txn->hdr_idx);
5209 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005210
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005211 while (cur_idx) {
5212 eol = sol + txn->hdr_idx.v[cur_idx].len;
5213 debug_hdr("srvhdr", s, sol, eol);
5214 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5215 cur_idx = txn->hdr_idx.v[cur_idx].next;
5216 }
5217 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005218
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219 /*
5220 * Now we quickly check if we have found a full valid response.
5221 * If not so, we check the FD and buffer states before leaving.
5222 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005223 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005224 * responses are checked first.
5225 *
5226 * Depending on whether the client is still there or not, we
5227 * may send an error response back or not. Note that normally
5228 * we should only check for HTTP status there, and check I/O
5229 * errors somewhere else.
5230 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005231
Willy Tarreau655dce92009-11-08 13:10:58 +01005232 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233 /* Invalid response */
5234 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5235 /* we detected a parsing error. We want to archive this response
5236 * in the dedicated proxy area for later troubleshooting.
5237 */
5238 hdr_response_bad:
5239 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005240 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005241
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005242 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005243 if (objt_server(s->target)) {
5244 objt_server(s->target)->counters.failed_resp++;
5245 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005246 }
Willy Tarreau64648412010-03-05 10:41:54 +01005247 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005248 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 rep->analysers = 0;
5250 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005251 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005252 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005253 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005254
5255 if (!(s->flags & SN_ERR_MASK))
5256 s->flags |= SN_ERR_PRXCOND;
5257 if (!(s->flags & SN_FINST_MASK))
5258 s->flags |= SN_FINST_H;
5259
5260 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005261 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005262
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005264 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005265 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005266 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005267 goto hdr_response_bad;
5268 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005269
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005270 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005271 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005272 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005273 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005274
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005275 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005276 if (objt_server(s->target)) {
5277 objt_server(s->target)->counters.failed_resp++;
5278 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005279 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005280
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005281 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005282 rep->analysers = 0;
5283 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005284 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005285 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005286 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005287
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005288 if (!(s->flags & SN_ERR_MASK))
5289 s->flags |= SN_ERR_SRVCL;
5290 if (!(s->flags & SN_FINST_MASK))
5291 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005292 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005293 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005294
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005295 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005296 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005297 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005298 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005299
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005300 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005301 if (objt_server(s->target)) {
5302 objt_server(s->target)->counters.failed_resp++;
5303 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005304 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005305
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005306 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005307 rep->analysers = 0;
5308 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005309 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005310 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005311 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005312
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 if (!(s->flags & SN_ERR_MASK))
5314 s->flags |= SN_ERR_SRVTO;
5315 if (!(s->flags & SN_FINST_MASK))
5316 s->flags |= SN_FINST_H;
5317 return 0;
5318 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005319
Willy Tarreauf003d372012-11-26 13:35:37 +01005320 /* client abort with an abortonclose */
5321 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5322 s->fe->fe_counters.cli_aborts++;
5323 s->be->be_counters.cli_aborts++;
5324 if (objt_server(s->target))
5325 objt_server(s->target)->counters.cli_aborts++;
5326
5327 rep->analysers = 0;
5328 channel_auto_close(rep);
5329
5330 txn->status = 400;
5331 bi_erase(rep);
5332 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5333
5334 if (!(s->flags & SN_ERR_MASK))
5335 s->flags |= SN_ERR_CLICL;
5336 if (!(s->flags & SN_FINST_MASK))
5337 s->flags |= SN_FINST_H;
5338
5339 /* process_session() will take care of the error */
5340 return 0;
5341 }
5342
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005343 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005344 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005345 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005346 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005347
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005348 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005349 if (objt_server(s->target)) {
5350 objt_server(s->target)->counters.failed_resp++;
5351 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005352 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005353
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005354 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005355 rep->analysers = 0;
5356 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005357 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005358 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005359 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005360
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005361 if (!(s->flags & SN_ERR_MASK))
5362 s->flags |= SN_ERR_SRVCL;
5363 if (!(s->flags & SN_FINST_MASK))
5364 s->flags |= SN_FINST_H;
5365 return 0;
5366 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005367
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005368 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005369 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005370 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005371 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005372
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005373 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005375 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005376
5377 if (!(s->flags & SN_ERR_MASK))
5378 s->flags |= SN_ERR_CLICL;
5379 if (!(s->flags & SN_FINST_MASK))
5380 s->flags |= SN_FINST_H;
5381
5382 /* process_session() will take care of the error */
5383 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005384 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005385
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005386 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005387 return 0;
5388 }
5389
5390 /* More interesting part now : we know that we have a complete
5391 * response which at least looks like HTTP. We have an indicator
5392 * of each header's length, so we can parse them quickly.
5393 */
5394
5395 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005396 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005397
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398 /*
5399 * 1: get the status code
5400 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005401 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005402 if (n < 1 || n > 5)
5403 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005404 /* when the client triggers a 4xx from the server, it's most often due
5405 * to a missing object or permission. These events should be tracked
5406 * because if they happen often, it may indicate a brute force or a
5407 * vulnerability scan.
5408 */
5409 if (n == 4)
5410 session_inc_http_err_ctr(s);
5411
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005412 if (objt_server(s->target))
5413 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005414
Willy Tarreau5b154472009-12-21 20:11:07 +01005415 /* check if the response is HTTP/1.1 or above */
5416 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005417 ((rep->buf->p[5] > '1') ||
5418 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005419 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005420
5421 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005422 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 +01005423
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005424 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005425 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005426
Willy Tarreau9b28e032012-10-12 23:49:43 +02005427 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005428
Willy Tarreau39650402010-03-15 19:44:39 +01005429 /* Adjust server's health based on status code. Note: status codes 501
5430 * and 505 are triggered on demand by client request, so we must not
5431 * count them as server failures.
5432 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005433 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005434 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005435 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005436 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005437 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005438 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005439
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005440 /*
5441 * 2: check for cacheability.
5442 */
5443
5444 switch (txn->status) {
5445 case 200:
5446 case 203:
5447 case 206:
5448 case 300:
5449 case 301:
5450 case 410:
5451 /* RFC2616 @13.4:
5452 * "A response received with a status code of
5453 * 200, 203, 206, 300, 301 or 410 MAY be stored
5454 * by a cache (...) unless a cache-control
5455 * directive prohibits caching."
5456 *
5457 * RFC2616 @9.5: POST method :
5458 * "Responses to this method are not cacheable,
5459 * unless the response includes appropriate
5460 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005461 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005462 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005463 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005464 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5465 break;
5466 default:
5467 break;
5468 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005469
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005470 /*
5471 * 3: we may need to capture headers
5472 */
5473 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005474 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005475 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005476 txn->rsp.cap, s->fe->rsp_cap);
5477
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005478 /* 4: determine the transfer-length.
5479 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5480 * the presence of a message-body in a RESPONSE and its transfer length
5481 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005482 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005483 * All responses to the HEAD request method MUST NOT include a
5484 * message-body, even though the presence of entity-header fields
5485 * might lead one to believe they do. All 1xx (informational), 204
5486 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5487 * message-body. All other responses do include a message-body,
5488 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005489 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005490 * 1. Any response which "MUST NOT" include a message-body (such as the
5491 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5492 * always terminated by the first empty line after the header fields,
5493 * regardless of the entity-header fields present in the message.
5494 *
5495 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5496 * the "chunked" transfer-coding (Section 6.2) is used, the
5497 * transfer-length is defined by the use of this transfer-coding.
5498 * If a Transfer-Encoding header field is present and the "chunked"
5499 * transfer-coding is not present, the transfer-length is defined by
5500 * the sender closing the connection.
5501 *
5502 * 3. If a Content-Length header field is present, its decimal value in
5503 * OCTETs represents both the entity-length and the transfer-length.
5504 * If a message is received with both a Transfer-Encoding header
5505 * field and a Content-Length header field, the latter MUST be ignored.
5506 *
5507 * 4. If the message uses the media type "multipart/byteranges", and
5508 * the transfer-length is not otherwise specified, then this self-
5509 * delimiting media type defines the transfer-length. This media
5510 * type MUST NOT be used unless the sender knows that the recipient
5511 * can parse it; the presence in a request of a Range header with
5512 * multiple byte-range specifiers from a 1.1 client implies that the
5513 * client can parse multipart/byteranges responses.
5514 *
5515 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005516 */
5517
5518 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005519 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005520 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005521 * FIXME: should we parse anyway and return an error on chunked encoding ?
5522 */
5523 if (txn->meth == HTTP_METH_HEAD ||
5524 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005525 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005526 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005527 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005528 goto skip_content_length;
5529 }
5530
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005531 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005532 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005533 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005534 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005535 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005536 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5537 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005538 /* bad transfer-encoding (chunked followed by something else) */
5539 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005540 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005541 break;
5542 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005543 }
5544
5545 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5546 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005547 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005548 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005549 signed long long cl;
5550
Willy Tarreauad14f752011-09-02 20:33:27 +02005551 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005552 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005553 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005554 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005555
Willy Tarreauad14f752011-09-02 20:33:27 +02005556 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005557 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005558 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005559 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005560
Willy Tarreauad14f752011-09-02 20:33:27 +02005561 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005562 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005563 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005564 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005565
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005566 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005567 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005568 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005569 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005570
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005571 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005572 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005573 }
5574
William Lallemand82fe75c2012-10-23 10:25:10 +02005575 if (s->fe->comp || s->be->comp)
5576 select_compression_response_header(s, rep->buf);
5577
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005578 /* FIXME: we should also implement the multipart/byterange method.
5579 * For now on, we resort to close mode in this case (unknown length).
5580 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005581skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005582
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005583 /* end of job, return OK */
5584 rep->analysers &= ~an_bit;
5585 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005586 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005587 return 1;
5588}
5589
5590/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005591 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5592 * and updates t->rep->analysers. It might make sense to explode it into several
5593 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005594 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005595int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005596{
5597 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005598 struct http_msg *msg = &txn->rsp;
5599 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005600 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005601 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005602
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005603 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 +02005604 now_ms, __FUNCTION__,
5605 t,
5606 rep,
5607 rep->rex, rep->wex,
5608 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005609 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005610 rep->analysers);
5611
Willy Tarreau655dce92009-11-08 13:10:58 +01005612 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005613 return 0;
5614
5615 rep->analysers &= ~an_bit;
5616 rep->analyse_exp = TICK_ETERNITY;
5617
Willy Tarreau5b154472009-12-21 20:11:07 +01005618 /* Now we have to check if we need to modify the Connection header.
5619 * This is more difficult on the response than it is on the request,
5620 * because we can have two different HTTP versions and we don't know
5621 * how the client will interprete a response. For instance, let's say
5622 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5623 * HTTP/1.1 response without any header. Maybe it will bound itself to
5624 * HTTP/1.0 because it only knows about it, and will consider the lack
5625 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5626 * the lack of header as a keep-alive. Thus we will use two flags
5627 * indicating how a request MAY be understood by the client. In case
5628 * of multiple possibilities, we'll fix the header to be explicit. If
5629 * ambiguous cases such as both close and keepalive are seen, then we
5630 * will fall back to explicit close. Note that we won't take risks with
5631 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005632 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005633 */
5634
Willy Tarreaudc008c52010-02-01 16:20:08 +01005635 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5636 txn->status == 101)) {
5637 /* Either we've established an explicit tunnel, or we're
5638 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005639 * to understand the next protocols. We have to switch to tunnel
5640 * mode, so that we transfer the request and responses then let
5641 * this protocol pass unmodified. When we later implement specific
5642 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005643 * header which contains information about that protocol for
5644 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005645 */
5646 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5647 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005648 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5649 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5650 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005651 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005652
Willy Tarreau60466522010-01-18 19:08:45 +01005653 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005654 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005655 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5656 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005657
Willy Tarreau60466522010-01-18 19:08:45 +01005658 /* now adjust header transformations depending on current state */
5659 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5660 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5661 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005662 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005663 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005664 }
Willy Tarreau60466522010-01-18 19:08:45 +01005665 else { /* SCL / KAL */
5666 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005667 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005668 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005669 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005670
Willy Tarreau60466522010-01-18 19:08:45 +01005671 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005672 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005673
Willy Tarreau60466522010-01-18 19:08:45 +01005674 /* Some keep-alive responses are converted to Server-close if
5675 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005676 */
Willy Tarreau60466522010-01-18 19:08:45 +01005677 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5678 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005679 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005680 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005681 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005682 }
5683
Willy Tarreau7959a552013-09-23 16:44:27 +02005684 /* we want to have the response time before we start processing it */
5685 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5686
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005687 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005688 /*
5689 * 3: we will have to evaluate the filters.
5690 * As opposed to version 1.2, now they will be evaluated in the
5691 * filters order and not in the header order. This means that
5692 * each filter has to be validated among all headers.
5693 *
5694 * Filters are tried with ->be first, then with ->fe if it is
5695 * different from ->be.
5696 */
5697
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005698 cur_proxy = t->be;
5699 while (1) {
5700 struct proxy *rule_set = cur_proxy;
5701
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005702 /* evaluate http-response rules */
5703 if (!http_res_last_rule)
5704 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5705
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005706 /* try headers filters */
5707 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005708 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005709 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005710 if (objt_server(t->target)) {
5711 objt_server(t->target)->counters.failed_resp++;
5712 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005713 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005714 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005715 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005716 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005717 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005718 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005719 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005720 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005721 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005722 if (!(t->flags & SN_ERR_MASK))
5723 t->flags |= SN_ERR_PRXCOND;
5724 if (!(t->flags & SN_FINST_MASK))
5725 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005726 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005727 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005728 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005729
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005730 /* has the response been denied ? */
5731 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005732 if (objt_server(t->target))
5733 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005734
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005735 t->be->be_counters.denied_resp++;
5736 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005737 if (t->listener->counters)
5738 t->listener->counters->denied_resp++;
5739
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005740 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005741 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005742
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005743 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005744 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005745 if (txn->status < 200)
5746 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005747 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005748 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005749 ret = acl_pass(ret);
5750 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5751 ret = !ret;
5752 if (!ret)
5753 continue;
5754 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005755 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005756 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005757 }
5758
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005759 /* check whether we're already working on the frontend */
5760 if (cur_proxy == t->fe)
5761 break;
5762 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005763 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005764
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005765 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005766 * We may be facing a 100-continue response, in which case this
5767 * is not the right response, and we're waiting for the next one.
5768 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005769 * next one.
5770 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005771 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005772 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005773 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005774 msg->msg_state = HTTP_MSG_RPBEFORE;
5775 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005776 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005777 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5778 return 1;
5779 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005780 else if (unlikely(txn->status < 200))
5781 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005782
5783 /* we don't have any 1xx status code now */
5784
5785 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005786 * 4: check for server cookie.
5787 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005788 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5789 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005790 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005791
Willy Tarreaubaaee002006-06-26 02:48:02 +02005792
Willy Tarreaua15645d2007-03-18 16:22:39 +01005793 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005794 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005795 */
Willy Tarreau67402132012-05-31 20:40:20 +02005796 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005797 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005798
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005799 /*
5800 * 6: add server cookie in the response if needed
5801 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005802 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005803 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005804 (!(t->flags & SN_DIRECT) ||
5805 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5806 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5807 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5808 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005809 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005810 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005811 /* the server is known, it's not the one the client requested, or the
5812 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005813 * insert a set-cookie here, except if we want to insert only on POST
5814 * requests and this one isn't. Note that servers which don't have cookies
5815 * (eg: some backup servers) will return a full cookie removal request.
5816 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005817 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005818 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005819 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5820 t->be->cookie_name);
5821 }
5822 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005823 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005824
5825 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5826 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005827 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5828 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5829 trash.len += 5;
5830
Willy Tarreauef4f3912010-10-07 21:00:29 +02005831 if (t->be->cookie_maxlife) {
5832 /* emit first_date, which is either the original one or
5833 * the current date.
5834 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005835 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005836 s30tob64(txn->cookie_first_date ?
5837 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005838 (date.tv_sec+3) >> 2, trash.str + trash.len);
5839 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005840 }
5841 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005842 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005843 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005844
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005845 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005846 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005847
Willy Tarreau4992dd22012-05-31 21:02:17 +02005848 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005849 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005850
5851 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005852 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005853
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005854 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005855 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005856
Willy Tarreauf1348312010-10-07 15:54:11 +02005857 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005858 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005859 /* the server did not change, only the date was updated */
5860 txn->flags |= TX_SCK_UPDATED;
5861 else
5862 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005863
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005864 /* Here, we will tell an eventual cache on the client side that we don't
5865 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5866 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5867 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5868 */
Willy Tarreau67402132012-05-31 20:40:20 +02005869 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005870
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005871 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5872
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005873 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005874 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005875 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005876 }
5877 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005878
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005879 /*
5880 * 7: check if result will be cacheable with a cookie.
5881 * We'll block the response if security checks have caught
5882 * nasty things such as a cacheable cookie.
5883 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005884 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5885 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005886 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005887
5888 /* we're in presence of a cacheable response containing
5889 * a set-cookie header. We'll block it as requested by
5890 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005892 if (objt_server(t->target))
5893 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005894
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005895 t->be->be_counters.denied_resp++;
5896 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005897 if (t->listener->counters)
5898 t->listener->counters->denied_resp++;
5899
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005900 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005901 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005902 send_log(t->be, LOG_ALERT,
5903 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005904 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005905 goto return_srv_prx_502;
5906 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005907
5908 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005909 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005910 * If an "Upgrade" token is found, the header is left untouched in order
5911 * not to have to deal with some client bugs : some of them fail an upgrade
5912 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005913 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005914 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5915 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5916 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005917 unsigned int want_flags = 0;
5918
5919 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5920 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5921 /* we want a keep-alive response here. Keep-alive header
5922 * required if either side is not 1.1.
5923 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005924 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005925 want_flags |= TX_CON_KAL_SET;
5926 }
5927 else {
5928 /* we want a close response here. Close header required if
5929 * the server is 1.1, regardless of the client.
5930 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005931 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005932 want_flags |= TX_CON_CLO_SET;
5933 }
5934
5935 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005936 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005937 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005938
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005939 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005940 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005941 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005942 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005943
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005944 /*************************************************************
5945 * OK, that's finished for the headers. We have done what we *
5946 * could. Let's switch to the DATA state. *
5947 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005948
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005949 /* if the user wants to log as soon as possible, without counting
5950 * bytes from the server, then this is the right moment. We have
5951 * to temporarily assign bytes_out to log what we currently have.
5952 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005953 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005954 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5955 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005956 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005957 t->logs.bytes_out = 0;
5958 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005960 /* Note: we must not try to cheat by jumping directly to DATA,
5961 * otherwise we would not let the client side wake up.
5962 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005963
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005964 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005965 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005966 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005967}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005968
Willy Tarreaud98cf932009-12-27 22:54:55 +01005969/* This function is an analyser which forwards response body (including chunk
5970 * sizes if any). It is called as soon as we must forward, even if we forward
5971 * zero byte. The only situation where it must not be called is when we're in
5972 * tunnel mode and we want to forward till the close. It's used both to forward
5973 * remaining data and to resync after end of body. It expects the msg_state to
5974 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5975 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005976 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005977 * bytes of pending data + the headers if not already done (between sol and sov).
5978 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005979 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005980int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005981{
5982 struct http_txn *txn = &s->txn;
5983 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005984 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005985 static struct buffer *tmpbuf = NULL;
5986 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005987 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005988 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005989
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005990 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5991 return 0;
5992
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005993 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005994 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005995 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005996 /* Output closed while we were sending data. We must abort and
5997 * wake the other side up.
5998 */
5999 msg->msg_state = HTTP_MSG_ERROR;
6000 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006001 return 1;
6002 }
6003
Willy Tarreau4fe41902010-06-07 22:27:41 +02006004 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006005 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006006
William Lallemand82fe75c2012-10-23 10:25:10 +02006007 /* this is the first time we need the compression buffer */
6008 if (s->comp_algo != NULL && tmpbuf == NULL) {
6009 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6010 goto aborted_xfer; /* no memory */
6011 }
6012
Willy Tarreaud98cf932009-12-27 22:54:55 +01006013 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006014 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006015 * rep->buf.p still points to the beginning of the message and msg->sol
6016 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006017 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006018 channel_forward(res, msg->sov);
6019 msg->next = 0;
6020 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006021
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006022 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006023 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006024 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006025 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006026 }
6027
William Lallemand82fe75c2012-10-23 10:25:10 +02006028 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006029 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02006030 if (ret < 0)
6031 goto missing_data; /* not enough spaces in buffers */
6032 compressing = 1;
6033 }
6034
Willy Tarreaud98cf932009-12-27 22:54:55 +01006035 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006036 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006037 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006038 if (s->comp_algo == NULL) {
6039 bytes = msg->sov - msg->sol;
6040 if (msg->chunk_len || bytes) {
6041 msg->sol = msg->sov;
6042 msg->next -= bytes; /* will be forwarded */
6043 msg->chunk_len += bytes;
6044 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6045 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006046 }
6047
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006048 switch (msg->msg_state - HTTP_MSG_DATA) {
6049 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006050 if (compressing) {
6051 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6052 if (ret < 0)
6053 goto aborted_xfer;
6054 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006055
6056 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006057 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01006058
6059 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006060 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006061 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006062 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006063 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006064 if (compressing && consumed_data) {
6065 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6066 compressing = 0;
6067 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006068 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006069 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006070 /* fall through for HTTP_MSG_CHUNK_CRLF */
6071
6072 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6073 /* we want the CRLF after the data */
6074
6075 ret = http_skip_chunk_crlf(msg);
6076 if (ret == 0)
6077 goto missing_data;
6078 else if (ret < 0) {
6079 if (msg->err_pos >= 0)
6080 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6081 goto return_bad_res;
6082 }
6083 /* skipping data in buffer for compression */
6084 if (compressing) {
6085 b_adv(res->buf, msg->next);
6086 msg->next = 0;
6087 msg->sov = 0;
6088 msg->sol = 0;
6089 }
6090 /* we're in MSG_CHUNK_SIZE now, fall through */
6091
6092 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006093 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006094 * set ->sov and ->next to point to the body and switch to DATA or
6095 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006096 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006097
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006098 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006099 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006100 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006101 else if (ret < 0) {
6102 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006103 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006104 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006105 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006106 if (compressing) {
6107 if (likely(msg->chunk_len > 0)) {
6108 /* skipping data if we are in compression mode */
6109 b_adv(res->buf, msg->next);
6110 msg->next = 0;
6111 msg->sov = 0;
6112 msg->sol = 0;
6113 } else {
6114 if (consumed_data) {
6115 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6116 compressing = 0;
6117 }
6118 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006119 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006120 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006121 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006122
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006123 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6124 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006125 if (ret == 0)
6126 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006127 else if (ret < 0) {
6128 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006129 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006130 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006131 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006132 if (s->comp_algo != NULL) {
6133 /* forwarding trailers */
6134 channel_forward(res, msg->next);
6135 msg->next = 0;
6136 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006137 /* we're in HTTP_MSG_DONE now, but we might still have
6138 * some data pending, so let's loop over once.
6139 */
6140 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006141
6142 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006143 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006144
6145 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006146 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006147 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6148 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006149 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006150 if (http_resync_states(s)) {
6151 http_silent_debug(__LINE__, s);
6152 /* some state changes occurred, maybe the analyser
6153 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006154 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006155 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006156 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006157 /* response errors are most likely due to
6158 * the client aborting the transfer.
6159 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006160 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006161 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006162 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006163 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006164 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006165 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006166 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006167 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006168 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006169 }
6170 }
6171
Willy Tarreaud98cf932009-12-27 22:54:55 +01006172 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006173 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006174 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6175 compressing = 0;
6176 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006177
6178 if (res->flags & CF_SHUTW)
6179 goto aborted_xfer;
6180
6181 /* stop waiting for data if the input is closed before the end. If the
6182 * client side was already closed, it means that the client has aborted,
6183 * so we don't want to count this as a server abort. Otherwise it's a
6184 * server abort.
6185 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006186 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006187 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6188 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006189 if (!(s->flags & SN_ERR_MASK))
6190 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006191 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006192 if (objt_server(s->target))
6193 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006194 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006195 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006196
Willy Tarreau40dba092010-03-04 18:14:51 +01006197 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006198 if (!s->req->analysers)
6199 goto return_bad_res;
6200
Willy Tarreauea953162012-05-18 23:41:28 +02006201 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006202 if (s->comp_algo == NULL) {
6203 bytes = msg->sov - msg->sol;
6204 if (msg->chunk_len || bytes) {
6205 msg->sol = msg->sov;
6206 msg->next -= bytes; /* will be forwarded */
6207 msg->chunk_len += bytes;
6208 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6209 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006210 }
6211
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006212 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006213 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006214 * Similarly, with keep-alive on the client side, we don't want to forward a
6215 * close.
6216 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006217 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006218 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6219 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006220 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006221
Willy Tarreau5c620922011-05-11 19:56:11 +02006222 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006223 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006224 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006225 * modes are already handled by the stream sock layer. We must not do
6226 * this in content-length mode because it could present the MSG_MORE
6227 * flag with the last block of forwarded data, which would cause an
6228 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006229 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006230 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006231 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006232
Willy Tarreaud98cf932009-12-27 22:54:55 +01006233 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006234 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006235 return 0;
6236
Willy Tarreau40dba092010-03-04 18:14:51 +01006237 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006238 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006239 if (objt_server(s->target))
6240 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006241
6242 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006243 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006244 /* don't send any error message as we're in the body */
6245 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006246 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006247 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006248 if (objt_server(s->target))
6249 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006250
6251 if (!(s->flags & SN_ERR_MASK))
6252 s->flags |= SN_ERR_PRXCOND;
6253 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006254 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006255 return 0;
6256
6257 aborted_xfer:
6258 txn->rsp.msg_state = HTTP_MSG_ERROR;
6259 /* don't send any error message as we're in the body */
6260 stream_int_retnclose(res->cons, NULL);
6261 res->analysers = 0;
6262 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6263
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006264 s->fe->fe_counters.cli_aborts++;
6265 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006266 if (objt_server(s->target))
6267 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006268
6269 if (!(s->flags & SN_ERR_MASK))
6270 s->flags |= SN_ERR_CLICL;
6271 if (!(s->flags & SN_FINST_MASK))
6272 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006273 return 0;
6274}
6275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006276/* Iterate the same filter through all request headers.
6277 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006278 * Since it can manage the switch to another backend, it updates the per-proxy
6279 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006281int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006283 char term;
6284 char *cur_ptr, *cur_end, *cur_next;
6285 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006286 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006287 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006288 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006290 last_hdr = 0;
6291
Willy Tarreau9b28e032012-10-12 23:49:43 +02006292 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006293 old_idx = 0;
6294
6295 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006296 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006297 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006298 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006299 (exp->action == ACT_ALLOW ||
6300 exp->action == ACT_DENY ||
6301 exp->action == ACT_TARPIT))
6302 return 0;
6303
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006304 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006305 if (!cur_idx)
6306 break;
6307
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006308 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006309 cur_ptr = cur_next;
6310 cur_end = cur_ptr + cur_hdr->len;
6311 cur_next = cur_end + cur_hdr->cr + 1;
6312
6313 /* Now we have one header between cur_ptr and cur_end,
6314 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315 */
6316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 /* The annoying part is that pattern matching needs
6318 * that we modify the contents to null-terminate all
6319 * strings before testing them.
6320 */
6321
6322 term = *cur_end;
6323 *cur_end = '\0';
6324
6325 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6326 switch (exp->action) {
6327 case ACT_SETBE:
6328 /* It is not possible to jump a second time.
6329 * FIXME: should we return an HTTP/500 here so that
6330 * the admin knows there's a problem ?
6331 */
6332 if (t->be != t->fe)
6333 break;
6334
6335 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006336 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006337 last_hdr = 1;
6338 break;
6339
6340 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006341 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006342 last_hdr = 1;
6343 break;
6344
6345 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006346 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 break;
6349
6350 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006351 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353 break;
6354
6355 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006356 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6357 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006358 /* FIXME: if the user adds a newline in the replacement, the
6359 * index will not be recalculated for now, and the new line
6360 * will not be counted as a new header.
6361 */
6362
6363 cur_end += delta;
6364 cur_next += delta;
6365 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006366 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006367 break;
6368
6369 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006370 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006371 cur_next += delta;
6372
Willy Tarreaufa355d42009-11-29 18:12:29 +01006373 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006374 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6375 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006376 cur_hdr->len = 0;
6377 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006378 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006379 break;
6380
6381 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006382 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006383 if (cur_end)
6384 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006386 /* keep the link from this header to next one in case of later
6387 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006388 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 old_idx = cur_idx;
6390 }
6391 return 0;
6392}
6393
6394
6395/* Apply the filter to the request line.
6396 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6397 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006398 * Since it can manage the switch to another backend, it updates the per-proxy
6399 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006400 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006401int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402{
6403 char term;
6404 char *cur_ptr, *cur_end;
6405 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006406 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006407 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408
Willy Tarreau3d300592007-03-18 18:34:41 +01006409 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006410 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006411 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006412 (exp->action == ACT_ALLOW ||
6413 exp->action == ACT_DENY ||
6414 exp->action == ACT_TARPIT))
6415 return 0;
6416 else if (exp->action == ACT_REMOVE)
6417 return 0;
6418
6419 done = 0;
6420
Willy Tarreau9b28e032012-10-12 23:49:43 +02006421 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006422 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006423
6424 /* Now we have the request line between cur_ptr and cur_end */
6425
6426 /* The annoying part is that pattern matching needs
6427 * that we modify the contents to null-terminate all
6428 * strings before testing them.
6429 */
6430
6431 term = *cur_end;
6432 *cur_end = '\0';
6433
6434 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6435 switch (exp->action) {
6436 case ACT_SETBE:
6437 /* It is not possible to jump a second time.
6438 * FIXME: should we return an HTTP/500 here so that
6439 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006440 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 if (t->be != t->fe)
6442 break;
6443
6444 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006445 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006446 done = 1;
6447 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006450 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 done = 1;
6452 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006455 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 done = 1;
6457 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006459 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006460 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006461 done = 1;
6462 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 case ACT_REPLACE:
6465 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006466 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6467 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 /* FIXME: if the user adds a newline in the replacement, the
6469 * index will not be recalculated for now, and the new line
6470 * will not be counted as a new header.
6471 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006472
Willy Tarreaufa355d42009-11-29 18:12:29 +01006473 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006474 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006475 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 HTTP_MSG_RQMETH,
6477 cur_ptr, cur_end + 1,
6478 NULL, NULL);
6479 if (unlikely(!cur_end))
6480 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006482 /* we have a full request and we know that we have either a CR
6483 * or an LF at <ptr>.
6484 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006485 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6486 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006487 /* there is no point trying this regex on headers */
6488 return 1;
6489 }
6490 }
6491 *cur_end = term; /* restore the string terminator */
6492 return done;
6493}
Willy Tarreau97de6242006-12-27 17:18:38 +01006494
Willy Tarreau58f10d72006-12-04 02:26:12 +01006495
Willy Tarreau58f10d72006-12-04 02:26:12 +01006496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006498 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006499 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006500 * unparsable request. Since it can manage the switch to another backend, it
6501 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006503int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006504{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006505 struct http_txn *txn = &s->txn;
6506 struct hdr_exp *exp;
6507
6508 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006509 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 /*
6512 * The interleaving of transformations and verdicts
6513 * makes it difficult to decide to continue or stop
6514 * the evaluation.
6515 */
6516
Willy Tarreau6c123b12010-01-28 20:22:06 +01006517 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6518 break;
6519
Willy Tarreau3d300592007-03-18 18:34:41 +01006520 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006521 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006522 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006523 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006524
6525 /* if this filter had a condition, evaluate it now and skip to
6526 * next filter if the condition does not match.
6527 */
6528 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006529 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006530 ret = acl_pass(ret);
6531 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6532 ret = !ret;
6533
6534 if (!ret)
6535 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 }
6537
6538 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006539 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 if (unlikely(ret < 0))
6541 return -1;
6542
6543 if (likely(ret == 0)) {
6544 /* The filter did not match the request, it can be
6545 * iterated through all headers.
6546 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006547 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006549 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551}
6552
6553
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554
Willy Tarreau58f10d72006-12-04 02:26:12 +01006555/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006556 * Try to retrieve the server associated to the appsession.
6557 * If the server is found, it's assigned to the session.
6558 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006559void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006560 struct http_txn *txn = &t->txn;
6561 appsess *asession = NULL;
6562 char *sessid_temp = NULL;
6563
Cyril Bontéb21570a2009-11-29 20:04:48 +01006564 if (len > t->be->appsession_len) {
6565 len = t->be->appsession_len;
6566 }
6567
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006568 if (t->be->options2 & PR_O2_AS_REQL) {
6569 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006570 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006571 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006572 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006573 }
6574
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006575 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006576 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6577 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6578 return;
6579 }
6580
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006581 memcpy(txn->sessid, buf, len);
6582 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006583 }
6584
6585 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6586 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6587 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6588 return;
6589 }
6590
Cyril Bontéb21570a2009-11-29 20:04:48 +01006591 memcpy(sessid_temp, buf, len);
6592 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006593
6594 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6595 /* free previously allocated memory */
6596 pool_free2(apools.sessid, sessid_temp);
6597
6598 if (asession != NULL) {
6599 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6600 if (!(t->be->options2 & PR_O2_AS_REQL))
6601 asession->request_count++;
6602
6603 if (asession->serverid != NULL) {
6604 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006605
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006606 while (srv) {
6607 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006608 if ((srv->state & SRV_RUNNING) ||
6609 (t->be->options & PR_O_PERSIST) ||
6610 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006611 /* we found the server and it's usable */
6612 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006613 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006614 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006615 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006616
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006617 break;
6618 } else {
6619 txn->flags &= ~TX_CK_MASK;
6620 txn->flags |= TX_CK_DOWN;
6621 }
6622 }
6623 srv = srv->next;
6624 }
6625 }
6626 }
6627}
6628
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629/* Find the end of a cookie value contained between <s> and <e>. It works the
6630 * same way as with headers above except that the semi-colon also ends a token.
6631 * See RFC2965 for more information. Note that it requires a valid header to
6632 * return a valid result.
6633 */
6634char *find_cookie_value_end(char *s, const char *e)
6635{
6636 int quoted, qdpair;
6637
6638 quoted = qdpair = 0;
6639 for (; s < e; s++) {
6640 if (qdpair) qdpair = 0;
6641 else if (quoted) {
6642 if (*s == '\\') qdpair = 1;
6643 else if (*s == '"') quoted = 0;
6644 }
6645 else if (*s == '"') quoted = 1;
6646 else if (*s == ',' || *s == ';') return s;
6647 }
6648 return s;
6649}
6650
6651/* Delete a value in a header between delimiters <from> and <next> in buffer
6652 * <buf>. The number of characters displaced is returned, and the pointer to
6653 * the first delimiter is updated if required. The function tries as much as
6654 * possible to respect the following principles :
6655 * - replace <from> delimiter by the <next> one unless <from> points to a
6656 * colon, in which case <next> is simply removed
6657 * - set exactly one space character after the new first delimiter, unless
6658 * there are not enough characters in the block being moved to do so.
6659 * - remove unneeded spaces before the previous delimiter and after the new
6660 * one.
6661 *
6662 * It is the caller's responsibility to ensure that :
6663 * - <from> points to a valid delimiter or the colon ;
6664 * - <next> points to a valid delimiter or the final CR/LF ;
6665 * - there are non-space chars before <from> ;
6666 * - there is a CR/LF at or after <next>.
6667 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006668int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006669{
6670 char *prev = *from;
6671
6672 if (*prev == ':') {
6673 /* We're removing the first value, preserve the colon and add a
6674 * space if possible.
6675 */
6676 if (!http_is_crlf[(unsigned char)*next])
6677 next++;
6678 prev++;
6679 if (prev < next)
6680 *prev++ = ' ';
6681
6682 while (http_is_spht[(unsigned char)*next])
6683 next++;
6684 } else {
6685 /* Remove useless spaces before the old delimiter. */
6686 while (http_is_spht[(unsigned char)*(prev-1)])
6687 prev--;
6688 *from = prev;
6689
6690 /* copy the delimiter and if possible a space if we're
6691 * not at the end of the line.
6692 */
6693 if (!http_is_crlf[(unsigned char)*next]) {
6694 *prev++ = *next++;
6695 if (prev + 1 < next)
6696 *prev++ = ' ';
6697 while (http_is_spht[(unsigned char)*next])
6698 next++;
6699 }
6700 }
6701 return buffer_replace2(buf, prev, next, NULL, 0);
6702}
6703
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006704/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006705 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006706 * desirable to call it only when needed. This code is quite complex because
6707 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6708 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006709 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006710void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006711{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006712 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006713 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006714 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6716 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006717
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006718 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006719 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006720 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006721
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006722 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006723 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006724 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006725
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006726 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006727 hdr_beg = hdr_next;
6728 hdr_end = hdr_beg + cur_hdr->len;
6729 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006730
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006731 /* We have one full header between hdr_beg and hdr_end, and the
6732 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006733 * "Cookie:" headers.
6734 */
6735
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006736 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006737 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738 old_idx = cur_idx;
6739 continue;
6740 }
6741
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006742 del_from = NULL; /* nothing to be deleted */
6743 preserve_hdr = 0; /* assume we may kill the whole header */
6744
Willy Tarreau58f10d72006-12-04 02:26:12 +01006745 /* Now look for cookies. Conforming to RFC2109, we have to support
6746 * attributes whose name begin with a '$', and associate them with
6747 * the right cookie, if we want to delete this cookie.
6748 * So there are 3 cases for each cookie read :
6749 * 1) it's a special attribute, beginning with a '$' : ignore it.
6750 * 2) it's a server id cookie that we *MAY* want to delete : save
6751 * some pointers on it (last semi-colon, beginning of cookie...)
6752 * 3) it's an application cookie : we *MAY* have to delete a previous
6753 * "special" cookie.
6754 * At the end of loop, if a "special" cookie remains, we may have to
6755 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006756 * *MUST* delete it.
6757 *
6758 * Note: RFC2965 is unclear about the processing of spaces around
6759 * the equal sign in the ATTR=VALUE form. A careful inspection of
6760 * the RFC explicitly allows spaces before it, and not within the
6761 * tokens (attrs or values). An inspection of RFC2109 allows that
6762 * too but section 10.1.3 lets one think that spaces may be allowed
6763 * after the equal sign too, resulting in some (rare) buggy
6764 * implementations trying to do that. So let's do what servers do.
6765 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6766 * allowed quoted strings in values, with any possible character
6767 * after a backslash, including control chars and delimitors, which
6768 * causes parsing to become ambiguous. Browsers also allow spaces
6769 * within values even without quotes.
6770 *
6771 * We have to keep multiple pointers in order to support cookie
6772 * removal at the beginning, middle or end of header without
6773 * corrupting the header. All of these headers are valid :
6774 *
6775 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6776 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6777 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6778 * | | | | | | | | |
6779 * | | | | | | | | hdr_end <--+
6780 * | | | | | | | +--> next
6781 * | | | | | | +----> val_end
6782 * | | | | | +-----------> val_beg
6783 * | | | | +--------------> equal
6784 * | | | +----------------> att_end
6785 * | | +---------------------> att_beg
6786 * | +--------------------------> prev
6787 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006788 */
6789
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006790 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6791 /* Iterate through all cookies on this line */
6792
6793 /* find att_beg */
6794 att_beg = prev + 1;
6795 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6796 att_beg++;
6797
6798 /* find att_end : this is the first character after the last non
6799 * space before the equal. It may be equal to hdr_end.
6800 */
6801 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006802
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006803 while (equal < hdr_end) {
6804 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006805 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006806 if (http_is_spht[(unsigned char)*equal++])
6807 continue;
6808 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006809 }
6810
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006811 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6812 * is between <att_beg> and <equal>, both may be identical.
6813 */
6814
6815 /* look for end of cookie if there is an equal sign */
6816 if (equal < hdr_end && *equal == '=') {
6817 /* look for the beginning of the value */
6818 val_beg = equal + 1;
6819 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6820 val_beg++;
6821
6822 /* find the end of the value, respecting quotes */
6823 next = find_cookie_value_end(val_beg, hdr_end);
6824
6825 /* make val_end point to the first white space or delimitor after the value */
6826 val_end = next;
6827 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6828 val_end--;
6829 } else {
6830 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006831 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006832
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006833 /* We have nothing to do with attributes beginning with '$'. However,
6834 * they will automatically be removed if a header before them is removed,
6835 * since they're supposed to be linked together.
6836 */
6837 if (*att_beg == '$')
6838 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006839
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 /* Ignore cookies with no equal sign */
6841 if (equal == next) {
6842 /* This is not our cookie, so we must preserve it. But if we already
6843 * scheduled another cookie for removal, we cannot remove the
6844 * complete header, but we can remove the previous block itself.
6845 */
6846 preserve_hdr = 1;
6847 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006848 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006849 val_end += delta;
6850 next += delta;
6851 hdr_end += delta;
6852 hdr_next += delta;
6853 cur_hdr->len += delta;
6854 http_msg_move_end(&txn->req, delta);
6855 prev = del_from;
6856 del_from = NULL;
6857 }
6858 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006859 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006860
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006861 /* if there are spaces around the equal sign, we need to
6862 * strip them otherwise we'll get trouble for cookie captures,
6863 * or even for rewrites. Since this happens extremely rarely,
6864 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006865 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006866 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6867 int stripped_before = 0;
6868 int stripped_after = 0;
6869
6870 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006871 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 equal += stripped_before;
6873 val_beg += stripped_before;
6874 }
6875
6876 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006877 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 val_beg += stripped_after;
6879 stripped_before += stripped_after;
6880 }
6881
6882 val_end += stripped_before;
6883 next += stripped_before;
6884 hdr_end += stripped_before;
6885 hdr_next += stripped_before;
6886 cur_hdr->len += stripped_before;
6887 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006888 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006890
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 /* First, let's see if we want to capture this cookie. We check
6892 * that we don't already have a client side cookie, because we
6893 * can only capture one. Also as an optimisation, we ignore
6894 * cookies shorter than the declared name.
6895 */
6896 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6897 (val_end - att_beg >= t->fe->capture_namelen) &&
6898 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6899 int log_len = val_end - att_beg;
6900
6901 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6902 Alert("HTTP logging : out of memory.\n");
6903 } else {
6904 if (log_len > t->fe->capture_len)
6905 log_len = t->fe->capture_len;
6906 memcpy(txn->cli_cookie, att_beg, log_len);
6907 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006910
Willy Tarreaubca99692010-10-06 19:25:55 +02006911 /* Persistence cookies in passive, rewrite or insert mode have the
6912 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006913 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006914 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006916 * For cookies in prefix mode, the form is :
6917 *
6918 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 */
6920 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6921 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6922 struct server *srv = t->be->srv;
6923 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006924
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006925 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6926 * have the server ID between val_beg and delim, and the original cookie between
6927 * delim+1 and val_end. Otherwise, delim==val_end :
6928 *
6929 * Cookie: NAME=SRV; # in all but prefix modes
6930 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6931 * | || || | |+-> next
6932 * | || || | +--> val_end
6933 * | || || +---------> delim
6934 * | || |+------------> val_beg
6935 * | || +-------------> att_end = equal
6936 * | |+-----------------> att_beg
6937 * | +------------------> prev
6938 * +-------------------------> hdr_beg
6939 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940
Willy Tarreau67402132012-05-31 20:40:20 +02006941 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006942 for (delim = val_beg; delim < val_end; delim++)
6943 if (*delim == COOKIE_DELIM)
6944 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006945 } else {
6946 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006947 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006948 /* Now check if the cookie contains a date field, which would
6949 * appear after a vertical bar ('|') just after the server name
6950 * and before the delimiter.
6951 */
6952 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6953 if (vbar1) {
6954 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006955 * right is the last seen date. It is a base64 encoded
6956 * 30-bit value representing the UNIX date since the
6957 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006958 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006959 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006960 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006961 if (val_end - vbar1 >= 5) {
6962 val = b64tos30(vbar1);
6963 if (val > 0)
6964 txn->cookie_last_date = val << 2;
6965 }
6966 /* look for a second vertical bar */
6967 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6968 if (vbar1 && (val_end - vbar1 > 5)) {
6969 val = b64tos30(vbar1 + 1);
6970 if (val > 0)
6971 txn->cookie_first_date = val << 2;
6972 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006973 }
6974 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975
Willy Tarreauf64d1412010-10-07 20:06:11 +02006976 /* if the cookie has an expiration date and the proxy wants to check
6977 * it, then we do that now. We first check if the cookie is too old,
6978 * then only if it has expired. We detect strict overflow because the
6979 * time resolution here is not great (4 seconds). Cookies with dates
6980 * in the future are ignored if their offset is beyond one day. This
6981 * allows an admin to fix timezone issues without expiring everyone
6982 * and at the same time avoids keeping unwanted side effects for too
6983 * long.
6984 */
6985 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006986 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6987 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006988 txn->flags &= ~TX_CK_MASK;
6989 txn->flags |= TX_CK_OLD;
6990 delim = val_beg; // let's pretend we have not found the cookie
6991 txn->cookie_first_date = 0;
6992 txn->cookie_last_date = 0;
6993 }
6994 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006995 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6996 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006997 txn->flags &= ~TX_CK_MASK;
6998 txn->flags |= TX_CK_EXPIRED;
6999 delim = val_beg; // let's pretend we have not found the cookie
7000 txn->cookie_first_date = 0;
7001 txn->cookie_last_date = 0;
7002 }
7003
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007004 /* Here, we'll look for the first running server which supports the cookie.
7005 * This allows to share a same cookie between several servers, for example
7006 * to dedicate backup servers to specific servers only.
7007 * However, to prevent clients from sticking to cookie-less backup server
7008 * when they have incidentely learned an empty cookie, we simply ignore
7009 * empty cookies and mark them as invalid.
7010 * The same behaviour is applied when persistence must be ignored.
7011 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007012 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007013 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007014
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007015 while (srv) {
7016 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7017 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7018 if ((srv->state & SRV_RUNNING) ||
7019 (t->be->options & PR_O_PERSIST) ||
7020 (t->flags & SN_FORCE_PRST)) {
7021 /* we found the server and we can use it */
7022 txn->flags &= ~TX_CK_MASK;
7023 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7024 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007025 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007026 break;
7027 } else {
7028 /* we found a server, but it's down,
7029 * mark it as such and go on in case
7030 * another one is available.
7031 */
7032 txn->flags &= ~TX_CK_MASK;
7033 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007034 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007036 srv = srv->next;
7037 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007038
Willy Tarreauf64d1412010-10-07 20:06:11 +02007039 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007040 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007041 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007042 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7043 txn->flags |= TX_CK_UNUSED;
7044 else
7045 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 /* depending on the cookie mode, we may have to either :
7049 * - delete the complete cookie if we're in insert+indirect mode, so that
7050 * the server never sees it ;
7051 * - remove the server id from the cookie value, and tag the cookie as an
7052 * application cookie so that it does not get accidentely removed later,
7053 * if we're in cookie prefix mode
7054 */
Willy Tarreau67402132012-05-31 20:40:20 +02007055 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007056 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007057
Willy Tarreau9b28e032012-10-12 23:49:43 +02007058 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007059 val_end += delta;
7060 next += delta;
7061 hdr_end += delta;
7062 hdr_next += delta;
7063 cur_hdr->len += delta;
7064 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007066 del_from = NULL;
7067 preserve_hdr = 1; /* we want to keep this cookie */
7068 }
7069 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007070 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 del_from = prev;
7072 }
7073 } else {
7074 /* This is not our cookie, so we must preserve it. But if we already
7075 * scheduled another cookie for removal, we cannot remove the
7076 * complete header, but we can remove the previous block itself.
7077 */
7078 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007080 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007081 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007082 if (att_beg >= del_from)
7083 att_beg += delta;
7084 if (att_end >= del_from)
7085 att_end += delta;
7086 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 val_end += delta;
7088 next += delta;
7089 hdr_end += delta;
7090 hdr_next += delta;
7091 cur_hdr->len += delta;
7092 http_msg_move_end(&txn->req, delta);
7093 prev = del_from;
7094 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007097
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007098 /* Look for the appsession cookie unless persistence must be ignored */
7099 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7100 int cmp_len, value_len;
7101 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007102
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 if (t->be->options2 & PR_O2_AS_PFX) {
7104 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7105 value_begin = att_beg + t->be->appsession_name_len;
7106 value_len = val_end - att_beg - t->be->appsession_name_len;
7107 } else {
7108 cmp_len = att_end - att_beg;
7109 value_begin = val_beg;
7110 value_len = val_end - val_beg;
7111 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007112
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007113 /* let's see if the cookie is our appcookie */
7114 if (cmp_len == t->be->appsession_name_len &&
7115 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7116 manage_client_side_appsession(t, value_begin, value_len);
7117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118 }
7119
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007120 /* continue with next cookie on this header line */
7121 att_beg = next;
7122 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 /* There are no more cookies on this line.
7125 * We may still have one (or several) marked for deletion at the
7126 * end of the line. We must do this now in two ways :
7127 * - if some cookies must be preserved, we only delete from the
7128 * mark to the end of line ;
7129 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007131 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007134 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007135 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007136 cur_hdr->len += delta;
7137 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007138 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007139
7140 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007141 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7142 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007143 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007144 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007147 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007148 }
7149
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007150 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007151 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007152 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007153}
7154
7155
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156/* Iterate the same filter through all response headers contained in <rtr>.
7157 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7158 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007159int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007160{
7161 char term;
7162 char *cur_ptr, *cur_end, *cur_next;
7163 int cur_idx, old_idx, last_hdr;
7164 struct http_txn *txn = &t->txn;
7165 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007166 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167
7168 last_hdr = 0;
7169
Willy Tarreau9b28e032012-10-12 23:49:43 +02007170 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171 old_idx = 0;
7172
7173 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007174 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007176 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 (exp->action == ACT_ALLOW ||
7178 exp->action == ACT_DENY))
7179 return 0;
7180
7181 cur_idx = txn->hdr_idx.v[old_idx].next;
7182 if (!cur_idx)
7183 break;
7184
7185 cur_hdr = &txn->hdr_idx.v[cur_idx];
7186 cur_ptr = cur_next;
7187 cur_end = cur_ptr + cur_hdr->len;
7188 cur_next = cur_end + cur_hdr->cr + 1;
7189
7190 /* Now we have one header between cur_ptr and cur_end,
7191 * and the next header starts at cur_next.
7192 */
7193
7194 /* The annoying part is that pattern matching needs
7195 * that we modify the contents to null-terminate all
7196 * strings before testing them.
7197 */
7198
7199 term = *cur_end;
7200 *cur_end = '\0';
7201
7202 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7203 switch (exp->action) {
7204 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007205 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 last_hdr = 1;
7207 break;
7208
7209 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007210 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 last_hdr = 1;
7212 break;
7213
7214 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007215 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7216 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217 /* FIXME: if the user adds a newline in the replacement, the
7218 * index will not be recalculated for now, and the new line
7219 * will not be counted as a new header.
7220 */
7221
7222 cur_end += delta;
7223 cur_next += delta;
7224 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007225 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226 break;
7227
7228 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007229 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007230 cur_next += delta;
7231
Willy Tarreaufa355d42009-11-29 18:12:29 +01007232 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7234 txn->hdr_idx.used--;
7235 cur_hdr->len = 0;
7236 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007237 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 break;
7239
7240 }
7241 }
7242 if (cur_end)
7243 *cur_end = term; /* restore the string terminator */
7244
7245 /* keep the link from this header to next one in case of later
7246 * removal of next header.
7247 */
7248 old_idx = cur_idx;
7249 }
7250 return 0;
7251}
7252
7253
7254/* Apply the filter to the status line in the response buffer <rtr>.
7255 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7256 * or -1 if a replacement resulted in an invalid status line.
7257 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007258int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007259{
7260 char term;
7261 char *cur_ptr, *cur_end;
7262 int done;
7263 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007264 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265
7266
Willy Tarreau3d300592007-03-18 18:34:41 +01007267 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007269 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270 (exp->action == ACT_ALLOW ||
7271 exp->action == ACT_DENY))
7272 return 0;
7273 else if (exp->action == ACT_REMOVE)
7274 return 0;
7275
7276 done = 0;
7277
Willy Tarreau9b28e032012-10-12 23:49:43 +02007278 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007279 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280
7281 /* Now we have the status line between cur_ptr and cur_end */
7282
7283 /* The annoying part is that pattern matching needs
7284 * that we modify the contents to null-terminate all
7285 * strings before testing them.
7286 */
7287
7288 term = *cur_end;
7289 *cur_end = '\0';
7290
7291 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7292 switch (exp->action) {
7293 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007294 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295 done = 1;
7296 break;
7297
7298 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007299 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007300 done = 1;
7301 break;
7302
7303 case ACT_REPLACE:
7304 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007305 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7306 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 /* FIXME: if the user adds a newline in the replacement, the
7308 * index will not be recalculated for now, and the new line
7309 * will not be counted as a new header.
7310 */
7311
Willy Tarreaufa355d42009-11-29 18:12:29 +01007312 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007314 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007315 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007316 cur_ptr, cur_end + 1,
7317 NULL, NULL);
7318 if (unlikely(!cur_end))
7319 return -1;
7320
7321 /* we have a full respnse and we know that we have either a CR
7322 * or an LF at <ptr>.
7323 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007324 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007325 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326 /* there is no point trying this regex on headers */
7327 return 1;
7328 }
7329 }
7330 *cur_end = term; /* restore the string terminator */
7331 return done;
7332}
7333
7334
7335
7336/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007337 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007338 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7339 * unparsable response.
7340 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007341int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007343 struct http_txn *txn = &s->txn;
7344 struct hdr_exp *exp;
7345
7346 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347 int ret;
7348
7349 /*
7350 * The interleaving of transformations and verdicts
7351 * makes it difficult to decide to continue or stop
7352 * the evaluation.
7353 */
7354
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007355 if (txn->flags & TX_SVDENY)
7356 break;
7357
Willy Tarreau3d300592007-03-18 18:34:41 +01007358 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7360 exp->action == ACT_PASS)) {
7361 exp = exp->next;
7362 continue;
7363 }
7364
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007365 /* if this filter had a condition, evaluate it now and skip to
7366 * next filter if the condition does not match.
7367 */
7368 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007369 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007370 ret = acl_pass(ret);
7371 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7372 ret = !ret;
7373 if (!ret)
7374 continue;
7375 }
7376
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007378 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007379 if (unlikely(ret < 0))
7380 return -1;
7381
7382 if (likely(ret == 0)) {
7383 /* The filter did not match the response, it can be
7384 * iterated through all headers.
7385 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007386 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 }
7389 return 0;
7390}
7391
7392
Willy Tarreaua15645d2007-03-18 16:22:39 +01007393/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007394 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007395 * desirable to call it only when needed. This function is also used when we
7396 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007397 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007398void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007399{
7400 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007401 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007402 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007404 char *hdr_beg, *hdr_end, *hdr_next;
7405 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 /* Iterate through the headers.
7408 * we start with the start line.
7409 */
7410 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007411 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412
7413 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7414 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007415 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416
7417 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007418 hdr_beg = hdr_next;
7419 hdr_end = hdr_beg + cur_hdr->len;
7420 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421
Willy Tarreau24581ba2010-08-31 22:39:35 +02007422 /* We have one full header between hdr_beg and hdr_end, and the
7423 * next header starts at hdr_next. We're only interested in
7424 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007425 */
7426
Willy Tarreau24581ba2010-08-31 22:39:35 +02007427 is_cookie2 = 0;
7428 prev = hdr_beg + 10;
7429 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007430 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7432 if (!val) {
7433 old_idx = cur_idx;
7434 continue;
7435 }
7436 is_cookie2 = 1;
7437 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007438 }
7439
Willy Tarreau24581ba2010-08-31 22:39:35 +02007440 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7441 * <prev> points to the colon.
7442 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007443 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444
Willy Tarreau24581ba2010-08-31 22:39:35 +02007445 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7446 * check-cache is enabled) and we are not interested in checking
7447 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007448 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007449 if (t->be->cookie_name == NULL &&
7450 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007451 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452 return;
7453
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 /* OK so now we know we have to process this response cookie.
7455 * The format of the Set-Cookie header is slightly different
7456 * from the format of the Cookie header in that it does not
7457 * support the comma as a cookie delimiter (thus the header
7458 * cannot be folded) because the Expires attribute described in
7459 * the original Netscape's spec may contain an unquoted date
7460 * with a comma inside. We have to live with this because
7461 * many browsers don't support Max-Age and some browsers don't
7462 * support quoted strings. However the Set-Cookie2 header is
7463 * clean.
7464 *
7465 * We have to keep multiple pointers in order to support cookie
7466 * removal at the beginning, middle or end of header without
7467 * corrupting the header (in case of set-cookie2). A special
7468 * pointer, <scav> points to the beginning of the set-cookie-av
7469 * fields after the first semi-colon. The <next> pointer points
7470 * either to the end of line (set-cookie) or next unquoted comma
7471 * (set-cookie2). All of these headers are valid :
7472 *
7473 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7474 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7475 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7476 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7477 * | | | | | | | | | |
7478 * | | | | | | | | +-> next hdr_end <--+
7479 * | | | | | | | +------------> scav
7480 * | | | | | | +--------------> val_end
7481 * | | | | | +--------------------> val_beg
7482 * | | | | +----------------------> equal
7483 * | | | +------------------------> att_end
7484 * | | +----------------------------> att_beg
7485 * | +------------------------------> prev
7486 * +-----------------------------------------> hdr_beg
7487 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488
Willy Tarreau24581ba2010-08-31 22:39:35 +02007489 for (; prev < hdr_end; prev = next) {
7490 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007491
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 /* find att_beg */
7493 att_beg = prev + 1;
7494 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7495 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496
Willy Tarreau24581ba2010-08-31 22:39:35 +02007497 /* find att_end : this is the first character after the last non
7498 * space before the equal. It may be equal to hdr_end.
7499 */
7500 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 while (equal < hdr_end) {
7503 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7504 break;
7505 if (http_is_spht[(unsigned char)*equal++])
7506 continue;
7507 att_end = equal;
7508 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509
Willy Tarreau24581ba2010-08-31 22:39:35 +02007510 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7511 * is between <att_beg> and <equal>, both may be identical.
7512 */
7513
7514 /* look for end of cookie if there is an equal sign */
7515 if (equal < hdr_end && *equal == '=') {
7516 /* look for the beginning of the value */
7517 val_beg = equal + 1;
7518 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7519 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 /* find the end of the value, respecting quotes */
7522 next = find_cookie_value_end(val_beg, hdr_end);
7523
7524 /* make val_end point to the first white space or delimitor after the value */
7525 val_end = next;
7526 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7527 val_end--;
7528 } else {
7529 /* <equal> points to next comma, semi-colon or EOL */
7530 val_beg = val_end = next = equal;
7531 }
7532
7533 if (next < hdr_end) {
7534 /* Set-Cookie2 supports multiple cookies, and <next> points to
7535 * a colon or semi-colon before the end. So skip all attr-value
7536 * pairs and look for the next comma. For Set-Cookie, since
7537 * commas are permitted in values, skip to the end.
7538 */
7539 if (is_cookie2)
7540 next = find_hdr_value_end(next, hdr_end);
7541 else
7542 next = hdr_end;
7543 }
7544
7545 /* Now everything is as on the diagram above */
7546
7547 /* Ignore cookies with no equal sign */
7548 if (equal == val_end)
7549 continue;
7550
7551 /* If there are spaces around the equal sign, we need to
7552 * strip them otherwise we'll get trouble for cookie captures,
7553 * or even for rewrites. Since this happens extremely rarely,
7554 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007556 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7557 int stripped_before = 0;
7558 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007559
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007561 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007562 equal += stripped_before;
7563 val_beg += stripped_before;
7564 }
7565
7566 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007567 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007568 val_beg += stripped_after;
7569 stripped_before += stripped_after;
7570 }
7571
7572 val_end += stripped_before;
7573 next += stripped_before;
7574 hdr_end += stripped_before;
7575 hdr_next += stripped_before;
7576 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007577 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007578 }
7579
7580 /* First, let's see if we want to capture this cookie. We check
7581 * that we don't already have a server side cookie, because we
7582 * can only capture one. Also as an optimisation, we ignore
7583 * cookies shorter than the declared name.
7584 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007585 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007586 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007587 (val_end - att_beg >= t->fe->capture_namelen) &&
7588 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7589 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007590 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007591 Alert("HTTP logging : out of memory.\n");
7592 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007593 else {
7594 if (log_len > t->fe->capture_len)
7595 log_len = t->fe->capture_len;
7596 memcpy(txn->srv_cookie, att_beg, log_len);
7597 txn->srv_cookie[log_len] = 0;
7598 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007599 }
7600
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007601 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007603 if (!(t->flags & SN_IGNORE_PRST) &&
7604 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7605 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007606 /* assume passive cookie by default */
7607 txn->flags &= ~TX_SCK_MASK;
7608 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609
7610 /* If the cookie is in insert mode on a known server, we'll delete
7611 * this occurrence because we'll insert another one later.
7612 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007613 * a direct access.
7614 */
Willy Tarreau67402132012-05-31 20:40:20 +02007615 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007616 /* The "preserve" flag was set, we don't want to touch the
7617 * server's cookie.
7618 */
7619 }
Willy Tarreau67402132012-05-31 20:40:20 +02007620 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7621 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007622 /* this cookie must be deleted */
7623 if (*prev == ':' && next == hdr_end) {
7624 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007625 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007626 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7627 txn->hdr_idx.used--;
7628 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007629 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007630 hdr_next += delta;
7631 http_msg_move_end(&txn->rsp, delta);
7632 /* note: while both invalid now, <next> and <hdr_end>
7633 * are still equal, so the for() will stop as expected.
7634 */
7635 } else {
7636 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007637 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007638 next = prev;
7639 hdr_end += delta;
7640 hdr_next += delta;
7641 cur_hdr->len += delta;
7642 http_msg_move_end(&txn->rsp, delta);
7643 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007644 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007645 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007646 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647 }
Willy Tarreau67402132012-05-31 20:40:20 +02007648 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007649 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650 * with this server since we know it.
7651 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007652 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007653 next += delta;
7654 hdr_end += delta;
7655 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007657 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007658
Willy Tarreauf1348312010-10-07 15:54:11 +02007659 txn->flags &= ~TX_SCK_MASK;
7660 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007661 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007662 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007663 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007664 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007666 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 next += delta;
7668 hdr_end += delta;
7669 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007671 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007672
Willy Tarreau827aee92011-03-10 16:55:02 +01007673 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007674 txn->flags &= ~TX_SCK_MASK;
7675 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 }
7677 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007678 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7679 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007680 int cmp_len, value_len;
7681 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682
Cyril Bontéb21570a2009-11-29 20:04:48 +01007683 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007684 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7685 value_begin = att_beg + t->be->appsession_name_len;
7686 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007687 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007688 cmp_len = att_end - att_beg;
7689 value_begin = val_beg;
7690 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007691 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007692
Cyril Bonté17530c32010-04-06 21:11:10 +02007693 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007694 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7695 /* free a possibly previously allocated memory */
7696 pool_free2(apools.sessid, txn->sessid);
7697
Cyril Bontéb21570a2009-11-29 20:04:48 +01007698 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007699 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007700 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7701 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7702 return;
7703 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007704 memcpy(txn->sessid, value_begin, value_len);
7705 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007706 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007707 }
7708 /* that's done for this cookie, check the next one on the same
7709 * line when next != hdr_end (only if is_cookie2).
7710 */
7711 }
7712 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007713 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007714 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007715
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007716 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007717 appsess *asession = NULL;
7718 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007719 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007720 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007721 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007722 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7723 Alert("Not enough Memory process_srv():asession:calloc().\n");
7724 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7725 return;
7726 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007727 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7728
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007729 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7730 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7731 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007732 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007733 return;
7734 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007735 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007736 asession->sessid[t->be->appsession_len] = 0;
7737
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007738 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007739 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007740 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007741 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007742 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007743 return;
7744 }
7745 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007746 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007747
7748 asession->request_count = 0;
7749 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7750 }
7751
7752 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7753 asession->request_count++;
7754 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007755}
7756
7757
Willy Tarreaua15645d2007-03-18 16:22:39 +01007758/*
7759 * Check if response is cacheable or not. Updates t->flags.
7760 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007761void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007762{
7763 struct http_txn *txn = &t->txn;
7764 char *p1, *p2;
7765
7766 char *cur_ptr, *cur_end, *cur_next;
7767 int cur_idx;
7768
Willy Tarreau5df51872007-11-25 16:20:08 +01007769 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007770 return;
7771
7772 /* Iterate through the headers.
7773 * we start with the start line.
7774 */
7775 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007776 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777
7778 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7779 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007780 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781
7782 cur_hdr = &txn->hdr_idx.v[cur_idx];
7783 cur_ptr = cur_next;
7784 cur_end = cur_ptr + cur_hdr->len;
7785 cur_next = cur_end + cur_hdr->cr + 1;
7786
7787 /* We have one full header between cur_ptr and cur_end, and the
7788 * next header starts at cur_next. We're only interested in
7789 * "Cookie:" headers.
7790 */
7791
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007792 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7793 if (val) {
7794 if ((cur_end - (cur_ptr + val) >= 8) &&
7795 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7796 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7797 return;
7798 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 }
7800
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007801 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7802 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007803 continue;
7804
7805 /* OK, right now we know we have a cache-control header at cur_ptr */
7806
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007807 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007808
7809 if (p1 >= cur_end) /* no more info */
7810 continue;
7811
7812 /* p1 is at the beginning of the value */
7813 p2 = p1;
7814
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007815 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007816 p2++;
7817
7818 /* we have a complete value between p1 and p2 */
7819 if (p2 < cur_end && *p2 == '=') {
7820 /* we have something of the form no-cache="set-cookie" */
7821 if ((cur_end - p1 >= 21) &&
7822 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7823 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007824 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 continue;
7826 }
7827
7828 /* OK, so we know that either p2 points to the end of string or to a comma */
7829 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007830 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7832 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7833 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007834 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 return;
7836 }
7837
7838 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007839 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840 continue;
7841 }
7842 }
7843}
7844
7845
Willy Tarreau58f10d72006-12-04 02:26:12 +01007846/*
7847 * Try to retrieve a known appsession in the URI, then the associated server.
7848 * If the server is found, it's assigned to the session.
7849 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007850void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007851{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007852 char *end_params, *first_param, *cur_param, *next_param;
7853 char separator;
7854 int value_len;
7855
7856 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007857
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007858 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007859 (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 +01007860 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007861 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007862
Cyril Bontéb21570a2009-11-29 20:04:48 +01007863 first_param = NULL;
7864 switch (mode) {
7865 case PR_O2_AS_M_PP:
7866 first_param = memchr(begin, ';', len);
7867 break;
7868 case PR_O2_AS_M_QS:
7869 first_param = memchr(begin, '?', len);
7870 break;
7871 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007872
Cyril Bontéb21570a2009-11-29 20:04:48 +01007873 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007875 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007876
Cyril Bontéb21570a2009-11-29 20:04:48 +01007877 switch (mode) {
7878 case PR_O2_AS_M_PP:
7879 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7880 end_params = (char *) begin + len;
7881 }
7882 separator = ';';
7883 break;
7884 case PR_O2_AS_M_QS:
7885 end_params = (char *) begin + len;
7886 separator = '&';
7887 break;
7888 default:
7889 /* unknown mode, shouldn't happen */
7890 return;
7891 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007892
Cyril Bontéb21570a2009-11-29 20:04:48 +01007893 cur_param = next_param = end_params;
7894 while (cur_param > first_param) {
7895 cur_param--;
7896 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7897 /* let's see if this is the appsession parameter */
7898 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7899 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7900 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7901 /* Cool... it's the right one */
7902 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7903 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7904 if (value_len > 0) {
7905 manage_client_side_appsession(t, cur_param, value_len);
7906 }
7907 break;
7908 }
7909 next_param = cur_param;
7910 }
7911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007912#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007913 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007914 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007915#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007916}
7917
Willy Tarreaub2513902006-12-17 14:52:38 +01007918/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007919 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007920 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007921 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007922 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007923 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007924 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007925 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007926 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007927int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007928{
7929 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007930 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007931 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007932 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007933
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007934 if (!uri_auth)
7935 return 0;
7936
Cyril Bonté70be45d2010-10-12 00:14:35 +02007937 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007938 return 0;
7939
Willy Tarreau295a8372011-03-10 11:25:07 +01007940 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007941 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007942 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007943
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007944 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007945 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007946 return 0;
7947
Willy Tarreau3a215be2012-03-09 21:39:51 +01007948 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007949 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007950 return 0;
7951
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007952 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007953 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007954 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007955 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007956 break;
7957 }
7958 h++;
7959 }
7960
7961 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007962 h = uri + uri_auth->uri_len;
7963 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007964 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007965 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007966 break;
7967 }
7968 h++;
7969 }
7970 }
7971
Willy Tarreau3a215be2012-03-09 21:39:51 +01007972 h = uri + uri_auth->uri_len;
7973 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007974 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007975 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007976 break;
7977 }
7978 h++;
7979 }
7980
Willy Tarreau3a215be2012-03-09 21:39:51 +01007981 h = uri + uri_auth->uri_len;
7982 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007983 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007984 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007985 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007986 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007987 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7988 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7989 si->applet.ctx.stats.st_code = i;
7990 break;
7991 }
7992 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007993 break;
7994 }
7995 h++;
7996 }
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02007997
7998 si->applet.ctx.stats.scope_str = 0;
7999 si->applet.ctx.stats.scope_len = 0;
8000 h = uri + uri_auth->uri_len;
8001 while (h <= uri + msg->sl.rq.u_l - 8) {
8002 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
8003 int itx = 0;
8004 const char *h2;
8005 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
8006 const char *err;
8007
8008 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
8009 h2 = h;
8010 si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p;
8011 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
8012 itx++;
8013 h++;
8014 }
8015
8016 if (itx > STAT_SCOPE_TXT_MAXLEN)
8017 itx = STAT_SCOPE_TXT_MAXLEN;
8018 si->applet.ctx.stats.scope_len = itx;
8019
8020 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
8021 memcpy(scope_txt, h2, itx);
8022 scope_txt[itx] = '\0';
8023 err = invalid_char(scope_txt);
8024 if (err) {
8025 /* bad char in search text => clear scope */
8026 si->applet.ctx.stats.scope_str = 0;
8027 si->applet.ctx.stats.scope_len = 0;
8028 }
8029 break;
8030 }
8031 h++;
8032 }
8033
8034
Willy Tarreaub2513902006-12-17 14:52:38 +01008035 return 1;
8036}
8037
Willy Tarreau4076a152009-04-02 15:18:36 +02008038/*
8039 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008040 * By default it tries to report the error position as msg->err_pos. However if
8041 * this one is not set, it will then report msg->next, which is the last known
8042 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008043 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008044 */
8045void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008046 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01008047 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008048{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008049 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008050 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008051
Willy Tarreau9b28e032012-10-12 23:49:43 +02008052 es->len = MIN(chn->buf->i, sizeof(es->buf));
8053 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008054 len1 = MIN(len1, es->len);
8055 len2 = es->len - len1; /* remaining data if buffer wraps */
8056
Willy Tarreau9b28e032012-10-12 23:49:43 +02008057 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008058 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008059 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008060
Willy Tarreau4076a152009-04-02 15:18:36 +02008061 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008062 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008063 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008064 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008065
Willy Tarreau4076a152009-04-02 15:18:36 +02008066 es->when = date; // user-visible date
8067 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008068 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008069 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008070 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01008071 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008072 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008073 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008074 es->s_flags = s->flags;
8075 es->t_flags = s->txn.flags;
8076 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008077 es->b_out = chn->buf->o;
8078 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008079 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008080 es->m_clen = msg->chunk_len;
8081 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008082}
Willy Tarreaub2513902006-12-17 14:52:38 +01008083
Willy Tarreau294c4732011-12-16 21:35:50 +01008084/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8085 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8086 * performed over the whole headers. Otherwise it must contain a valid header
8087 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8088 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8089 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8090 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008091 * -1. The value fetch stops at commas, so this function is suited for use with
8092 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008093 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008094 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008095unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008096 struct hdr_idx *idx, int occ,
8097 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008098{
Willy Tarreau294c4732011-12-16 21:35:50 +01008099 struct hdr_ctx local_ctx;
8100 char *ptr_hist[MAX_HDR_HISTORY];
8101 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008102 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008103 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008104
Willy Tarreau294c4732011-12-16 21:35:50 +01008105 if (!ctx) {
8106 local_ctx.idx = 0;
8107 ctx = &local_ctx;
8108 }
8109
Willy Tarreaubce70882009-09-07 11:51:47 +02008110 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008111 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008112 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008113 occ--;
8114 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008115 *vptr = ctx->line + ctx->val;
8116 *vlen = ctx->vlen;
8117 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008118 }
8119 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008120 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008121 }
8122
8123 /* negative occurrence, we scan all the list then walk back */
8124 if (-occ > MAX_HDR_HISTORY)
8125 return 0;
8126
Willy Tarreau294c4732011-12-16 21:35:50 +01008127 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008128 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008129 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8130 len_hist[hist_ptr] = ctx->vlen;
8131 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008132 hist_ptr = 0;
8133 found++;
8134 }
8135 if (-occ > found)
8136 return 0;
8137 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008138 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8139 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8140 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008141 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008142 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008143 if (hist_ptr >= MAX_HDR_HISTORY)
8144 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008145 *vptr = ptr_hist[hist_ptr];
8146 *vlen = len_hist[hist_ptr];
8147 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008148}
8149
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008150/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8151 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8152 * performed over the whole headers. Otherwise it must contain a valid header
8153 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8154 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8155 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8156 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8157 * -1. This function differs from http_get_hdr() in that it only returns full
8158 * line header values and does not stop at commas.
8159 * The return value is 0 if nothing was found, or non-zero otherwise.
8160 */
8161unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8162 struct hdr_idx *idx, int occ,
8163 struct hdr_ctx *ctx, char **vptr, int *vlen)
8164{
8165 struct hdr_ctx local_ctx;
8166 char *ptr_hist[MAX_HDR_HISTORY];
8167 int len_hist[MAX_HDR_HISTORY];
8168 unsigned int hist_ptr;
8169 int found;
8170
8171 if (!ctx) {
8172 local_ctx.idx = 0;
8173 ctx = &local_ctx;
8174 }
8175
8176 if (occ >= 0) {
8177 /* search from the beginning */
8178 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8179 occ--;
8180 if (occ <= 0) {
8181 *vptr = ctx->line + ctx->val;
8182 *vlen = ctx->vlen;
8183 return 1;
8184 }
8185 }
8186 return 0;
8187 }
8188
8189 /* negative occurrence, we scan all the list then walk back */
8190 if (-occ > MAX_HDR_HISTORY)
8191 return 0;
8192
8193 found = hist_ptr = 0;
8194 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8195 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8196 len_hist[hist_ptr] = ctx->vlen;
8197 if (++hist_ptr >= MAX_HDR_HISTORY)
8198 hist_ptr = 0;
8199 found++;
8200 }
8201 if (-occ > found)
8202 return 0;
8203 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8204 * find occurrence -occ, so we have to check [hist_ptr+occ].
8205 */
8206 hist_ptr += occ;
8207 if (hist_ptr >= MAX_HDR_HISTORY)
8208 hist_ptr -= MAX_HDR_HISTORY;
8209 *vptr = ptr_hist[hist_ptr];
8210 *vlen = len_hist[hist_ptr];
8211 return 1;
8212}
8213
Willy Tarreaubaaee002006-06-26 02:48:02 +02008214/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008215 * Print a debug line with a header. Always stop at the first CR or LF char,
8216 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8217 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008218 */
8219void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8220{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008221 int max;
8222 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01008223 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
8224 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008225
8226 for (max = 0; start + max < end; max++)
8227 if (start[max] == '\r' || start[max] == '\n')
8228 break;
8229
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008230 UBOUND(max, trash.size - trash.len - 3);
8231 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8232 trash.str[trash.len++] = '\n';
8233 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008234}
8235
Willy Tarreau0937bc42009-12-22 15:03:09 +01008236/*
8237 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8238 * the required fields are properly allocated and that we only need to (re)init
8239 * them. This should be used before processing any new request.
8240 */
8241void http_init_txn(struct session *s)
8242{
8243 struct http_txn *txn = &s->txn;
8244 struct proxy *fe = s->fe;
8245
8246 txn->flags = 0;
8247 txn->status = -1;
8248
Willy Tarreauf64d1412010-10-07 20:06:11 +02008249 txn->cookie_first_date = 0;
8250 txn->cookie_last_date = 0;
8251
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008252 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008253 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008254 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008255 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008256 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008257 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008258 txn->req.chunk_len = 0LL;
8259 txn->req.body_len = 0LL;
8260 txn->rsp.chunk_len = 0LL;
8261 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008262 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8263 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008264 txn->req.chn = s->req;
8265 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008266
8267 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008268
8269 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8270 if (fe->options2 & PR_O2_REQBUG_OK)
8271 txn->req.err_pos = -1; /* let buggy requests pass */
8272
Willy Tarreau46023632010-01-07 22:51:47 +01008273 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008274 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8275
Willy Tarreau46023632010-01-07 22:51:47 +01008276 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008277 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8278
8279 if (txn->hdr_idx.v)
8280 hdr_idx_init(&txn->hdr_idx);
8281}
8282
8283/* to be used at the end of a transaction */
8284void http_end_txn(struct session *s)
8285{
8286 struct http_txn *txn = &s->txn;
8287
8288 /* these ones will have been dynamically allocated */
8289 pool_free2(pool2_requri, txn->uri);
8290 pool_free2(pool2_capture, txn->cli_cookie);
8291 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008292 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008293 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008294
William Lallemanda73203e2012-03-12 12:48:57 +01008295 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008296 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008297 txn->uri = NULL;
8298 txn->srv_cookie = NULL;
8299 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008300
8301 if (txn->req.cap) {
8302 struct cap_hdr *h;
8303 for (h = s->fe->req_cap; h; h = h->next)
8304 pool_free2(h->pool, txn->req.cap[h->index]);
8305 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8306 }
8307
8308 if (txn->rsp.cap) {
8309 struct cap_hdr *h;
8310 for (h = s->fe->rsp_cap; h; h = h->next)
8311 pool_free2(h->pool, txn->rsp.cap[h->index]);
8312 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8313 }
8314
Willy Tarreau0937bc42009-12-22 15:03:09 +01008315}
8316
8317/* to be used at the end of a transaction to prepare a new one */
8318void http_reset_txn(struct session *s)
8319{
8320 http_end_txn(s);
8321 http_init_txn(s);
8322
8323 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008324 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008325 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008326 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008327 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008328 /* re-init store persistence */
8329 s->store_count = 0;
8330
Willy Tarreau0937bc42009-12-22 15:03:09 +01008331 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008332
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008333 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008334
Willy Tarreau739cfba2010-01-25 23:11:14 +01008335 /* We must trim any excess data from the response buffer, because we
8336 * may have blocked an invalid response from a server that we don't
8337 * want to accidentely forward once we disable the analysers, nor do
8338 * we want those data to come along with next response. A typical
8339 * example of such data would be from a buggy server responding to
8340 * a HEAD with some data, or sending more than the advertised
8341 * content-length.
8342 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008343 if (unlikely(s->rep->buf->i))
8344 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008345
Willy Tarreau0937bc42009-12-22 15:03:09 +01008346 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008347 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008348
Willy Tarreaud04e8582010-05-31 12:31:35 +02008349 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008350 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008351
8352 s->req->rex = TICK_ETERNITY;
8353 s->req->wex = TICK_ETERNITY;
8354 s->req->analyse_exp = TICK_ETERNITY;
8355 s->rep->rex = TICK_ETERNITY;
8356 s->rep->wex = TICK_ETERNITY;
8357 s->rep->analyse_exp = TICK_ETERNITY;
8358}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008359
Willy Tarreauff011f22011-01-06 17:51:27 +01008360void free_http_req_rules(struct list *r) {
8361 struct http_req_rule *tr, *pr;
8362
8363 list_for_each_entry_safe(pr, tr, r, list) {
8364 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008365 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008366 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008367
8368 free(pr);
8369 }
8370}
8371
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008372/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008373struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8374{
8375 struct http_req_rule *rule;
8376 int cur_arg;
8377
8378 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8379 if (!rule) {
8380 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008381 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008382 }
8383
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008384 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008385 rule->action = HTTP_REQ_ACT_ALLOW;
8386 cur_arg = 1;
8387 } else if (!strcmp(args[0], "deny")) {
8388 rule->action = HTTP_REQ_ACT_DENY;
8389 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008390 } else if (!strcmp(args[0], "tarpit")) {
8391 rule->action = HTTP_REQ_ACT_TARPIT;
8392 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008393 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008394 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008395 cur_arg = 1;
8396
8397 while(*args[cur_arg]) {
8398 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008399 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008400 cur_arg+=2;
8401 continue;
8402 } else
8403 break;
8404 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008405 } else if (!strcmp(args[0], "set-nice")) {
8406 rule->action = HTTP_REQ_ACT_SET_NICE;
8407 cur_arg = 1;
8408
8409 if (!*args[cur_arg] ||
8410 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8411 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8412 file, linenum, args[0]);
8413 goto out_err;
8414 }
8415 rule->arg.nice = atoi(args[cur_arg]);
8416 if (rule->arg.nice < -1024)
8417 rule->arg.nice = -1024;
8418 else if (rule->arg.nice > 1024)
8419 rule->arg.nice = 1024;
8420 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008421 } else if (!strcmp(args[0], "set-tos")) {
8422#ifdef IP_TOS
8423 char *err;
8424 rule->action = HTTP_REQ_ACT_SET_TOS;
8425 cur_arg = 1;
8426
8427 if (!*args[cur_arg] ||
8428 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8429 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8430 file, linenum, args[0]);
8431 goto out_err;
8432 }
8433
8434 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8435 if (err && *err != '\0') {
8436 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8437 file, linenum, err, args[0]);
8438 goto out_err;
8439 }
8440 cur_arg++;
8441#else
8442 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8443 goto out_err;
8444#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008445 } else if (!strcmp(args[0], "set-mark")) {
8446#ifdef SO_MARK
8447 char *err;
8448 rule->action = HTTP_REQ_ACT_SET_MARK;
8449 cur_arg = 1;
8450
8451 if (!*args[cur_arg] ||
8452 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8453 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8454 file, linenum, args[0]);
8455 goto out_err;
8456 }
8457
8458 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8459 if (err && *err != '\0') {
8460 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8461 file, linenum, err, args[0]);
8462 goto out_err;
8463 }
8464 cur_arg++;
8465 global.last_checks |= LSTCHK_NETADM;
8466#else
8467 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8468 goto out_err;
8469#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008470 } else if (!strcmp(args[0], "set-log-level")) {
8471 rule->action = HTTP_REQ_ACT_SET_LOGL;
8472 cur_arg = 1;
8473
8474 if (!*args[cur_arg] ||
8475 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8476 bad_log_level:
8477 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8478 file, linenum, args[0]);
8479 goto out_err;
8480 }
8481 if (strcmp(args[cur_arg], "silent") == 0)
8482 rule->arg.loglevel = -1;
8483 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8484 goto bad_log_level;
8485 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008486 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8487 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8488 cur_arg = 1;
8489
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008490 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8491 (*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 +01008492 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8493 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008494 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008495 }
8496
8497 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8498 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8499 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008500
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008501 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008502 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8503 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008504 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008505 } else if (strcmp(args[0], "redirect") == 0) {
8506 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008507 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008508
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008509 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008510 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8511 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8512 goto out_err;
8513 }
8514
8515 /* this redirect rule might already contain a parsed condition which
8516 * we'll pass to the http-request rule.
8517 */
8518 rule->action = HTTP_REQ_ACT_REDIR;
8519 rule->arg.redir = redir;
8520 rule->cond = redir->cond;
8521 redir->cond = NULL;
8522 cur_arg = 2;
8523 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008524 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008525 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 +01008526 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008527 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008528 }
8529
8530 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8531 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008532 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008533
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008534 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8535 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8536 file, linenum, args[0], errmsg);
8537 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008538 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008539 }
8540 rule->cond = cond;
8541 }
8542 else if (*args[cur_arg]) {
8543 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8544 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8545 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008546 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008547 }
8548
8549 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008550 out_err:
8551 free(rule);
8552 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008553}
8554
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008555/* parse an "http-respose" rule */
8556struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8557{
8558 struct http_res_rule *rule;
8559 int cur_arg;
8560
8561 rule = calloc(1, sizeof(*rule));
8562 if (!rule) {
8563 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8564 goto out_err;
8565 }
8566
8567 if (!strcmp(args[0], "allow")) {
8568 rule->action = HTTP_RES_ACT_ALLOW;
8569 cur_arg = 1;
8570 } else if (!strcmp(args[0], "deny")) {
8571 rule->action = HTTP_RES_ACT_DENY;
8572 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008573 } else if (!strcmp(args[0], "set-nice")) {
8574 rule->action = HTTP_RES_ACT_SET_NICE;
8575 cur_arg = 1;
8576
8577 if (!*args[cur_arg] ||
8578 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8579 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8580 file, linenum, args[0]);
8581 goto out_err;
8582 }
8583 rule->arg.nice = atoi(args[cur_arg]);
8584 if (rule->arg.nice < -1024)
8585 rule->arg.nice = -1024;
8586 else if (rule->arg.nice > 1024)
8587 rule->arg.nice = 1024;
8588 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008589 } else if (!strcmp(args[0], "set-tos")) {
8590#ifdef IP_TOS
8591 char *err;
8592 rule->action = HTTP_RES_ACT_SET_TOS;
8593 cur_arg = 1;
8594
8595 if (!*args[cur_arg] ||
8596 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8597 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8598 file, linenum, args[0]);
8599 goto out_err;
8600 }
8601
8602 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8603 if (err && *err != '\0') {
8604 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8605 file, linenum, err, args[0]);
8606 goto out_err;
8607 }
8608 cur_arg++;
8609#else
8610 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8611 goto out_err;
8612#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008613 } else if (!strcmp(args[0], "set-mark")) {
8614#ifdef SO_MARK
8615 char *err;
8616 rule->action = HTTP_RES_ACT_SET_MARK;
8617 cur_arg = 1;
8618
8619 if (!*args[cur_arg] ||
8620 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8621 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8622 file, linenum, args[0]);
8623 goto out_err;
8624 }
8625
8626 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8627 if (err && *err != '\0') {
8628 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8629 file, linenum, err, args[0]);
8630 goto out_err;
8631 }
8632 cur_arg++;
8633 global.last_checks |= LSTCHK_NETADM;
8634#else
8635 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8636 goto out_err;
8637#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008638 } else if (!strcmp(args[0], "set-log-level")) {
8639 rule->action = HTTP_RES_ACT_SET_LOGL;
8640 cur_arg = 1;
8641
8642 if (!*args[cur_arg] ||
8643 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8644 bad_log_level:
8645 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8646 file, linenum, args[0]);
8647 goto out_err;
8648 }
8649 if (strcmp(args[cur_arg], "silent") == 0)
8650 rule->arg.loglevel = -1;
8651 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8652 goto bad_log_level;
8653 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008654 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8655 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8656 cur_arg = 1;
8657
8658 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8659 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8660 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8661 file, linenum, args[0]);
8662 goto out_err;
8663 }
8664
8665 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8666 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8667 LIST_INIT(&rule->arg.hdr_add.fmt);
8668
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008669 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008670 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8671 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8672 cur_arg += 2;
8673 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008674 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 +02008675 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8676 goto out_err;
8677 }
8678
8679 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8680 struct acl_cond *cond;
8681 char *errmsg = NULL;
8682
8683 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8684 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8685 file, linenum, args[0], errmsg);
8686 free(errmsg);
8687 goto out_err;
8688 }
8689 rule->cond = cond;
8690 }
8691 else if (*args[cur_arg]) {
8692 Alert("parsing [%s:%d]: 'http-response %s' expects"
8693 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8694 file, linenum, args[0], args[cur_arg]);
8695 goto out_err;
8696 }
8697
8698 return rule;
8699 out_err:
8700 free(rule);
8701 return NULL;
8702}
8703
Willy Tarreau4baae242012-12-27 12:00:31 +01008704/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008705 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8706 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008707 */
8708struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008709 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008710{
8711 struct redirect_rule *rule;
8712 int cur_arg;
8713 int type = REDIRECT_TYPE_NONE;
8714 int code = 302;
8715 const char *destination = NULL;
8716 const char *cookie = NULL;
8717 int cookie_set = 0;
8718 unsigned int flags = REDIRECT_FLAG_NONE;
8719 struct acl_cond *cond = NULL;
8720
8721 cur_arg = 0;
8722 while (*(args[cur_arg])) {
8723 if (strcmp(args[cur_arg], "location") == 0) {
8724 if (!*args[cur_arg + 1])
8725 goto missing_arg;
8726
8727 type = REDIRECT_TYPE_LOCATION;
8728 cur_arg++;
8729 destination = args[cur_arg];
8730 }
8731 else if (strcmp(args[cur_arg], "prefix") == 0) {
8732 if (!*args[cur_arg + 1])
8733 goto missing_arg;
8734
8735 type = REDIRECT_TYPE_PREFIX;
8736 cur_arg++;
8737 destination = args[cur_arg];
8738 }
8739 else if (strcmp(args[cur_arg], "scheme") == 0) {
8740 if (!*args[cur_arg + 1])
8741 goto missing_arg;
8742
8743 type = REDIRECT_TYPE_SCHEME;
8744 cur_arg++;
8745 destination = args[cur_arg];
8746 }
8747 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8748 if (!*args[cur_arg + 1])
8749 goto missing_arg;
8750
8751 cur_arg++;
8752 cookie = args[cur_arg];
8753 cookie_set = 1;
8754 }
8755 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8756 if (!*args[cur_arg + 1])
8757 goto missing_arg;
8758
8759 cur_arg++;
8760 cookie = args[cur_arg];
8761 cookie_set = 0;
8762 }
8763 else if (strcmp(args[cur_arg], "code") == 0) {
8764 if (!*args[cur_arg + 1])
8765 goto missing_arg;
8766
8767 cur_arg++;
8768 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008769 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008770 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008771 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008772 args[cur_arg - 1], args[cur_arg]);
8773 return NULL;
8774 }
8775 }
8776 else if (!strcmp(args[cur_arg],"drop-query")) {
8777 flags |= REDIRECT_FLAG_DROP_QS;
8778 }
8779 else if (!strcmp(args[cur_arg],"append-slash")) {
8780 flags |= REDIRECT_FLAG_APPEND_SLASH;
8781 }
8782 else if (strcmp(args[cur_arg], "if") == 0 ||
8783 strcmp(args[cur_arg], "unless") == 0) {
8784 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8785 if (!cond) {
8786 memprintf(errmsg, "error in condition: %s", *errmsg);
8787 return NULL;
8788 }
8789 break;
8790 }
8791 else {
8792 memprintf(errmsg,
8793 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8794 args[cur_arg]);
8795 return NULL;
8796 }
8797 cur_arg++;
8798 }
8799
8800 if (type == REDIRECT_TYPE_NONE) {
8801 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8802 return NULL;
8803 }
8804
8805 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8806 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008807 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008808
8809 if (!use_fmt) {
8810 /* old-style static redirect rule */
8811 rule->rdr_str = strdup(destination);
8812 rule->rdr_len = strlen(destination);
8813 }
8814 else {
8815 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008816
8817 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8818 * if prefix == "/", we don't want to add anything, otherwise it
8819 * makes it hard for the user to configure a self-redirection.
8820 */
8821 proxy->conf.args.ctx = ARGC_RDR;
8822 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8823 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8824 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8825 }
8826 }
8827
Willy Tarreau4baae242012-12-27 12:00:31 +01008828 if (cookie) {
8829 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8830 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8831 */
8832 rule->cookie_len = strlen(cookie);
8833 if (cookie_set) {
8834 rule->cookie_str = malloc(rule->cookie_len + 10);
8835 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8836 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8837 rule->cookie_len += 9;
8838 } else {
8839 rule->cookie_str = malloc(rule->cookie_len + 21);
8840 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8841 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8842 rule->cookie_len += 20;
8843 }
8844 }
8845 rule->type = type;
8846 rule->code = code;
8847 rule->flags = flags;
8848 LIST_INIT(&rule->list);
8849 return rule;
8850
8851 missing_arg:
8852 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8853 return NULL;
8854}
8855
Willy Tarreau8797c062007-05-07 00:55:35 +02008856/************************************************************************/
8857/* The code below is dedicated to ACL parsing and matching */
8858/************************************************************************/
8859
8860
Willy Tarreau14174bc2012-04-16 14:34:04 +02008861/* This function ensures that the prerequisites for an L7 fetch are ready,
8862 * which means that a request or response is ready. If some data is missing,
8863 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008864 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8865 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008866 *
8867 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008868 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8869 * decide whether or not an HTTP message is present ;
8870 * 0 if the requested data cannot be fetched or if it is certain that
8871 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008872 * 1 if an HTTP message is ready
8873 */
8874static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008875smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008876 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008877{
8878 struct http_txn *txn = l7;
8879 struct http_msg *msg = &txn->req;
8880
8881 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8882 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8883 */
8884
8885 if (unlikely(!s || !txn))
8886 return 0;
8887
8888 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008889 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008890
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008891 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008892 if (unlikely(!s->req))
8893 return 0;
8894
Willy Tarreauaae75e32013-03-29 12:31:49 +01008895 /* If the buffer does not leave enough free space at the end,
8896 * we must first realign it.
8897 */
8898 if (s->req->buf->p > s->req->buf->data &&
8899 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8900 buffer_slow_realign(s->req->buf);
8901
Willy Tarreau14174bc2012-04-16 14:34:04 +02008902 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008903 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008904 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008905
8906 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008907 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008908 http_msg_analyzer(msg, &txn->hdr_idx);
8909
8910 /* Still no valid request ? */
8911 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008912 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008913 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008914 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008915 }
8916 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008917 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008918 return 0;
8919 }
8920
8921 /* OK we just got a valid HTTP request. We have some minor
8922 * preparation to perform so that further checks can rely
8923 * on HTTP tests.
8924 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008925
8926 /* If the request was parsed but was too large, we must absolutely
8927 * return an error so that it is not processed. At the moment this
8928 * cannot happen, but if the parsers are to change in the future,
8929 * we want this check to be maintained.
8930 */
8931 if (unlikely(s->req->buf->i + s->req->buf->p >
8932 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8933 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008934 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008935 return 1;
8936 }
8937
Willy Tarreau9b28e032012-10-12 23:49:43 +02008938 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008939 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8940 s->flags |= SN_REDIRECTABLE;
8941
Willy Tarreau506d0502013-07-06 13:29:24 +02008942 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8943 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008944 }
8945
Willy Tarreau506d0502013-07-06 13:29:24 +02008946 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008947 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008948 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008949
8950 /* otherwise everything's ready for the request */
8951 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008952 else {
8953 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008954 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8955 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008956 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008957 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008958 }
8959
8960 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008961 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008962 return 1;
8963}
Willy Tarreau8797c062007-05-07 00:55:35 +02008964
Willy Tarreauc0239e02012-04-16 14:42:55 +02008965#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008966 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 +02008967
Willy Tarreau24e32d82012-04-23 23:55:44 +02008968#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008969 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 +02008970
Willy Tarreau8797c062007-05-07 00:55:35 +02008971
8972/* 1. Check on METHOD
8973 * We use the pre-parsed method if it is known, and store its number as an
8974 * integer. If it is unknown, we use the pointer and the length.
8975 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008976static int pat_parse_meth(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008977{
8978 int len, meth;
8979
Willy Tarreauae8b7962007-06-09 23:10:04 +02008980 len = strlen(*text);
8981 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008982
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01008983 pattern->smp = smp;
Willy Tarreau8797c062007-05-07 00:55:35 +02008984 pattern->val.i = meth;
8985 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008986 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008987 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008988 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008989 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008990 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008991 pattern->len = len;
8992 }
8993 return 1;
8994}
8995
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008996/* This function fetches the method of current HTTP request and stores
8997 * it in the global pattern struct as a chunk. There are two possibilities :
8998 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8999 * in <len> and <ptr> is NULL ;
9000 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9001 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009002 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009003 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009004static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009005smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009006 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009007{
9008 int meth;
9009 struct http_txn *txn = l7;
9010
Willy Tarreau24e32d82012-04-23 23:55:44 +02009011 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009012
Willy Tarreau8797c062007-05-07 00:55:35 +02009013 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02009014 smp->type = SMP_T_UINT;
9015 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009016 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009017 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9018 /* ensure the indexes are not affected */
9019 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009020 smp->type = SMP_T_CSTR;
9021 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009022 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009023 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009024 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009025 return 1;
9026}
9027
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009028/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01009029static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02009030{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009031 int icase;
9032
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009033
Willy Tarreauf853c462012-04-23 18:53:56 +02009034 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009035 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009036 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009037 return PAT_MATCH;
9038 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009039 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009040
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009041 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9042 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009043 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009044
9045 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009046 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009047 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009048
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009049 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009050 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9051 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009052 return PAT_NOMATCH;
9053 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009054}
9055
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009056static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009057smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009058 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009059{
9060 struct http_txn *txn = l7;
9061 char *ptr;
9062 int len;
9063
Willy Tarreauc0239e02012-04-16 14:42:55 +02009064 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009065
Willy Tarreau8797c062007-05-07 00:55:35 +02009066 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009067 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009068
9069 while ((len-- > 0) && (*ptr++ != '/'));
9070 if (len <= 0)
9071 return 0;
9072
Willy Tarreauf853c462012-04-23 18:53:56 +02009073 smp->type = SMP_T_CSTR;
9074 smp->data.str.str = ptr;
9075 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009076
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009077 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009078 return 1;
9079}
9080
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009081static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009082smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009083 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009084{
9085 struct http_txn *txn = l7;
9086 char *ptr;
9087 int len;
9088
Willy Tarreauc0239e02012-04-16 14:42:55 +02009089 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009090
Willy Tarreauf26b2522012-12-14 08:33:14 +01009091 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9092 return 0;
9093
Willy Tarreau8797c062007-05-07 00:55:35 +02009094 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009095 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009096
9097 while ((len-- > 0) && (*ptr++ != '/'));
9098 if (len <= 0)
9099 return 0;
9100
Willy Tarreauf853c462012-04-23 18:53:56 +02009101 smp->type = SMP_T_CSTR;
9102 smp->data.str.str = ptr;
9103 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009104
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009105 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009106 return 1;
9107}
9108
9109/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009110static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009111smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009112 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009113{
9114 struct http_txn *txn = l7;
9115 char *ptr;
9116 int len;
9117
Willy Tarreauc0239e02012-04-16 14:42:55 +02009118 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009119
Willy Tarreauf26b2522012-12-14 08:33:14 +01009120 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9121 return 0;
9122
Willy Tarreau8797c062007-05-07 00:55:35 +02009123 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009124 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009125
Willy Tarreauf853c462012-04-23 18:53:56 +02009126 smp->type = SMP_T_UINT;
9127 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009128 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009129 return 1;
9130}
9131
9132/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009133static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009134smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009135 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009136{
9137 struct http_txn *txn = l7;
9138
Willy Tarreauc0239e02012-04-16 14:42:55 +02009139 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009140
Willy Tarreauf853c462012-04-23 18:53:56 +02009141 smp->type = SMP_T_CSTR;
9142 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009143 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009144 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009145 return 1;
9146}
9147
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009148static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009149smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009150 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009151{
9152 struct http_txn *txn = l7;
9153
Willy Tarreauc0239e02012-04-16 14:42:55 +02009154 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009155
9156 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009157 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
9158 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009159 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009160 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009161 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009162
9163 /*
9164 * If we are parsing url in frontend space, we prepare backend stage
9165 * to not parse again the same url ! optimization lazyness...
9166 */
9167 if (px->options & PR_O_HTTP_PROXY)
9168 l4->flags |= SN_ADDR_SET;
9169
Willy Tarreau37406352012-04-23 16:16:37 +02009170 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009171 return 1;
9172}
9173
9174static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009175smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009176 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009177{
9178 struct http_txn *txn = l7;
9179
Willy Tarreauc0239e02012-04-16 14:42:55 +02009180 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009181
9182 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009183 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02009184 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009185 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009186
9187 if (px->options & PR_O_HTTP_PROXY)
9188 l4->flags |= SN_ADDR_SET;
9189
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009190 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009191 return 1;
9192}
9193
Willy Tarreau185b5c42012-04-26 15:11:51 +02009194/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9195 * Accepts an optional argument of type string containing the header field name,
9196 * and an optional argument of type signed or unsigned integer to request an
9197 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009198 * headers are considered from the first one. It does not stop on commas and
9199 * returns full lines instead (useful for User-Agent or Date for example).
9200 */
9201static int
9202smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009203 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009204{
9205 struct http_txn *txn = l7;
9206 struct hdr_idx *idx = &txn->hdr_idx;
9207 struct hdr_ctx *ctx = smp->ctx.a[0];
9208 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9209 int occ = 0;
9210 const char *name_str = NULL;
9211 int name_len = 0;
9212
9213 if (!ctx) {
9214 /* first call */
9215 ctx = &static_hdr_ctx;
9216 ctx->idx = 0;
9217 smp->ctx.a[0] = ctx;
9218 }
9219
9220 if (args) {
9221 if (args[0].type != ARGT_STR)
9222 return 0;
9223 name_str = args[0].data.str.str;
9224 name_len = args[0].data.str.len;
9225
9226 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9227 occ = args[1].data.uint;
9228 }
9229
9230 CHECK_HTTP_MESSAGE_FIRST();
9231
9232 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9233 /* search for header from the beginning */
9234 ctx->idx = 0;
9235
9236 if (!occ && !(opt & SMP_OPT_ITERATE))
9237 /* no explicit occurrence and single fetch => last header by default */
9238 occ = -1;
9239
9240 if (!occ)
9241 /* prepare to report multiple occurrences for ACL fetches */
9242 smp->flags |= SMP_F_NOT_LAST;
9243
9244 smp->type = SMP_T_CSTR;
9245 smp->flags |= SMP_F_VOL_HDR;
9246 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9247 return 1;
9248
9249 smp->flags &= ~SMP_F_NOT_LAST;
9250 return 0;
9251}
9252
9253/* 6. Check on HTTP header count. The number of occurrences is returned.
9254 * Accepts exactly 1 argument of type string. It does not stop on commas and
9255 * returns full lines instead (useful for User-Agent or Date for example).
9256 */
9257static int
9258smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009259 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009260{
9261 struct http_txn *txn = l7;
9262 struct hdr_idx *idx = &txn->hdr_idx;
9263 struct hdr_ctx ctx;
9264 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9265 int cnt;
9266
9267 if (!args || args->type != ARGT_STR)
9268 return 0;
9269
9270 CHECK_HTTP_MESSAGE_FIRST();
9271
9272 ctx.idx = 0;
9273 cnt = 0;
9274 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9275 cnt++;
9276
9277 smp->type = SMP_T_UINT;
9278 smp->data.uint = cnt;
9279 smp->flags = SMP_F_VOL_HDR;
9280 return 1;
9281}
9282
9283/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9284 * Accepts an optional argument of type string containing the header field name,
9285 * and an optional argument of type signed or unsigned integer to request an
9286 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009287 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009288 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009289static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009290smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009291 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009292{
9293 struct http_txn *txn = l7;
9294 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009295 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009296 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009297 int occ = 0;
9298 const char *name_str = NULL;
9299 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009300
Willy Tarreaua890d072013-04-02 12:01:06 +02009301 if (!ctx) {
9302 /* first call */
9303 ctx = &static_hdr_ctx;
9304 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009305 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009306 }
9307
Willy Tarreau185b5c42012-04-26 15:11:51 +02009308 if (args) {
9309 if (args[0].type != ARGT_STR)
9310 return 0;
9311 name_str = args[0].data.str.str;
9312 name_len = args[0].data.str.len;
9313
9314 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9315 occ = args[1].data.uint;
9316 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009317
Willy Tarreaue333ec92012-04-16 16:26:40 +02009318 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009319
Willy Tarreau185b5c42012-04-26 15:11:51 +02009320 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009321 /* search for header from the beginning */
9322 ctx->idx = 0;
9323
Willy Tarreau185b5c42012-04-26 15:11:51 +02009324 if (!occ && !(opt & SMP_OPT_ITERATE))
9325 /* no explicit occurrence and single fetch => last header by default */
9326 occ = -1;
9327
9328 if (!occ)
9329 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009330 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009331
Willy Tarreau185b5c42012-04-26 15:11:51 +02009332 smp->type = SMP_T_CSTR;
9333 smp->flags |= SMP_F_VOL_HDR;
9334 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 +02009335 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009336
Willy Tarreau37406352012-04-23 16:16:37 +02009337 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009338 return 0;
9339}
9340
Willy Tarreauc11416f2007-06-17 16:58:38 +02009341/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009342 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009343 */
9344static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009345smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009346 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009347{
9348 struct http_txn *txn = l7;
9349 struct hdr_idx *idx = &txn->hdr_idx;
9350 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009351 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009352 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009353
Willy Tarreau24e32d82012-04-23 23:55:44 +02009354 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009355 return 0;
9356
Willy Tarreaue333ec92012-04-16 16:26:40 +02009357 CHECK_HTTP_MESSAGE_FIRST();
9358
Willy Tarreau33a7e692007-06-10 19:45:56 +02009359 ctx.idx = 0;
9360 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009361 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 +02009362 cnt++;
9363
Willy Tarreauf853c462012-04-23 18:53:56 +02009364 smp->type = SMP_T_UINT;
9365 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009366 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009367 return 1;
9368}
9369
Willy Tarreau185b5c42012-04-26 15:11:51 +02009370/* Fetch an HTTP header's integer value. The integer value is returned. It
9371 * takes a mandatory argument of type string and an optional one of type int
9372 * to designate a specific occurrence. It returns an unsigned integer, which
9373 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009374 */
9375static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009376smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009377 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009378{
Willy Tarreauef38c392013-07-22 16:29:32 +02009379 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009380
Willy Tarreauf853c462012-04-23 18:53:56 +02009381 if (ret > 0) {
9382 smp->type = SMP_T_UINT;
9383 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9384 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009385
Willy Tarreaud53e2422012-04-16 17:21:11 +02009386 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009387}
9388
Cyril Bonté69fa9922012-10-25 00:01:06 +02009389/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9390 * and an optional one of type int to designate a specific occurrence.
9391 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009392 */
9393static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009394smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009395 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009396{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009397 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009398
Willy Tarreauef38c392013-07-22 16:29:32 +02009399 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009400 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9401 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009402 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009403 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009404 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009405 if (smp->data.str.len < temp->size - 1) {
9406 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9407 temp->str[smp->data.str.len] = '\0';
9408 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9409 smp->type = SMP_T_IPV6;
9410 break;
9411 }
9412 }
9413 }
9414
Willy Tarreaud53e2422012-04-16 17:21:11 +02009415 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009416 if (!(smp->flags & SMP_F_NOT_LAST))
9417 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009418 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009419 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009420}
9421
Willy Tarreau737b0c12007-06-10 21:28:46 +02009422/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9423 * the first '/' after the possible hostname, and ends before the possible '?'.
9424 */
9425static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009426smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009427 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009428{
9429 struct http_txn *txn = l7;
9430 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009431
Willy Tarreauc0239e02012-04-16 14:42:55 +02009432 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009433
Willy Tarreau9b28e032012-10-12 23:49:43 +02009434 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009435 ptr = http_get_path(txn);
9436 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009437 return 0;
9438
9439 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009440 smp->type = SMP_T_CSTR;
9441 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009442
9443 while (ptr < end && *ptr != '?')
9444 ptr++;
9445
Willy Tarreauf853c462012-04-23 18:53:56 +02009446 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009447 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009448 return 1;
9449}
9450
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009451/* This produces a concatenation of the first occurrence of the Host header
9452 * followed by the path component if it begins with a slash ('/'). This means
9453 * that '*' will not be added, resulting in exactly the first Host entry.
9454 * If no Host header is found, then the path is returned as-is. The returned
9455 * value is stored in the trash so it does not need to be marked constant.
9456 */
9457static int
9458smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009459 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009460{
9461 struct http_txn *txn = l7;
9462 char *ptr, *end, *beg;
9463 struct hdr_ctx ctx;
9464
9465 CHECK_HTTP_MESSAGE_FIRST();
9466
9467 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009468 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 +02009469 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009470 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009471
9472 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009473 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009474 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009475 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009476 smp->data.str.len = ctx.vlen;
9477
9478 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009479 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 +02009480 beg = http_get_path(txn);
9481 if (!beg)
9482 beg = end;
9483
9484 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9485
9486 if (beg < ptr && *beg == '/') {
9487 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9488 smp->data.str.len += ptr - beg;
9489 }
9490
9491 smp->flags = SMP_F_VOL_1ST;
9492 return 1;
9493}
9494
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009495/* This produces a 32-bit hash of the concatenation of the first occurrence of
9496 * the Host header followed by the path component if it begins with a slash ('/').
9497 * This means that '*' will not be added, resulting in exactly the first Host
9498 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009499 * is hashed using the path hash followed by a full avalanche hash and provides a
9500 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009501 * high-traffic sites without having to store whole paths.
9502 */
9503static int
9504smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009505 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009506{
9507 struct http_txn *txn = l7;
9508 struct hdr_ctx ctx;
9509 unsigned int hash = 0;
9510 char *ptr, *beg, *end;
9511 int len;
9512
9513 CHECK_HTTP_MESSAGE_FIRST();
9514
9515 ctx.idx = 0;
9516 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9517 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9518 ptr = ctx.line + ctx.val;
9519 len = ctx.vlen;
9520 while (len--)
9521 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9522 }
9523
9524 /* now retrieve the path */
9525 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9526 beg = http_get_path(txn);
9527 if (!beg)
9528 beg = end;
9529
9530 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9531
9532 if (beg < ptr && *beg == '/') {
9533 while (beg < ptr)
9534 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9535 }
9536 hash = full_hash(hash);
9537
9538 smp->type = SMP_T_UINT;
9539 smp->data.uint = hash;
9540 smp->flags = SMP_F_VOL_1ST;
9541 return 1;
9542}
9543
Willy Tarreau4a550602012-12-09 14:53:32 +01009544/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009545 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9546 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9547 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009548 * that in environments where IPv6 is insignificant, truncating the output to
9549 * 8 bytes would still work.
9550 */
9551static int
9552smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009553 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009554{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009555 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01009556
Willy Tarreauef38c392013-07-22 16:29:32 +02009557 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009558 return 0;
9559
Willy Tarreau47ca5452012-12-23 20:22:19 +01009560 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009561 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9562 temp->len += sizeof(smp->data.uint);
9563
9564 switch (l4->si[0].conn->addr.from.ss_family) {
9565 case AF_INET:
9566 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
9567 temp->len += 4;
9568 break;
9569 case AF_INET6:
9570 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
9571 temp->len += 16;
9572 break;
9573 default:
9574 return 0;
9575 }
9576
9577 smp->data.str = *temp;
9578 smp->type = SMP_T_BIN;
9579 return 1;
9580}
9581
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009582static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009583smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009584 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009585{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009586 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9587 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9588 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009589
Willy Tarreau24e32d82012-04-23 23:55:44 +02009590 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009591
Willy Tarreauf853c462012-04-23 18:53:56 +02009592 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009593 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009594 return 1;
9595}
9596
Willy Tarreau7f18e522010-10-22 20:04:13 +02009597/* return a valid test if the current request is the first one on the connection */
9598static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009599smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009600 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009601{
9602 if (!s)
9603 return 0;
9604
Willy Tarreauf853c462012-04-23 18:53:56 +02009605 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009606 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009607 return 1;
9608}
9609
Willy Tarreau34db1082012-04-19 17:16:54 +02009610/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009611static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009612smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009613 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009614{
9615
Willy Tarreau24e32d82012-04-23 23:55:44 +02009616 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009617 return 0;
9618
Willy Tarreauc0239e02012-04-16 14:42:55 +02009619 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009620
Willy Tarreauc0239e02012-04-16 14:42:55 +02009621 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009622 return 0;
9623
Willy Tarreauf853c462012-04-23 18:53:56 +02009624 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009625 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 +01009626 return 1;
9627}
Willy Tarreau8797c062007-05-07 00:55:35 +02009628
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009629/* Accepts exactly 1 argument of type userlist */
9630static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009631smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009632 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009633{
9634
9635 if (!args || args->type != ARGT_USR)
9636 return 0;
9637
9638 CHECK_HTTP_MESSAGE_FIRST();
9639
9640 if (!get_http_auth(l4))
9641 return 0;
9642
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009643 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009644 smp->ctx.a[0] = args->data.usr; /* user list */
9645 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9646 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9647
9648 /* if the user does not belong to the userlist or has a wrong password,
9649 * report that it unconditionally does not match. Otherwise we return
9650 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009651 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009652 */
9653 smp->type = SMP_T_BOOL;
9654 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9655 if (smp->data.uint)
9656 smp->type = SMP_T_UINT;
9657
9658 return 1;
9659}
9660
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009661/* Try to find the next occurrence of a cookie name in a cookie header value.
9662 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9663 * the cookie value is returned into *value and *value_l, and the function
9664 * returns a pointer to the next pointer to search from if the value was found.
9665 * Otherwise if the cookie was not found, NULL is returned and neither value
9666 * nor value_l are touched. The input <hdr> string should first point to the
9667 * header's value, and the <hdr_end> pointer must point to the first character
9668 * not part of the value. <list> must be non-zero if value may represent a list
9669 * of values (cookie headers). This makes it faster to abort parsing when no
9670 * list is expected.
9671 */
9672static char *
9673extract_cookie_value(char *hdr, const char *hdr_end,
9674 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009675 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009676{
9677 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9678 char *next;
9679
9680 /* we search at least a cookie name followed by an equal, and more
9681 * generally something like this :
9682 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9683 */
9684 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9685 /* Iterate through all cookies on this line */
9686
9687 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9688 att_beg++;
9689
9690 /* find att_end : this is the first character after the last non
9691 * space before the equal. It may be equal to hdr_end.
9692 */
9693 equal = att_end = att_beg;
9694
9695 while (equal < hdr_end) {
9696 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9697 break;
9698 if (http_is_spht[(unsigned char)*equal++])
9699 continue;
9700 att_end = equal;
9701 }
9702
9703 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9704 * is between <att_beg> and <equal>, both may be identical.
9705 */
9706
9707 /* look for end of cookie if there is an equal sign */
9708 if (equal < hdr_end && *equal == '=') {
9709 /* look for the beginning of the value */
9710 val_beg = equal + 1;
9711 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9712 val_beg++;
9713
9714 /* find the end of the value, respecting quotes */
9715 next = find_cookie_value_end(val_beg, hdr_end);
9716
9717 /* make val_end point to the first white space or delimitor after the value */
9718 val_end = next;
9719 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9720 val_end--;
9721 } else {
9722 val_beg = val_end = next = equal;
9723 }
9724
9725 /* We have nothing to do with attributes beginning with '$'. However,
9726 * they will automatically be removed if a header before them is removed,
9727 * since they're supposed to be linked together.
9728 */
9729 if (*att_beg == '$')
9730 continue;
9731
9732 /* Ignore cookies with no equal sign */
9733 if (equal == next)
9734 continue;
9735
9736 /* Now we have the cookie name between att_beg and att_end, and
9737 * its value between val_beg and val_end.
9738 */
9739
9740 if (att_end - att_beg == cookie_name_l &&
9741 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9742 /* let's return this value and indicate where to go on from */
9743 *value = val_beg;
9744 *value_l = val_end - val_beg;
9745 return next + 1;
9746 }
9747
9748 /* Set-Cookie headers only have the name in the first attr=value part */
9749 if (!list)
9750 break;
9751 }
9752
9753 return NULL;
9754}
9755
Willy Tarreaue333ec92012-04-16 16:26:40 +02009756/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009757 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009758 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009759 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009760 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009761 * Accepts exactly 1 argument of type string. If the input options indicate
9762 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009763 */
9764static int
Willy Tarreau51539362012-05-08 12:46:28 +02009765smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009766 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009767{
9768 struct http_txn *txn = l7;
9769 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009770 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009771 const struct http_msg *msg;
9772 const char *hdr_name;
9773 int hdr_name_len;
9774 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009775 int occ = 0;
9776 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009777
Willy Tarreau24e32d82012-04-23 23:55:44 +02009778 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009779 return 0;
9780
Willy Tarreaua890d072013-04-02 12:01:06 +02009781 if (!ctx) {
9782 /* first call */
9783 ctx = &static_hdr_ctx;
9784 ctx->idx = 0;
9785 smp->ctx.a[2] = ctx;
9786 }
9787
Willy Tarreaue333ec92012-04-16 16:26:40 +02009788 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009789
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009790 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009791 msg = &txn->req;
9792 hdr_name = "Cookie";
9793 hdr_name_len = 6;
9794 } else {
9795 msg = &txn->rsp;
9796 hdr_name = "Set-Cookie";
9797 hdr_name_len = 10;
9798 }
9799
Willy Tarreau28376d62012-04-26 21:26:10 +02009800 if (!occ && !(opt & SMP_OPT_ITERATE))
9801 /* no explicit occurrence and single fetch => last cookie by default */
9802 occ = -1;
9803
9804 /* OK so basically here, either we want only one value and it's the
9805 * last one, or we want to iterate over all of them and we fetch the
9806 * next one.
9807 */
9808
Willy Tarreau9b28e032012-10-12 23:49:43 +02009809 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009810 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009811 /* search for the header from the beginning, we must first initialize
9812 * the search parameters.
9813 */
Willy Tarreau37406352012-04-23 16:16:37 +02009814 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009815 ctx->idx = 0;
9816 }
9817
Willy Tarreau28376d62012-04-26 21:26:10 +02009818 smp->flags |= SMP_F_VOL_HDR;
9819
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009820 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009821 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9822 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009823 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9824 goto out;
9825
Willy Tarreau24e32d82012-04-23 23:55:44 +02009826 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009827 continue;
9828
Willy Tarreau37406352012-04-23 16:16:37 +02009829 smp->ctx.a[0] = ctx->line + ctx->val;
9830 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009831 }
9832
Willy Tarreauf853c462012-04-23 18:53:56 +02009833 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009834 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009835 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009836 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009837 &smp->data.str.str,
9838 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009839 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009840 found = 1;
9841 if (occ >= 0) {
9842 /* one value was returned into smp->data.str.{str,len} */
9843 smp->flags |= SMP_F_NOT_LAST;
9844 return 1;
9845 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009846 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009847 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009848 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009849 /* all cookie headers and values were scanned. If we're looking for the
9850 * last occurrence, we may return it now.
9851 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009852 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009853 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009854 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009855}
9856
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009857/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009858 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009859 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009860 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009861 */
9862static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009863smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009864 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009865{
9866 struct http_txn *txn = l7;
9867 struct hdr_idx *idx = &txn->hdr_idx;
9868 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009869 const struct http_msg *msg;
9870 const char *hdr_name;
9871 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009872 int cnt;
9873 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009874 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009875
Willy Tarreau24e32d82012-04-23 23:55:44 +02009876 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009877 return 0;
9878
Willy Tarreaue333ec92012-04-16 16:26:40 +02009879 CHECK_HTTP_MESSAGE_FIRST();
9880
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009881 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009882 msg = &txn->req;
9883 hdr_name = "Cookie";
9884 hdr_name_len = 6;
9885 } else {
9886 msg = &txn->rsp;
9887 hdr_name = "Set-Cookie";
9888 hdr_name_len = 10;
9889 }
9890
Willy Tarreau9b28e032012-10-12 23:49:43 +02009891 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009892 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009893 ctx.idx = 0;
9894 cnt = 0;
9895
9896 while (1) {
9897 /* Note: val_beg == NULL every time we need to fetch a new header */
9898 if (!val_beg) {
9899 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9900 break;
9901
Willy Tarreau24e32d82012-04-23 23:55:44 +02009902 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009903 continue;
9904
9905 val_beg = ctx.line + ctx.val;
9906 val_end = val_beg + ctx.vlen;
9907 }
9908
Willy Tarreauf853c462012-04-23 18:53:56 +02009909 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009910 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009911 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009912 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009913 &smp->data.str.str,
9914 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009915 cnt++;
9916 }
9917 }
9918
Willy Tarreauf853c462012-04-23 18:53:56 +02009919 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009920 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009921 return 1;
9922}
9923
Willy Tarreau51539362012-05-08 12:46:28 +02009924/* Fetch an cookie's integer value. The integer value is returned. It
9925 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9926 */
9927static int
9928smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009929 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009930{
Willy Tarreauef38c392013-07-22 16:29:32 +02009931 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009932
9933 if (ret > 0) {
9934 smp->type = SMP_T_UINT;
9935 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9936 }
9937
9938 return ret;
9939}
9940
Willy Tarreau8797c062007-05-07 00:55:35 +02009941/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009942/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009943/************************************************************************/
9944
David Cournapeau16023ee2010-12-23 20:55:41 +09009945/*
9946 * Given a path string and its length, find the position of beginning of the
9947 * query string. Returns NULL if no query string is found in the path.
9948 *
9949 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9950 *
9951 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9952 */
bedis4c75cca2012-10-05 08:38:24 +02009953static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009954{
9955 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009956
bedis4c75cca2012-10-05 08:38:24 +02009957 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009958 return p ? p + 1 : NULL;
9959}
9960
bedis4c75cca2012-10-05 08:38:24 +02009961static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009962{
bedis4c75cca2012-10-05 08:38:24 +02009963 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009964}
9965
9966/*
9967 * Given a url parameter, find the starting position of the first occurence,
9968 * or NULL if the parameter is not found.
9969 *
9970 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9971 * the function will return query_string+8.
9972 */
9973static char*
9974find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009975 char* url_param_name, size_t url_param_name_l,
9976 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009977{
9978 char *pos, *last;
9979
9980 pos = query_string;
9981 last = query_string + query_string_l - url_param_name_l - 1;
9982
9983 while (pos <= last) {
9984 if (pos[url_param_name_l] == '=') {
9985 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9986 return pos;
9987 pos += url_param_name_l + 1;
9988 }
bedis4c75cca2012-10-05 08:38:24 +02009989 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009990 pos++;
9991 pos++;
9992 }
9993 return NULL;
9994}
9995
9996/*
9997 * Given a url parameter name, returns its value and size into *value and
9998 * *value_l respectively, and returns non-zero. If the parameter is not found,
9999 * zero is returned and value/value_l are not touched.
10000 */
10001static int
10002find_url_param_value(char* path, size_t path_l,
10003 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010004 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010005{
10006 char *query_string, *qs_end;
10007 char *arg_start;
10008 char *value_start, *value_end;
10009
bedis4c75cca2012-10-05 08:38:24 +020010010 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010011 if (!query_string)
10012 return 0;
10013
10014 qs_end = path + path_l;
10015 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010016 url_param_name, url_param_name_l,
10017 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010018 if (!arg_start)
10019 return 0;
10020
10021 value_start = arg_start + url_param_name_l + 1;
10022 value_end = value_start;
10023
bedis4c75cca2012-10-05 08:38:24 +020010024 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010025 value_end++;
10026
10027 *value = value_start;
10028 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010029 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010030}
10031
10032static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010033smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010034 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010035{
bedis4c75cca2012-10-05 08:38:24 +020010036 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010037 struct http_txn *txn = l7;
10038 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010039
bedis4c75cca2012-10-05 08:38:24 +020010040 if (!args || args[0].type != ARGT_STR ||
10041 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010042 return 0;
10043
10044 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010045
bedis4c75cca2012-10-05 08:38:24 +020010046 if (args[1].type)
10047 delim = *args[1].data.str.str;
10048
Willy Tarreau9b28e032012-10-12 23:49:43 +020010049 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010050 args->data.str.str, args->data.str.len,
10051 &smp->data.str.str, &smp->data.str.len,
10052 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010053 return 0;
10054
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010055 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010056 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010057 return 1;
10058}
10059
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010060/* Return the signed integer value for the specified url parameter (see url_param
10061 * above).
10062 */
10063static int
10064smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010065 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010066{
Willy Tarreauef38c392013-07-22 16:29:32 +020010067 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010068
10069 if (ret > 0) {
10070 smp->type = SMP_T_UINT;
10071 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10072 }
10073
10074 return ret;
10075}
10076
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010077/* This produces a 32-bit hash of the concatenation of the first occurrence of
10078 * the Host header followed by the path component if it begins with a slash ('/').
10079 * This means that '*' will not be added, resulting in exactly the first Host
10080 * entry. If no Host header is found, then the path is used. The resulting value
10081 * is hashed using the url hash followed by a full avalanche hash and provides a
10082 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10083 * high-traffic sites without having to store whole paths.
10084 * this differs from the base32 functions in that it includes the url parameters
10085 * as well as the path
10086 */
10087static int
10088smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010089 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010090{
10091 struct http_txn *txn = l7;
10092 struct hdr_ctx ctx;
10093 unsigned int hash = 0;
10094 char *ptr, *beg, *end;
10095 int len;
10096
10097 CHECK_HTTP_MESSAGE_FIRST();
10098
10099 ctx.idx = 0;
10100 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10101 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10102 ptr = ctx.line + ctx.val;
10103 len = ctx.vlen;
10104 while (len--)
10105 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10106 }
10107
10108 /* now retrieve the path */
10109 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10110 beg = http_get_path(txn);
10111 if (!beg)
10112 beg = end;
10113
10114 for (ptr = beg; ptr < end ; ptr++);
10115
10116 if (beg < ptr && *beg == '/') {
10117 while (beg < ptr)
10118 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10119 }
10120 hash = full_hash(hash);
10121
10122 smp->type = SMP_T_UINT;
10123 smp->data.uint = hash;
10124 smp->flags = SMP_F_VOL_1ST;
10125 return 1;
10126}
10127
10128/* This concatenates the source address with the 32-bit hash of the Host and
10129 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10130 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10131 * on the source address length. The URL hash is stored before the address so
10132 * that in environments where IPv6 is insignificant, truncating the output to
10133 * 8 bytes would still work.
10134 */
10135static int
10136smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010137 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010138{
10139 struct chunk *temp;
10140
Willy Tarreaue155ec22013-11-18 18:33:22 +010010141 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010142 return 0;
10143
10144 temp = get_trash_chunk();
10145 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10146 temp->len += sizeof(smp->data.uint);
10147
10148 switch (l4->si[0].conn->addr.from.ss_family) {
10149 case AF_INET:
10150 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
10151 temp->len += 4;
10152 break;
10153 case AF_INET6:
10154 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
10155 temp->len += 16;
10156 break;
10157 default:
10158 return 0;
10159 }
10160
10161 smp->data.str = *temp;
10162 smp->type = SMP_T_BIN;
10163 return 1;
10164}
10165
Willy Tarreau185b5c42012-04-26 15:11:51 +020010166/* This function is used to validate the arguments passed to any "hdr" fetch
10167 * keyword. These keywords support an optional positive or negative occurrence
10168 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10169 * is assumed that the types are already the correct ones. Returns 0 on error,
10170 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10171 * error message in case of error, that the caller is responsible for freeing.
10172 * The initial location must either be freeable or NULL.
10173 */
10174static int val_hdr(struct arg *arg, char **err_msg)
10175{
10176 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010177 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010178 return 0;
10179 }
10180 return 1;
10181}
10182
Willy Tarreau276fae92013-07-25 14:36:01 +020010183/* takes an UINT value on input supposed to represent the time since EPOCH,
10184 * adds an optional offset found in args[0] and emits a string representing
10185 * the date in RFC-1123/5322 format.
10186 */
10187static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10188{
10189 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10190 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10191 struct chunk *temp;
10192 struct tm *tm;
10193 time_t curr_date = smp->data.uint;
10194
10195 /* add offset */
10196 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10197 curr_date += args[0].data.sint;
10198
10199 tm = gmtime(&curr_date);
10200
10201 temp = get_trash_chunk();
10202 temp->len = snprintf(temp->str, temp->size - temp->len,
10203 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10204 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10205 tm->tm_hour, tm->tm_min, tm->tm_sec);
10206
10207 smp->data.str = *temp;
10208 smp->type = SMP_T_STR;
10209 return 1;
10210}
10211
Willy Tarreau4a568972010-05-12 08:08:50 +020010212/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010213/* All supported ACL keywords must be declared here. */
10214/************************************************************************/
10215
10216/* Note: must not be declared <const> as its list will be overwritten.
10217 * Please take care of keeping this list alphabetically sorted.
10218 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010219static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010220 { "base", "base", pat_parse_str, pat_match_str },
10221 { "base_beg", "base", pat_parse_str, pat_match_beg },
10222 { "base_dir", "base", pat_parse_str, pat_match_dir },
10223 { "base_dom", "base", pat_parse_str, pat_match_dom },
10224 { "base_end", "base", pat_parse_str, pat_match_end },
10225 { "base_len", "base", pat_parse_int, pat_match_len },
10226 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10227 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010228
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010229 { "cook", "req.cook", pat_parse_str, pat_match_str },
10230 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10231 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10232 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10233 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10234 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10235 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10236 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010237
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010238 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10239 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10240 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10241 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10242 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10243 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10244 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10245 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010246
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010247 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010248
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010249 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010250
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010251 { "path", "path", pat_parse_str, pat_match_str },
10252 { "path_beg", "path", pat_parse_str, pat_match_beg },
10253 { "path_dir", "path", pat_parse_str, pat_match_dir },
10254 { "path_dom", "path", pat_parse_str, pat_match_dom },
10255 { "path_end", "path", pat_parse_str, pat_match_end },
10256 { "path_len", "path", pat_parse_int, pat_match_len },
10257 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10258 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010259
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010260 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10261 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010262
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010263 { "scook", "res.cook", pat_parse_str, pat_match_str },
10264 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10265 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10266 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10267 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10268 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10269 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10270 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010271
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010272 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10273 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10274 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10275 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10276 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10277 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10278 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10279 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010280
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010281 { "url", "url", pat_parse_str, pat_match_str },
10282 { "url_beg", "url", pat_parse_str, pat_match_beg },
10283 { "url_dir", "url", pat_parse_str, pat_match_dir },
10284 { "url_dom", "url", pat_parse_str, pat_match_dom },
10285 { "url_end", "url", pat_parse_str, pat_match_end },
10286 { "url_len", "url", pat_parse_int, pat_match_len },
10287 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10288 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010289
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010290 { "urlp", "urlp", pat_parse_str, pat_match_str },
10291 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10292 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10293 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10294 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10295 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10296 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10297 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010298
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010299 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010300}};
10301
10302/************************************************************************/
10303/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010304/************************************************************************/
10305/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010306static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010307 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10308 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10309 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10310
10311 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10312 * are only here to match the ACL's name, are request-only and are used
10313 * for ACL compatibility only.
10314 */
10315 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10316 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10317 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10318 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10319
10320 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10321 * only here to match the ACL's name, are request-only and are used for
10322 * ACL compatibility only.
10323 */
10324 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10325 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10326 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10327 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10328
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010329 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10330 { "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 +010010331 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10332 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10333 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010334
10335 /* HTTP protocol on the request path */
10336 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010337 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010338
10339 /* HTTP version on the request path */
10340 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010341 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010342
10343 /* HTTP version on the response path */
10344 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010345 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10346
Willy Tarreau18ed2562013-01-14 15:56:36 +010010347 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10348 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10349 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10350 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10351
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010352 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10353 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010354 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10355 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10356 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10357 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10358
10359 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10360 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10361 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10362 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10363
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010364 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10365 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010366 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10367 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10368 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10369 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10370
Willy Tarreau409bcde2013-01-08 00:31:00 +010010371 /* scook is valid only on the response and is used for ACL compatibility */
10372 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10373 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10374 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10375 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10376
10377 /* shdr is valid only on the response and is used for ACL compatibility */
10378 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10379 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10380 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10381 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10382
10383 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10384 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010385 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10386 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010387 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10388 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10389 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10390 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10391 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10392 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010393}};
10394
Willy Tarreau8797c062007-05-07 00:55:35 +020010395
Willy Tarreau276fae92013-07-25 14:36:01 +020010396/* Note: must not be declared <const> as its list will be overwritten */
10397static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10398 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10399 { NULL, NULL, 0, 0, 0 },
10400}};
10401
Willy Tarreau8797c062007-05-07 00:55:35 +020010402__attribute__((constructor))
10403static void __http_protocol_init(void)
10404{
10405 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010406 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010407 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010408}
10409
10410
Willy Tarreau58f10d72006-12-04 02:26:12 +010010411/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010412 * Local variables:
10413 * c-indent-level: 8
10414 * c-basic-offset: 8
10415 * End:
10416 */