blob: 3a4c535d3458da41d8e2243400ecb07124ac9975 [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;
3343
3344 /* build redirect message */
3345 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003346 case 308:
3347 msg_fmt = HTTP_308;
3348 break;
3349 case 307:
3350 msg_fmt = HTTP_307;
3351 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003352 case 303:
3353 msg_fmt = HTTP_303;
3354 break;
3355 case 301:
3356 msg_fmt = HTTP_301;
3357 break;
3358 case 302:
3359 default:
3360 msg_fmt = HTTP_302;
3361 break;
3362 }
3363
3364 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3365 return 0;
3366
3367 switch(rule->type) {
3368 case REDIRECT_TYPE_SCHEME: {
3369 const char *path;
3370 const char *host;
3371 struct hdr_ctx ctx;
3372 int pathlen;
3373 int hostlen;
3374
3375 host = "";
3376 hostlen = 0;
3377 ctx.idx = 0;
3378 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3379 host = ctx.line + ctx.val;
3380 hostlen = ctx.vlen;
3381 }
3382
3383 path = http_get_path(txn);
3384 /* build message using path */
3385 if (path) {
3386 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3387 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3388 int qs = 0;
3389 while (qs < pathlen) {
3390 if (path[qs] == '?') {
3391 pathlen = qs;
3392 break;
3393 }
3394 qs++;
3395 }
3396 }
3397 } else {
3398 path = "/";
3399 pathlen = 1;
3400 }
3401
3402 /* check if we can add scheme + "://" + host + path */
3403 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3404 return 0;
3405
3406 /* add scheme */
3407 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3408 trash.len += rule->rdr_len;
3409
3410 /* add "://" */
3411 memcpy(trash.str + trash.len, "://", 3);
3412 trash.len += 3;
3413
3414 /* add host */
3415 memcpy(trash.str + trash.len, host, hostlen);
3416 trash.len += hostlen;
3417
3418 /* add path */
3419 memcpy(trash.str + trash.len, path, pathlen);
3420 trash.len += pathlen;
3421
3422 /* append a slash at the end of the location is needed and missing */
3423 if (trash.len && trash.str[trash.len - 1] != '/' &&
3424 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3425 if (trash.len > trash.size - 5)
3426 return 0;
3427 trash.str[trash.len] = '/';
3428 trash.len++;
3429 }
3430
3431 break;
3432 }
3433 case REDIRECT_TYPE_PREFIX: {
3434 const char *path;
3435 int pathlen;
3436
3437 path = http_get_path(txn);
3438 /* build message using path */
3439 if (path) {
3440 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3441 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3442 int qs = 0;
3443 while (qs < pathlen) {
3444 if (path[qs] == '?') {
3445 pathlen = qs;
3446 break;
3447 }
3448 qs++;
3449 }
3450 }
3451 } else {
3452 path = "/";
3453 pathlen = 1;
3454 }
3455
3456 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3457 return 0;
3458
3459 /* add prefix. Note that if prefix == "/", we don't want to
3460 * add anything, otherwise it makes it hard for the user to
3461 * configure a self-redirection.
3462 */
3463 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3464 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3465 trash.len += rule->rdr_len;
3466 }
3467
3468 /* add path */
3469 memcpy(trash.str + trash.len, path, pathlen);
3470 trash.len += pathlen;
3471
3472 /* append a slash at the end of the location is needed and missing */
3473 if (trash.len && trash.str[trash.len - 1] != '/' &&
3474 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3475 if (trash.len > trash.size - 5)
3476 return 0;
3477 trash.str[trash.len] = '/';
3478 trash.len++;
3479 }
3480
3481 break;
3482 }
3483 case REDIRECT_TYPE_LOCATION:
3484 default:
3485 if (trash.len + rule->rdr_len > trash.size - 4)
3486 return 0;
3487
3488 /* add location */
3489 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3490 trash.len += rule->rdr_len;
3491 break;
3492 }
3493
3494 if (rule->cookie_len) {
3495 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3496 trash.len += 14;
3497 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3498 trash.len += rule->cookie_len;
3499 memcpy(trash.str + trash.len, "\r\n", 2);
3500 trash.len += 2;
3501 }
3502
3503 /* add end of headers and the keep-alive/close status.
3504 * We may choose to set keep-alive if the Location begins
3505 * with a slash, because the client will come back to the
3506 * same server.
3507 */
3508 txn->status = rule->code;
3509 /* let's log the request time */
3510 s->logs.tv_request = now;
3511
3512 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3513 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3514 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3515 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3516 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3517 /* keep-alive possible */
3518 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3519 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3520 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3521 trash.len += 30;
3522 } else {
3523 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3524 trash.len += 24;
3525 }
3526 }
3527 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3528 trash.len += 4;
3529 bo_inject(txn->rsp.chn, trash.str, trash.len);
3530 /* "eat" the request */
3531 bi_fast_delete(txn->req.chn->buf, msg->sov);
3532 msg->sov = 0;
3533 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3534 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3535 txn->req.msg_state = HTTP_MSG_CLOSED;
3536 txn->rsp.msg_state = HTTP_MSG_DONE;
3537 } else {
3538 /* keep-alive not possible */
3539 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3540 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3541 trash.len += 29;
3542 } else {
3543 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3544 trash.len += 23;
3545 }
3546 stream_int_retnclose(txn->req.chn->prod, &trash);
3547 txn->req.chn->analysers = 0;
3548 }
3549
3550 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003551 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003552 if (!(s->flags & SN_FINST_MASK))
3553 s->flags |= SN_FINST_R;
3554
3555 return 1;
3556}
3557
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003558/* This stream analyser runs all HTTP request processing which is common to
3559 * frontends and backends, which means blocking ACLs, filters, connection-close,
3560 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003561 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003562 * either needs more data or wants to immediately abort the request (eg: deny,
3563 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003564 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003565int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003566{
Willy Tarreaud787e662009-07-07 10:14:51 +02003567 struct http_txn *txn = &s->txn;
3568 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003569 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003570 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003571 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003572 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003573 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003574
Willy Tarreau655dce92009-11-08 13:10:58 +01003575 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003576 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003577 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003578 return 0;
3579 }
3580
Willy Tarreau3a816292009-07-07 10:55:49 +02003581 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003582 req->analyse_exp = TICK_ETERNITY;
3583
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003584 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 +02003585 now_ms, __FUNCTION__,
3586 s,
3587 req,
3588 req->rex, req->wex,
3589 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003590 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003591 req->analysers);
3592
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003593 /* first check whether we have some ACLs set to block this request */
3594 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003595 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003596
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003597 ret = acl_pass(ret);
3598 if (cond->pol == ACL_COND_UNLESS)
3599 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003600
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 if (ret) {
3602 txn->status = 403;
3603 /* let's log the request time */
3604 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003605 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003606 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003607 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003608 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003609 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003610
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003611 /* just in case we have some per-backend tracking */
3612 session_inc_be_http_req_ctr(s);
3613
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003614 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003615 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003616
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003618 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003619 do_stats = stats_check_uri(s->rep->prod, txn, px);
3620 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003621 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 +01003622 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003623 else
3624 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003625
Willy Tarreau3b44e722013-11-16 10:28:23 +01003626 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3627 * blocked by an http-request rule.
3628 */
3629 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003630 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003631 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003632 }
Willy Tarreau06619262006-12-17 08:37:22 +01003633
Willy Tarreau3b44e722013-11-16 10:28:23 +01003634 /* return a 403 if either rule has blocked */
3635 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003636 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003637 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003638 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003639 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003640 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003641 s->fe->fe_counters.denied_req++;
3642 if (s->fe != s->be)
3643 s->be->be_counters.denied_req++;
3644 if (s->listener->counters)
3645 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003646 goto return_prx_cond;
3647 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003648
3649 /* When a connection is tarpitted, we use the tarpit timeout,
3650 * which may be the same as the connect timeout if unspecified.
3651 * If unset, then set it to zero because we really want it to
3652 * eventually expire. We build the tarpit as an analyser.
3653 */
3654 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003655 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003656 /* wipe the request out so that we can drop the connection early
3657 * if the client closes first.
3658 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003659 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003660 req->analysers = 0; /* remove switching rules etc... */
3661 req->analysers |= AN_REQ_HTTP_TARPIT;
3662 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3663 if (!req->analyse_exp)
3664 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003665 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003666 s->fe->fe_counters.denied_req++;
3667 if (s->fe != s->be)
3668 s->be->be_counters.denied_req++;
3669 if (s->listener->counters)
3670 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003671 return 1;
3672 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003673 }
Willy Tarreau06619262006-12-17 08:37:22 +01003674
Willy Tarreau5b154472009-12-21 20:11:07 +01003675 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3676 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003677 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3678 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003679 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3680 * avoid to redo the same work if FE and BE have the same settings (common).
3681 * The method consists in checking if options changed between the two calls
3682 * (implying that either one is non-null, or one of them is non-null and we
3683 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003684 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003685
Willy Tarreaudc008c52010-02-01 16:20:08 +01003686 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3687 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3688 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3689 (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 +01003690 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003691
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003692 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3693 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003694 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003695 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3696 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003697 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003698 tmp = TX_CON_WANT_CLO;
3699
Willy Tarreau5b154472009-12-21 20:11:07 +01003700 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3701 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003702
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003703 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3704 /* parse the Connection header and possibly clean it */
3705 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003706 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003707 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3708 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003709 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003710 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003711 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003712 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003713 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003714
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003715 /* check if client or config asks for explicit close in KAL/SCL */
3716 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3717 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3718 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003719 (!(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 +01003720 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003721 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003722 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003723 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3724 }
Willy Tarreau78599912009-10-17 20:12:21 +02003725
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003726 /* we can be blocked here because the request needs to be authenticated,
3727 * either to pass or to access stats.
3728 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003729 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003730 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003731
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003732 if (!realm)
3733 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3734
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003735 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003736 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003737 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003738 /* on 401 we still count one error, because normal browsing
3739 * won't significantly increase the counter but brute force
3740 * attempts will.
3741 */
3742 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003743 goto return_prx_cond;
3744 }
3745
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003746 /* add request headers from the rule sets in the same order */
3747 list_for_each_entry(wl, &px->req_add, list) {
3748 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003749 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003750 ret = acl_pass(ret);
3751 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3752 ret = !ret;
3753 if (!ret)
3754 continue;
3755 }
3756
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003757 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003758 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003759 }
3760
3761 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3762 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3763 goto return_bad_req;
3764 req->analyse_exp = TICK_ETERNITY;
3765 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003766 }
3767
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003768 if (unlikely(do_stats)) {
3769 /* process the stats request now */
3770 if (!http_handle_stats(s, req)) {
3771 /* we need more data, let's come back here later */
3772 req->analysers |= an_bit;
3773 channel_dont_connect(req);
Willy Tarreau7fe33002013-04-21 08:04:22 +02003774 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003775 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003776 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003777 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003778
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003779 /* check whether we have some ACLs set to redirect this request */
3780 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003781 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003782 int ret;
3783
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003784 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003785 ret = acl_pass(ret);
3786 if (rule->cond->pol == ACL_COND_UNLESS)
3787 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003788 if (!ret)
3789 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003790 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003791 if (!http_apply_redirect_rule(rule, s, txn))
3792 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003793
Willy Tarreau71241ab2012-12-27 11:30:54 +01003794 req->analyse_exp = TICK_ETERNITY;
3795 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003796 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003797
Willy Tarreau2be39392010-01-03 17:24:51 +01003798 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3799 * If this happens, then the data will not come immediately, so we must
3800 * send all what we have without waiting. Note that due to the small gain
3801 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003802 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003803 * itself once used.
3804 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003805 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003806
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003807 /* that's OK for us now, let's move on to next analysers */
3808 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003809
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003810 return_bad_req:
3811 /* We centralize bad requests processing here */
3812 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3813 /* we detected a parsing error. We want to archive this request
3814 * in the dedicated proxy area for later troubleshooting.
3815 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003816 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003817 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003818
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003819 txn->req.msg_state = HTTP_MSG_ERROR;
3820 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003821 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003822
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003823 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003824 if (s->listener->counters)
3825 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003826
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003827 return_prx_cond:
3828 if (!(s->flags & SN_ERR_MASK))
3829 s->flags |= SN_ERR_PRXCOND;
3830 if (!(s->flags & SN_FINST_MASK))
3831 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003833 req->analysers = 0;
3834 req->analyse_exp = TICK_ETERNITY;
3835 return 0;
3836}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003837
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003838/* This function performs all the processing enabled for the current request.
3839 * It returns 1 if the processing can continue on next analysers, or zero if it
3840 * needs more data, encounters an error, or wants to immediately abort the
3841 * request. It relies on buffers flags, and updates s->req->analysers.
3842 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003843int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003844{
3845 struct http_txn *txn = &s->txn;
3846 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003847
Willy Tarreau655dce92009-11-08 13:10:58 +01003848 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003849 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003850 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003851 return 0;
3852 }
3853
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003854 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 +02003855 now_ms, __FUNCTION__,
3856 s,
3857 req,
3858 req->rex, req->wex,
3859 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003860 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003861 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003862
William Lallemand82fe75c2012-10-23 10:25:10 +02003863 if (s->fe->comp || s->be->comp)
3864 select_compression_request_header(s, req->buf);
3865
Willy Tarreau59234e92008-11-30 23:51:27 +01003866 /*
3867 * Right now, we know that we have processed the entire headers
3868 * and that unwanted requests have been filtered out. We can do
3869 * whatever we want with the remaining request. Also, now we
3870 * may have separate values for ->fe, ->be.
3871 */
Willy Tarreau06619262006-12-17 08:37:22 +01003872
Willy Tarreau59234e92008-11-30 23:51:27 +01003873 /*
3874 * If HTTP PROXY is set we simply get remote server address
3875 * parsing incoming request.
3876 */
3877 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003878 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 +01003879 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003880
Willy Tarreau59234e92008-11-30 23:51:27 +01003881 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003882 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003883 * Note that doing so might move headers in the request, but
3884 * the fields will stay coherent and the URI will not move.
3885 * This should only be performed in the backend.
3886 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003887 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003888 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3889 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003890
Willy Tarreau59234e92008-11-30 23:51:27 +01003891 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003892 * 8: the appsession cookie was looked up very early in 1.2,
3893 * so let's do the same now.
3894 */
3895
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003896 /* It needs to look into the URI unless persistence must be ignored */
3897 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003898 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 +01003899 }
3900
William Lallemanda73203e2012-03-12 12:48:57 +01003901 /* add unique-id if "header-unique-id" is specified */
3902
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003903 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3904 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3905 goto return_bad_req;
3906 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003907 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003908 }
William Lallemanda73203e2012-03-12 12:48:57 +01003909
3910 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3912 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003913 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003914 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003915 goto return_bad_req;
3916 }
3917
Cyril Bontéb21570a2009-11-29 20:04:48 +01003918 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003919 * 9: add X-Forwarded-For if either the frontend or the backend
3920 * asks for it.
3921 */
3922 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003923 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003924 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003925 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3926 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003927 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003928 /* The header is set to be added only if none is present
3929 * and we found it, so don't do anything.
3930 */
3931 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003932 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003933 /* Add an X-Forwarded-For header unless the source IP is
3934 * in the 'except' network range.
3935 */
3936 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003937 (((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 +01003938 != s->fe->except_net.s_addr) &&
3939 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003940 (((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 +01003941 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003942 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003943 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003944 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003945
3946 /* Note: we rely on the backend to get the header name to be used for
3947 * x-forwarded-for, because the header is really meant for the backends.
3948 * However, if the backend did not specify any option, we have to rely
3949 * on the frontend's header name.
3950 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003951 if (s->be->fwdfor_hdr_len) {
3952 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003953 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003954 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003955 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003956 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003957 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003958 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003959
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003960 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003961 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003962 }
3963 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003964 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 /* FIXME: for the sake of completeness, we should also support
3966 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003967 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003968 int len;
3969 char pn[INET6_ADDRSTRLEN];
3970 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003971 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003972 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003973
Willy Tarreau59234e92008-11-30 23:51:27 +01003974 /* Note: we rely on the backend to get the header name to be used for
3975 * x-forwarded-for, because the header is really meant for the backends.
3976 * However, if the backend did not specify any option, we have to rely
3977 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003978 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003979 if (s->be->fwdfor_hdr_len) {
3980 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003981 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003982 } else {
3983 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003984 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003985 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003986 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003987
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 goto return_bad_req;
3990 }
3991 }
3992
3993 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003994 * 10: add X-Original-To if either the frontend or the backend
3995 * asks for it.
3996 */
3997 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3998
3999 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004000 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004001 /* Add an X-Original-To header unless the destination IP is
4002 * in the 'except' network range.
4003 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004004 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004005
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004006 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004007 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004008 (((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 +02004009 != s->fe->except_to.s_addr) &&
4010 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004011 (((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 +02004012 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004013 int len;
4014 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004015 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004016
4017 /* Note: we rely on the backend to get the header name to be used for
4018 * x-original-to, because the header is really meant for the backends.
4019 * However, if the backend did not specify any option, we have to rely
4020 * on the frontend's header name.
4021 */
4022 if (s->be->orgto_hdr_len) {
4023 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004024 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004025 } else {
4026 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004027 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004028 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004029 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004031 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004032 goto return_bad_req;
4033 }
4034 }
4035 }
4036
Willy Tarreau50fc7772012-11-11 22:19:57 +01004037 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4038 * If an "Upgrade" token is found, the header is left untouched in order not to have
4039 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4040 * "Upgrade" is present in the Connection header.
4041 */
4042 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4043 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4044 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004045 unsigned int want_flags = 0;
4046
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004047 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004048 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
4049 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
4050 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004051 want_flags |= TX_CON_CLO_SET;
4052 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004053 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
4054 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
4055 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004056 want_flags |= TX_CON_KAL_SET;
4057 }
4058
4059 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004060 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004061 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004062
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004063
Willy Tarreau522d6c02009-12-06 18:49:18 +01004064 /* If we have no server assigned yet and we're balancing on url_param
4065 * with a POST request, we may be interested in checking the body for
4066 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004067 */
4068 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4069 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004070 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004071 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004072 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004073 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004074 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004075
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004076 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004077 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004078#ifdef TCP_QUICKACK
4079 /* We expect some data from the client. Unless we know for sure
4080 * we already have a full request, we have to re-enable quick-ack
4081 * in case we previously disabled it, otherwise we might cause
4082 * the client to delay further data.
4083 */
4084 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004085 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004086 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004087 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004088#endif
4089 }
Willy Tarreau03945942009-12-22 16:50:27 +01004090
Willy Tarreau59234e92008-11-30 23:51:27 +01004091 /*************************************************************
4092 * OK, that's finished for the headers. We have done what we *
4093 * could. Let's switch to the DATA state. *
4094 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004095 req->analyse_exp = TICK_ETERNITY;
4096 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004097
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004098 /* if the server closes the connection, we want to immediately react
4099 * and close the socket to save packets and syscalls.
4100 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004101 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4102 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004103
Willy Tarreau59234e92008-11-30 23:51:27 +01004104 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004105 /* OK let's go on with the BODY now */
4106 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004107
Willy Tarreau59234e92008-11-30 23:51:27 +01004108 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004109 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004110 /* we detected a parsing error. We want to archive this request
4111 * in the dedicated proxy area for later troubleshooting.
4112 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004113 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004114 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004115
Willy Tarreau59234e92008-11-30 23:51:27 +01004116 txn->req.msg_state = HTTP_MSG_ERROR;
4117 txn->status = 400;
4118 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004119 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004120
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004121 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004122 if (s->listener->counters)
4123 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004124
Willy Tarreau59234e92008-11-30 23:51:27 +01004125 if (!(s->flags & SN_ERR_MASK))
4126 s->flags |= SN_ERR_PRXCOND;
4127 if (!(s->flags & SN_FINST_MASK))
4128 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004129 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004130}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004131
Willy Tarreau60b85b02008-11-30 23:28:40 +01004132/* This function is an analyser which processes the HTTP tarpit. It always
4133 * returns zero, at the beginning because it prevents any other processing
4134 * from occurring, and at the end because it terminates the request.
4135 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004136int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004137{
4138 struct http_txn *txn = &s->txn;
4139
4140 /* This connection is being tarpitted. The CLIENT side has
4141 * already set the connect expiration date to the right
4142 * timeout. We just have to check that the client is still
4143 * there and that the timeout has not expired.
4144 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004145 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004146 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004147 !tick_is_expired(req->analyse_exp, now_ms))
4148 return 0;
4149
4150 /* We will set the queue timer to the time spent, just for
4151 * logging purposes. We fake a 500 server error, so that the
4152 * attacker will not suspect his connection has been tarpitted.
4153 * It will not cause trouble to the logs because we can exclude
4154 * the tarpitted connections by filtering on the 'PT' status flags.
4155 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004156 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4157
4158 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004159 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004160 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004161
4162 req->analysers = 0;
4163 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004164
Willy Tarreau60b85b02008-11-30 23:28:40 +01004165 if (!(s->flags & SN_ERR_MASK))
4166 s->flags |= SN_ERR_PRXCOND;
4167 if (!(s->flags & SN_FINST_MASK))
4168 s->flags |= SN_FINST_T;
4169 return 0;
4170}
4171
Willy Tarreaud34af782008-11-30 23:36:37 +01004172/* This function is an analyser which processes the HTTP request body. It looks
4173 * for parameters to be used for the load balancing algorithm (url_param). It
4174 * must only be called after the standard HTTP request processing has occurred,
4175 * because it expects the request to be parsed. It returns zero if it needs to
4176 * read more data, or 1 once it has completed its analysis.
4177 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004178int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004179{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004180 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004181 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004182 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004183
4184 /* We have to parse the HTTP request body to find any required data.
4185 * "balance url_param check_post" should have been the only way to get
4186 * into this. We were brought here after HTTP header analysis, so all
4187 * related structures are ready.
4188 */
4189
Willy Tarreau522d6c02009-12-06 18:49:18 +01004190 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4191 goto missing_data;
4192
4193 if (msg->msg_state < HTTP_MSG_100_SENT) {
4194 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4195 * send an HTTP/1.1 100 Continue intermediate response.
4196 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004197 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004198 struct hdr_ctx ctx;
4199 ctx.idx = 0;
4200 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004201 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004202 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004203 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004204 }
4205 }
4206 msg->msg_state = HTTP_MSG_100_SENT;
4207 }
4208
4209 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004210 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004211 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004212 * is still null. We must save the body in msg->next because it
4213 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004214 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004215 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004216
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004217 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004218 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4219 else
4220 msg->msg_state = HTTP_MSG_DATA;
4221 }
4222
4223 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004224 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004225 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004226 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004227 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004228 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004229
Willy Tarreau115acb92009-12-26 13:56:06 +01004230 if (!ret)
4231 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004232 else if (ret < 0) {
4233 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004234 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004235 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004236 }
4237
Willy Tarreaud98cf932009-12-27 22:54:55 +01004238 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004239 * We have the first data byte is in msg->sov. We're waiting for at
4240 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004241 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004242
Willy Tarreau124d9912011-03-01 20:30:48 +01004243 if (msg->body_len < limit)
4244 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004245
Willy Tarreau9b28e032012-10-12 23:49:43 +02004246 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004247 goto http_end;
4248
4249 missing_data:
4250 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004251 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004252 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004253 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004254 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004255
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004256 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004257 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004258 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004259
4260 if (!(s->flags & SN_ERR_MASK))
4261 s->flags |= SN_ERR_CLITO;
4262 if (!(s->flags & SN_FINST_MASK))
4263 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004264 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004265 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004266
4267 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004268 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004269 /* Not enough data. We'll re-use the http-request
4270 * timeout here. Ideally, we should set the timeout
4271 * relative to the accept() date. We just set the
4272 * request timeout once at the beginning of the
4273 * request.
4274 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004275 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004276 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004277 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004278 return 0;
4279 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004280
4281 http_end:
4282 /* The situation will not evolve, so let's give up on the analysis. */
4283 s->logs.tv_request = now; /* update the request timer to reflect full request */
4284 req->analysers &= ~an_bit;
4285 req->analyse_exp = TICK_ETERNITY;
4286 return 1;
4287
4288 return_bad_req: /* let's centralize all bad requests */
4289 txn->req.msg_state = HTTP_MSG_ERROR;
4290 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004291 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004292
Willy Tarreau79ebac62010-06-07 13:47:49 +02004293 if (!(s->flags & SN_ERR_MASK))
4294 s->flags |= SN_ERR_PRXCOND;
4295 if (!(s->flags & SN_FINST_MASK))
4296 s->flags |= SN_FINST_R;
4297
Willy Tarreau522d6c02009-12-06 18:49:18 +01004298 return_err_msg:
4299 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004300 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004301 if (s->listener->counters)
4302 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004303 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004304}
4305
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004306/* send a server's name with an outgoing request over an established connection.
4307 * Note: this function is designed to be called once the request has been scheduled
4308 * for being forwarded. This is the reason why it rewinds the buffer before
4309 * proceeding.
4310 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004311int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004312
4313 struct hdr_ctx ctx;
4314
Mark Lamourinec2247f02012-01-04 13:02:01 -05004315 char *hdr_name = be->server_id_hdr_name;
4316 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004317 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004318 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004319 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004320
William Lallemandd9e90662012-01-30 17:27:17 +01004321 ctx.idx = 0;
4322
Willy Tarreau9b28e032012-10-12 23:49:43 +02004323 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004324 if (old_o) {
4325 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004326 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004327 }
4328
Willy Tarreau9b28e032012-10-12 23:49:43 +02004329 old_i = chn->buf->i;
4330 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 -05004331 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004332 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004333 }
4334
4335 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004336 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004337 memcpy(hdr_val, hdr_name, hdr_name_len);
4338 hdr_val += hdr_name_len;
4339 *hdr_val++ = ':';
4340 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004341 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4342 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004343
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004344 if (old_o) {
4345 /* If this was a forwarded request, we must readjust the amount of
4346 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004347 * variations. Note that if the request was already scheduled for
4348 * forwarding, it had its req->sol pointing to the body, which
4349 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004350 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004351 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004352 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004353 }
4354
Mark Lamourinec2247f02012-01-04 13:02:01 -05004355 return 0;
4356}
4357
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358/* Terminate current transaction and prepare a new one. This is very tricky
4359 * right now but it works.
4360 */
4361void http_end_txn_clean_session(struct session *s)
4362{
4363 /* FIXME: We need a more portable way of releasing a backend's and a
4364 * server's connections. We need a safer way to reinitialize buffer
4365 * flags. We also need a more accurate method for computing per-request
4366 * data.
4367 */
4368 http_silent_debug(__LINE__, s);
4369
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004370 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004371 si_shutr(s->req->cons);
4372 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373
4374 http_silent_debug(__LINE__, s);
4375
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004376 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004377 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004378 if (unlikely(s->srv_conn))
4379 sess_change_server(s, NULL);
4380 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004381
4382 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4383 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004384 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004385
4386 if (s->txn.status) {
4387 int n;
4388
4389 n = s->txn.status / 100;
4390 if (n < 1 || n > 5)
4391 n = 0;
4392
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004393 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004394 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004395 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004396 s->fe->fe_counters.p.http.comp_rsp++;
4397 }
Willy Tarreau24657792010-02-26 10:30:28 +01004398 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004399 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004400 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004401 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004402 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004403 s->be->be_counters.p.http.comp_rsp++;
4404 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405 }
4406
4407 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004408 s->logs.bytes_in -= s->req->buf->i;
4409 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410
4411 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004412 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413 !(s->flags & SN_MONITOR) &&
4414 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4415 s->do_log(s);
4416 }
4417
4418 s->logs.accept_date = date; /* user-visible date for logging */
4419 s->logs.tv_accept = now; /* corrected date for internal use */
4420 tv_zero(&s->logs.tv_request);
4421 s->logs.t_queue = -1;
4422 s->logs.t_connect = -1;
4423 s->logs.t_data = -1;
4424 s->logs.t_close = 0;
4425 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4426 s->logs.srv_queue_size = 0; /* we will get this number soon */
4427
Willy Tarreau9b28e032012-10-12 23:49:43 +02004428 s->logs.bytes_in = s->req->total = s->req->buf->i;
4429 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430
4431 if (s->pend_pos)
4432 pendconn_free(s->pend_pos);
4433
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004434 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 if (s->flags & SN_CURR_SESS) {
4436 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004437 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004438 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004439 if (may_dequeue_tasks(objt_server(s->target), s->be))
4440 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441 }
4442
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004443 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444
4445 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004446 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4447 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004448 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004449 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004450 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 s->req->cons->err_loc = NULL;
4452 s->req->cons->exp = TICK_ETERNITY;
4453 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004454 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4455 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 +02004456 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 +01004457 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004458
4459 if (s->flags & SN_COMP_READY)
4460 s->comp_algo->end(&s->comp_ctx);
4461 s->comp_algo = NULL;
4462 s->flags &= ~SN_COMP_READY;
4463
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 s->txn.meth = 0;
4465 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004466 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004467 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468 s->req->cons->flags |= SI_FL_INDEP_STR;
4469
Willy Tarreau96e31212011-05-30 18:10:30 +02004470 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004471 s->req->flags |= CF_NEVER_WAIT;
4472 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004473 }
4474
Willy Tarreau610ecce2010-01-04 21:15:02 +01004475 /* if the request buffer is not empty, it means we're
4476 * about to process another request, so send pending
4477 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004478 * Just don't do this if the buffer is close to be full,
4479 * because the request will wait for it to flush a little
4480 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004482 if (s->req->buf->i) {
4483 if (s->rep->buf->o &&
4484 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4485 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004486 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004487 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004488
4489 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004490 channel_auto_read(s->req);
4491 channel_auto_close(s->req);
4492 channel_auto_read(s->rep);
4493 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494
Willy Tarreau342b11c2010-11-24 16:22:09 +01004495 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004496 s->rep->analysers = 0;
4497
4498 http_silent_debug(__LINE__, s);
4499}
4500
4501
4502/* This function updates the request state machine according to the response
4503 * state machine and buffer flags. It returns 1 if it changes anything (flag
4504 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4505 * it is only used to find when a request/response couple is complete. Both
4506 * this function and its equivalent should loop until both return zero. It
4507 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4508 */
4509int http_sync_req_state(struct session *s)
4510{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004511 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004512 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004513 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 unsigned int old_state = txn->req.msg_state;
4515
4516 http_silent_debug(__LINE__, s);
4517 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4518 return 0;
4519
4520 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004521 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004522 * We can shut the read side unless we want to abort_on_close,
4523 * or we have a POST request. The issue with POST requests is
4524 * that some browsers still send a CRLF after the request, and
4525 * this CRLF must be read so that it does not remain in the kernel
4526 * buffers, otherwise a close could cause an RST on some systems
4527 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004528 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004529 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004530 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531
Willy Tarreau40f151a2012-12-20 12:10:09 +01004532 /* if the server closes the connection, we want to immediately react
4533 * and close the socket to save packets and syscalls.
4534 */
4535 chn->cons->flags |= SI_FL_NOHALF;
4536
Willy Tarreau610ecce2010-01-04 21:15:02 +01004537 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4538 goto wait_other_side;
4539
4540 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4541 /* The server has not finished to respond, so we
4542 * don't want to move in order not to upset it.
4543 */
4544 goto wait_other_side;
4545 }
4546
4547 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4548 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004549 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004551 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004552 goto wait_other_side;
4553 }
4554
4555 /* When we get here, it means that both the request and the
4556 * response have finished receiving. Depending on the connection
4557 * mode, we'll have to wait for the last bytes to leave in either
4558 * direction, and sometimes for a close to be effective.
4559 */
4560
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004561 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4562 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004563 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4564 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004565 }
4566 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4567 /* Option forceclose is set, or either side wants to close,
4568 * let's enforce it now that we're not expecting any new
4569 * data to come. The caller knows the session is complete
4570 * once both states are CLOSED.
4571 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004572 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4573 channel_shutr_now(chn);
4574 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004575 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004576 }
4577 else {
4578 /* The last possible modes are keep-alive and tunnel. Since tunnel
4579 * mode does not set the body analyser, we can't reach this place
4580 * in tunnel mode, so we're left with keep-alive only.
4581 * This mode is currently not implemented, we switch to tunnel mode.
4582 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004583 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004584 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004585 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004586 }
4587
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004588 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004590 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004591
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004592 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004593 txn->req.msg_state = HTTP_MSG_CLOSING;
4594 goto http_msg_closing;
4595 }
4596 else {
4597 txn->req.msg_state = HTTP_MSG_CLOSED;
4598 goto http_msg_closed;
4599 }
4600 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004601 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 }
4603
4604 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4605 http_msg_closing:
4606 /* nothing else to forward, just waiting for the output buffer
4607 * to be empty and for the shutw_now to take effect.
4608 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004609 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004610 txn->req.msg_state = HTTP_MSG_CLOSED;
4611 goto http_msg_closed;
4612 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004613 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004614 txn->req.msg_state = HTTP_MSG_ERROR;
4615 goto wait_other_side;
4616 }
4617 }
4618
4619 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4620 http_msg_closed:
4621 goto wait_other_side;
4622 }
4623
4624 wait_other_side:
4625 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004626 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627}
4628
4629
4630/* This function updates the response state machine according to the request
4631 * state machine and buffer flags. It returns 1 if it changes anything (flag
4632 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4633 * it is only used to find when a request/response couple is complete. Both
4634 * this function and its equivalent should loop until both return zero. It
4635 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4636 */
4637int http_sync_res_state(struct session *s)
4638{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004639 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004640 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004641 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004642 unsigned int old_state = txn->rsp.msg_state;
4643
4644 http_silent_debug(__LINE__, s);
4645 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4646 return 0;
4647
4648 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4649 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004650 * still monitor the server connection for a possible close
4651 * while the request is being uploaded, so we don't disable
4652 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004653 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004654 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004655
4656 if (txn->req.msg_state == HTTP_MSG_ERROR)
4657 goto wait_other_side;
4658
4659 if (txn->req.msg_state < HTTP_MSG_DONE) {
4660 /* The client seems to still be sending data, probably
4661 * because we got an error response during an upload.
4662 * We have the choice of either breaking the connection
4663 * or letting it pass through. Let's do the later.
4664 */
4665 goto wait_other_side;
4666 }
4667
4668 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4669 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004670 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004672 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004673 goto wait_other_side;
4674 }
4675
4676 /* When we get here, it means that both the request and the
4677 * response have finished receiving. Depending on the connection
4678 * mode, we'll have to wait for the last bytes to leave in either
4679 * direction, and sometimes for a close to be effective.
4680 */
4681
4682 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4683 /* Server-close mode : shut read and wait for the request
4684 * side to close its output buffer. The caller will detect
4685 * when we're in DONE and the other is in CLOSED and will
4686 * catch that for the final cleanup.
4687 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004688 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4689 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004690 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004691 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4692 /* Option forceclose is set, or either side wants to close,
4693 * let's enforce it now that we're not expecting any new
4694 * data to come. The caller knows the session is complete
4695 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004696 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004697 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4698 channel_shutr_now(chn);
4699 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004700 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004701 }
4702 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004703 /* The last possible modes are keep-alive and tunnel. Since tunnel
4704 * mode does not set the body analyser, we can't reach this place
4705 * in tunnel mode, so we're left with keep-alive only.
4706 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004708 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004709 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004710 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004711 }
4712
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004713 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004715 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4717 goto http_msg_closing;
4718 }
4719 else {
4720 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4721 goto http_msg_closed;
4722 }
4723 }
4724 goto wait_other_side;
4725 }
4726
4727 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4728 http_msg_closing:
4729 /* nothing else to forward, just waiting for the output buffer
4730 * to be empty and for the shutw_now to take effect.
4731 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004732 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004733 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4734 goto http_msg_closed;
4735 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004736 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004737 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004738 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004739 if (objt_server(s->target))
4740 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 goto wait_other_side;
4742 }
4743 }
4744
4745 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4746 http_msg_closed:
4747 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004748 bi_erase(chn);
4749 channel_auto_close(chn);
4750 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 goto wait_other_side;
4752 }
4753
4754 wait_other_side:
4755 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004756 /* We force the response to leave immediately if we're waiting for the
4757 * other side, since there is no pending shutdown to push it out.
4758 */
4759 if (!channel_is_empty(chn))
4760 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004761 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004762}
4763
4764
4765/* Resync the request and response state machines. Return 1 if either state
4766 * changes.
4767 */
4768int http_resync_states(struct session *s)
4769{
4770 struct http_txn *txn = &s->txn;
4771 int old_req_state = txn->req.msg_state;
4772 int old_res_state = txn->rsp.msg_state;
4773
4774 http_silent_debug(__LINE__, s);
4775 http_sync_req_state(s);
4776 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004777 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004778 if (!http_sync_res_state(s))
4779 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004780 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004781 if (!http_sync_req_state(s))
4782 break;
4783 }
4784 http_silent_debug(__LINE__, s);
4785 /* OK, both state machines agree on a compatible state.
4786 * There are a few cases we're interested in :
4787 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4788 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4789 * directions, so let's simply disable both analysers.
4790 * - HTTP_MSG_CLOSED on the response only means we must abort the
4791 * request.
4792 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4793 * with server-close mode means we've completed one request and we
4794 * must re-initialize the server connection.
4795 */
4796
4797 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4798 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4799 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4800 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4801 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004802 channel_auto_close(s->req);
4803 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004805 channel_auto_close(s->rep);
4806 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004808 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4809 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004810 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004811 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004812 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004813 channel_auto_close(s->rep);
4814 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004815 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004816 channel_abort(s->req);
4817 channel_auto_close(s->req);
4818 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004819 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004820 }
4821 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4822 txn->rsp.msg_state == HTTP_MSG_DONE &&
4823 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4824 /* server-close: terminate this server connection and
4825 * reinitialize a fresh-new transaction.
4826 */
4827 http_end_txn_clean_session(s);
4828 }
4829
4830 http_silent_debug(__LINE__, s);
4831 return txn->req.msg_state != old_req_state ||
4832 txn->rsp.msg_state != old_res_state;
4833}
4834
Willy Tarreaud98cf932009-12-27 22:54:55 +01004835/* This function is an analyser which forwards request body (including chunk
4836 * sizes if any). It is called as soon as we must forward, even if we forward
4837 * zero byte. The only situation where it must not be called is when we're in
4838 * tunnel mode and we want to forward till the close. It's used both to forward
4839 * remaining data and to resync after end of body. It expects the msg_state to
4840 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4841 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004842 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004843 * bytes of pending data + the headers if not already done (between sol and sov).
4844 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004845 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004846int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847{
4848 struct http_txn *txn = &s->txn;
4849 struct http_msg *msg = &s->txn.req;
4850
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004851 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4852 return 0;
4853
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004854 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004855 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004856 /* Output closed while we were sending data. We must abort and
4857 * wake the other side up.
4858 */
4859 msg->msg_state = HTTP_MSG_ERROR;
4860 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004861 return 1;
4862 }
4863
Willy Tarreau4fe41902010-06-07 22:27:41 +02004864 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004865 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866
4867 /* Note that we don't have to send 100-continue back because we don't
4868 * need the data to complete our job, and it's up to the server to
4869 * decide whether to return 100, 417 or anything else in return of
4870 * an "Expect: 100-continue" header.
4871 */
4872
4873 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004874 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004875 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004876 * is still null. We must save the body in msg->next because it
4877 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004878 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004879 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004880
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004881 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004882 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004883 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004884 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004885 }
4886
Willy Tarreaud98cf932009-12-27 22:54:55 +01004887 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004888 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004889
Willy Tarreau610ecce2010-01-04 21:15:02 +01004890 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004891 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004892 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004893 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004894 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004895 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004896 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004897 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004898 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004899
Willy Tarreaucaabe412010-01-03 23:08:28 +01004900 if (msg->msg_state == HTTP_MSG_DATA) {
4901 /* must still forward */
4902 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004903 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004904
4905 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004906 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004907 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004909 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004910 }
4911 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004912 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004913 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004914 * TRAILERS state.
4915 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004916 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004917
Willy Tarreau54d23df2012-10-25 19:04:45 +02004918 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004919 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004920 else if (ret < 0) {
4921 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004922 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004923 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004924 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004925 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004926 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004927 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004928 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004929 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004930 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004931
4932 if (ret == 0)
4933 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004934 else if (ret < 0) {
4935 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004936 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004937 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004938 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004939 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004940 /* we're in MSG_CHUNK_SIZE now */
4941 }
4942 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004943 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004944
4945 if (ret == 0)
4946 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004947 else if (ret < 0) {
4948 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004949 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004950 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004952 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004953 /* we're in HTTP_MSG_DONE now */
4954 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004955 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004956 int old_state = msg->msg_state;
4957
Willy Tarreau610ecce2010-01-04 21:15:02 +01004958 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004959 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004960 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4961 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004962 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 if (http_resync_states(s)) {
4964 /* some state changes occurred, maybe the analyser
4965 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004966 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004967 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004968 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004969 /* request errors are most likely due to
4970 * the server aborting the transfer.
4971 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004972 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004973 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004974 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004975 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004976 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004977 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004978 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004979 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004980
4981 /* If "option abortonclose" is set on the backend, we
4982 * want to monitor the client's connection and forward
4983 * any shutdown notification to the server, which will
4984 * decide whether to close or to go on processing the
4985 * request.
4986 */
4987 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004988 channel_auto_read(req);
4989 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004990 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004991 else if (s->txn.meth == HTTP_METH_POST) {
4992 /* POST requests may require to read extra CRLF
4993 * sent by broken browsers and which could cause
4994 * an RST to be sent upon close on some systems
4995 * (eg: Linux).
4996 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004997 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004998 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004999
Willy Tarreau610ecce2010-01-04 21:15:02 +01005000 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005001 }
5002 }
5003
Willy Tarreaud98cf932009-12-27 22:54:55 +01005004 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005005 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005006 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005007 if (!(s->flags & SN_ERR_MASK))
5008 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005009 if (!(s->flags & SN_FINST_MASK)) {
5010 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5011 s->flags |= SN_FINST_H;
5012 else
5013 s->flags |= SN_FINST_D;
5014 }
5015
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005016 s->fe->fe_counters.cli_aborts++;
5017 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005018 if (objt_server(s->target))
5019 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005020
5021 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005022 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005023
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005024 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005025 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005026 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005027
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005028 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005029 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005030 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005031 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005032 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005033
Willy Tarreau5c620922011-05-11 19:56:11 +02005034 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005035 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005036 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005037 * modes are already handled by the stream sock layer. We must not do
5038 * this in content-length mode because it could present the MSG_MORE
5039 * flag with the last block of forwarded data, which would cause an
5040 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005041 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005042 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005043 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005044
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005046 return 0;
5047
Willy Tarreaud98cf932009-12-27 22:54:55 +01005048 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005049 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005050 if (s->listener->counters)
5051 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005052 return_bad_req_stats_ok:
5053 txn->req.msg_state = HTTP_MSG_ERROR;
5054 if (txn->status) {
5055 /* Note: we don't send any error if some data were already sent */
5056 stream_int_retnclose(req->prod, NULL);
5057 } else {
5058 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005059 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005060 }
5061 req->analysers = 0;
5062 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005063
5064 if (!(s->flags & SN_ERR_MASK))
5065 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005066 if (!(s->flags & SN_FINST_MASK)) {
5067 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5068 s->flags |= SN_FINST_H;
5069 else
5070 s->flags |= SN_FINST_D;
5071 }
5072 return 0;
5073
5074 aborted_xfer:
5075 txn->req.msg_state = HTTP_MSG_ERROR;
5076 if (txn->status) {
5077 /* Note: we don't send any error if some data were already sent */
5078 stream_int_retnclose(req->prod, NULL);
5079 } else {
5080 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005081 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005082 }
5083 req->analysers = 0;
5084 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5085
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005086 s->fe->fe_counters.srv_aborts++;
5087 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005088 if (objt_server(s->target))
5089 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005090
5091 if (!(s->flags & SN_ERR_MASK))
5092 s->flags |= SN_ERR_SRVCL;
5093 if (!(s->flags & SN_FINST_MASK)) {
5094 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5095 s->flags |= SN_FINST_H;
5096 else
5097 s->flags |= SN_FINST_D;
5098 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005099 return 0;
5100}
5101
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005102/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5103 * processing can continue on next analysers, or zero if it either needs more
5104 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5105 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5106 * when it has nothing left to do, and may remove any analyser when it wants to
5107 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005108 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005109int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005110{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005111 struct http_txn *txn = &s->txn;
5112 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005113 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005114 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005115 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005116 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005117
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005118 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 +02005119 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005121 rep,
5122 rep->rex, rep->wex,
5123 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005124 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005125 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005126
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005127 /*
5128 * Now parse the partial (or complete) lines.
5129 * We will check the response syntax, and also join multi-line
5130 * headers. An index of all the lines will be elaborated while
5131 * parsing.
5132 *
5133 * For the parsing, we use a 28 states FSM.
5134 *
5135 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005136 * rep->buf->p = beginning of response
5137 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5138 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005139 * msg->eol = end of current header or line (LF or CRLF)
5140 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 */
5142
Willy Tarreau83e3af02009-12-28 17:39:57 +01005143 /* There's a protected area at the end of the buffer for rewriting
5144 * purposes. We don't want to start to parse the request if the
5145 * protected area is affected, because we may have to move processed
5146 * data later, which is much more complicated.
5147 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005148 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005149 if (unlikely(!channel_reserved(rep))) {
5150 /* some data has still not left the buffer, wake us once that's done */
5151 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5152 goto abort_response;
5153 channel_dont_close(rep);
5154 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5155 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005156 }
5157
Willy Tarreau379357a2013-06-08 12:55:46 +02005158 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5159 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5160 buffer_slow_realign(rep->buf);
5161
Willy Tarreau9b28e032012-10-12 23:49:43 +02005162 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005163 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005164 }
5165
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005166 /* 1: we might have to print this header in debug mode */
5167 if (unlikely((global.mode & MODE_DEBUG) &&
5168 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005169 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005170 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005171
Willy Tarreau9b28e032012-10-12 23:49:43 +02005172 sol = rep->buf->p;
5173 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005174 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005175
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005176 sol += hdr_idx_first_pos(&txn->hdr_idx);
5177 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005178
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005179 while (cur_idx) {
5180 eol = sol + txn->hdr_idx.v[cur_idx].len;
5181 debug_hdr("srvhdr", s, sol, eol);
5182 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5183 cur_idx = txn->hdr_idx.v[cur_idx].next;
5184 }
5185 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005186
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005187 /*
5188 * Now we quickly check if we have found a full valid response.
5189 * If not so, we check the FD and buffer states before leaving.
5190 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005191 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005192 * responses are checked first.
5193 *
5194 * Depending on whether the client is still there or not, we
5195 * may send an error response back or not. Note that normally
5196 * we should only check for HTTP status there, and check I/O
5197 * errors somewhere else.
5198 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005199
Willy Tarreau655dce92009-11-08 13:10:58 +01005200 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005201 /* Invalid response */
5202 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5203 /* we detected a parsing error. We want to archive this response
5204 * in the dedicated proxy area for later troubleshooting.
5205 */
5206 hdr_response_bad:
5207 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005208 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005210 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005211 if (objt_server(s->target)) {
5212 objt_server(s->target)->counters.failed_resp++;
5213 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005214 }
Willy Tarreau64648412010-03-05 10:41:54 +01005215 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005216 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005217 rep->analysers = 0;
5218 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005219 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005220 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005221 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005222
5223 if (!(s->flags & SN_ERR_MASK))
5224 s->flags |= SN_ERR_PRXCOND;
5225 if (!(s->flags & SN_FINST_MASK))
5226 s->flags |= SN_FINST_H;
5227
5228 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005229 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005230
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005231 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005232 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005233 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005234 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005235 goto hdr_response_bad;
5236 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005237
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005238 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005239 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005240 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005241 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005242
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005243 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005244 if (objt_server(s->target)) {
5245 objt_server(s->target)->counters.failed_resp++;
5246 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005247 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005248
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005249 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005250 rep->analysers = 0;
5251 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005252 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005253 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005254 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005255
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005256 if (!(s->flags & SN_ERR_MASK))
5257 s->flags |= SN_ERR_SRVCL;
5258 if (!(s->flags & SN_FINST_MASK))
5259 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005260 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005261 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005262
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005264 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005265 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005266 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005267
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005268 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005269 if (objt_server(s->target)) {
5270 objt_server(s->target)->counters.failed_resp++;
5271 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005272 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005273
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005274 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005275 rep->analysers = 0;
5276 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005277 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005278 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005279 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005280
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005281 if (!(s->flags & SN_ERR_MASK))
5282 s->flags |= SN_ERR_SRVTO;
5283 if (!(s->flags & SN_FINST_MASK))
5284 s->flags |= SN_FINST_H;
5285 return 0;
5286 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005287
Willy Tarreauf003d372012-11-26 13:35:37 +01005288 /* client abort with an abortonclose */
5289 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5290 s->fe->fe_counters.cli_aborts++;
5291 s->be->be_counters.cli_aborts++;
5292 if (objt_server(s->target))
5293 objt_server(s->target)->counters.cli_aborts++;
5294
5295 rep->analysers = 0;
5296 channel_auto_close(rep);
5297
5298 txn->status = 400;
5299 bi_erase(rep);
5300 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5301
5302 if (!(s->flags & SN_ERR_MASK))
5303 s->flags |= SN_ERR_CLICL;
5304 if (!(s->flags & SN_FINST_MASK))
5305 s->flags |= SN_FINST_H;
5306
5307 /* process_session() will take care of the error */
5308 return 0;
5309 }
5310
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005311 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005312 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005313 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005314 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005315
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005316 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005317 if (objt_server(s->target)) {
5318 objt_server(s->target)->counters.failed_resp++;
5319 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005320 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005321
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005322 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005323 rep->analysers = 0;
5324 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005325 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005326 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005327 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005328
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005329 if (!(s->flags & SN_ERR_MASK))
5330 s->flags |= SN_ERR_SRVCL;
5331 if (!(s->flags & SN_FINST_MASK))
5332 s->flags |= SN_FINST_H;
5333 return 0;
5334 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005335
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005336 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005337 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005338 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005339 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005340
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005341 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005343 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005344
5345 if (!(s->flags & SN_ERR_MASK))
5346 s->flags |= SN_ERR_CLICL;
5347 if (!(s->flags & SN_FINST_MASK))
5348 s->flags |= SN_FINST_H;
5349
5350 /* process_session() will take care of the error */
5351 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005352 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005353
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005354 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005355 return 0;
5356 }
5357
5358 /* More interesting part now : we know that we have a complete
5359 * response which at least looks like HTTP. We have an indicator
5360 * of each header's length, so we can parse them quickly.
5361 */
5362
5363 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005364 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005365
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005366 /*
5367 * 1: get the status code
5368 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005369 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005370 if (n < 1 || n > 5)
5371 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005372 /* when the client triggers a 4xx from the server, it's most often due
5373 * to a missing object or permission. These events should be tracked
5374 * because if they happen often, it may indicate a brute force or a
5375 * vulnerability scan.
5376 */
5377 if (n == 4)
5378 session_inc_http_err_ctr(s);
5379
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005380 if (objt_server(s->target))
5381 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005382
Willy Tarreau5b154472009-12-21 20:11:07 +01005383 /* check if the response is HTTP/1.1 or above */
5384 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005385 ((rep->buf->p[5] > '1') ||
5386 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005387 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005388
5389 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005390 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 +01005391
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005392 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005393 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005394
Willy Tarreau9b28e032012-10-12 23:49:43 +02005395 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005396
Willy Tarreau39650402010-03-15 19:44:39 +01005397 /* Adjust server's health based on status code. Note: status codes 501
5398 * and 505 are triggered on demand by client request, so we must not
5399 * count them as server failures.
5400 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005401 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005402 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005403 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005404 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005405 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005406 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005407
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005408 /*
5409 * 2: check for cacheability.
5410 */
5411
5412 switch (txn->status) {
5413 case 200:
5414 case 203:
5415 case 206:
5416 case 300:
5417 case 301:
5418 case 410:
5419 /* RFC2616 @13.4:
5420 * "A response received with a status code of
5421 * 200, 203, 206, 300, 301 or 410 MAY be stored
5422 * by a cache (...) unless a cache-control
5423 * directive prohibits caching."
5424 *
5425 * RFC2616 @9.5: POST method :
5426 * "Responses to this method are not cacheable,
5427 * unless the response includes appropriate
5428 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005429 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005430 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005431 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005432 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5433 break;
5434 default:
5435 break;
5436 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005437
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005438 /*
5439 * 3: we may need to capture headers
5440 */
5441 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005442 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005443 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005444 txn->rsp.cap, s->fe->rsp_cap);
5445
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005446 /* 4: determine the transfer-length.
5447 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5448 * the presence of a message-body in a RESPONSE and its transfer length
5449 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005450 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005451 * All responses to the HEAD request method MUST NOT include a
5452 * message-body, even though the presence of entity-header fields
5453 * might lead one to believe they do. All 1xx (informational), 204
5454 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5455 * message-body. All other responses do include a message-body,
5456 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005457 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005458 * 1. Any response which "MUST NOT" include a message-body (such as the
5459 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5460 * always terminated by the first empty line after the header fields,
5461 * regardless of the entity-header fields present in the message.
5462 *
5463 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5464 * the "chunked" transfer-coding (Section 6.2) is used, the
5465 * transfer-length is defined by the use of this transfer-coding.
5466 * If a Transfer-Encoding header field is present and the "chunked"
5467 * transfer-coding is not present, the transfer-length is defined by
5468 * the sender closing the connection.
5469 *
5470 * 3. If a Content-Length header field is present, its decimal value in
5471 * OCTETs represents both the entity-length and the transfer-length.
5472 * If a message is received with both a Transfer-Encoding header
5473 * field and a Content-Length header field, the latter MUST be ignored.
5474 *
5475 * 4. If the message uses the media type "multipart/byteranges", and
5476 * the transfer-length is not otherwise specified, then this self-
5477 * delimiting media type defines the transfer-length. This media
5478 * type MUST NOT be used unless the sender knows that the recipient
5479 * can parse it; the presence in a request of a Range header with
5480 * multiple byte-range specifiers from a 1.1 client implies that the
5481 * client can parse multipart/byteranges responses.
5482 *
5483 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005484 */
5485
5486 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005487 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005488 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005489 * FIXME: should we parse anyway and return an error on chunked encoding ?
5490 */
5491 if (txn->meth == HTTP_METH_HEAD ||
5492 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005493 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005494 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005495 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005496 goto skip_content_length;
5497 }
5498
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005499 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005500 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005501 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005502 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005503 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005504 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5505 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005506 /* bad transfer-encoding (chunked followed by something else) */
5507 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005508 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005509 break;
5510 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005511 }
5512
5513 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5514 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005515 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005516 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005517 signed long long cl;
5518
Willy Tarreauad14f752011-09-02 20:33:27 +02005519 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005520 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005521 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005522 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005523
Willy Tarreauad14f752011-09-02 20:33:27 +02005524 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005525 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005526 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005527 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005528
Willy Tarreauad14f752011-09-02 20:33:27 +02005529 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005530 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005531 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005532 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005533
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005534 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005535 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005536 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005537 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005538
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005539 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005540 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005541 }
5542
William Lallemand82fe75c2012-10-23 10:25:10 +02005543 if (s->fe->comp || s->be->comp)
5544 select_compression_response_header(s, rep->buf);
5545
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005546 /* FIXME: we should also implement the multipart/byterange method.
5547 * For now on, we resort to close mode in this case (unknown length).
5548 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005549skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005551 /* end of job, return OK */
5552 rep->analysers &= ~an_bit;
5553 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005554 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005555 return 1;
5556}
5557
5558/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005559 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5560 * and updates t->rep->analysers. It might make sense to explode it into several
5561 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005562 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005563int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005564{
5565 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005566 struct http_msg *msg = &txn->rsp;
5567 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005568 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005569 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005570
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005571 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 +02005572 now_ms, __FUNCTION__,
5573 t,
5574 rep,
5575 rep->rex, rep->wex,
5576 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005577 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005578 rep->analysers);
5579
Willy Tarreau655dce92009-11-08 13:10:58 +01005580 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005581 return 0;
5582
5583 rep->analysers &= ~an_bit;
5584 rep->analyse_exp = TICK_ETERNITY;
5585
Willy Tarreau5b154472009-12-21 20:11:07 +01005586 /* Now we have to check if we need to modify the Connection header.
5587 * This is more difficult on the response than it is on the request,
5588 * because we can have two different HTTP versions and we don't know
5589 * how the client will interprete a response. For instance, let's say
5590 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5591 * HTTP/1.1 response without any header. Maybe it will bound itself to
5592 * HTTP/1.0 because it only knows about it, and will consider the lack
5593 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5594 * the lack of header as a keep-alive. Thus we will use two flags
5595 * indicating how a request MAY be understood by the client. In case
5596 * of multiple possibilities, we'll fix the header to be explicit. If
5597 * ambiguous cases such as both close and keepalive are seen, then we
5598 * will fall back to explicit close. Note that we won't take risks with
5599 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005600 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005601 */
5602
Willy Tarreaudc008c52010-02-01 16:20:08 +01005603 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5604 txn->status == 101)) {
5605 /* Either we've established an explicit tunnel, or we're
5606 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005607 * to understand the next protocols. We have to switch to tunnel
5608 * mode, so that we transfer the request and responses then let
5609 * this protocol pass unmodified. When we later implement specific
5610 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005611 * header which contains information about that protocol for
5612 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005613 */
5614 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5615 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005616 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5617 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5618 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005619 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005620
Willy Tarreau60466522010-01-18 19:08:45 +01005621 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005622 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005623 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5624 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005625
Willy Tarreau60466522010-01-18 19:08:45 +01005626 /* now adjust header transformations depending on current state */
5627 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5628 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5629 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005630 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005631 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005632 }
Willy Tarreau60466522010-01-18 19:08:45 +01005633 else { /* SCL / KAL */
5634 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005635 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005636 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005637 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005638
Willy Tarreau60466522010-01-18 19:08:45 +01005639 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005640 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005641
Willy Tarreau60466522010-01-18 19:08:45 +01005642 /* Some keep-alive responses are converted to Server-close if
5643 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005644 */
Willy Tarreau60466522010-01-18 19:08:45 +01005645 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5646 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005647 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005648 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005649 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005650 }
5651
Willy Tarreau7959a552013-09-23 16:44:27 +02005652 /* we want to have the response time before we start processing it */
5653 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5654
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005655 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005656 /*
5657 * 3: we will have to evaluate the filters.
5658 * As opposed to version 1.2, now they will be evaluated in the
5659 * filters order and not in the header order. This means that
5660 * each filter has to be validated among all headers.
5661 *
5662 * Filters are tried with ->be first, then with ->fe if it is
5663 * different from ->be.
5664 */
5665
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005666 cur_proxy = t->be;
5667 while (1) {
5668 struct proxy *rule_set = cur_proxy;
5669
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005670 /* evaluate http-response rules */
5671 if (!http_res_last_rule)
5672 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5673
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005674 /* try headers filters */
5675 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005676 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005677 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005678 if (objt_server(t->target)) {
5679 objt_server(t->target)->counters.failed_resp++;
5680 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005681 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005682 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005683 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005684 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005685 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005686 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005687 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005688 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005689 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005690 if (!(t->flags & SN_ERR_MASK))
5691 t->flags |= SN_ERR_PRXCOND;
5692 if (!(t->flags & SN_FINST_MASK))
5693 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005694 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005695 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005696 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005697
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005698 /* has the response been denied ? */
5699 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005700 if (objt_server(t->target))
5701 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005702
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005703 t->be->be_counters.denied_resp++;
5704 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005705 if (t->listener->counters)
5706 t->listener->counters->denied_resp++;
5707
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005708 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005709 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005710
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005711 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005712 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005713 if (txn->status < 200)
5714 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005715 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005716 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005717 ret = acl_pass(ret);
5718 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5719 ret = !ret;
5720 if (!ret)
5721 continue;
5722 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005723 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005724 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005725 }
5726
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005727 /* check whether we're already working on the frontend */
5728 if (cur_proxy == t->fe)
5729 break;
5730 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005731 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005732
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005733 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005734 * We may be facing a 100-continue response, in which case this
5735 * is not the right response, and we're waiting for the next one.
5736 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005737 * next one.
5738 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005739 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005740 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005741 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005742 msg->msg_state = HTTP_MSG_RPBEFORE;
5743 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005744 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005745 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5746 return 1;
5747 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005748 else if (unlikely(txn->status < 200))
5749 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005750
5751 /* we don't have any 1xx status code now */
5752
5753 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005754 * 4: check for server cookie.
5755 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005756 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5757 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005758 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005759
Willy Tarreaubaaee002006-06-26 02:48:02 +02005760
Willy Tarreaua15645d2007-03-18 16:22:39 +01005761 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005762 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005763 */
Willy Tarreau67402132012-05-31 20:40:20 +02005764 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005765 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005767 /*
5768 * 6: add server cookie in the response if needed
5769 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005770 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005771 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005772 (!(t->flags & SN_DIRECT) ||
5773 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5774 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5775 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5776 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005777 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005778 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005779 /* the server is known, it's not the one the client requested, or the
5780 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005781 * insert a set-cookie here, except if we want to insert only on POST
5782 * requests and this one isn't. Note that servers which don't have cookies
5783 * (eg: some backup servers) will return a full cookie removal request.
5784 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005785 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005786 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005787 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5788 t->be->cookie_name);
5789 }
5790 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005791 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005792
5793 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5794 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005795 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5796 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5797 trash.len += 5;
5798
Willy Tarreauef4f3912010-10-07 21:00:29 +02005799 if (t->be->cookie_maxlife) {
5800 /* emit first_date, which is either the original one or
5801 * the current date.
5802 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005803 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005804 s30tob64(txn->cookie_first_date ?
5805 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005806 (date.tv_sec+3) >> 2, trash.str + trash.len);
5807 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005808 }
5809 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005810 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005811 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005812
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005813 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005814 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005815
Willy Tarreau4992dd22012-05-31 21:02:17 +02005816 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005817 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005818
5819 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005820 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005821
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005822 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005823 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005824
Willy Tarreauf1348312010-10-07 15:54:11 +02005825 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005826 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005827 /* the server did not change, only the date was updated */
5828 txn->flags |= TX_SCK_UPDATED;
5829 else
5830 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005831
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005832 /* Here, we will tell an eventual cache on the client side that we don't
5833 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5834 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5835 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5836 */
Willy Tarreau67402132012-05-31 20:40:20 +02005837 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005838
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005839 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5840
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005841 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005842 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005843 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005844 }
5845 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005846
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005847 /*
5848 * 7: check if result will be cacheable with a cookie.
5849 * We'll block the response if security checks have caught
5850 * nasty things such as a cacheable cookie.
5851 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005852 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5853 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005854 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005855
5856 /* we're in presence of a cacheable response containing
5857 * a set-cookie header. We'll block it as requested by
5858 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005859 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005860 if (objt_server(t->target))
5861 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005862
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005863 t->be->be_counters.denied_resp++;
5864 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005865 if (t->listener->counters)
5866 t->listener->counters->denied_resp++;
5867
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005868 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005869 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005870 send_log(t->be, LOG_ALERT,
5871 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005872 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005873 goto return_srv_prx_502;
5874 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005875
5876 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005877 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005878 * If an "Upgrade" token is found, the header is left untouched in order
5879 * not to have to deal with some client bugs : some of them fail an upgrade
5880 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005881 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005882 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5883 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5884 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005885 unsigned int want_flags = 0;
5886
5887 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5888 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5889 /* we want a keep-alive response here. Keep-alive header
5890 * required if either side is not 1.1.
5891 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005892 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005893 want_flags |= TX_CON_KAL_SET;
5894 }
5895 else {
5896 /* we want a close response here. Close header required if
5897 * the server is 1.1, regardless of the client.
5898 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005899 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005900 want_flags |= TX_CON_CLO_SET;
5901 }
5902
5903 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005904 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005905 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005907 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005908 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005909 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005910 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005911
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005912 /*************************************************************
5913 * OK, that's finished for the headers. We have done what we *
5914 * could. Let's switch to the DATA state. *
5915 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005916
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005917 /* if the user wants to log as soon as possible, without counting
5918 * bytes from the server, then this is the right moment. We have
5919 * to temporarily assign bytes_out to log what we currently have.
5920 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005921 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005922 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5923 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005924 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005925 t->logs.bytes_out = 0;
5926 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005927
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005928 /* Note: we must not try to cheat by jumping directly to DATA,
5929 * otherwise we would not let the client side wake up.
5930 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005931
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005932 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005933 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005934 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005936
Willy Tarreaud98cf932009-12-27 22:54:55 +01005937/* This function is an analyser which forwards response body (including chunk
5938 * sizes if any). It is called as soon as we must forward, even if we forward
5939 * zero byte. The only situation where it must not be called is when we're in
5940 * tunnel mode and we want to forward till the close. It's used both to forward
5941 * remaining data and to resync after end of body. It expects the msg_state to
5942 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5943 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005944 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005945 * bytes of pending data + the headers if not already done (between sol and sov).
5946 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005947 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005948int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005949{
5950 struct http_txn *txn = &s->txn;
5951 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005952 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005953 static struct buffer *tmpbuf = NULL;
5954 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005955 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005956 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005957
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005958 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5959 return 0;
5960
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005961 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005962 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005963 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005964 /* Output closed while we were sending data. We must abort and
5965 * wake the other side up.
5966 */
5967 msg->msg_state = HTTP_MSG_ERROR;
5968 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005969 return 1;
5970 }
5971
Willy Tarreau4fe41902010-06-07 22:27:41 +02005972 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005973 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005974
William Lallemand82fe75c2012-10-23 10:25:10 +02005975 /* this is the first time we need the compression buffer */
5976 if (s->comp_algo != NULL && tmpbuf == NULL) {
5977 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5978 goto aborted_xfer; /* no memory */
5979 }
5980
Willy Tarreaud98cf932009-12-27 22:54:55 +01005981 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005982 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005983 * rep->buf.p still points to the beginning of the message and msg->sol
5984 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005985 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005986 channel_forward(res, msg->sov);
5987 msg->next = 0;
5988 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005989
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005990 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005991 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005992 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005993 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005994 }
5995
William Lallemand82fe75c2012-10-23 10:25:10 +02005996 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005997 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005998 if (ret < 0)
5999 goto missing_data; /* not enough spaces in buffers */
6000 compressing = 1;
6001 }
6002
Willy Tarreaud98cf932009-12-27 22:54:55 +01006003 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006004 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006005 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006006 if (s->comp_algo == NULL) {
6007 bytes = msg->sov - msg->sol;
6008 if (msg->chunk_len || bytes) {
6009 msg->sol = msg->sov;
6010 msg->next -= bytes; /* will be forwarded */
6011 msg->chunk_len += bytes;
6012 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6013 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006014 }
6015
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006016 switch (msg->msg_state - HTTP_MSG_DATA) {
6017 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006018 if (compressing) {
6019 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6020 if (ret < 0)
6021 goto aborted_xfer;
6022 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006023
6024 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006025 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01006026
6027 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006028 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006029 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006030 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006031 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006032 if (compressing && consumed_data) {
6033 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6034 compressing = 0;
6035 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006036 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006037 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006038 /* fall through for HTTP_MSG_CHUNK_CRLF */
6039
6040 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6041 /* we want the CRLF after the data */
6042
6043 ret = http_skip_chunk_crlf(msg);
6044 if (ret == 0)
6045 goto missing_data;
6046 else if (ret < 0) {
6047 if (msg->err_pos >= 0)
6048 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6049 goto return_bad_res;
6050 }
6051 /* skipping data in buffer for compression */
6052 if (compressing) {
6053 b_adv(res->buf, msg->next);
6054 msg->next = 0;
6055 msg->sov = 0;
6056 msg->sol = 0;
6057 }
6058 /* we're in MSG_CHUNK_SIZE now, fall through */
6059
6060 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006061 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006062 * set ->sov and ->next to point to the body and switch to DATA or
6063 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006064 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006065
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006066 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006067 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006068 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006069 else if (ret < 0) {
6070 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006071 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006072 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006073 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006074 if (compressing) {
6075 if (likely(msg->chunk_len > 0)) {
6076 /* skipping data if we are in compression mode */
6077 b_adv(res->buf, msg->next);
6078 msg->next = 0;
6079 msg->sov = 0;
6080 msg->sol = 0;
6081 } else {
6082 if (consumed_data) {
6083 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6084 compressing = 0;
6085 }
6086 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006087 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006088 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006089 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006090
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006091 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6092 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006093 if (ret == 0)
6094 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006095 else if (ret < 0) {
6096 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006097 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006098 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006099 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006100 if (s->comp_algo != NULL) {
6101 /* forwarding trailers */
6102 channel_forward(res, msg->next);
6103 msg->next = 0;
6104 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006105 /* we're in HTTP_MSG_DONE now, but we might still have
6106 * some data pending, so let's loop over once.
6107 */
6108 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006109
6110 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006111 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006112
6113 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006114 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006115 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6116 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006117 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006118 if (http_resync_states(s)) {
6119 http_silent_debug(__LINE__, s);
6120 /* some state changes occurred, maybe the analyser
6121 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006122 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006123 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006124 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006125 /* response errors are most likely due to
6126 * the client aborting the transfer.
6127 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006128 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006129 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006130 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006131 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006132 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006133 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006134 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006135 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006136 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006137 }
6138 }
6139
Willy Tarreaud98cf932009-12-27 22:54:55 +01006140 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006141 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006142 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6143 compressing = 0;
6144 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006145
6146 if (res->flags & CF_SHUTW)
6147 goto aborted_xfer;
6148
6149 /* stop waiting for data if the input is closed before the end. If the
6150 * client side was already closed, it means that the client has aborted,
6151 * so we don't want to count this as a server abort. Otherwise it's a
6152 * server abort.
6153 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006154 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006155 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6156 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006157 if (!(s->flags & SN_ERR_MASK))
6158 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006159 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006160 if (objt_server(s->target))
6161 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006162 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006163 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006164
Willy Tarreau40dba092010-03-04 18:14:51 +01006165 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006166 if (!s->req->analysers)
6167 goto return_bad_res;
6168
Willy Tarreauea953162012-05-18 23:41:28 +02006169 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006170 if (s->comp_algo == NULL) {
6171 bytes = msg->sov - msg->sol;
6172 if (msg->chunk_len || bytes) {
6173 msg->sol = msg->sov;
6174 msg->next -= bytes; /* will be forwarded */
6175 msg->chunk_len += bytes;
6176 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6177 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006178 }
6179
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006180 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006181 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006182 * Similarly, with keep-alive on the client side, we don't want to forward a
6183 * close.
6184 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006185 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006186 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6187 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006188 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006189
Willy Tarreau5c620922011-05-11 19:56:11 +02006190 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006191 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006192 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006193 * modes are already handled by the stream sock layer. We must not do
6194 * this in content-length mode because it could present the MSG_MORE
6195 * flag with the last block of forwarded data, which would cause an
6196 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006197 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006198 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006199 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006200
Willy Tarreaud98cf932009-12-27 22:54:55 +01006201 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006202 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006203 return 0;
6204
Willy Tarreau40dba092010-03-04 18:14:51 +01006205 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006206 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006207 if (objt_server(s->target))
6208 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006209
6210 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006211 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006212 /* don't send any error message as we're in the body */
6213 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006214 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006215 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006216 if (objt_server(s->target))
6217 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006218
6219 if (!(s->flags & SN_ERR_MASK))
6220 s->flags |= SN_ERR_PRXCOND;
6221 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006222 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006223 return 0;
6224
6225 aborted_xfer:
6226 txn->rsp.msg_state = HTTP_MSG_ERROR;
6227 /* don't send any error message as we're in the body */
6228 stream_int_retnclose(res->cons, NULL);
6229 res->analysers = 0;
6230 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6231
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006232 s->fe->fe_counters.cli_aborts++;
6233 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006234 if (objt_server(s->target))
6235 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006236
6237 if (!(s->flags & SN_ERR_MASK))
6238 s->flags |= SN_ERR_CLICL;
6239 if (!(s->flags & SN_FINST_MASK))
6240 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006241 return 0;
6242}
6243
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006244/* Iterate the same filter through all request headers.
6245 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006246 * Since it can manage the switch to another backend, it updates the per-proxy
6247 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006248 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006249int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006251 char term;
6252 char *cur_ptr, *cur_end, *cur_next;
6253 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006254 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006255 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006256 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006258 last_hdr = 0;
6259
Willy Tarreau9b28e032012-10-12 23:49:43 +02006260 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006261 old_idx = 0;
6262
6263 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006264 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006265 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006266 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006267 (exp->action == ACT_ALLOW ||
6268 exp->action == ACT_DENY ||
6269 exp->action == ACT_TARPIT))
6270 return 0;
6271
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006272 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006273 if (!cur_idx)
6274 break;
6275
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006276 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006277 cur_ptr = cur_next;
6278 cur_end = cur_ptr + cur_hdr->len;
6279 cur_next = cur_end + cur_hdr->cr + 1;
6280
6281 /* Now we have one header between cur_ptr and cur_end,
6282 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283 */
6284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006285 /* The annoying part is that pattern matching needs
6286 * that we modify the contents to null-terminate all
6287 * strings before testing them.
6288 */
6289
6290 term = *cur_end;
6291 *cur_end = '\0';
6292
6293 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6294 switch (exp->action) {
6295 case ACT_SETBE:
6296 /* It is not possible to jump a second time.
6297 * FIXME: should we return an HTTP/500 here so that
6298 * the admin knows there's a problem ?
6299 */
6300 if (t->be != t->fe)
6301 break;
6302
6303 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006304 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006305 last_hdr = 1;
6306 break;
6307
6308 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006309 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006310 last_hdr = 1;
6311 break;
6312
6313 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006314 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006315 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006316 break;
6317
6318 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006319 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006320 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006321 break;
6322
6323 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006324 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6325 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006326 /* FIXME: if the user adds a newline in the replacement, the
6327 * index will not be recalculated for now, and the new line
6328 * will not be counted as a new header.
6329 */
6330
6331 cur_end += delta;
6332 cur_next += delta;
6333 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006334 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006335 break;
6336
6337 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006338 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006339 cur_next += delta;
6340
Willy Tarreaufa355d42009-11-29 18:12:29 +01006341 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006342 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6343 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006344 cur_hdr->len = 0;
6345 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006346 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 break;
6348
6349 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006351 if (cur_end)
6352 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006354 /* keep the link from this header to next one in case of later
6355 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006356 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 old_idx = cur_idx;
6358 }
6359 return 0;
6360}
6361
6362
6363/* Apply the filter to the request line.
6364 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6365 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366 * Since it can manage the switch to another backend, it updates the per-proxy
6367 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006368 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006369int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370{
6371 char term;
6372 char *cur_ptr, *cur_end;
6373 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006374 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006375 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376
Willy Tarreau3d300592007-03-18 18:34:41 +01006377 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006378 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006379 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380 (exp->action == ACT_ALLOW ||
6381 exp->action == ACT_DENY ||
6382 exp->action == ACT_TARPIT))
6383 return 0;
6384 else if (exp->action == ACT_REMOVE)
6385 return 0;
6386
6387 done = 0;
6388
Willy Tarreau9b28e032012-10-12 23:49:43 +02006389 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006390 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006391
6392 /* Now we have the request line between cur_ptr and cur_end */
6393
6394 /* The annoying part is that pattern matching needs
6395 * that we modify the contents to null-terminate all
6396 * strings before testing them.
6397 */
6398
6399 term = *cur_end;
6400 *cur_end = '\0';
6401
6402 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6403 switch (exp->action) {
6404 case ACT_SETBE:
6405 /* It is not possible to jump a second time.
6406 * FIXME: should we return an HTTP/500 here so that
6407 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006409 if (t->be != t->fe)
6410 break;
6411
6412 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006413 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006414 done = 1;
6415 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006417 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006418 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006419 done = 1;
6420 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006422 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006423 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424 done = 1;
6425 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006427 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006428 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 done = 1;
6430 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 case ACT_REPLACE:
6433 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006434 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6435 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006436 /* FIXME: if the user adds a newline in the replacement, the
6437 * index will not be recalculated for now, and the new line
6438 * will not be counted as a new header.
6439 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006440
Willy Tarreaufa355d42009-11-29 18:12:29 +01006441 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006442 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006443 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006444 HTTP_MSG_RQMETH,
6445 cur_ptr, cur_end + 1,
6446 NULL, NULL);
6447 if (unlikely(!cur_end))
6448 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006450 /* we have a full request and we know that we have either a CR
6451 * or an LF at <ptr>.
6452 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006453 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6454 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006455 /* there is no point trying this regex on headers */
6456 return 1;
6457 }
6458 }
6459 *cur_end = term; /* restore the string terminator */
6460 return done;
6461}
Willy Tarreau97de6242006-12-27 17:18:38 +01006462
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463
Willy Tarreau58f10d72006-12-04 02:26:12 +01006464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006466 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468 * unparsable request. Since it can manage the switch to another backend, it
6469 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006470 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006471int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006473 struct http_txn *txn = &s->txn;
6474 struct hdr_exp *exp;
6475
6476 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006479 /*
6480 * The interleaving of transformations and verdicts
6481 * makes it difficult to decide to continue or stop
6482 * the evaluation.
6483 */
6484
Willy Tarreau6c123b12010-01-28 20:22:06 +01006485 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6486 break;
6487
Willy Tarreau3d300592007-03-18 18:34:41 +01006488 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006489 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006490 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006492
6493 /* if this filter had a condition, evaluate it now and skip to
6494 * next filter if the condition does not match.
6495 */
6496 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006497 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006498 ret = acl_pass(ret);
6499 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6500 ret = !ret;
6501
6502 if (!ret)
6503 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006504 }
6505
6506 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006507 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006508 if (unlikely(ret < 0))
6509 return -1;
6510
6511 if (likely(ret == 0)) {
6512 /* The filter did not match the request, it can be
6513 * iterated through all headers.
6514 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006515 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006516 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006517 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006518 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006519}
6520
6521
Willy Tarreaua15645d2007-03-18 16:22:39 +01006522
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006524 * Try to retrieve the server associated to the appsession.
6525 * If the server is found, it's assigned to the session.
6526 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006527void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006528 struct http_txn *txn = &t->txn;
6529 appsess *asession = NULL;
6530 char *sessid_temp = NULL;
6531
Cyril Bontéb21570a2009-11-29 20:04:48 +01006532 if (len > t->be->appsession_len) {
6533 len = t->be->appsession_len;
6534 }
6535
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006536 if (t->be->options2 & PR_O2_AS_REQL) {
6537 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006538 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006539 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006540 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006541 }
6542
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006543 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006544 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6545 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6546 return;
6547 }
6548
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006549 memcpy(txn->sessid, buf, len);
6550 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006551 }
6552
6553 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6554 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6555 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6556 return;
6557 }
6558
Cyril Bontéb21570a2009-11-29 20:04:48 +01006559 memcpy(sessid_temp, buf, len);
6560 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006561
6562 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6563 /* free previously allocated memory */
6564 pool_free2(apools.sessid, sessid_temp);
6565
6566 if (asession != NULL) {
6567 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6568 if (!(t->be->options2 & PR_O2_AS_REQL))
6569 asession->request_count++;
6570
6571 if (asession->serverid != NULL) {
6572 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006573
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006574 while (srv) {
6575 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006576 if ((srv->state & SRV_RUNNING) ||
6577 (t->be->options & PR_O_PERSIST) ||
6578 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006579 /* we found the server and it's usable */
6580 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006581 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006582 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006583 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006584
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006585 break;
6586 } else {
6587 txn->flags &= ~TX_CK_MASK;
6588 txn->flags |= TX_CK_DOWN;
6589 }
6590 }
6591 srv = srv->next;
6592 }
6593 }
6594 }
6595}
6596
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597/* Find the end of a cookie value contained between <s> and <e>. It works the
6598 * same way as with headers above except that the semi-colon also ends a token.
6599 * See RFC2965 for more information. Note that it requires a valid header to
6600 * return a valid result.
6601 */
6602char *find_cookie_value_end(char *s, const char *e)
6603{
6604 int quoted, qdpair;
6605
6606 quoted = qdpair = 0;
6607 for (; s < e; s++) {
6608 if (qdpair) qdpair = 0;
6609 else if (quoted) {
6610 if (*s == '\\') qdpair = 1;
6611 else if (*s == '"') quoted = 0;
6612 }
6613 else if (*s == '"') quoted = 1;
6614 else if (*s == ',' || *s == ';') return s;
6615 }
6616 return s;
6617}
6618
6619/* Delete a value in a header between delimiters <from> and <next> in buffer
6620 * <buf>. The number of characters displaced is returned, and the pointer to
6621 * the first delimiter is updated if required. The function tries as much as
6622 * possible to respect the following principles :
6623 * - replace <from> delimiter by the <next> one unless <from> points to a
6624 * colon, in which case <next> is simply removed
6625 * - set exactly one space character after the new first delimiter, unless
6626 * there are not enough characters in the block being moved to do so.
6627 * - remove unneeded spaces before the previous delimiter and after the new
6628 * one.
6629 *
6630 * It is the caller's responsibility to ensure that :
6631 * - <from> points to a valid delimiter or the colon ;
6632 * - <next> points to a valid delimiter or the final CR/LF ;
6633 * - there are non-space chars before <from> ;
6634 * - there is a CR/LF at or after <next>.
6635 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006636int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006637{
6638 char *prev = *from;
6639
6640 if (*prev == ':') {
6641 /* We're removing the first value, preserve the colon and add a
6642 * space if possible.
6643 */
6644 if (!http_is_crlf[(unsigned char)*next])
6645 next++;
6646 prev++;
6647 if (prev < next)
6648 *prev++ = ' ';
6649
6650 while (http_is_spht[(unsigned char)*next])
6651 next++;
6652 } else {
6653 /* Remove useless spaces before the old delimiter. */
6654 while (http_is_spht[(unsigned char)*(prev-1)])
6655 prev--;
6656 *from = prev;
6657
6658 /* copy the delimiter and if possible a space if we're
6659 * not at the end of the line.
6660 */
6661 if (!http_is_crlf[(unsigned char)*next]) {
6662 *prev++ = *next++;
6663 if (prev + 1 < next)
6664 *prev++ = ' ';
6665 while (http_is_spht[(unsigned char)*next])
6666 next++;
6667 }
6668 }
6669 return buffer_replace2(buf, prev, next, NULL, 0);
6670}
6671
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006672/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006673 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006674 * desirable to call it only when needed. This code is quite complex because
6675 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6676 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006677 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006678void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006679{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006680 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006681 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006682 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6684 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006686 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006687 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006688 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006689
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006690 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006691 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006692 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006693
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006694 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 hdr_beg = hdr_next;
6696 hdr_end = hdr_beg + cur_hdr->len;
6697 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006699 /* We have one full header between hdr_beg and hdr_end, and the
6700 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006701 * "Cookie:" headers.
6702 */
6703
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006704 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006705 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006706 old_idx = cur_idx;
6707 continue;
6708 }
6709
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006710 del_from = NULL; /* nothing to be deleted */
6711 preserve_hdr = 0; /* assume we may kill the whole header */
6712
Willy Tarreau58f10d72006-12-04 02:26:12 +01006713 /* Now look for cookies. Conforming to RFC2109, we have to support
6714 * attributes whose name begin with a '$', and associate them with
6715 * the right cookie, if we want to delete this cookie.
6716 * So there are 3 cases for each cookie read :
6717 * 1) it's a special attribute, beginning with a '$' : ignore it.
6718 * 2) it's a server id cookie that we *MAY* want to delete : save
6719 * some pointers on it (last semi-colon, beginning of cookie...)
6720 * 3) it's an application cookie : we *MAY* have to delete a previous
6721 * "special" cookie.
6722 * At the end of loop, if a "special" cookie remains, we may have to
6723 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006724 * *MUST* delete it.
6725 *
6726 * Note: RFC2965 is unclear about the processing of spaces around
6727 * the equal sign in the ATTR=VALUE form. A careful inspection of
6728 * the RFC explicitly allows spaces before it, and not within the
6729 * tokens (attrs or values). An inspection of RFC2109 allows that
6730 * too but section 10.1.3 lets one think that spaces may be allowed
6731 * after the equal sign too, resulting in some (rare) buggy
6732 * implementations trying to do that. So let's do what servers do.
6733 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6734 * allowed quoted strings in values, with any possible character
6735 * after a backslash, including control chars and delimitors, which
6736 * causes parsing to become ambiguous. Browsers also allow spaces
6737 * within values even without quotes.
6738 *
6739 * We have to keep multiple pointers in order to support cookie
6740 * removal at the beginning, middle or end of header without
6741 * corrupting the header. All of these headers are valid :
6742 *
6743 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6744 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6745 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6746 * | | | | | | | | |
6747 * | | | | | | | | hdr_end <--+
6748 * | | | | | | | +--> next
6749 * | | | | | | +----> val_end
6750 * | | | | | +-----------> val_beg
6751 * | | | | +--------------> equal
6752 * | | | +----------------> att_end
6753 * | | +---------------------> att_beg
6754 * | +--------------------------> prev
6755 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756 */
6757
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006758 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6759 /* Iterate through all cookies on this line */
6760
6761 /* find att_beg */
6762 att_beg = prev + 1;
6763 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6764 att_beg++;
6765
6766 /* find att_end : this is the first character after the last non
6767 * space before the equal. It may be equal to hdr_end.
6768 */
6769 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 while (equal < hdr_end) {
6772 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006773 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006774 if (http_is_spht[(unsigned char)*equal++])
6775 continue;
6776 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006777 }
6778
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006779 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6780 * is between <att_beg> and <equal>, both may be identical.
6781 */
6782
6783 /* look for end of cookie if there is an equal sign */
6784 if (equal < hdr_end && *equal == '=') {
6785 /* look for the beginning of the value */
6786 val_beg = equal + 1;
6787 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6788 val_beg++;
6789
6790 /* find the end of the value, respecting quotes */
6791 next = find_cookie_value_end(val_beg, hdr_end);
6792
6793 /* make val_end point to the first white space or delimitor after the value */
6794 val_end = next;
6795 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6796 val_end--;
6797 } else {
6798 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006799 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006800
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006801 /* We have nothing to do with attributes beginning with '$'. However,
6802 * they will automatically be removed if a header before them is removed,
6803 * since they're supposed to be linked together.
6804 */
6805 if (*att_beg == '$')
6806 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006807
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006808 /* Ignore cookies with no equal sign */
6809 if (equal == next) {
6810 /* This is not our cookie, so we must preserve it. But if we already
6811 * scheduled another cookie for removal, we cannot remove the
6812 * complete header, but we can remove the previous block itself.
6813 */
6814 preserve_hdr = 1;
6815 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006816 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006817 val_end += delta;
6818 next += delta;
6819 hdr_end += delta;
6820 hdr_next += delta;
6821 cur_hdr->len += delta;
6822 http_msg_move_end(&txn->req, delta);
6823 prev = del_from;
6824 del_from = NULL;
6825 }
6826 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006827 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006828
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006829 /* if there are spaces around the equal sign, we need to
6830 * strip them otherwise we'll get trouble for cookie captures,
6831 * or even for rewrites. Since this happens extremely rarely,
6832 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006833 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6835 int stripped_before = 0;
6836 int stripped_after = 0;
6837
6838 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006839 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 equal += stripped_before;
6841 val_beg += stripped_before;
6842 }
6843
6844 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006845 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006846 val_beg += stripped_after;
6847 stripped_before += stripped_after;
6848 }
6849
6850 val_end += stripped_before;
6851 next += stripped_before;
6852 hdr_end += stripped_before;
6853 hdr_next += stripped_before;
6854 cur_hdr->len += stripped_before;
6855 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006856 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006857 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006858
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006859 /* First, let's see if we want to capture this cookie. We check
6860 * that we don't already have a client side cookie, because we
6861 * can only capture one. Also as an optimisation, we ignore
6862 * cookies shorter than the declared name.
6863 */
6864 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6865 (val_end - att_beg >= t->fe->capture_namelen) &&
6866 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6867 int log_len = val_end - att_beg;
6868
6869 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6870 Alert("HTTP logging : out of memory.\n");
6871 } else {
6872 if (log_len > t->fe->capture_len)
6873 log_len = t->fe->capture_len;
6874 memcpy(txn->cli_cookie, att_beg, log_len);
6875 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006876 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006878
Willy Tarreaubca99692010-10-06 19:25:55 +02006879 /* Persistence cookies in passive, rewrite or insert mode have the
6880 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006881 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006882 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006884 * For cookies in prefix mode, the form is :
6885 *
6886 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006887 */
6888 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6889 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6890 struct server *srv = t->be->srv;
6891 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006892
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006893 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6894 * have the server ID between val_beg and delim, and the original cookie between
6895 * delim+1 and val_end. Otherwise, delim==val_end :
6896 *
6897 * Cookie: NAME=SRV; # in all but prefix modes
6898 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6899 * | || || | |+-> next
6900 * | || || | +--> val_end
6901 * | || || +---------> delim
6902 * | || |+------------> val_beg
6903 * | || +-------------> att_end = equal
6904 * | |+-----------------> att_beg
6905 * | +------------------> prev
6906 * +-------------------------> hdr_beg
6907 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908
Willy Tarreau67402132012-05-31 20:40:20 +02006909 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006910 for (delim = val_beg; delim < val_end; delim++)
6911 if (*delim == COOKIE_DELIM)
6912 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006913 } else {
6914 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006916 /* Now check if the cookie contains a date field, which would
6917 * appear after a vertical bar ('|') just after the server name
6918 * and before the delimiter.
6919 */
6920 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6921 if (vbar1) {
6922 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006923 * right is the last seen date. It is a base64 encoded
6924 * 30-bit value representing the UNIX date since the
6925 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006926 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006927 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006928 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006929 if (val_end - vbar1 >= 5) {
6930 val = b64tos30(vbar1);
6931 if (val > 0)
6932 txn->cookie_last_date = val << 2;
6933 }
6934 /* look for a second vertical bar */
6935 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6936 if (vbar1 && (val_end - vbar1 > 5)) {
6937 val = b64tos30(vbar1 + 1);
6938 if (val > 0)
6939 txn->cookie_first_date = val << 2;
6940 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006941 }
6942 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943
Willy Tarreauf64d1412010-10-07 20:06:11 +02006944 /* if the cookie has an expiration date and the proxy wants to check
6945 * it, then we do that now. We first check if the cookie is too old,
6946 * then only if it has expired. We detect strict overflow because the
6947 * time resolution here is not great (4 seconds). Cookies with dates
6948 * in the future are ignored if their offset is beyond one day. This
6949 * allows an admin to fix timezone issues without expiring everyone
6950 * and at the same time avoids keeping unwanted side effects for too
6951 * long.
6952 */
6953 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006954 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6955 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006956 txn->flags &= ~TX_CK_MASK;
6957 txn->flags |= TX_CK_OLD;
6958 delim = val_beg; // let's pretend we have not found the cookie
6959 txn->cookie_first_date = 0;
6960 txn->cookie_last_date = 0;
6961 }
6962 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006963 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6964 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006965 txn->flags &= ~TX_CK_MASK;
6966 txn->flags |= TX_CK_EXPIRED;
6967 delim = val_beg; // let's pretend we have not found the cookie
6968 txn->cookie_first_date = 0;
6969 txn->cookie_last_date = 0;
6970 }
6971
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006972 /* Here, we'll look for the first running server which supports the cookie.
6973 * This allows to share a same cookie between several servers, for example
6974 * to dedicate backup servers to specific servers only.
6975 * However, to prevent clients from sticking to cookie-less backup server
6976 * when they have incidentely learned an empty cookie, we simply ignore
6977 * empty cookies and mark them as invalid.
6978 * The same behaviour is applied when persistence must be ignored.
6979 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006980 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 while (srv) {
6984 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6985 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6986 if ((srv->state & SRV_RUNNING) ||
6987 (t->be->options & PR_O_PERSIST) ||
6988 (t->flags & SN_FORCE_PRST)) {
6989 /* we found the server and we can use it */
6990 txn->flags &= ~TX_CK_MASK;
6991 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6992 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006993 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 break;
6995 } else {
6996 /* we found a server, but it's down,
6997 * mark it as such and go on in case
6998 * another one is available.
6999 */
7000 txn->flags &= ~TX_CK_MASK;
7001 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007002 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007003 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007004 srv = srv->next;
7005 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007006
Willy Tarreauf64d1412010-10-07 20:06:11 +02007007 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007008 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007009 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007010 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7011 txn->flags |= TX_CK_UNUSED;
7012 else
7013 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007014 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007016 /* depending on the cookie mode, we may have to either :
7017 * - delete the complete cookie if we're in insert+indirect mode, so that
7018 * the server never sees it ;
7019 * - remove the server id from the cookie value, and tag the cookie as an
7020 * application cookie so that it does not get accidentely removed later,
7021 * if we're in cookie prefix mode
7022 */
Willy Tarreau67402132012-05-31 20:40:20 +02007023 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007024 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007025
Willy Tarreau9b28e032012-10-12 23:49:43 +02007026 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 val_end += delta;
7028 next += delta;
7029 hdr_end += delta;
7030 hdr_next += delta;
7031 cur_hdr->len += delta;
7032 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007033
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007034 del_from = NULL;
7035 preserve_hdr = 1; /* we want to keep this cookie */
7036 }
7037 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007038 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007039 del_from = prev;
7040 }
7041 } else {
7042 /* This is not our cookie, so we must preserve it. But if we already
7043 * scheduled another cookie for removal, we cannot remove the
7044 * complete header, but we can remove the previous block itself.
7045 */
7046 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007049 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007050 if (att_beg >= del_from)
7051 att_beg += delta;
7052 if (att_end >= del_from)
7053 att_end += delta;
7054 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 val_end += delta;
7056 next += delta;
7057 hdr_end += delta;
7058 hdr_next += delta;
7059 cur_hdr->len += delta;
7060 http_msg_move_end(&txn->req, delta);
7061 prev = del_from;
7062 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007063 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007064 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007066 /* Look for the appsession cookie unless persistence must be ignored */
7067 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7068 int cmp_len, value_len;
7069 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007070
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 if (t->be->options2 & PR_O2_AS_PFX) {
7072 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7073 value_begin = att_beg + t->be->appsession_name_len;
7074 value_len = val_end - att_beg - t->be->appsession_name_len;
7075 } else {
7076 cmp_len = att_end - att_beg;
7077 value_begin = val_beg;
7078 value_len = val_end - val_beg;
7079 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007080
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007081 /* let's see if the cookie is our appcookie */
7082 if (cmp_len == t->be->appsession_name_len &&
7083 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7084 manage_client_side_appsession(t, value_begin, value_len);
7085 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086 }
7087
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 /* continue with next cookie on this header line */
7089 att_beg = next;
7090 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 /* There are no more cookies on this line.
7093 * We may still have one (or several) marked for deletion at the
7094 * end of the line. We must do this now in two ways :
7095 * - if some cookies must be preserved, we only delete from the
7096 * mark to the end of line ;
7097 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007099 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007102 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104 cur_hdr->len += delta;
7105 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007106 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107
7108 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007109 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7110 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007111 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007112 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007114 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007115 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116 }
7117
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007118 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121}
7122
7123
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124/* Iterate the same filter through all response headers contained in <rtr>.
7125 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7126 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007127int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128{
7129 char term;
7130 char *cur_ptr, *cur_end, *cur_next;
7131 int cur_idx, old_idx, last_hdr;
7132 struct http_txn *txn = &t->txn;
7133 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007134 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007135
7136 last_hdr = 0;
7137
Willy Tarreau9b28e032012-10-12 23:49:43 +02007138 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007139 old_idx = 0;
7140
7141 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007142 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007144 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145 (exp->action == ACT_ALLOW ||
7146 exp->action == ACT_DENY))
7147 return 0;
7148
7149 cur_idx = txn->hdr_idx.v[old_idx].next;
7150 if (!cur_idx)
7151 break;
7152
7153 cur_hdr = &txn->hdr_idx.v[cur_idx];
7154 cur_ptr = cur_next;
7155 cur_end = cur_ptr + cur_hdr->len;
7156 cur_next = cur_end + cur_hdr->cr + 1;
7157
7158 /* Now we have one header between cur_ptr and cur_end,
7159 * and the next header starts at cur_next.
7160 */
7161
7162 /* The annoying part is that pattern matching needs
7163 * that we modify the contents to null-terminate all
7164 * strings before testing them.
7165 */
7166
7167 term = *cur_end;
7168 *cur_end = '\0';
7169
7170 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7171 switch (exp->action) {
7172 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007173 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 last_hdr = 1;
7175 break;
7176
7177 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007178 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 last_hdr = 1;
7180 break;
7181
7182 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007183 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7184 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 /* FIXME: if the user adds a newline in the replacement, the
7186 * index will not be recalculated for now, and the new line
7187 * will not be counted as a new header.
7188 */
7189
7190 cur_end += delta;
7191 cur_next += delta;
7192 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007193 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 break;
7195
7196 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007197 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198 cur_next += delta;
7199
Willy Tarreaufa355d42009-11-29 18:12:29 +01007200 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007201 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7202 txn->hdr_idx.used--;
7203 cur_hdr->len = 0;
7204 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007205 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 break;
7207
7208 }
7209 }
7210 if (cur_end)
7211 *cur_end = term; /* restore the string terminator */
7212
7213 /* keep the link from this header to next one in case of later
7214 * removal of next header.
7215 */
7216 old_idx = cur_idx;
7217 }
7218 return 0;
7219}
7220
7221
7222/* Apply the filter to the status line in the response buffer <rtr>.
7223 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7224 * or -1 if a replacement resulted in an invalid status line.
7225 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007226int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227{
7228 char term;
7229 char *cur_ptr, *cur_end;
7230 int done;
7231 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007232 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233
7234
Willy Tarreau3d300592007-03-18 18:34:41 +01007235 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007237 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 (exp->action == ACT_ALLOW ||
7239 exp->action == ACT_DENY))
7240 return 0;
7241 else if (exp->action == ACT_REMOVE)
7242 return 0;
7243
7244 done = 0;
7245
Willy Tarreau9b28e032012-10-12 23:49:43 +02007246 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007247 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248
7249 /* Now we have the status line between cur_ptr and cur_end */
7250
7251 /* The annoying part is that pattern matching needs
7252 * that we modify the contents to null-terminate all
7253 * strings before testing them.
7254 */
7255
7256 term = *cur_end;
7257 *cur_end = '\0';
7258
7259 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7260 switch (exp->action) {
7261 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007262 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 done = 1;
7264 break;
7265
7266 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007267 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 done = 1;
7269 break;
7270
7271 case ACT_REPLACE:
7272 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007273 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7274 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 /* FIXME: if the user adds a newline in the replacement, the
7276 * index will not be recalculated for now, and the new line
7277 * will not be counted as a new header.
7278 */
7279
Willy Tarreaufa355d42009-11-29 18:12:29 +01007280 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007282 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007283 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 cur_ptr, cur_end + 1,
7285 NULL, NULL);
7286 if (unlikely(!cur_end))
7287 return -1;
7288
7289 /* we have a full respnse and we know that we have either a CR
7290 * or an LF at <ptr>.
7291 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007292 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007293 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 /* there is no point trying this regex on headers */
7295 return 1;
7296 }
7297 }
7298 *cur_end = term; /* restore the string terminator */
7299 return done;
7300}
7301
7302
7303
7304/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007305 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7307 * unparsable response.
7308 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007309int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007311 struct http_txn *txn = &s->txn;
7312 struct hdr_exp *exp;
7313
7314 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315 int ret;
7316
7317 /*
7318 * The interleaving of transformations and verdicts
7319 * makes it difficult to decide to continue or stop
7320 * the evaluation.
7321 */
7322
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007323 if (txn->flags & TX_SVDENY)
7324 break;
7325
Willy Tarreau3d300592007-03-18 18:34:41 +01007326 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7328 exp->action == ACT_PASS)) {
7329 exp = exp->next;
7330 continue;
7331 }
7332
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007333 /* if this filter had a condition, evaluate it now and skip to
7334 * next filter if the condition does not match.
7335 */
7336 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007337 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007338 ret = acl_pass(ret);
7339 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7340 ret = !ret;
7341 if (!ret)
7342 continue;
7343 }
7344
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007346 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347 if (unlikely(ret < 0))
7348 return -1;
7349
7350 if (likely(ret == 0)) {
7351 /* The filter did not match the response, it can be
7352 * iterated through all headers.
7353 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007354 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 }
7357 return 0;
7358}
7359
7360
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007362 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007363 * desirable to call it only when needed. This function is also used when we
7364 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007366void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367{
7368 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007369 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007370 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007372 char *hdr_beg, *hdr_end, *hdr_next;
7373 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007374
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 /* Iterate through the headers.
7376 * we start with the start line.
7377 */
7378 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007379 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380
7381 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7382 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007383 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384
7385 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007386 hdr_beg = hdr_next;
7387 hdr_end = hdr_beg + cur_hdr->len;
7388 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389
Willy Tarreau24581ba2010-08-31 22:39:35 +02007390 /* We have one full header between hdr_beg and hdr_end, and the
7391 * next header starts at hdr_next. We're only interested in
7392 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007393 */
7394
Willy Tarreau24581ba2010-08-31 22:39:35 +02007395 is_cookie2 = 0;
7396 prev = hdr_beg + 10;
7397 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007398 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007399 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7400 if (!val) {
7401 old_idx = cur_idx;
7402 continue;
7403 }
7404 is_cookie2 = 1;
7405 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 }
7407
Willy Tarreau24581ba2010-08-31 22:39:35 +02007408 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7409 * <prev> points to the colon.
7410 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007411 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412
Willy Tarreau24581ba2010-08-31 22:39:35 +02007413 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7414 * check-cache is enabled) and we are not interested in checking
7415 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007416 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007417 if (t->be->cookie_name == NULL &&
7418 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007419 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007420 return;
7421
Willy Tarreau24581ba2010-08-31 22:39:35 +02007422 /* OK so now we know we have to process this response cookie.
7423 * The format of the Set-Cookie header is slightly different
7424 * from the format of the Cookie header in that it does not
7425 * support the comma as a cookie delimiter (thus the header
7426 * cannot be folded) because the Expires attribute described in
7427 * the original Netscape's spec may contain an unquoted date
7428 * with a comma inside. We have to live with this because
7429 * many browsers don't support Max-Age and some browsers don't
7430 * support quoted strings. However the Set-Cookie2 header is
7431 * clean.
7432 *
7433 * We have to keep multiple pointers in order to support cookie
7434 * removal at the beginning, middle or end of header without
7435 * corrupting the header (in case of set-cookie2). A special
7436 * pointer, <scav> points to the beginning of the set-cookie-av
7437 * fields after the first semi-colon. The <next> pointer points
7438 * either to the end of line (set-cookie) or next unquoted comma
7439 * (set-cookie2). All of these headers are valid :
7440 *
7441 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7442 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7443 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7444 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7445 * | | | | | | | | | |
7446 * | | | | | | | | +-> next hdr_end <--+
7447 * | | | | | | | +------------> scav
7448 * | | | | | | +--------------> val_end
7449 * | | | | | +--------------------> val_beg
7450 * | | | | +----------------------> equal
7451 * | | | +------------------------> att_end
7452 * | | +----------------------------> att_beg
7453 * | +------------------------------> prev
7454 * +-----------------------------------------> hdr_beg
7455 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456
Willy Tarreau24581ba2010-08-31 22:39:35 +02007457 for (; prev < hdr_end; prev = next) {
7458 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007459
Willy Tarreau24581ba2010-08-31 22:39:35 +02007460 /* find att_beg */
7461 att_beg = prev + 1;
7462 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7463 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007464
Willy Tarreau24581ba2010-08-31 22:39:35 +02007465 /* find att_end : this is the first character after the last non
7466 * space before the equal. It may be equal to hdr_end.
7467 */
7468 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007469
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 while (equal < hdr_end) {
7471 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7472 break;
7473 if (http_is_spht[(unsigned char)*equal++])
7474 continue;
7475 att_end = equal;
7476 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477
Willy Tarreau24581ba2010-08-31 22:39:35 +02007478 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7479 * is between <att_beg> and <equal>, both may be identical.
7480 */
7481
7482 /* look for end of cookie if there is an equal sign */
7483 if (equal < hdr_end && *equal == '=') {
7484 /* look for the beginning of the value */
7485 val_beg = equal + 1;
7486 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7487 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488
Willy Tarreau24581ba2010-08-31 22:39:35 +02007489 /* find the end of the value, respecting quotes */
7490 next = find_cookie_value_end(val_beg, hdr_end);
7491
7492 /* make val_end point to the first white space or delimitor after the value */
7493 val_end = next;
7494 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7495 val_end--;
7496 } else {
7497 /* <equal> points to next comma, semi-colon or EOL */
7498 val_beg = val_end = next = equal;
7499 }
7500
7501 if (next < hdr_end) {
7502 /* Set-Cookie2 supports multiple cookies, and <next> points to
7503 * a colon or semi-colon before the end. So skip all attr-value
7504 * pairs and look for the next comma. For Set-Cookie, since
7505 * commas are permitted in values, skip to the end.
7506 */
7507 if (is_cookie2)
7508 next = find_hdr_value_end(next, hdr_end);
7509 else
7510 next = hdr_end;
7511 }
7512
7513 /* Now everything is as on the diagram above */
7514
7515 /* Ignore cookies with no equal sign */
7516 if (equal == val_end)
7517 continue;
7518
7519 /* If there are spaces around the equal sign, we need to
7520 * strip them otherwise we'll get trouble for cookie captures,
7521 * or even for rewrites. Since this happens extremely rarely,
7522 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007524 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7525 int stripped_before = 0;
7526 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007527
Willy Tarreau24581ba2010-08-31 22:39:35 +02007528 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007529 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007530 equal += stripped_before;
7531 val_beg += stripped_before;
7532 }
7533
7534 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007535 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007536 val_beg += stripped_after;
7537 stripped_before += stripped_after;
7538 }
7539
7540 val_end += stripped_before;
7541 next += stripped_before;
7542 hdr_end += stripped_before;
7543 hdr_next += stripped_before;
7544 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007545 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007546 }
7547
7548 /* First, let's see if we want to capture this cookie. We check
7549 * that we don't already have a server side cookie, because we
7550 * can only capture one. Also as an optimisation, we ignore
7551 * cookies shorter than the declared name.
7552 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007553 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007554 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007555 (val_end - att_beg >= t->fe->capture_namelen) &&
7556 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7557 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007558 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007559 Alert("HTTP logging : out of memory.\n");
7560 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007561 else {
7562 if (log_len > t->fe->capture_len)
7563 log_len = t->fe->capture_len;
7564 memcpy(txn->srv_cookie, att_beg, log_len);
7565 txn->srv_cookie[log_len] = 0;
7566 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007567 }
7568
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007569 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007570 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007571 if (!(t->flags & SN_IGNORE_PRST) &&
7572 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7573 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007574 /* assume passive cookie by default */
7575 txn->flags &= ~TX_SCK_MASK;
7576 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007577
7578 /* If the cookie is in insert mode on a known server, we'll delete
7579 * this occurrence because we'll insert another one later.
7580 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007581 * a direct access.
7582 */
Willy Tarreau67402132012-05-31 20:40:20 +02007583 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007584 /* The "preserve" flag was set, we don't want to touch the
7585 * server's cookie.
7586 */
7587 }
Willy Tarreau67402132012-05-31 20:40:20 +02007588 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7589 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007590 /* this cookie must be deleted */
7591 if (*prev == ':' && next == hdr_end) {
7592 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007593 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007594 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7595 txn->hdr_idx.used--;
7596 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007597 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007598 hdr_next += delta;
7599 http_msg_move_end(&txn->rsp, delta);
7600 /* note: while both invalid now, <next> and <hdr_end>
7601 * are still equal, so the for() will stop as expected.
7602 */
7603 } else {
7604 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007605 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007606 next = prev;
7607 hdr_end += delta;
7608 hdr_next += delta;
7609 cur_hdr->len += delta;
7610 http_msg_move_end(&txn->rsp, delta);
7611 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007612 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007613 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007614 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007615 }
Willy Tarreau67402132012-05-31 20:40:20 +02007616 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007617 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007618 * with this server since we know it.
7619 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007620 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007621 next += delta;
7622 hdr_end += delta;
7623 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007624 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007625 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007626
Willy Tarreauf1348312010-10-07 15:54:11 +02007627 txn->flags &= ~TX_SCK_MASK;
7628 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007630 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007632 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007633 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007634 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007635 next += delta;
7636 hdr_end += delta;
7637 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007639 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640
Willy Tarreau827aee92011-03-10 16:55:02 +01007641 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007642 txn->flags &= ~TX_SCK_MASK;
7643 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007644 }
7645 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007646 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7647 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007648 int cmp_len, value_len;
7649 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650
Cyril Bontéb21570a2009-11-29 20:04:48 +01007651 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007652 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7653 value_begin = att_beg + t->be->appsession_name_len;
7654 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007655 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007656 cmp_len = att_end - att_beg;
7657 value_begin = val_beg;
7658 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007659 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007660
Cyril Bonté17530c32010-04-06 21:11:10 +02007661 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007662 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7663 /* free a possibly previously allocated memory */
7664 pool_free2(apools.sessid, txn->sessid);
7665
Cyril Bontéb21570a2009-11-29 20:04:48 +01007666 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007667 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007668 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7669 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7670 return;
7671 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007672 memcpy(txn->sessid, value_begin, value_len);
7673 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007675 }
7676 /* that's done for this cookie, check the next one on the same
7677 * line when next != hdr_end (only if is_cookie2).
7678 */
7679 }
7680 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007682 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007683
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007684 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007685 appsess *asession = NULL;
7686 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007687 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007688 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007689 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007690 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7691 Alert("Not enough Memory process_srv():asession:calloc().\n");
7692 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7693 return;
7694 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007695 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7696
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007697 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7698 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7699 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007700 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007701 return;
7702 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007703 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007704 asession->sessid[t->be->appsession_len] = 0;
7705
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007706 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007707 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007708 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007709 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007710 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007711 return;
7712 }
7713 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007714 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007715
7716 asession->request_count = 0;
7717 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7718 }
7719
7720 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7721 asession->request_count++;
7722 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723}
7724
7725
Willy Tarreaua15645d2007-03-18 16:22:39 +01007726/*
7727 * Check if response is cacheable or not. Updates t->flags.
7728 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007729void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007730{
7731 struct http_txn *txn = &t->txn;
7732 char *p1, *p2;
7733
7734 char *cur_ptr, *cur_end, *cur_next;
7735 int cur_idx;
7736
Willy Tarreau5df51872007-11-25 16:20:08 +01007737 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007738 return;
7739
7740 /* Iterate through the headers.
7741 * we start with the start line.
7742 */
7743 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007744 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007745
7746 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7747 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007748 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007749
7750 cur_hdr = &txn->hdr_idx.v[cur_idx];
7751 cur_ptr = cur_next;
7752 cur_end = cur_ptr + cur_hdr->len;
7753 cur_next = cur_end + cur_hdr->cr + 1;
7754
7755 /* We have one full header between cur_ptr and cur_end, and the
7756 * next header starts at cur_next. We're only interested in
7757 * "Cookie:" headers.
7758 */
7759
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007760 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7761 if (val) {
7762 if ((cur_end - (cur_ptr + val) >= 8) &&
7763 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7764 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7765 return;
7766 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007767 }
7768
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007769 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7770 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007771 continue;
7772
7773 /* OK, right now we know we have a cache-control header at cur_ptr */
7774
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007775 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776
7777 if (p1 >= cur_end) /* no more info */
7778 continue;
7779
7780 /* p1 is at the beginning of the value */
7781 p2 = p1;
7782
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007783 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784 p2++;
7785
7786 /* we have a complete value between p1 and p2 */
7787 if (p2 < cur_end && *p2 == '=') {
7788 /* we have something of the form no-cache="set-cookie" */
7789 if ((cur_end - p1 >= 21) &&
7790 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7791 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007792 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793 continue;
7794 }
7795
7796 /* OK, so we know that either p2 points to the end of string or to a comma */
7797 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007798 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7800 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7801 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007802 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007803 return;
7804 }
7805
7806 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007807 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007808 continue;
7809 }
7810 }
7811}
7812
7813
Willy Tarreau58f10d72006-12-04 02:26:12 +01007814/*
7815 * Try to retrieve a known appsession in the URI, then the associated server.
7816 * If the server is found, it's assigned to the session.
7817 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007818void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007819{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007820 char *end_params, *first_param, *cur_param, *next_param;
7821 char separator;
7822 int value_len;
7823
7824 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007825
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007826 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007827 (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 +01007828 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007829 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007830
Cyril Bontéb21570a2009-11-29 20:04:48 +01007831 first_param = NULL;
7832 switch (mode) {
7833 case PR_O2_AS_M_PP:
7834 first_param = memchr(begin, ';', len);
7835 break;
7836 case PR_O2_AS_M_QS:
7837 first_param = memchr(begin, '?', len);
7838 break;
7839 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007840
Cyril Bontéb21570a2009-11-29 20:04:48 +01007841 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007842 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007843 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007844
Cyril Bontéb21570a2009-11-29 20:04:48 +01007845 switch (mode) {
7846 case PR_O2_AS_M_PP:
7847 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7848 end_params = (char *) begin + len;
7849 }
7850 separator = ';';
7851 break;
7852 case PR_O2_AS_M_QS:
7853 end_params = (char *) begin + len;
7854 separator = '&';
7855 break;
7856 default:
7857 /* unknown mode, shouldn't happen */
7858 return;
7859 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007860
Cyril Bontéb21570a2009-11-29 20:04:48 +01007861 cur_param = next_param = end_params;
7862 while (cur_param > first_param) {
7863 cur_param--;
7864 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7865 /* let's see if this is the appsession parameter */
7866 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7867 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7868 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7869 /* Cool... it's the right one */
7870 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7871 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7872 if (value_len > 0) {
7873 manage_client_side_appsession(t, cur_param, value_len);
7874 }
7875 break;
7876 }
7877 next_param = cur_param;
7878 }
7879 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007880#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007881 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007882 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007883#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007884}
7885
Willy Tarreaub2513902006-12-17 14:52:38 +01007886/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007887 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007888 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007889 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007890 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007891 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007892 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007893 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007894 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007895int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007896{
7897 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007898 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007899 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007900 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007901
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007902 if (!uri_auth)
7903 return 0;
7904
Cyril Bonté70be45d2010-10-12 00:14:35 +02007905 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007906 return 0;
7907
Willy Tarreau295a8372011-03-10 11:25:07 +01007908 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007909 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007910 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007912 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007913 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007914 return 0;
7915
Willy Tarreau3a215be2012-03-09 21:39:51 +01007916 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007917 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007918 return 0;
7919
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007920 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007921 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007922 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007923 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007924 break;
7925 }
7926 h++;
7927 }
7928
7929 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007930 h = uri + uri_auth->uri_len;
7931 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007932 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007933 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007934 break;
7935 }
7936 h++;
7937 }
7938 }
7939
Willy Tarreau3a215be2012-03-09 21:39:51 +01007940 h = uri + uri_auth->uri_len;
7941 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007942 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007943 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007944 break;
7945 }
7946 h++;
7947 }
7948
Willy Tarreau3a215be2012-03-09 21:39:51 +01007949 h = uri + uri_auth->uri_len;
7950 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007951 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007952 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007953 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007954 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007955 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7956 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7957 si->applet.ctx.stats.st_code = i;
7958 break;
7959 }
7960 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007961 break;
7962 }
7963 h++;
7964 }
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02007965
7966 si->applet.ctx.stats.scope_str = 0;
7967 si->applet.ctx.stats.scope_len = 0;
7968 h = uri + uri_auth->uri_len;
7969 while (h <= uri + msg->sl.rq.u_l - 8) {
7970 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
7971 int itx = 0;
7972 const char *h2;
7973 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
7974 const char *err;
7975
7976 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
7977 h2 = h;
7978 si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p;
7979 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
7980 itx++;
7981 h++;
7982 }
7983
7984 if (itx > STAT_SCOPE_TXT_MAXLEN)
7985 itx = STAT_SCOPE_TXT_MAXLEN;
7986 si->applet.ctx.stats.scope_len = itx;
7987
7988 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
7989 memcpy(scope_txt, h2, itx);
7990 scope_txt[itx] = '\0';
7991 err = invalid_char(scope_txt);
7992 if (err) {
7993 /* bad char in search text => clear scope */
7994 si->applet.ctx.stats.scope_str = 0;
7995 si->applet.ctx.stats.scope_len = 0;
7996 }
7997 break;
7998 }
7999 h++;
8000 }
8001
8002
Willy Tarreaub2513902006-12-17 14:52:38 +01008003 return 1;
8004}
8005
Willy Tarreau4076a152009-04-02 15:18:36 +02008006/*
8007 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008008 * By default it tries to report the error position as msg->err_pos. However if
8009 * this one is not set, it will then report msg->next, which is the last known
8010 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008011 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008012 */
8013void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008014 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01008015 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008016{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008017 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008018 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008019
Willy Tarreau9b28e032012-10-12 23:49:43 +02008020 es->len = MIN(chn->buf->i, sizeof(es->buf));
8021 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008022 len1 = MIN(len1, es->len);
8023 len2 = es->len - len1; /* remaining data if buffer wraps */
8024
Willy Tarreau9b28e032012-10-12 23:49:43 +02008025 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008026 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008027 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008028
Willy Tarreau4076a152009-04-02 15:18:36 +02008029 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008030 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008031 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008032 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008033
Willy Tarreau4076a152009-04-02 15:18:36 +02008034 es->when = date; // user-visible date
8035 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008036 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008037 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008038 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01008039 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008040 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008041 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008042 es->s_flags = s->flags;
8043 es->t_flags = s->txn.flags;
8044 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008045 es->b_out = chn->buf->o;
8046 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008047 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008048 es->m_clen = msg->chunk_len;
8049 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008050}
Willy Tarreaub2513902006-12-17 14:52:38 +01008051
Willy Tarreau294c4732011-12-16 21:35:50 +01008052/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8053 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8054 * performed over the whole headers. Otherwise it must contain a valid header
8055 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8056 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8057 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8058 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008059 * -1. The value fetch stops at commas, so this function is suited for use with
8060 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008061 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008062 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008063unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008064 struct hdr_idx *idx, int occ,
8065 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008066{
Willy Tarreau294c4732011-12-16 21:35:50 +01008067 struct hdr_ctx local_ctx;
8068 char *ptr_hist[MAX_HDR_HISTORY];
8069 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008070 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008071 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008072
Willy Tarreau294c4732011-12-16 21:35:50 +01008073 if (!ctx) {
8074 local_ctx.idx = 0;
8075 ctx = &local_ctx;
8076 }
8077
Willy Tarreaubce70882009-09-07 11:51:47 +02008078 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008079 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008080 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008081 occ--;
8082 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008083 *vptr = ctx->line + ctx->val;
8084 *vlen = ctx->vlen;
8085 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008086 }
8087 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008088 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008089 }
8090
8091 /* negative occurrence, we scan all the list then walk back */
8092 if (-occ > MAX_HDR_HISTORY)
8093 return 0;
8094
Willy Tarreau294c4732011-12-16 21:35:50 +01008095 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008096 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008097 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8098 len_hist[hist_ptr] = ctx->vlen;
8099 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008100 hist_ptr = 0;
8101 found++;
8102 }
8103 if (-occ > found)
8104 return 0;
8105 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008106 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8107 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8108 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008109 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008110 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008111 if (hist_ptr >= MAX_HDR_HISTORY)
8112 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008113 *vptr = ptr_hist[hist_ptr];
8114 *vlen = len_hist[hist_ptr];
8115 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008116}
8117
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008118/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8119 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8120 * performed over the whole headers. Otherwise it must contain a valid header
8121 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8122 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8123 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8124 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8125 * -1. This function differs from http_get_hdr() in that it only returns full
8126 * line header values and does not stop at commas.
8127 * The return value is 0 if nothing was found, or non-zero otherwise.
8128 */
8129unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8130 struct hdr_idx *idx, int occ,
8131 struct hdr_ctx *ctx, char **vptr, int *vlen)
8132{
8133 struct hdr_ctx local_ctx;
8134 char *ptr_hist[MAX_HDR_HISTORY];
8135 int len_hist[MAX_HDR_HISTORY];
8136 unsigned int hist_ptr;
8137 int found;
8138
8139 if (!ctx) {
8140 local_ctx.idx = 0;
8141 ctx = &local_ctx;
8142 }
8143
8144 if (occ >= 0) {
8145 /* search from the beginning */
8146 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8147 occ--;
8148 if (occ <= 0) {
8149 *vptr = ctx->line + ctx->val;
8150 *vlen = ctx->vlen;
8151 return 1;
8152 }
8153 }
8154 return 0;
8155 }
8156
8157 /* negative occurrence, we scan all the list then walk back */
8158 if (-occ > MAX_HDR_HISTORY)
8159 return 0;
8160
8161 found = hist_ptr = 0;
8162 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8163 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8164 len_hist[hist_ptr] = ctx->vlen;
8165 if (++hist_ptr >= MAX_HDR_HISTORY)
8166 hist_ptr = 0;
8167 found++;
8168 }
8169 if (-occ > found)
8170 return 0;
8171 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8172 * find occurrence -occ, so we have to check [hist_ptr+occ].
8173 */
8174 hist_ptr += occ;
8175 if (hist_ptr >= MAX_HDR_HISTORY)
8176 hist_ptr -= MAX_HDR_HISTORY;
8177 *vptr = ptr_hist[hist_ptr];
8178 *vlen = len_hist[hist_ptr];
8179 return 1;
8180}
8181
Willy Tarreaubaaee002006-06-26 02:48:02 +02008182/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008183 * Print a debug line with a header. Always stop at the first CR or LF char,
8184 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8185 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008186 */
8187void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8188{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008189 int max;
8190 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01008191 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
8192 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008193
8194 for (max = 0; start + max < end; max++)
8195 if (start[max] == '\r' || start[max] == '\n')
8196 break;
8197
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008198 UBOUND(max, trash.size - trash.len - 3);
8199 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8200 trash.str[trash.len++] = '\n';
8201 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008202}
8203
Willy Tarreau0937bc42009-12-22 15:03:09 +01008204/*
8205 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8206 * the required fields are properly allocated and that we only need to (re)init
8207 * them. This should be used before processing any new request.
8208 */
8209void http_init_txn(struct session *s)
8210{
8211 struct http_txn *txn = &s->txn;
8212 struct proxy *fe = s->fe;
8213
8214 txn->flags = 0;
8215 txn->status = -1;
8216
Willy Tarreauf64d1412010-10-07 20:06:11 +02008217 txn->cookie_first_date = 0;
8218 txn->cookie_last_date = 0;
8219
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008220 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008221 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008222 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008223 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008224 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008225 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008226 txn->req.chunk_len = 0LL;
8227 txn->req.body_len = 0LL;
8228 txn->rsp.chunk_len = 0LL;
8229 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008230 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8231 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008232 txn->req.chn = s->req;
8233 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008234
8235 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008236
8237 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8238 if (fe->options2 & PR_O2_REQBUG_OK)
8239 txn->req.err_pos = -1; /* let buggy requests pass */
8240
Willy Tarreau46023632010-01-07 22:51:47 +01008241 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008242 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8243
Willy Tarreau46023632010-01-07 22:51:47 +01008244 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008245 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8246
8247 if (txn->hdr_idx.v)
8248 hdr_idx_init(&txn->hdr_idx);
8249}
8250
8251/* to be used at the end of a transaction */
8252void http_end_txn(struct session *s)
8253{
8254 struct http_txn *txn = &s->txn;
8255
8256 /* these ones will have been dynamically allocated */
8257 pool_free2(pool2_requri, txn->uri);
8258 pool_free2(pool2_capture, txn->cli_cookie);
8259 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008260 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008261 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008262
William Lallemanda73203e2012-03-12 12:48:57 +01008263 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008264 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008265 txn->uri = NULL;
8266 txn->srv_cookie = NULL;
8267 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008268
8269 if (txn->req.cap) {
8270 struct cap_hdr *h;
8271 for (h = s->fe->req_cap; h; h = h->next)
8272 pool_free2(h->pool, txn->req.cap[h->index]);
8273 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8274 }
8275
8276 if (txn->rsp.cap) {
8277 struct cap_hdr *h;
8278 for (h = s->fe->rsp_cap; h; h = h->next)
8279 pool_free2(h->pool, txn->rsp.cap[h->index]);
8280 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8281 }
8282
Willy Tarreau0937bc42009-12-22 15:03:09 +01008283}
8284
8285/* to be used at the end of a transaction to prepare a new one */
8286void http_reset_txn(struct session *s)
8287{
8288 http_end_txn(s);
8289 http_init_txn(s);
8290
8291 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008292 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008293 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008294 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008295 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008296 /* re-init store persistence */
8297 s->store_count = 0;
8298
Willy Tarreau0937bc42009-12-22 15:03:09 +01008299 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008300
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008301 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008302
Willy Tarreau739cfba2010-01-25 23:11:14 +01008303 /* We must trim any excess data from the response buffer, because we
8304 * may have blocked an invalid response from a server that we don't
8305 * want to accidentely forward once we disable the analysers, nor do
8306 * we want those data to come along with next response. A typical
8307 * example of such data would be from a buggy server responding to
8308 * a HEAD with some data, or sending more than the advertised
8309 * content-length.
8310 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008311 if (unlikely(s->rep->buf->i))
8312 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008313
Willy Tarreau0937bc42009-12-22 15:03:09 +01008314 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008315 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008316
Willy Tarreaud04e8582010-05-31 12:31:35 +02008317 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008318 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008319
8320 s->req->rex = TICK_ETERNITY;
8321 s->req->wex = TICK_ETERNITY;
8322 s->req->analyse_exp = TICK_ETERNITY;
8323 s->rep->rex = TICK_ETERNITY;
8324 s->rep->wex = TICK_ETERNITY;
8325 s->rep->analyse_exp = TICK_ETERNITY;
8326}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008327
Willy Tarreauff011f22011-01-06 17:51:27 +01008328void free_http_req_rules(struct list *r) {
8329 struct http_req_rule *tr, *pr;
8330
8331 list_for_each_entry_safe(pr, tr, r, list) {
8332 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008333 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008334 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008335
8336 free(pr);
8337 }
8338}
8339
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008340/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008341struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8342{
8343 struct http_req_rule *rule;
8344 int cur_arg;
8345
8346 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8347 if (!rule) {
8348 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008349 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008350 }
8351
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008352 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008353 rule->action = HTTP_REQ_ACT_ALLOW;
8354 cur_arg = 1;
8355 } else if (!strcmp(args[0], "deny")) {
8356 rule->action = HTTP_REQ_ACT_DENY;
8357 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008358 } else if (!strcmp(args[0], "tarpit")) {
8359 rule->action = HTTP_REQ_ACT_TARPIT;
8360 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008361 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008362 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008363 cur_arg = 1;
8364
8365 while(*args[cur_arg]) {
8366 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008367 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008368 cur_arg+=2;
8369 continue;
8370 } else
8371 break;
8372 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008373 } else if (!strcmp(args[0], "set-nice")) {
8374 rule->action = HTTP_REQ_ACT_SET_NICE;
8375 cur_arg = 1;
8376
8377 if (!*args[cur_arg] ||
8378 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8379 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8380 file, linenum, args[0]);
8381 goto out_err;
8382 }
8383 rule->arg.nice = atoi(args[cur_arg]);
8384 if (rule->arg.nice < -1024)
8385 rule->arg.nice = -1024;
8386 else if (rule->arg.nice > 1024)
8387 rule->arg.nice = 1024;
8388 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008389 } else if (!strcmp(args[0], "set-tos")) {
8390#ifdef IP_TOS
8391 char *err;
8392 rule->action = HTTP_REQ_ACT_SET_TOS;
8393 cur_arg = 1;
8394
8395 if (!*args[cur_arg] ||
8396 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8397 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8398 file, linenum, args[0]);
8399 goto out_err;
8400 }
8401
8402 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8403 if (err && *err != '\0') {
8404 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8405 file, linenum, err, args[0]);
8406 goto out_err;
8407 }
8408 cur_arg++;
8409#else
8410 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8411 goto out_err;
8412#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008413 } else if (!strcmp(args[0], "set-mark")) {
8414#ifdef SO_MARK
8415 char *err;
8416 rule->action = HTTP_REQ_ACT_SET_MARK;
8417 cur_arg = 1;
8418
8419 if (!*args[cur_arg] ||
8420 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8421 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8422 file, linenum, args[0]);
8423 goto out_err;
8424 }
8425
8426 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8427 if (err && *err != '\0') {
8428 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8429 file, linenum, err, args[0]);
8430 goto out_err;
8431 }
8432 cur_arg++;
8433 global.last_checks |= LSTCHK_NETADM;
8434#else
8435 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8436 goto out_err;
8437#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008438 } else if (!strcmp(args[0], "set-log-level")) {
8439 rule->action = HTTP_REQ_ACT_SET_LOGL;
8440 cur_arg = 1;
8441
8442 if (!*args[cur_arg] ||
8443 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8444 bad_log_level:
8445 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8446 file, linenum, args[0]);
8447 goto out_err;
8448 }
8449 if (strcmp(args[cur_arg], "silent") == 0)
8450 rule->arg.loglevel = -1;
8451 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8452 goto bad_log_level;
8453 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008454 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8455 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8456 cur_arg = 1;
8457
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008458 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8459 (*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 +01008460 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8461 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008462 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008463 }
8464
8465 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8466 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8467 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008468
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008469 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008470 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8471 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008472 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008473 } else if (strcmp(args[0], "redirect") == 0) {
8474 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008475 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008476
8477 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8478 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8479 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8480 goto out_err;
8481 }
8482
8483 /* this redirect rule might already contain a parsed condition which
8484 * we'll pass to the http-request rule.
8485 */
8486 rule->action = HTTP_REQ_ACT_REDIR;
8487 rule->arg.redir = redir;
8488 rule->cond = redir->cond;
8489 redir->cond = NULL;
8490 cur_arg = 2;
8491 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008492 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008493 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 +01008494 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008495 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008496 }
8497
8498 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8499 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008500 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008501
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008502 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8503 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8504 file, linenum, args[0], errmsg);
8505 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008506 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008507 }
8508 rule->cond = cond;
8509 }
8510 else if (*args[cur_arg]) {
8511 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8512 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8513 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008514 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008515 }
8516
8517 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008518 out_err:
8519 free(rule);
8520 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008521}
8522
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008523/* parse an "http-respose" rule */
8524struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8525{
8526 struct http_res_rule *rule;
8527 int cur_arg;
8528
8529 rule = calloc(1, sizeof(*rule));
8530 if (!rule) {
8531 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8532 goto out_err;
8533 }
8534
8535 if (!strcmp(args[0], "allow")) {
8536 rule->action = HTTP_RES_ACT_ALLOW;
8537 cur_arg = 1;
8538 } else if (!strcmp(args[0], "deny")) {
8539 rule->action = HTTP_RES_ACT_DENY;
8540 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008541 } else if (!strcmp(args[0], "set-nice")) {
8542 rule->action = HTTP_RES_ACT_SET_NICE;
8543 cur_arg = 1;
8544
8545 if (!*args[cur_arg] ||
8546 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8547 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8548 file, linenum, args[0]);
8549 goto out_err;
8550 }
8551 rule->arg.nice = atoi(args[cur_arg]);
8552 if (rule->arg.nice < -1024)
8553 rule->arg.nice = -1024;
8554 else if (rule->arg.nice > 1024)
8555 rule->arg.nice = 1024;
8556 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008557 } else if (!strcmp(args[0], "set-tos")) {
8558#ifdef IP_TOS
8559 char *err;
8560 rule->action = HTTP_RES_ACT_SET_TOS;
8561 cur_arg = 1;
8562
8563 if (!*args[cur_arg] ||
8564 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8565 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8566 file, linenum, args[0]);
8567 goto out_err;
8568 }
8569
8570 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8571 if (err && *err != '\0') {
8572 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8573 file, linenum, err, args[0]);
8574 goto out_err;
8575 }
8576 cur_arg++;
8577#else
8578 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8579 goto out_err;
8580#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008581 } else if (!strcmp(args[0], "set-mark")) {
8582#ifdef SO_MARK
8583 char *err;
8584 rule->action = HTTP_RES_ACT_SET_MARK;
8585 cur_arg = 1;
8586
8587 if (!*args[cur_arg] ||
8588 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8589 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8590 file, linenum, args[0]);
8591 goto out_err;
8592 }
8593
8594 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8595 if (err && *err != '\0') {
8596 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8597 file, linenum, err, args[0]);
8598 goto out_err;
8599 }
8600 cur_arg++;
8601 global.last_checks |= LSTCHK_NETADM;
8602#else
8603 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8604 goto out_err;
8605#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008606 } else if (!strcmp(args[0], "set-log-level")) {
8607 rule->action = HTTP_RES_ACT_SET_LOGL;
8608 cur_arg = 1;
8609
8610 if (!*args[cur_arg] ||
8611 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8612 bad_log_level:
8613 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8614 file, linenum, args[0]);
8615 goto out_err;
8616 }
8617 if (strcmp(args[cur_arg], "silent") == 0)
8618 rule->arg.loglevel = -1;
8619 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8620 goto bad_log_level;
8621 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008622 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8623 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8624 cur_arg = 1;
8625
8626 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8627 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8628 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8629 file, linenum, args[0]);
8630 goto out_err;
8631 }
8632
8633 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8634 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8635 LIST_INIT(&rule->arg.hdr_add.fmt);
8636
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008637 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008638 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8639 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8640 cur_arg += 2;
8641 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008642 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 +02008643 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8644 goto out_err;
8645 }
8646
8647 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8648 struct acl_cond *cond;
8649 char *errmsg = NULL;
8650
8651 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8652 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8653 file, linenum, args[0], errmsg);
8654 free(errmsg);
8655 goto out_err;
8656 }
8657 rule->cond = cond;
8658 }
8659 else if (*args[cur_arg]) {
8660 Alert("parsing [%s:%d]: 'http-response %s' expects"
8661 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8662 file, linenum, args[0], args[cur_arg]);
8663 goto out_err;
8664 }
8665
8666 return rule;
8667 out_err:
8668 free(rule);
8669 return NULL;
8670}
8671
Willy Tarreau4baae242012-12-27 12:00:31 +01008672/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8673 * with <err> filled with the error message.
8674 */
8675struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8676 const char **args, char **errmsg)
8677{
8678 struct redirect_rule *rule;
8679 int cur_arg;
8680 int type = REDIRECT_TYPE_NONE;
8681 int code = 302;
8682 const char *destination = NULL;
8683 const char *cookie = NULL;
8684 int cookie_set = 0;
8685 unsigned int flags = REDIRECT_FLAG_NONE;
8686 struct acl_cond *cond = NULL;
8687
8688 cur_arg = 0;
8689 while (*(args[cur_arg])) {
8690 if (strcmp(args[cur_arg], "location") == 0) {
8691 if (!*args[cur_arg + 1])
8692 goto missing_arg;
8693
8694 type = REDIRECT_TYPE_LOCATION;
8695 cur_arg++;
8696 destination = args[cur_arg];
8697 }
8698 else if (strcmp(args[cur_arg], "prefix") == 0) {
8699 if (!*args[cur_arg + 1])
8700 goto missing_arg;
8701
8702 type = REDIRECT_TYPE_PREFIX;
8703 cur_arg++;
8704 destination = args[cur_arg];
8705 }
8706 else if (strcmp(args[cur_arg], "scheme") == 0) {
8707 if (!*args[cur_arg + 1])
8708 goto missing_arg;
8709
8710 type = REDIRECT_TYPE_SCHEME;
8711 cur_arg++;
8712 destination = args[cur_arg];
8713 }
8714 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8715 if (!*args[cur_arg + 1])
8716 goto missing_arg;
8717
8718 cur_arg++;
8719 cookie = args[cur_arg];
8720 cookie_set = 1;
8721 }
8722 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8723 if (!*args[cur_arg + 1])
8724 goto missing_arg;
8725
8726 cur_arg++;
8727 cookie = args[cur_arg];
8728 cookie_set = 0;
8729 }
8730 else if (strcmp(args[cur_arg], "code") == 0) {
8731 if (!*args[cur_arg + 1])
8732 goto missing_arg;
8733
8734 cur_arg++;
8735 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008736 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008737 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008738 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008739 args[cur_arg - 1], args[cur_arg]);
8740 return NULL;
8741 }
8742 }
8743 else if (!strcmp(args[cur_arg],"drop-query")) {
8744 flags |= REDIRECT_FLAG_DROP_QS;
8745 }
8746 else if (!strcmp(args[cur_arg],"append-slash")) {
8747 flags |= REDIRECT_FLAG_APPEND_SLASH;
8748 }
8749 else if (strcmp(args[cur_arg], "if") == 0 ||
8750 strcmp(args[cur_arg], "unless") == 0) {
8751 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8752 if (!cond) {
8753 memprintf(errmsg, "error in condition: %s", *errmsg);
8754 return NULL;
8755 }
8756 break;
8757 }
8758 else {
8759 memprintf(errmsg,
8760 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8761 args[cur_arg]);
8762 return NULL;
8763 }
8764 cur_arg++;
8765 }
8766
8767 if (type == REDIRECT_TYPE_NONE) {
8768 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8769 return NULL;
8770 }
8771
8772 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8773 rule->cond = cond;
8774 rule->rdr_str = strdup(destination);
8775 rule->rdr_len = strlen(destination);
8776 if (cookie) {
8777 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8778 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8779 */
8780 rule->cookie_len = strlen(cookie);
8781 if (cookie_set) {
8782 rule->cookie_str = malloc(rule->cookie_len + 10);
8783 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8784 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8785 rule->cookie_len += 9;
8786 } else {
8787 rule->cookie_str = malloc(rule->cookie_len + 21);
8788 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8789 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8790 rule->cookie_len += 20;
8791 }
8792 }
8793 rule->type = type;
8794 rule->code = code;
8795 rule->flags = flags;
8796 LIST_INIT(&rule->list);
8797 return rule;
8798
8799 missing_arg:
8800 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8801 return NULL;
8802}
8803
Willy Tarreau8797c062007-05-07 00:55:35 +02008804/************************************************************************/
8805/* The code below is dedicated to ACL parsing and matching */
8806/************************************************************************/
8807
8808
Willy Tarreau14174bc2012-04-16 14:34:04 +02008809/* This function ensures that the prerequisites for an L7 fetch are ready,
8810 * which means that a request or response is ready. If some data is missing,
8811 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008812 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8813 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008814 *
8815 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008816 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8817 * decide whether or not an HTTP message is present ;
8818 * 0 if the requested data cannot be fetched or if it is certain that
8819 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008820 * 1 if an HTTP message is ready
8821 */
8822static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008823smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008824 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008825{
8826 struct http_txn *txn = l7;
8827 struct http_msg *msg = &txn->req;
8828
8829 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8830 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8831 */
8832
8833 if (unlikely(!s || !txn))
8834 return 0;
8835
8836 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008837 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008838
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008839 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008840 if (unlikely(!s->req))
8841 return 0;
8842
Willy Tarreauaae75e32013-03-29 12:31:49 +01008843 /* If the buffer does not leave enough free space at the end,
8844 * we must first realign it.
8845 */
8846 if (s->req->buf->p > s->req->buf->data &&
8847 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8848 buffer_slow_realign(s->req->buf);
8849
Willy Tarreau14174bc2012-04-16 14:34:04 +02008850 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008851 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008852 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008853
8854 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008855 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008856 http_msg_analyzer(msg, &txn->hdr_idx);
8857
8858 /* Still no valid request ? */
8859 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008860 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008861 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008862 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008863 }
8864 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008865 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008866 return 0;
8867 }
8868
8869 /* OK we just got a valid HTTP request. We have some minor
8870 * preparation to perform so that further checks can rely
8871 * on HTTP tests.
8872 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008873
8874 /* If the request was parsed but was too large, we must absolutely
8875 * return an error so that it is not processed. At the moment this
8876 * cannot happen, but if the parsers are to change in the future,
8877 * we want this check to be maintained.
8878 */
8879 if (unlikely(s->req->buf->i + s->req->buf->p >
8880 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8881 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008882 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008883 return 1;
8884 }
8885
Willy Tarreau9b28e032012-10-12 23:49:43 +02008886 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008887 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8888 s->flags |= SN_REDIRECTABLE;
8889
Willy Tarreau506d0502013-07-06 13:29:24 +02008890 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8891 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008892 }
8893
Willy Tarreau506d0502013-07-06 13:29:24 +02008894 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008895 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008896 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008897
8898 /* otherwise everything's ready for the request */
8899 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008900 else {
8901 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008902 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8903 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008904 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008905 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008906 }
8907
8908 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008909 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008910 return 1;
8911}
Willy Tarreau8797c062007-05-07 00:55:35 +02008912
Willy Tarreauc0239e02012-04-16 14:42:55 +02008913#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008914 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 +02008915
Willy Tarreau24e32d82012-04-23 23:55:44 +02008916#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008917 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 +02008918
Willy Tarreau8797c062007-05-07 00:55:35 +02008919
8920/* 1. Check on METHOD
8921 * We use the pre-parsed method if it is known, and store its number as an
8922 * integer. If it is unknown, we use the pointer and the length.
8923 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008924static 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 +02008925{
8926 int len, meth;
8927
Willy Tarreauae8b7962007-06-09 23:10:04 +02008928 len = strlen(*text);
8929 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008930
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01008931 pattern->smp = smp;
Willy Tarreau8797c062007-05-07 00:55:35 +02008932 pattern->val.i = meth;
8933 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008934 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008935 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008936 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008937 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008938 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008939 pattern->len = len;
8940 }
8941 return 1;
8942}
8943
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008944/* This function fetches the method of current HTTP request and stores
8945 * it in the global pattern struct as a chunk. There are two possibilities :
8946 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8947 * in <len> and <ptr> is NULL ;
8948 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8949 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008950 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008951 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008952static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008953smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008954 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008955{
8956 int meth;
8957 struct http_txn *txn = l7;
8958
Willy Tarreau24e32d82012-04-23 23:55:44 +02008959 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008960
Willy Tarreau8797c062007-05-07 00:55:35 +02008961 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008962 smp->type = SMP_T_UINT;
8963 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008964 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008965 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8966 /* ensure the indexes are not affected */
8967 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008968 smp->type = SMP_T_CSTR;
8969 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008970 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008971 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008972 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008973 return 1;
8974}
8975
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008976/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008977static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008978{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008979 int icase;
8980
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008981
Willy Tarreauf853c462012-04-23 18:53:56 +02008982 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008983 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008984 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008985 return PAT_MATCH;
8986 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008987 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008988
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008989 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8990 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008991 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008992
8993 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008994 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008995 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008996
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008997 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008998 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8999 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009000 return PAT_NOMATCH;
9001 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009002}
9003
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009004static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009005smp_fetch_rqver(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 struct http_txn *txn = l7;
9009 char *ptr;
9010 int len;
9011
Willy Tarreauc0239e02012-04-16 14:42:55 +02009012 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009013
Willy Tarreau8797c062007-05-07 00:55:35 +02009014 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009015 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009016
9017 while ((len-- > 0) && (*ptr++ != '/'));
9018 if (len <= 0)
9019 return 0;
9020
Willy Tarreauf853c462012-04-23 18:53:56 +02009021 smp->type = SMP_T_CSTR;
9022 smp->data.str.str = ptr;
9023 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009024
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009025 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009026 return 1;
9027}
9028
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009029static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009030smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009031 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009032{
9033 struct http_txn *txn = l7;
9034 char *ptr;
9035 int len;
9036
Willy Tarreauc0239e02012-04-16 14:42:55 +02009037 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009038
Willy Tarreauf26b2522012-12-14 08:33:14 +01009039 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9040 return 0;
9041
Willy Tarreau8797c062007-05-07 00:55:35 +02009042 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009043 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009044
9045 while ((len-- > 0) && (*ptr++ != '/'));
9046 if (len <= 0)
9047 return 0;
9048
Willy Tarreauf853c462012-04-23 18:53:56 +02009049 smp->type = SMP_T_CSTR;
9050 smp->data.str.str = ptr;
9051 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009052
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009053 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009054 return 1;
9055}
9056
9057/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009058static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009059smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009060 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009061{
9062 struct http_txn *txn = l7;
9063 char *ptr;
9064 int len;
9065
Willy Tarreauc0239e02012-04-16 14:42:55 +02009066 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009067
Willy Tarreauf26b2522012-12-14 08:33:14 +01009068 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9069 return 0;
9070
Willy Tarreau8797c062007-05-07 00:55:35 +02009071 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009072 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009073
Willy Tarreauf853c462012-04-23 18:53:56 +02009074 smp->type = SMP_T_UINT;
9075 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009076 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009077 return 1;
9078}
9079
9080/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009081static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009082smp_fetch_url(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
Willy Tarreauc0239e02012-04-16 14:42:55 +02009087 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009088
Willy Tarreauf853c462012-04-23 18:53:56 +02009089 smp->type = SMP_T_CSTR;
9090 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009091 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009092 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009093 return 1;
9094}
9095
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009096static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009097smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009098 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009099{
9100 struct http_txn *txn = l7;
9101
Willy Tarreauc0239e02012-04-16 14:42:55 +02009102 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009103
9104 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009105 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
9106 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009107 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009108 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009109 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009110
9111 /*
9112 * If we are parsing url in frontend space, we prepare backend stage
9113 * to not parse again the same url ! optimization lazyness...
9114 */
9115 if (px->options & PR_O_HTTP_PROXY)
9116 l4->flags |= SN_ADDR_SET;
9117
Willy Tarreau37406352012-04-23 16:16:37 +02009118 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009119 return 1;
9120}
9121
9122static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009123smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009124 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009125{
9126 struct http_txn *txn = l7;
9127
Willy Tarreauc0239e02012-04-16 14:42:55 +02009128 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009129
9130 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009131 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 +02009132 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009133 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009134
9135 if (px->options & PR_O_HTTP_PROXY)
9136 l4->flags |= SN_ADDR_SET;
9137
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009138 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009139 return 1;
9140}
9141
Willy Tarreau185b5c42012-04-26 15:11:51 +02009142/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9143 * Accepts an optional argument of type string containing the header field name,
9144 * and an optional argument of type signed or unsigned integer to request an
9145 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009146 * headers are considered from the first one. It does not stop on commas and
9147 * returns full lines instead (useful for User-Agent or Date for example).
9148 */
9149static int
9150smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009151 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009152{
9153 struct http_txn *txn = l7;
9154 struct hdr_idx *idx = &txn->hdr_idx;
9155 struct hdr_ctx *ctx = smp->ctx.a[0];
9156 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9157 int occ = 0;
9158 const char *name_str = NULL;
9159 int name_len = 0;
9160
9161 if (!ctx) {
9162 /* first call */
9163 ctx = &static_hdr_ctx;
9164 ctx->idx = 0;
9165 smp->ctx.a[0] = ctx;
9166 }
9167
9168 if (args) {
9169 if (args[0].type != ARGT_STR)
9170 return 0;
9171 name_str = args[0].data.str.str;
9172 name_len = args[0].data.str.len;
9173
9174 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9175 occ = args[1].data.uint;
9176 }
9177
9178 CHECK_HTTP_MESSAGE_FIRST();
9179
9180 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9181 /* search for header from the beginning */
9182 ctx->idx = 0;
9183
9184 if (!occ && !(opt & SMP_OPT_ITERATE))
9185 /* no explicit occurrence and single fetch => last header by default */
9186 occ = -1;
9187
9188 if (!occ)
9189 /* prepare to report multiple occurrences for ACL fetches */
9190 smp->flags |= SMP_F_NOT_LAST;
9191
9192 smp->type = SMP_T_CSTR;
9193 smp->flags |= SMP_F_VOL_HDR;
9194 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9195 return 1;
9196
9197 smp->flags &= ~SMP_F_NOT_LAST;
9198 return 0;
9199}
9200
9201/* 6. Check on HTTP header count. The number of occurrences is returned.
9202 * Accepts exactly 1 argument of type string. It does not stop on commas and
9203 * returns full lines instead (useful for User-Agent or Date for example).
9204 */
9205static int
9206smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009207 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009208{
9209 struct http_txn *txn = l7;
9210 struct hdr_idx *idx = &txn->hdr_idx;
9211 struct hdr_ctx ctx;
9212 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9213 int cnt;
9214
9215 if (!args || args->type != ARGT_STR)
9216 return 0;
9217
9218 CHECK_HTTP_MESSAGE_FIRST();
9219
9220 ctx.idx = 0;
9221 cnt = 0;
9222 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9223 cnt++;
9224
9225 smp->type = SMP_T_UINT;
9226 smp->data.uint = cnt;
9227 smp->flags = SMP_F_VOL_HDR;
9228 return 1;
9229}
9230
9231/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9232 * Accepts an optional argument of type string containing the header field name,
9233 * and an optional argument of type signed or unsigned integer to request an
9234 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009235 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009236 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009237static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009238smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009239 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009240{
9241 struct http_txn *txn = l7;
9242 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009243 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009244 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009245 int occ = 0;
9246 const char *name_str = NULL;
9247 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009248
Willy Tarreaua890d072013-04-02 12:01:06 +02009249 if (!ctx) {
9250 /* first call */
9251 ctx = &static_hdr_ctx;
9252 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009253 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009254 }
9255
Willy Tarreau185b5c42012-04-26 15:11:51 +02009256 if (args) {
9257 if (args[0].type != ARGT_STR)
9258 return 0;
9259 name_str = args[0].data.str.str;
9260 name_len = args[0].data.str.len;
9261
9262 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9263 occ = args[1].data.uint;
9264 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009265
Willy Tarreaue333ec92012-04-16 16:26:40 +02009266 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009267
Willy Tarreau185b5c42012-04-26 15:11:51 +02009268 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009269 /* search for header from the beginning */
9270 ctx->idx = 0;
9271
Willy Tarreau185b5c42012-04-26 15:11:51 +02009272 if (!occ && !(opt & SMP_OPT_ITERATE))
9273 /* no explicit occurrence and single fetch => last header by default */
9274 occ = -1;
9275
9276 if (!occ)
9277 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009278 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009279
Willy Tarreau185b5c42012-04-26 15:11:51 +02009280 smp->type = SMP_T_CSTR;
9281 smp->flags |= SMP_F_VOL_HDR;
9282 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 +02009283 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009284
Willy Tarreau37406352012-04-23 16:16:37 +02009285 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009286 return 0;
9287}
9288
Willy Tarreauc11416f2007-06-17 16:58:38 +02009289/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009290 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009291 */
9292static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009293smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009294 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009295{
9296 struct http_txn *txn = l7;
9297 struct hdr_idx *idx = &txn->hdr_idx;
9298 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009299 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009300 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009301
Willy Tarreau24e32d82012-04-23 23:55:44 +02009302 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009303 return 0;
9304
Willy Tarreaue333ec92012-04-16 16:26:40 +02009305 CHECK_HTTP_MESSAGE_FIRST();
9306
Willy Tarreau33a7e692007-06-10 19:45:56 +02009307 ctx.idx = 0;
9308 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009309 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 +02009310 cnt++;
9311
Willy Tarreauf853c462012-04-23 18:53:56 +02009312 smp->type = SMP_T_UINT;
9313 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009314 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009315 return 1;
9316}
9317
Willy Tarreau185b5c42012-04-26 15:11:51 +02009318/* Fetch an HTTP header's integer value. The integer value is returned. It
9319 * takes a mandatory argument of type string and an optional one of type int
9320 * to designate a specific occurrence. It returns an unsigned integer, which
9321 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009322 */
9323static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009324smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009325 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009326{
Willy Tarreauef38c392013-07-22 16:29:32 +02009327 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009328
Willy Tarreauf853c462012-04-23 18:53:56 +02009329 if (ret > 0) {
9330 smp->type = SMP_T_UINT;
9331 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9332 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009333
Willy Tarreaud53e2422012-04-16 17:21:11 +02009334 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009335}
9336
Cyril Bonté69fa9922012-10-25 00:01:06 +02009337/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9338 * and an optional one of type int to designate a specific occurrence.
9339 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009340 */
9341static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009342smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009343 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009344{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009345 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009346
Willy Tarreauef38c392013-07-22 16:29:32 +02009347 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009348 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9349 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009350 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009351 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009352 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009353 if (smp->data.str.len < temp->size - 1) {
9354 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9355 temp->str[smp->data.str.len] = '\0';
9356 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9357 smp->type = SMP_T_IPV6;
9358 break;
9359 }
9360 }
9361 }
9362
Willy Tarreaud53e2422012-04-16 17:21:11 +02009363 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009364 if (!(smp->flags & SMP_F_NOT_LAST))
9365 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009366 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009367 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009368}
9369
Willy Tarreau737b0c12007-06-10 21:28:46 +02009370/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9371 * the first '/' after the possible hostname, and ends before the possible '?'.
9372 */
9373static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009374smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009375 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009376{
9377 struct http_txn *txn = l7;
9378 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009379
Willy Tarreauc0239e02012-04-16 14:42:55 +02009380 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009381
Willy Tarreau9b28e032012-10-12 23:49:43 +02009382 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009383 ptr = http_get_path(txn);
9384 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009385 return 0;
9386
9387 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009388 smp->type = SMP_T_CSTR;
9389 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009390
9391 while (ptr < end && *ptr != '?')
9392 ptr++;
9393
Willy Tarreauf853c462012-04-23 18:53:56 +02009394 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009395 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009396 return 1;
9397}
9398
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009399/* This produces a concatenation of the first occurrence of the Host header
9400 * followed by the path component if it begins with a slash ('/'). This means
9401 * that '*' will not be added, resulting in exactly the first Host entry.
9402 * If no Host header is found, then the path is returned as-is. The returned
9403 * value is stored in the trash so it does not need to be marked constant.
9404 */
9405static int
9406smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009407 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009408{
9409 struct http_txn *txn = l7;
9410 char *ptr, *end, *beg;
9411 struct hdr_ctx ctx;
9412
9413 CHECK_HTTP_MESSAGE_FIRST();
9414
9415 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009416 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 +02009417 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009418 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009419
9420 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009421 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009422 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009423 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009424 smp->data.str.len = ctx.vlen;
9425
9426 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009427 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 +02009428 beg = http_get_path(txn);
9429 if (!beg)
9430 beg = end;
9431
9432 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9433
9434 if (beg < ptr && *beg == '/') {
9435 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9436 smp->data.str.len += ptr - beg;
9437 }
9438
9439 smp->flags = SMP_F_VOL_1ST;
9440 return 1;
9441}
9442
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009443/* This produces a 32-bit hash of the concatenation of the first occurrence of
9444 * the Host header followed by the path component if it begins with a slash ('/').
9445 * This means that '*' will not be added, resulting in exactly the first Host
9446 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009447 * is hashed using the path hash followed by a full avalanche hash and provides a
9448 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009449 * high-traffic sites without having to store whole paths.
9450 */
9451static int
9452smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009453 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009454{
9455 struct http_txn *txn = l7;
9456 struct hdr_ctx ctx;
9457 unsigned int hash = 0;
9458 char *ptr, *beg, *end;
9459 int len;
9460
9461 CHECK_HTTP_MESSAGE_FIRST();
9462
9463 ctx.idx = 0;
9464 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9465 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9466 ptr = ctx.line + ctx.val;
9467 len = ctx.vlen;
9468 while (len--)
9469 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9470 }
9471
9472 /* now retrieve the path */
9473 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9474 beg = http_get_path(txn);
9475 if (!beg)
9476 beg = end;
9477
9478 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9479
9480 if (beg < ptr && *beg == '/') {
9481 while (beg < ptr)
9482 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9483 }
9484 hash = full_hash(hash);
9485
9486 smp->type = SMP_T_UINT;
9487 smp->data.uint = hash;
9488 smp->flags = SMP_F_VOL_1ST;
9489 return 1;
9490}
9491
Willy Tarreau4a550602012-12-09 14:53:32 +01009492/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009493 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9494 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9495 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009496 * that in environments where IPv6 is insignificant, truncating the output to
9497 * 8 bytes would still work.
9498 */
9499static int
9500smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009501 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009502{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009503 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01009504
Willy Tarreauef38c392013-07-22 16:29:32 +02009505 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009506 return 0;
9507
Willy Tarreau47ca5452012-12-23 20:22:19 +01009508 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009509 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9510 temp->len += sizeof(smp->data.uint);
9511
9512 switch (l4->si[0].conn->addr.from.ss_family) {
9513 case AF_INET:
9514 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
9515 temp->len += 4;
9516 break;
9517 case AF_INET6:
9518 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
9519 temp->len += 16;
9520 break;
9521 default:
9522 return 0;
9523 }
9524
9525 smp->data.str = *temp;
9526 smp->type = SMP_T_BIN;
9527 return 1;
9528}
9529
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009530static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009531smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009532 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009533{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009534 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9535 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9536 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009537
Willy Tarreau24e32d82012-04-23 23:55:44 +02009538 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009539
Willy Tarreauf853c462012-04-23 18:53:56 +02009540 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009541 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009542 return 1;
9543}
9544
Willy Tarreau7f18e522010-10-22 20:04:13 +02009545/* return a valid test if the current request is the first one on the connection */
9546static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009547smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009548 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009549{
9550 if (!s)
9551 return 0;
9552
Willy Tarreauf853c462012-04-23 18:53:56 +02009553 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009554 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009555 return 1;
9556}
9557
Willy Tarreau34db1082012-04-19 17:16:54 +02009558/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009559static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009560smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009561 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009562{
9563
Willy Tarreau24e32d82012-04-23 23:55:44 +02009564 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009565 return 0;
9566
Willy Tarreauc0239e02012-04-16 14:42:55 +02009567 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009568
Willy Tarreauc0239e02012-04-16 14:42:55 +02009569 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009570 return 0;
9571
Willy Tarreauf853c462012-04-23 18:53:56 +02009572 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009573 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 +01009574 return 1;
9575}
Willy Tarreau8797c062007-05-07 00:55:35 +02009576
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009577/* Accepts exactly 1 argument of type userlist */
9578static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009579smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009580 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009581{
9582
9583 if (!args || args->type != ARGT_USR)
9584 return 0;
9585
9586 CHECK_HTTP_MESSAGE_FIRST();
9587
9588 if (!get_http_auth(l4))
9589 return 0;
9590
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009591 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009592 smp->ctx.a[0] = args->data.usr; /* user list */
9593 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9594 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9595
9596 /* if the user does not belong to the userlist or has a wrong password,
9597 * report that it unconditionally does not match. Otherwise we return
9598 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009599 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009600 */
9601 smp->type = SMP_T_BOOL;
9602 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9603 if (smp->data.uint)
9604 smp->type = SMP_T_UINT;
9605
9606 return 1;
9607}
9608
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009609/* Try to find the next occurrence of a cookie name in a cookie header value.
9610 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9611 * the cookie value is returned into *value and *value_l, and the function
9612 * returns a pointer to the next pointer to search from if the value was found.
9613 * Otherwise if the cookie was not found, NULL is returned and neither value
9614 * nor value_l are touched. The input <hdr> string should first point to the
9615 * header's value, and the <hdr_end> pointer must point to the first character
9616 * not part of the value. <list> must be non-zero if value may represent a list
9617 * of values (cookie headers). This makes it faster to abort parsing when no
9618 * list is expected.
9619 */
9620static char *
9621extract_cookie_value(char *hdr, const char *hdr_end,
9622 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009623 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009624{
9625 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9626 char *next;
9627
9628 /* we search at least a cookie name followed by an equal, and more
9629 * generally something like this :
9630 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9631 */
9632 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9633 /* Iterate through all cookies on this line */
9634
9635 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9636 att_beg++;
9637
9638 /* find att_end : this is the first character after the last non
9639 * space before the equal. It may be equal to hdr_end.
9640 */
9641 equal = att_end = att_beg;
9642
9643 while (equal < hdr_end) {
9644 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9645 break;
9646 if (http_is_spht[(unsigned char)*equal++])
9647 continue;
9648 att_end = equal;
9649 }
9650
9651 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9652 * is between <att_beg> and <equal>, both may be identical.
9653 */
9654
9655 /* look for end of cookie if there is an equal sign */
9656 if (equal < hdr_end && *equal == '=') {
9657 /* look for the beginning of the value */
9658 val_beg = equal + 1;
9659 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9660 val_beg++;
9661
9662 /* find the end of the value, respecting quotes */
9663 next = find_cookie_value_end(val_beg, hdr_end);
9664
9665 /* make val_end point to the first white space or delimitor after the value */
9666 val_end = next;
9667 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9668 val_end--;
9669 } else {
9670 val_beg = val_end = next = equal;
9671 }
9672
9673 /* We have nothing to do with attributes beginning with '$'. However,
9674 * they will automatically be removed if a header before them is removed,
9675 * since they're supposed to be linked together.
9676 */
9677 if (*att_beg == '$')
9678 continue;
9679
9680 /* Ignore cookies with no equal sign */
9681 if (equal == next)
9682 continue;
9683
9684 /* Now we have the cookie name between att_beg and att_end, and
9685 * its value between val_beg and val_end.
9686 */
9687
9688 if (att_end - att_beg == cookie_name_l &&
9689 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9690 /* let's return this value and indicate where to go on from */
9691 *value = val_beg;
9692 *value_l = val_end - val_beg;
9693 return next + 1;
9694 }
9695
9696 /* Set-Cookie headers only have the name in the first attr=value part */
9697 if (!list)
9698 break;
9699 }
9700
9701 return NULL;
9702}
9703
Willy Tarreaue333ec92012-04-16 16:26:40 +02009704/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009705 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009706 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009707 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009708 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009709 * Accepts exactly 1 argument of type string. If the input options indicate
9710 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009711 */
9712static int
Willy Tarreau51539362012-05-08 12:46:28 +02009713smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009714 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009715{
9716 struct http_txn *txn = l7;
9717 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009718 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009719 const struct http_msg *msg;
9720 const char *hdr_name;
9721 int hdr_name_len;
9722 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009723 int occ = 0;
9724 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009725
Willy Tarreau24e32d82012-04-23 23:55:44 +02009726 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009727 return 0;
9728
Willy Tarreaua890d072013-04-02 12:01:06 +02009729 if (!ctx) {
9730 /* first call */
9731 ctx = &static_hdr_ctx;
9732 ctx->idx = 0;
9733 smp->ctx.a[2] = ctx;
9734 }
9735
Willy Tarreaue333ec92012-04-16 16:26:40 +02009736 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009737
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009738 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009739 msg = &txn->req;
9740 hdr_name = "Cookie";
9741 hdr_name_len = 6;
9742 } else {
9743 msg = &txn->rsp;
9744 hdr_name = "Set-Cookie";
9745 hdr_name_len = 10;
9746 }
9747
Willy Tarreau28376d62012-04-26 21:26:10 +02009748 if (!occ && !(opt & SMP_OPT_ITERATE))
9749 /* no explicit occurrence and single fetch => last cookie by default */
9750 occ = -1;
9751
9752 /* OK so basically here, either we want only one value and it's the
9753 * last one, or we want to iterate over all of them and we fetch the
9754 * next one.
9755 */
9756
Willy Tarreau9b28e032012-10-12 23:49:43 +02009757 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009758 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009759 /* search for the header from the beginning, we must first initialize
9760 * the search parameters.
9761 */
Willy Tarreau37406352012-04-23 16:16:37 +02009762 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009763 ctx->idx = 0;
9764 }
9765
Willy Tarreau28376d62012-04-26 21:26:10 +02009766 smp->flags |= SMP_F_VOL_HDR;
9767
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009768 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009769 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9770 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009771 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9772 goto out;
9773
Willy Tarreau24e32d82012-04-23 23:55:44 +02009774 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009775 continue;
9776
Willy Tarreau37406352012-04-23 16:16:37 +02009777 smp->ctx.a[0] = ctx->line + ctx->val;
9778 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009779 }
9780
Willy Tarreauf853c462012-04-23 18:53:56 +02009781 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009782 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009783 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009784 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009785 &smp->data.str.str,
9786 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009787 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009788 found = 1;
9789 if (occ >= 0) {
9790 /* one value was returned into smp->data.str.{str,len} */
9791 smp->flags |= SMP_F_NOT_LAST;
9792 return 1;
9793 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009794 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009795 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009796 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009797 /* all cookie headers and values were scanned. If we're looking for the
9798 * last occurrence, we may return it now.
9799 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009800 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009801 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009802 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009803}
9804
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009805/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009806 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009807 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009808 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009809 */
9810static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009811smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009812 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009813{
9814 struct http_txn *txn = l7;
9815 struct hdr_idx *idx = &txn->hdr_idx;
9816 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009817 const struct http_msg *msg;
9818 const char *hdr_name;
9819 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009820 int cnt;
9821 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009822 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009823
Willy Tarreau24e32d82012-04-23 23:55:44 +02009824 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009825 return 0;
9826
Willy Tarreaue333ec92012-04-16 16:26:40 +02009827 CHECK_HTTP_MESSAGE_FIRST();
9828
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009829 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009830 msg = &txn->req;
9831 hdr_name = "Cookie";
9832 hdr_name_len = 6;
9833 } else {
9834 msg = &txn->rsp;
9835 hdr_name = "Set-Cookie";
9836 hdr_name_len = 10;
9837 }
9838
Willy Tarreau9b28e032012-10-12 23:49:43 +02009839 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009840 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009841 ctx.idx = 0;
9842 cnt = 0;
9843
9844 while (1) {
9845 /* Note: val_beg == NULL every time we need to fetch a new header */
9846 if (!val_beg) {
9847 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9848 break;
9849
Willy Tarreau24e32d82012-04-23 23:55:44 +02009850 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009851 continue;
9852
9853 val_beg = ctx.line + ctx.val;
9854 val_end = val_beg + ctx.vlen;
9855 }
9856
Willy Tarreauf853c462012-04-23 18:53:56 +02009857 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009858 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009859 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009860 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009861 &smp->data.str.str,
9862 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009863 cnt++;
9864 }
9865 }
9866
Willy Tarreauf853c462012-04-23 18:53:56 +02009867 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009868 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009869 return 1;
9870}
9871
Willy Tarreau51539362012-05-08 12:46:28 +02009872/* Fetch an cookie's integer value. The integer value is returned. It
9873 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9874 */
9875static int
9876smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009877 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009878{
Willy Tarreauef38c392013-07-22 16:29:32 +02009879 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009880
9881 if (ret > 0) {
9882 smp->type = SMP_T_UINT;
9883 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9884 }
9885
9886 return ret;
9887}
9888
Willy Tarreau8797c062007-05-07 00:55:35 +02009889/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009890/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009891/************************************************************************/
9892
David Cournapeau16023ee2010-12-23 20:55:41 +09009893/*
9894 * Given a path string and its length, find the position of beginning of the
9895 * query string. Returns NULL if no query string is found in the path.
9896 *
9897 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9898 *
9899 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9900 */
bedis4c75cca2012-10-05 08:38:24 +02009901static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009902{
9903 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009904
bedis4c75cca2012-10-05 08:38:24 +02009905 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009906 return p ? p + 1 : NULL;
9907}
9908
bedis4c75cca2012-10-05 08:38:24 +02009909static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009910{
bedis4c75cca2012-10-05 08:38:24 +02009911 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009912}
9913
9914/*
9915 * Given a url parameter, find the starting position of the first occurence,
9916 * or NULL if the parameter is not found.
9917 *
9918 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9919 * the function will return query_string+8.
9920 */
9921static char*
9922find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009923 char* url_param_name, size_t url_param_name_l,
9924 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009925{
9926 char *pos, *last;
9927
9928 pos = query_string;
9929 last = query_string + query_string_l - url_param_name_l - 1;
9930
9931 while (pos <= last) {
9932 if (pos[url_param_name_l] == '=') {
9933 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9934 return pos;
9935 pos += url_param_name_l + 1;
9936 }
bedis4c75cca2012-10-05 08:38:24 +02009937 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009938 pos++;
9939 pos++;
9940 }
9941 return NULL;
9942}
9943
9944/*
9945 * Given a url parameter name, returns its value and size into *value and
9946 * *value_l respectively, and returns non-zero. If the parameter is not found,
9947 * zero is returned and value/value_l are not touched.
9948 */
9949static int
9950find_url_param_value(char* path, size_t path_l,
9951 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009952 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009953{
9954 char *query_string, *qs_end;
9955 char *arg_start;
9956 char *value_start, *value_end;
9957
bedis4c75cca2012-10-05 08:38:24 +02009958 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009959 if (!query_string)
9960 return 0;
9961
9962 qs_end = path + path_l;
9963 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009964 url_param_name, url_param_name_l,
9965 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009966 if (!arg_start)
9967 return 0;
9968
9969 value_start = arg_start + url_param_name_l + 1;
9970 value_end = value_start;
9971
bedis4c75cca2012-10-05 08:38:24 +02009972 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009973 value_end++;
9974
9975 *value = value_start;
9976 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009977 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009978}
9979
9980static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009981smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009982 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009983{
bedis4c75cca2012-10-05 08:38:24 +02009984 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009985 struct http_txn *txn = l7;
9986 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009987
bedis4c75cca2012-10-05 08:38:24 +02009988 if (!args || args[0].type != ARGT_STR ||
9989 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009990 return 0;
9991
9992 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009993
bedis4c75cca2012-10-05 08:38:24 +02009994 if (args[1].type)
9995 delim = *args[1].data.str.str;
9996
Willy Tarreau9b28e032012-10-12 23:49:43 +02009997 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009998 args->data.str.str, args->data.str.len,
9999 &smp->data.str.str, &smp->data.str.len,
10000 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010001 return 0;
10002
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010003 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010004 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010005 return 1;
10006}
10007
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010008/* Return the signed integer value for the specified url parameter (see url_param
10009 * above).
10010 */
10011static int
10012smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010013 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010014{
Willy Tarreauef38c392013-07-22 16:29:32 +020010015 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010016
10017 if (ret > 0) {
10018 smp->type = SMP_T_UINT;
10019 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10020 }
10021
10022 return ret;
10023}
10024
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010025/* This produces a 32-bit hash of the concatenation of the first occurrence of
10026 * the Host header followed by the path component if it begins with a slash ('/').
10027 * This means that '*' will not be added, resulting in exactly the first Host
10028 * entry. If no Host header is found, then the path is used. The resulting value
10029 * is hashed using the url hash followed by a full avalanche hash and provides a
10030 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10031 * high-traffic sites without having to store whole paths.
10032 * this differs from the base32 functions in that it includes the url parameters
10033 * as well as the path
10034 */
10035static int
10036smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010037 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010038{
10039 struct http_txn *txn = l7;
10040 struct hdr_ctx ctx;
10041 unsigned int hash = 0;
10042 char *ptr, *beg, *end;
10043 int len;
10044
10045 CHECK_HTTP_MESSAGE_FIRST();
10046
10047 ctx.idx = 0;
10048 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10049 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10050 ptr = ctx.line + ctx.val;
10051 len = ctx.vlen;
10052 while (len--)
10053 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10054 }
10055
10056 /* now retrieve the path */
10057 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10058 beg = http_get_path(txn);
10059 if (!beg)
10060 beg = end;
10061
10062 for (ptr = beg; ptr < end ; ptr++);
10063
10064 if (beg < ptr && *beg == '/') {
10065 while (beg < ptr)
10066 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10067 }
10068 hash = full_hash(hash);
10069
10070 smp->type = SMP_T_UINT;
10071 smp->data.uint = hash;
10072 smp->flags = SMP_F_VOL_1ST;
10073 return 1;
10074}
10075
10076/* This concatenates the source address with the 32-bit hash of the Host and
10077 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10078 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10079 * on the source address length. The URL hash is stored before the address so
10080 * that in environments where IPv6 is insignificant, truncating the output to
10081 * 8 bytes would still work.
10082 */
10083static int
10084smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010085 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010086{
10087 struct chunk *temp;
10088
Willy Tarreaue155ec22013-11-18 18:33:22 +010010089 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010090 return 0;
10091
10092 temp = get_trash_chunk();
10093 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10094 temp->len += sizeof(smp->data.uint);
10095
10096 switch (l4->si[0].conn->addr.from.ss_family) {
10097 case AF_INET:
10098 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
10099 temp->len += 4;
10100 break;
10101 case AF_INET6:
10102 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
10103 temp->len += 16;
10104 break;
10105 default:
10106 return 0;
10107 }
10108
10109 smp->data.str = *temp;
10110 smp->type = SMP_T_BIN;
10111 return 1;
10112}
10113
Willy Tarreau185b5c42012-04-26 15:11:51 +020010114/* This function is used to validate the arguments passed to any "hdr" fetch
10115 * keyword. These keywords support an optional positive or negative occurrence
10116 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10117 * is assumed that the types are already the correct ones. Returns 0 on error,
10118 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10119 * error message in case of error, that the caller is responsible for freeing.
10120 * The initial location must either be freeable or NULL.
10121 */
10122static int val_hdr(struct arg *arg, char **err_msg)
10123{
10124 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010125 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010126 return 0;
10127 }
10128 return 1;
10129}
10130
Willy Tarreau276fae92013-07-25 14:36:01 +020010131/* takes an UINT value on input supposed to represent the time since EPOCH,
10132 * adds an optional offset found in args[0] and emits a string representing
10133 * the date in RFC-1123/5322 format.
10134 */
10135static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10136{
10137 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10138 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10139 struct chunk *temp;
10140 struct tm *tm;
10141 time_t curr_date = smp->data.uint;
10142
10143 /* add offset */
10144 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10145 curr_date += args[0].data.sint;
10146
10147 tm = gmtime(&curr_date);
10148
10149 temp = get_trash_chunk();
10150 temp->len = snprintf(temp->str, temp->size - temp->len,
10151 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10152 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10153 tm->tm_hour, tm->tm_min, tm->tm_sec);
10154
10155 smp->data.str = *temp;
10156 smp->type = SMP_T_STR;
10157 return 1;
10158}
10159
Willy Tarreau4a568972010-05-12 08:08:50 +020010160/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010161/* All supported ACL keywords must be declared here. */
10162/************************************************************************/
10163
10164/* Note: must not be declared <const> as its list will be overwritten.
10165 * Please take care of keeping this list alphabetically sorted.
10166 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010167static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010168 { "base", "base", pat_parse_str, pat_match_str },
10169 { "base_beg", "base", pat_parse_str, pat_match_beg },
10170 { "base_dir", "base", pat_parse_str, pat_match_dir },
10171 { "base_dom", "base", pat_parse_str, pat_match_dom },
10172 { "base_end", "base", pat_parse_str, pat_match_end },
10173 { "base_len", "base", pat_parse_int, pat_match_len },
10174 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10175 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010176
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010177 { "cook", "req.cook", pat_parse_str, pat_match_str },
10178 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10179 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10180 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10181 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10182 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10183 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10184 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010185
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010186 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10187 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10188 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10189 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10190 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10191 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10192 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10193 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010194
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010195 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010196
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010197 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010198
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010199 { "path", "path", pat_parse_str, pat_match_str },
10200 { "path_beg", "path", pat_parse_str, pat_match_beg },
10201 { "path_dir", "path", pat_parse_str, pat_match_dir },
10202 { "path_dom", "path", pat_parse_str, pat_match_dom },
10203 { "path_end", "path", pat_parse_str, pat_match_end },
10204 { "path_len", "path", pat_parse_int, pat_match_len },
10205 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10206 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010207
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010208 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10209 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010210
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010211 { "scook", "res.cook", pat_parse_str, pat_match_str },
10212 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10213 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10214 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10215 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10216 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10217 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10218 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010219
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010220 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10221 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10222 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10223 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10224 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10225 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10226 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10227 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010228
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010229 { "url", "url", pat_parse_str, pat_match_str },
10230 { "url_beg", "url", pat_parse_str, pat_match_beg },
10231 { "url_dir", "url", pat_parse_str, pat_match_dir },
10232 { "url_dom", "url", pat_parse_str, pat_match_dom },
10233 { "url_end", "url", pat_parse_str, pat_match_end },
10234 { "url_len", "url", pat_parse_int, pat_match_len },
10235 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10236 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010237
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010238 { "urlp", "urlp", pat_parse_str, pat_match_str },
10239 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10240 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10241 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10242 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10243 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10244 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10245 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010246
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010247 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010248}};
10249
10250/************************************************************************/
10251/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010252/************************************************************************/
10253/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010254static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010255 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10256 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10257 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10258
10259 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10260 * are only here to match the ACL's name, are request-only and are used
10261 * for ACL compatibility only.
10262 */
10263 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10264 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10265 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10266 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10267
10268 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10269 * only here to match the ACL's name, are request-only and are used for
10270 * ACL compatibility only.
10271 */
10272 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10273 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10274 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10275 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10276
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010277 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10278 { "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 +010010279 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10280 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10281 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010282
10283 /* HTTP protocol on the request path */
10284 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010285 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010286
10287 /* HTTP version on the request path */
10288 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010289 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010290
10291 /* HTTP version on the response path */
10292 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010293 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10294
Willy Tarreau18ed2562013-01-14 15:56:36 +010010295 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10296 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10297 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10298 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10299
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010300 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10301 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010302 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10303 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10304 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10305 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10306
10307 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10308 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10309 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10310 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10311
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010312 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10313 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010314 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10315 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10316 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10317 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10318
Willy Tarreau409bcde2013-01-08 00:31:00 +010010319 /* scook is valid only on the response and is used for ACL compatibility */
10320 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10321 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10322 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10323 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10324
10325 /* shdr is valid only on the response and is used for ACL compatibility */
10326 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10327 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10328 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10329 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10330
10331 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10332 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010333 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10334 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010335 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10336 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10337 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10338 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10339 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10340 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010341}};
10342
Willy Tarreau8797c062007-05-07 00:55:35 +020010343
Willy Tarreau276fae92013-07-25 14:36:01 +020010344/* Note: must not be declared <const> as its list will be overwritten */
10345static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10346 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10347 { NULL, NULL, 0, 0, 0 },
10348}};
10349
Willy Tarreau8797c062007-05-07 00:55:35 +020010350__attribute__((constructor))
10351static void __http_protocol_init(void)
10352{
10353 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010354 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010355 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010356}
10357
10358
Willy Tarreau58f10d72006-12-04 02:26:12 +010010359/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010360 * Local variables:
10361 * c-indent-level: 8
10362 * c-basic-offset: 8
10363 * End:
10364 */