blob: 36965573f17519c4715cd99156ae64db50066a46 [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>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010064#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/task.h>
66
Willy Tarreau522d6c02009-12-06 18:49:18 +010067const char HTTP_100[] =
68 "HTTP/1.1 100 Continue\r\n\r\n";
69
70const struct chunk http_100_chunk = {
71 .str = (char *)&HTTP_100,
72 .len = sizeof(HTTP_100)-1
73};
74
Willy Tarreaua9679ac2010-01-03 17:32:57 +010075/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079 "Location: "; /* not terminated since it will be concatenated with the URL */
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010083 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Location: "; /* not terminated since it will be concatenated with the URL */
86
87/* same as 302 except that the browser MUST retry with the GET method */
88const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040094
95/* same as 302 except that the browser MUST retry with the same method */
96const char *HTTP_307 =
97 "HTTP/1.1 307 Temporary Redirect\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Content-length: 0\r\n"
100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
102/* same as 301 except that the browser MUST retry with the same method */
103const char *HTTP_308 =
104 "HTTP/1.1 308 Permanent Redirect\r\n"
105 "Content-length: 0\r\n"
106 "Location: "; /* not terminated since it will be concatenated with the URL */
107
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
109const char *HTTP_401_fmt =
110 "HTTP/1.0 401 Unauthorized\r\n"
111 "Cache-Control: no-cache\r\n"
112 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200113 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
115 "\r\n"
116 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
117
Willy Tarreau844a7e72010-01-31 21:46:18 +0100118const char *HTTP_407_fmt =
119 "HTTP/1.0 407 Unauthorized\r\n"
120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
122 "Content-Type: text/html\r\n"
123 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
124 "\r\n"
125 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
126
Willy Tarreau0f772532006-12-23 20:51:41 +0100127
128const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200129 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 [HTTP_ERR_400] = 400,
131 [HTTP_ERR_403] = 403,
132 [HTTP_ERR_408] = 408,
133 [HTTP_ERR_500] = 500,
134 [HTTP_ERR_502] = 502,
135 [HTTP_ERR_503] = 503,
136 [HTTP_ERR_504] = 504,
137};
138
Willy Tarreau80587432006-12-24 17:47:20 +0100139static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] =
141 "HTTP/1.0 200 OK\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
147
Willy Tarreau0f772532006-12-23 20:51:41 +0100148 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100149 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
155
156 [HTTP_ERR_403] =
157 "HTTP/1.0 403 Forbidden\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
163
164 [HTTP_ERR_408] =
165 "HTTP/1.0 408 Request Time-out\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
171
172 [HTTP_ERR_500] =
173 "HTTP/1.0 500 Server Error\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
179
180 [HTTP_ERR_502] =
181 "HTTP/1.0 502 Bad Gateway\r\n"
182 "Cache-Control: no-cache\r\n"
183 "Connection: close\r\n"
184 "Content-Type: text/html\r\n"
185 "\r\n"
186 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
187
188 [HTTP_ERR_503] =
189 "HTTP/1.0 503 Service Unavailable\r\n"
190 "Cache-Control: no-cache\r\n"
191 "Connection: close\r\n"
192 "Content-Type: text/html\r\n"
193 "\r\n"
194 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
195
196 [HTTP_ERR_504] =
197 "HTTP/1.0 504 Gateway Time-out\r\n"
198 "Cache-Control: no-cache\r\n"
199 "Connection: close\r\n"
200 "Content-Type: text/html\r\n"
201 "\r\n"
202 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
203
204};
205
Cyril Bonté19979e12012-04-04 12:57:21 +0200206/* status codes available for the stats admin page (strictly 4 chars length) */
207const char *stat_status_codes[STAT_STATUS_SIZE] = {
208 [STAT_STATUS_DENY] = "DENY",
209 [STAT_STATUS_DONE] = "DONE",
210 [STAT_STATUS_ERRP] = "ERRP",
211 [STAT_STATUS_EXCD] = "EXCD",
212 [STAT_STATUS_NONE] = "NONE",
213 [STAT_STATUS_PART] = "PART",
214 [STAT_STATUS_UNKN] = "UNKN",
215};
216
217
Willy Tarreau80587432006-12-24 17:47:20 +0100218/* We must put the messages here since GCC cannot initialize consts depending
219 * on strlen().
220 */
221struct chunk http_err_chunks[HTTP_ERR_SIZE];
222
Willy Tarreaua890d072013-04-02 12:01:06 +0200223/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
224static struct hdr_ctx static_hdr_ctx;
225
Willy Tarreau42250582007-04-01 01:30:43 +0200226#define FD_SETS_ARE_BITFIELDS
227#ifdef FD_SETS_ARE_BITFIELDS
228/*
229 * This map is used with all the FD_* macros to check whether a particular bit
230 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
231 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
232 * byte should be encoded. Be careful to always pass bytes from 0 to 255
233 * exclusively to the macros.
234 */
235fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
236fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237
238#else
239#error "Check if your OS uses bitfields for fd_sets"
240#endif
241
Willy Tarreau80587432006-12-24 17:47:20 +0100242void init_proto_http()
243{
Willy Tarreau42250582007-04-01 01:30:43 +0200244 int i;
245 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100246 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200247
Willy Tarreau80587432006-12-24 17:47:20 +0100248 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
249 if (!http_err_msgs[msg]) {
250 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
251 abort();
252 }
253
254 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
255 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
256 }
Willy Tarreau42250582007-04-01 01:30:43 +0200257
258 /* initialize the log header encoding map : '{|}"#' should be encoded with
259 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
260 * URL encoding only requires '"', '#' to be encoded as well as non-
261 * printable characters above.
262 */
263 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
264 memset(url_encode_map, 0, sizeof(url_encode_map));
265 for (i = 0; i < 32; i++) {
266 FD_SET(i, hdr_encode_map);
267 FD_SET(i, url_encode_map);
268 }
269 for (i = 127; i < 256; i++) {
270 FD_SET(i, hdr_encode_map);
271 FD_SET(i, url_encode_map);
272 }
273
274 tmp = "\"#{|}";
275 while (*tmp) {
276 FD_SET(*tmp, hdr_encode_map);
277 tmp++;
278 }
279
280 tmp = "\"#";
281 while (*tmp) {
282 FD_SET(*tmp, url_encode_map);
283 tmp++;
284 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200285
286 /* memory allocations */
287 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100288 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100289}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290
Willy Tarreau53b6c742006-12-17 13:37:46 +0100291/*
292 * We have 26 list of methods (1 per first letter), each of which can have
293 * up to 3 entries (2 valid, 1 null).
294 */
295struct http_method_desc {
296 http_meth_t meth;
297 int len;
298 const char text[8];
299};
300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100302 ['C' - 'A'] = {
303 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
304 },
305 ['D' - 'A'] = {
306 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
307 },
308 ['G' - 'A'] = {
309 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
310 },
311 ['H' - 'A'] = {
312 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
313 },
314 ['P' - 'A'] = {
315 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
316 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
317 },
318 ['T' - 'A'] = {
319 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
320 },
321 /* rest is empty like this :
322 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
323 */
324};
325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100326/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200327 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100328 * RFC2616 for those chars.
329 */
330
331const char http_is_spht[256] = {
332 [' '] = 1, ['\t'] = 1,
333};
334
335const char http_is_crlf[256] = {
336 ['\r'] = 1, ['\n'] = 1,
337};
338
339const char http_is_lws[256] = {
340 [' '] = 1, ['\t'] = 1,
341 ['\r'] = 1, ['\n'] = 1,
342};
343
344const char http_is_sep[256] = {
345 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
346 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
347 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
348 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
349 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
350};
351
352const char http_is_ctl[256] = {
353 [0 ... 31] = 1,
354 [127] = 1,
355};
356
357/*
358 * A token is any ASCII char that is neither a separator nor a CTL char.
359 * Do not overwrite values in assignment since gcc-2.95 will not handle
360 * them correctly. Instead, define every non-CTL char's status.
361 */
362const char http_is_token[256] = {
363 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
364 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
365 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
366 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
367 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
368 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
369 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
370 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
371 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
372 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
373 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
374 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
375 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
376 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
377 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
378 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
379 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
380 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
381 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
382 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
383 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
384 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
385 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
386 ['|'] = 1, ['}'] = 0, ['~'] = 1,
387};
388
389
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100390/*
391 * An http ver_token is any ASCII which can be found in an HTTP version,
392 * which includes 'H', 'T', 'P', '/', '.' and any digit.
393 */
394const char http_is_ver_token[256] = {
395 ['.'] = 1, ['/'] = 1,
396 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
397 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
398 ['H'] = 1, ['P'] = 1, ['T'] = 1,
399};
400
401
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100403 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
404 */
405#if defined(DEBUG_FSM)
406static void http_silent_debug(int line, struct session *s)
407{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100408 chunk_printf(&trash,
409 "[%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",
410 line,
411 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
412 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);
413 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100414
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100415 chunk_printf(&trash,
416 " %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",
417 line,
418 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
419 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);
420 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100421}
422#else
423#define http_silent_debug(l,s) do { } while (0)
424#endif
425
426/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100427 * Adds a header and its CRLF at the tail of the message's buffer, just before
428 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100429 * The header is also automatically added to the index <hdr_idx>, and the end
430 * of headers is automatically adjusted. The number of bytes added is returned
431 * on success, otherwise <0 is returned indicating an error.
432 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100433int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100434{
435 int bytes, len;
436
437 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200438 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439 if (!bytes)
440 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100441 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100442 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
443}
444
445/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100446 * Adds a header and its CRLF at the tail of the message's buffer, just before
447 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 * the buffer is only opened and the space reserved, but nothing is copied.
449 * The header is also automatically added to the index <hdr_idx>, and the end
450 * of headers is automatically adjusted. The number of bytes added is returned
451 * on success, otherwise <0 is returned indicating an error.
452 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100453int http_header_add_tail2(struct http_msg *msg,
454 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100455{
456 int bytes;
457
Willy Tarreau9b28e032012-10-12 23:49:43 +0200458 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 if (!bytes)
460 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100461 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100462 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
463}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
465/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100466 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
467 * If so, returns the position of the first non-space character relative to
468 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
469 * to return a pointer to the place after the first space. Returns 0 if the
470 * header name does not match. Checks are case-insensitive.
471 */
472int http_header_match2(const char *hdr, const char *end,
473 const char *name, int len)
474{
475 const char *val;
476
477 if (hdr + len >= end)
478 return 0;
479 if (hdr[len] != ':')
480 return 0;
481 if (strncasecmp(hdr, name, len) != 0)
482 return 0;
483 val = hdr + len + 1;
484 while (val < end && HTTP_IS_SPHT(*val))
485 val++;
486 if ((val >= end) && (len + 2 <= end - hdr))
487 return len + 2; /* we may replace starting from second space */
488 return val - hdr;
489}
490
Willy Tarreau68085d82010-01-18 14:54:04 +0100491/* Find the end of the header value contained between <s> and <e>. See RFC2616,
492 * par 2.2 for more information. Note that it requires a valid header to return
493 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200494 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100495char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200496{
497 int quoted, qdpair;
498
499 quoted = qdpair = 0;
500 for (; s < e; s++) {
501 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200502 else if (quoted) {
503 if (*s == '\\') qdpair = 1;
504 else if (*s == '"') quoted = 0;
505 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 else if (*s == '"') quoted = 1;
507 else if (*s == ',') return s;
508 }
509 return s;
510}
511
512/* Find the first or next occurrence of header <name> in message buffer <sol>
513 * using headers index <idx>, and return it in the <ctx> structure. This
514 * structure holds everything necessary to use the header and find next
515 * occurrence. If its <idx> member is 0, the header is searched from the
516 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100517 * 1 when it finds a value, and 0 when there is no more. It is designed to work
518 * with headers defined as comma-separated lists. As a special case, if ctx->val
519 * is NULL when searching for a new values of a header, the current header is
520 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 */
522int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100523 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 struct hdr_ctx *ctx)
525{
Willy Tarreau68085d82010-01-18 14:54:04 +0100526 char *eol, *sov;
527 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 cur_idx = ctx->idx;
530 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 /* We have previously returned a value, let's search
532 * another one on the same line.
533 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200534 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200535 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100536 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200537 eol = sol + idx->v[cur_idx].len;
538
539 if (sov >= eol)
540 /* no more values in this header */
541 goto next_hdr;
542
Willy Tarreau68085d82010-01-18 14:54:04 +0100543 /* values remaining for this header, skip the comma but save it
544 * for later use (eg: for header deletion).
545 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200546 sov++;
547 while (sov < eol && http_is_lws[(unsigned char)*sov])
548 sov++;
549
550 goto return_hdr;
551 }
552
553 /* first request for this header */
554 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100555 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200556 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 while (cur_idx) {
558 eol = sol + idx->v[cur_idx].len;
559
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200560 if (len == 0) {
561 /* No argument was passed, we want any header.
562 * To achieve this, we simply build a fake request. */
563 while (sol + len < eol && sol[len] != ':')
564 len++;
565 name = sol;
566 }
567
Willy Tarreau33a7e692007-06-10 19:45:56 +0200568 if ((len < eol - sol) &&
569 (sol[len] == ':') &&
570 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 sov = sol + len + 1;
573 while (sov < eol && http_is_lws[(unsigned char)*sov])
574 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100575
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100577 ctx->prev = old_idx;
578 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 ctx->idx = cur_idx;
580 ctx->val = sov - sol;
581
582 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200583 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200584 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200585 eol--;
586 ctx->tws++;
587 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200588 ctx->vlen = eol - sov;
589 return 1;
590 }
591 next_hdr:
592 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100593 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594 cur_idx = idx->v[cur_idx].next;
595 }
596 return 0;
597}
598
599int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100600 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200601 struct hdr_ctx *ctx)
602{
603 return http_find_header2(name, strlen(name), sol, idx, ctx);
604}
605
Willy Tarreau68085d82010-01-18 14:54:04 +0100606/* Remove one value of a header. This only works on a <ctx> returned by one of
607 * the http_find_header functions. The value is removed, as well as surrounding
608 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100609 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 * message <msg>. The new index is returned. If it is zero, it means there is
611 * no more header, so any processing may stop. The ctx is always left in a form
612 * that can be handled by http_find_header2() to find next occurrence.
613 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100614int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100615{
616 int cur_idx = ctx->idx;
617 char *sol = ctx->line;
618 struct hdr_idx_elem *hdr;
619 int delta, skip_comma;
620
621 if (!cur_idx)
622 return 0;
623
624 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200625 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100626 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200627 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 http_msg_move_end(msg, delta);
629 idx->used--;
630 hdr->len = 0; /* unused entry */
631 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100632 if (idx->tail == ctx->idx)
633 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 ctx->idx = ctx->prev; /* walk back to the end of previous header */
635 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
636 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200637 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100638 return ctx->idx;
639 }
640
641 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200642 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
643 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100644 */
645
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200646 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200647 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200648 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100649 NULL, 0);
650 hdr->len += delta;
651 http_msg_move_end(msg, delta);
652 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200653 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100654 return ctx->idx;
655}
656
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100657/* This function handles a server error at the stream interface level. The
658 * stream interface is assumed to be already in a closed state. An optional
659 * message is copied into the input buffer, and an HTTP status code stored.
660 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100661 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100663static void http_server_error(struct session *t, struct stream_interface *si,
664 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200666 channel_auto_read(si->ob);
667 channel_abort(si->ob);
668 channel_auto_close(si->ob);
669 channel_erase(si->ob);
670 channel_auto_close(si->ib);
671 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100672 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100673 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200674 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
676 if (!(t->flags & SN_ERR_MASK))
677 t->flags |= err;
678 if (!(t->flags & SN_FINST_MASK))
679 t->flags |= finst;
680}
681
Willy Tarreau80587432006-12-24 17:47:20 +0100682/* This function returns the appropriate error location for the given session
683 * and message.
684 */
685
Willy Tarreau783f2582012-09-04 12:19:04 +0200686struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100687{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200688 if (s->be->errmsg[msgnum].str)
689 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100690 else if (s->fe->errmsg[msgnum].str)
691 return &s->fe->errmsg[msgnum];
692 else
693 return &http_err_chunks[msgnum];
694}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696/*
697 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
698 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
699 */
700static http_meth_t find_http_meth(const char *str, const int len)
701{
702 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100703 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100704
705 m = ((unsigned)*str - 'A');
706
707 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100708 for (h = http_methods[m]; h->len > 0; h++) {
709 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100710 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100711 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100712 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100713 };
714 return HTTP_METH_OTHER;
715 }
716 return HTTP_METH_NONE;
717
718}
719
Willy Tarreau21d2af32008-02-14 20:25:24 +0100720/* Parse the URI from the given transaction (which is assumed to be in request
721 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
722 * It is returned otherwise.
723 */
724static char *
725http_get_path(struct http_txn *txn)
726{
727 char *ptr, *end;
728
Willy Tarreau9b28e032012-10-12 23:49:43 +0200729 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100730 end = ptr + txn->req.sl.rq.u_l;
731
732 if (ptr >= end)
733 return NULL;
734
735 /* RFC2616, par. 5.1.2 :
736 * Request-URI = "*" | absuri | abspath | authority
737 */
738
739 if (*ptr == '*')
740 return NULL;
741
742 if (isalpha((unsigned char)*ptr)) {
743 /* this is a scheme as described by RFC3986, par. 3.1 */
744 ptr++;
745 while (ptr < end &&
746 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
747 ptr++;
748 /* skip '://' */
749 if (ptr == end || *ptr++ != ':')
750 return NULL;
751 if (ptr == end || *ptr++ != '/')
752 return NULL;
753 if (ptr == end || *ptr++ != '/')
754 return NULL;
755 }
756 /* skip [user[:passwd]@]host[:[port]] */
757
758 while (ptr < end && *ptr != '/')
759 ptr++;
760
761 if (ptr == end)
762 return NULL;
763
764 /* OK, we got the '/' ! */
765 return ptr;
766}
767
Willy Tarreau71241ab2012-12-27 11:30:54 +0100768/* Returns a 302 for a redirectable request that reaches a server working in
769 * in redirect mode. This may only be called just after the stream interface
770 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
771 * follow normal proxy processing. NOTE: this function is designed to support
772 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100774void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775{
776 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200779 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780
781 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100782 trash.len = strlen(HTTP_302);
783 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100785 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100786
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100788 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 return;
790
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100791 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100792 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100793 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
794 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100795 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200797 /* 3: add the request URI. Since it was already forwarded, we need
798 * to temporarily rewind the buffer.
799 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200801 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200802
Willy Tarreauefb453c2008-10-26 20:49:47 +0100803 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200804 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 +0200805
Willy Tarreau9b28e032012-10-12 23:49:43 +0200806 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200807
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808 if (!path)
809 return;
810
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100811 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812 return;
813
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100814 memcpy(trash.str + trash.len, path, len);
815 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100816
817 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
819 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100820 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100821 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
822 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100823 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824
825 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200826 si_shutr(si);
827 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828 si->err_type = SI_ET_NONE;
829 si->err_loc = NULL;
830 si->state = SI_ST_CLO;
831
832 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100833 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834
835 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100836 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837}
838
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100839/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 * that the server side is closed. Note that err_type is actually a
841 * bitmask, where almost only aborts may be cumulated with other
842 * values. We consider that aborted operations are more important
843 * than timeouts or errors due to the fact that nobody else in the
844 * logs might explain incomplete retries. All others should avoid
845 * being cumulated. It should normally not be possible to have multiple
846 * aborts at once, but just in case, the first one in sequence is reported.
847 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100848void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100850 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851
852 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200854 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100859 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200860 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100861 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100862 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200863 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100864 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100865 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200866 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100867 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100868 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200869 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100871 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200872 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100873}
874
Willy Tarreau42250582007-04-01 01:30:43 +0200875extern const char sess_term_cond[8];
876extern const char sess_fin_state[8];
877extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200878struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100879struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100880struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100881
Willy Tarreau117f59e2007-03-04 18:17:17 +0100882/*
883 * Capture headers from message starting at <som> according to header list
884 * <cap_hdr>, and fill the <idx> structure appropriately.
885 */
886void capture_headers(char *som, struct hdr_idx *idx,
887 char **cap, struct cap_hdr *cap_hdr)
888{
889 char *eol, *sol, *col, *sov;
890 int cur_idx;
891 struct cap_hdr *h;
892 int len;
893
894 sol = som + hdr_idx_first_pos(idx);
895 cur_idx = hdr_idx_first_idx(idx);
896
897 while (cur_idx) {
898 eol = sol + idx->v[cur_idx].len;
899
900 col = sol;
901 while (col < eol && *col != ':')
902 col++;
903
904 sov = col + 1;
905 while (sov < eol && http_is_lws[(unsigned char)*sov])
906 sov++;
907
908 for (h = cap_hdr; h; h = h->next) {
909 if ((h->namelen == col - sol) &&
910 (strncasecmp(sol, h->name, h->namelen) == 0)) {
911 if (cap[h->index] == NULL)
912 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200913 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100914
915 if (cap[h->index] == NULL) {
916 Alert("HTTP capture : out of memory.\n");
917 continue;
918 }
919
920 len = eol - sov;
921 if (len > h->len)
922 len = h->len;
923
924 memcpy(cap[h->index], sov, len);
925 cap[h->index][len]=0;
926 }
927 }
928 sol = eol + idx->v[cur_idx].cr + 1;
929 cur_idx = idx->v[cur_idx].next;
930 }
931}
932
933
Willy Tarreau42250582007-04-01 01:30:43 +0200934/* either we find an LF at <ptr> or we jump to <bad>.
935 */
936#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
937
938/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
939 * otherwise to <http_msg_ood> with <state> set to <st>.
940 */
941#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
942 ptr++; \
943 if (likely(ptr < end)) \
944 goto good; \
945 else { \
946 state = (st); \
947 goto http_msg_ood; \
948 } \
949 } while (0)
950
951
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100953 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100954 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
955 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
956 * will give undefined results.
957 * Note that it is upon the caller's responsibility to ensure that ptr < end,
958 * and that msg->sol points to the beginning of the response.
959 * If a complete line is found (which implies that at least one CR or LF is
960 * found before <end>, the updated <ptr> is returned, otherwise NULL is
961 * returned indicating an incomplete line (which does not mean that parts have
962 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
963 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
964 * upon next call.
965 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200966 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
968 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200969 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200971const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100972 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100973 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100974{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200975 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100976
Willy Tarreau8973c702007-01-21 23:58:29 +0100977 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100978 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200979 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100980 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100981 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
982
983 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100984 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100985 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
986 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100987 state = HTTP_MSG_ERROR;
988 break;
989
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200991 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100992 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 goto http_msg_rpcode;
995 }
996 if (likely(HTTP_IS_SPHT(*ptr)))
997 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
998 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100999 state = HTTP_MSG_ERROR;
1000 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001001
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001003 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 if (likely(!HTTP_IS_LWS(*ptr)))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1006
1007 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001008 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1010 }
1011
1012 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001013 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 http_msg_rsp_reason:
1015 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001016 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 msg->sl.st.r_l = 0;
1018 goto http_msg_rpline_eol;
1019
Willy Tarreau8973c702007-01-21 23:58:29 +01001020 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001021 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001023 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 goto http_msg_rpreason;
1025 }
1026 if (likely(HTTP_IS_SPHT(*ptr)))
1027 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1028 /* so it's a CR/LF, so there is no reason phrase */
1029 goto http_msg_rsp_reason;
1030
Willy Tarreau8973c702007-01-21 23:58:29 +01001031 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001032 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001033 if (likely(!HTTP_IS_CRLF(*ptr)))
1034 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001035 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 http_msg_rpline_eol:
1037 /* We have seen the end of line. Note that we do not
1038 * necessarily have the \n yet, but at least we know that we
1039 * have EITHER \r OR \n, otherwise the response would not be
1040 * complete. We can then record the response length and return
1041 * to the caller which will be able to register it.
1042 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001043 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001044 return ptr;
1045
1046#ifdef DEBUG_FULL
1047 default:
1048 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1049 exit(1);
1050#endif
1051 }
1052
1053 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001054 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001055 if (ret_state)
1056 *ret_state = state;
1057 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001058 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001059 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001060}
1061
Willy Tarreau8973c702007-01-21 23:58:29 +01001062/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 * This function parses a request line between <ptr> and <end>, starting with
1064 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1065 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1066 * will give undefined results.
1067 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1068 * and that msg->sol points to the beginning of the request.
1069 * If a complete line is found (which implies that at least one CR or LF is
1070 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1071 * returned indicating an incomplete line (which does not mean that parts have
1072 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1073 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1074 * upon next call.
1075 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001076 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1078 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001079 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001080 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001081const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001082 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001083 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001084{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001085 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001089 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 if (likely(HTTP_IS_TOKEN(*ptr)))
1091 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001094 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 if (likely(HTTP_IS_CRLF(*ptr))) {
1099 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001100 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001102 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001104 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001106 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 msg->sl.rq.v_l = 0;
1108 goto http_msg_rqline_eol;
1109 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001110 state = HTTP_MSG_ERROR;
1111 break;
1112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001114 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001116 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 goto http_msg_rquri;
1118 }
1119 if (likely(HTTP_IS_SPHT(*ptr)))
1120 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1121 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1122 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001124 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001125 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001126 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001127 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001129 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001130 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001131 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1132 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001133
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001135 /* non-ASCII chars are forbidden unless option
1136 * accept-invalid-http-request is enabled in the frontend.
1137 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001138 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001139 if (msg->err_pos < -1)
1140 goto invalid_char;
1141 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001142 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001143 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1144 }
1145
1146 if (likely(HTTP_IS_CRLF(*ptr))) {
1147 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1148 goto http_msg_req09_uri_e;
1149 }
1150
1151 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001152 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001153 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001154 state = HTTP_MSG_ERROR;
1155 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001158 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001160 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 goto http_msg_rqver;
1162 }
1163 if (likely(HTTP_IS_SPHT(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1165 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1166 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001170 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001172
1173 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001174 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001175 http_msg_rqline_eol:
1176 /* We have seen the end of line. Note that we do not
1177 * necessarily have the \n yet, but at least we know that we
1178 * have EITHER \r OR \n, otherwise the request would not be
1179 * complete. We can then record the request length and return
1180 * to the caller which will be able to register it.
1181 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001182 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001183 return ptr;
1184 }
1185
1186 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001187 state = HTTP_MSG_ERROR;
1188 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190#ifdef DEBUG_FULL
1191 default:
1192 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1193 exit(1);
1194#endif
1195 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001198 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 if (ret_state)
1200 *ret_state = state;
1201 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001202 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001203 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001205
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001206/*
1207 * Returns the data from Authorization header. Function may be called more
1208 * than once so data is stored in txn->auth_data. When no header is found
1209 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1210 * searching again for something we are unable to find anyway.
1211 */
1212
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001213char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001214
1215int
1216get_http_auth(struct session *s)
1217{
1218
1219 struct http_txn *txn = &s->txn;
1220 struct chunk auth_method;
1221 struct hdr_ctx ctx;
1222 char *h, *p;
1223 int len;
1224
1225#ifdef DEBUG_AUTH
1226 printf("Auth for session %p: %d\n", s, txn->auth.method);
1227#endif
1228
1229 if (txn->auth.method == HTTP_AUTH_WRONG)
1230 return 0;
1231
1232 if (txn->auth.method)
1233 return 1;
1234
1235 txn->auth.method = HTTP_AUTH_WRONG;
1236
1237 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001238
1239 if (txn->flags & TX_USE_PX_CONN) {
1240 h = "Proxy-Authorization";
1241 len = strlen(h);
1242 } else {
1243 h = "Authorization";
1244 len = strlen(h);
1245 }
1246
Willy Tarreau9b28e032012-10-12 23:49:43 +02001247 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248 return 0;
1249
1250 h = ctx.line + ctx.val;
1251
1252 p = memchr(h, ' ', ctx.vlen);
1253 if (!p || p == h)
1254 return 0;
1255
1256 chunk_initlen(&auth_method, h, 0, p-h);
1257 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1258
1259 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1260
1261 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001262 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001263
1264 if (len < 0)
1265 return 0;
1266
1267
1268 get_http_auth_buff[len] = '\0';
1269
1270 p = strchr(get_http_auth_buff, ':');
1271
1272 if (!p)
1273 return 0;
1274
1275 txn->auth.user = get_http_auth_buff;
1276 *p = '\0';
1277 txn->auth.pass = p+1;
1278
1279 txn->auth.method = HTTP_AUTH_BASIC;
1280 return 1;
1281 }
1282
1283 return 0;
1284}
1285
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8973c702007-01-21 23:58:29 +01001287/*
1288 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001289 * depending on the initial msg->msg_state. The caller is responsible for
1290 * ensuring that the message does not wrap. The function can be preempted
1291 * everywhere when data are missing and recalled at the exact same location
1292 * with no information loss. The message may even be realigned between two
1293 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001294 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001295 * fields. Note that msg->sol will be initialized after completing the first
1296 * state, so that none of the msg pointers has to be initialized prior to the
1297 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001299void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001301 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001303 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001304
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001305 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001306 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001307 ptr = buf->p + msg->next;
1308 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 if (unlikely(ptr >= end))
1311 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001314 /*
1315 * First, states that are specific to the response only.
1316 * We check them first so that request and headers are
1317 * closer to each other (accessed more often).
1318 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001320 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001322 /* we have a start of message, but we have to check
1323 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001324 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001325 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001326 if (unlikely(ptr != buf->p)) {
1327 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001328 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001329 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001330 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 }
Willy Tarreau26927362012-05-18 23:22:52 +02001332 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001333 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 hdr_idx_init(idx);
1335 state = HTTP_MSG_RPVER;
1336 goto http_msg_rpver;
1337 }
1338
1339 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1340 goto http_msg_invalid;
1341
1342 if (unlikely(*ptr == '\n'))
1343 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1344 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1345 /* stop here */
1346
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001348 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 EXPECT_LF_HERE(ptr, http_msg_invalid);
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1351 /* stop here */
1352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001354 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 case HTTP_MSG_RPVER_SP:
1356 case HTTP_MSG_RPCODE:
1357 case HTTP_MSG_RPCODE_SP:
1358 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001359 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001360 state, ptr, end,
1361 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 if (unlikely(!ptr))
1363 return;
1364
1365 /* we have a full response and we know that we have either a CR
1366 * or an LF at <ptr>.
1367 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1369
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001370 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 if (likely(*ptr == '\r'))
1372 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1373 goto http_msg_rpline_end;
1374
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001376 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 /* msg->sol must point to the first of CR or LF. */
1378 EXPECT_LF_HERE(ptr, http_msg_invalid);
1379 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1380 /* stop here */
1381
1382 /*
1383 * Second, states that are specific to the request only
1384 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001386 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001388 /* we have a start of message, but we have to check
1389 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001390 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001391 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001392 if (likely(ptr != buf->p)) {
1393 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001394 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001395 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001396 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 }
Willy Tarreau26927362012-05-18 23:22:52 +02001398 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001399 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 state = HTTP_MSG_RQMETH;
1401 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1405 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 if (unlikely(*ptr == '\n'))
1408 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1409 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001413 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 EXPECT_LF_HERE(ptr, http_msg_invalid);
1415 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001419 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 case HTTP_MSG_RQMETH_SP:
1421 case HTTP_MSG_RQURI:
1422 case HTTP_MSG_RQURI_SP:
1423 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001424 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001425 state, ptr, end,
1426 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 if (unlikely(!ptr))
1428 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 /* we have a full request and we know that we have either a CR
1431 * or an LF at <ptr>.
1432 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001435 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 if (likely(*ptr == '\r'))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 /* check for HTTP/0.9 request : no version information available.
1443 * msg->sol must point to the first of CR or LF.
1444 */
1445 if (unlikely(msg->sl.rq.v_l == 0))
1446 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 EXPECT_LF_HERE(ptr, http_msg_invalid);
1449 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001451
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 /*
1453 * Common states below
1454 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001457 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 if (likely(!HTTP_IS_CRLF(*ptr))) {
1459 goto http_msg_hdr_name;
1460 }
1461
1462 if (likely(*ptr == '\r'))
1463 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1464 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 /* assumes msg->sol points to the first char */
1469 if (likely(HTTP_IS_TOKEN(*ptr)))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001472 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001475 if (likely(msg->err_pos < -1) || *ptr == '\n')
1476 goto http_msg_invalid;
1477
1478 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001479 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001480
1481 /* and we still accept this non-token character */
1482 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001485 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001486 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 if (likely(HTTP_IS_SPHT(*ptr)))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001491 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(!HTTP_IS_CRLF(*ptr))) {
1494 goto http_msg_hdr_val;
1495 }
1496
1497 if (likely(*ptr == '\r'))
1498 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1499 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001502 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 EXPECT_LF_HERE(ptr, http_msg_invalid);
1504 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001507 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(HTTP_IS_SPHT(*ptr))) {
1509 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001510 for (; buf->p + msg->sov < ptr; msg->sov++)
1511 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 goto http_msg_hdr_l1_sp;
1513 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001514 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001515 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 goto http_msg_complete_header;
1517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001519 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001520 /* assumes msg->sol points to the first char, and msg->sov
1521 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 */
1523 if (likely(!HTTP_IS_CRLF(*ptr)))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001526 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 /* Note: we could also copy eol into ->eoh so that we have the
1528 * real header end in case it ends with lots of LWS, but is this
1529 * really needed ?
1530 */
1531 if (likely(*ptr == '\r'))
1532 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1533 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 EXPECT_LF_HERE(ptr, http_msg_invalid);
1538 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001541 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1543 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 for (; buf->p + msg->eol < ptr; msg->eol++)
1545 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 goto http_msg_hdr_val;
1547 }
1548 http_msg_complete_header:
1549 /*
1550 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001551 * Assumes msg->sol points to the first char, msg->sov points
1552 * to the first character of the value and msg->eol to the
1553 * first CR or LF so we know how the line ends. We insert last
1554 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001556 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 idx, idx->tail) < 0))
1558 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001559
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(!HTTP_IS_CRLF(*ptr))) {
1562 goto http_msg_hdr_name;
1563 }
1564
1565 if (likely(*ptr == '\r'))
1566 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1567 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 /* Assumes msg->sol points to the first of either CR or LF */
1572 EXPECT_LF_HERE(ptr, http_msg_invalid);
1573 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001574 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001575 msg->eoh = msg->sol;
1576 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001579
1580 case HTTP_MSG_ERROR:
1581 /* this may only happen if we call http_msg_analyser() twice with an error */
1582 break;
1583
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584#ifdef DEBUG_FULL
1585 default:
1586 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1587 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001588#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 }
1590 http_msg_ood:
1591 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001592 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001593 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 http_msg_invalid:
1597 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001598 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001599 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 return;
1601}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001602
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1604 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1605 * nothing is done and 1 is returned.
1606 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001607static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608{
1609 int delta;
1610 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612
1613 if (msg->sl.rq.v_l != 0)
1614 return 1;
1615
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 delta = 0;
1618
1619 if (msg->sl.rq.u_l == 0) {
1620 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001621 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001622 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001623 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001624 }
1625 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001626 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001627 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001628 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001629 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001630 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001631 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001632 NULL, NULL);
1633 if (unlikely(!cur_end))
1634 return 0;
1635
1636 /* we have a full HTTP/1.0 request now and we know that
1637 * we have either a CR or an LF at <ptr>.
1638 */
1639 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1640 return 1;
1641}
1642
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001643/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001644 * and "keep-alive" values. If we already know that some headers may safely
1645 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001646 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1647 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001648 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1650 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1651 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001652 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001654void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001655{
Willy Tarreau5b154472009-12-21 20:11:07 +01001656 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001657 const char *hdr_val = "Connection";
1658 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001659
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001661 return;
1662
Willy Tarreau88d349d2010-01-25 12:15:43 +01001663 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1664 hdr_val = "Proxy-Connection";
1665 hdr_len = 16;
1666 }
1667
Willy Tarreau5b154472009-12-21 20:11:07 +01001668 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001669 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001670 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1672 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001673 if (to_del & 2)
1674 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 else
1676 txn->flags |= TX_CON_KAL_SET;
1677 }
1678 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1679 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001680 if (to_del & 1)
1681 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001682 else
1683 txn->flags |= TX_CON_CLO_SET;
1684 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001685 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1686 txn->flags |= TX_HDR_CONN_UPG;
1687 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001688 }
1689
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690 txn->flags |= TX_HDR_CONN_PRS;
1691 return;
1692}
Willy Tarreau5b154472009-12-21 20:11:07 +01001693
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001694/* Apply desired changes on the Connection: header. Values may be removed and/or
1695 * added depending on the <wanted> flags, which are exclusively composed of
1696 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1697 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1698 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001699void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700{
1701 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001702 const char *hdr_val = "Connection";
1703 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001704
1705 ctx.idx = 0;
1706
Willy Tarreau88d349d2010-01-25 12:15:43 +01001707
1708 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1709 hdr_val = "Proxy-Connection";
1710 hdr_len = 16;
1711 }
1712
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001714 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001715 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1716 if (wanted & TX_CON_KAL_SET)
1717 txn->flags |= TX_CON_KAL_SET;
1718 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001719 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001720 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001721 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1722 if (wanted & TX_CON_CLO_SET)
1723 txn->flags |= TX_CON_CLO_SET;
1724 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001726 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001727 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001728
1729 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1730 return;
1731
1732 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1733 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001734 hdr_val = "Connection: close";
1735 hdr_len = 17;
1736 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1737 hdr_val = "Proxy-Connection: close";
1738 hdr_len = 23;
1739 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001740 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001741 }
1742
1743 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1744 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001745 hdr_val = "Connection: keep-alive";
1746 hdr_len = 22;
1747 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1748 hdr_val = "Proxy-Connection: keep-alive";
1749 hdr_len = 28;
1750 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001751 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001752 }
1753 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001754}
1755
Willy Tarreaua458b672012-03-05 11:17:50 +01001756/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001757 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001758 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001759 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001760 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001761 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001762static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001763{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001764 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001765 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001766 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 const char *end = buf->data + buf->size;
1768 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001769 unsigned int chunk = 0;
1770
1771 /* The chunk size is in the following form, though we are only
1772 * interested in the size and CRLF :
1773 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1774 */
1775 while (1) {
1776 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001777 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001778 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 if (c < 0) /* not a hex digit anymore */
1781 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001782 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001783 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001784 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001785 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001786 chunk = (chunk << 4) + c;
1787 }
1788
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001790 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001791 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792
1793 while (http_is_spht[(unsigned char)*ptr]) {
1794 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001795 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001796 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001797 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001798 }
1799
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 /* Up to there, we know that at least one byte is present at *ptr. Check
1801 * for the end of chunk size.
1802 */
1803 while (1) {
1804 if (likely(HTTP_IS_CRLF(*ptr))) {
1805 /* we now have a CR or an LF at ptr */
1806 if (likely(*ptr == '\r')) {
1807 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001808 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001809 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810 return 0;
1811 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001812
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001814 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001815 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001816 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 /* done */
1818 break;
1819 }
1820 else if (*ptr == ';') {
1821 /* chunk extension, ends at next CRLF */
1822 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001823 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001824 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826
1827 while (!HTTP_IS_CRLF(*ptr)) {
1828 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001829 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001830 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001831 return 0;
1832 }
1833 /* we have a CRLF now, loop above */
1834 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 }
1839
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001841 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001843 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001844 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001845 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001846 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001847 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001848 msg->chunk_len = chunk;
1849 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001850 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001851 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001852 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001853 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001854 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001855}
1856
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001857/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001858 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 * the trailers is found, it is automatically scheduled to be forwarded,
1860 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1861 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001862 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001863 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001864 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001865 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1866 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001867 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001868 * matches the length of trailers already parsed and not forwarded. It is also
1869 * important to note that this function is designed to be able to parse wrapped
1870 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001872static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001875
Willy Tarreaua458b672012-03-05 11:17:50 +01001876 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001877 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001878 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001879 const char *ptr = b_ptr(buf, msg->next);
1880 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001881 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882
1883 /* scan current line and stop at LF or CRLF */
1884 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001885 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 return 0;
1887
1888 if (*ptr == '\n') {
1889 if (!p1)
1890 p1 = ptr;
1891 p2 = ptr;
1892 break;
1893 }
1894
1895 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001896 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001897 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001899 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 p1 = ptr;
1901 }
1902
1903 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 if (ptr >= buf->data + buf->size)
1905 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 }
1907
1908 /* after LF; point to beginning of next line */
1909 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001910 if (p2 >= buf->data + buf->size)
1911 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001914 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001916
1917 /* schedule this line for forwarding */
1918 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 if (msg->sov >= buf->size)
1920 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001923 /* LF/CRLF at beginning of line => end of trailers at p2.
1924 * Everything was scheduled for forwarding, there's nothing
1925 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001926 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001927 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 msg->msg_state = HTTP_MSG_DONE;
1929 return 1;
1930 }
1931 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001932 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 }
1934}
1935
Willy Tarreau54d23df2012-10-25 19:04:45 +02001936/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001938 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001939 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1941 * not enough data are available, the function does not change anything and
1942 * returns zero. If a parse error is encountered, the function returns < 0 and
1943 * does not change anything. Note: this function is designed to parse wrapped
1944 * CRLF at the end of the buffer.
1945 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001946static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001948 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001949 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 int bytes;
1951
1952 /* NB: we'll check data availabilty at the end. It's not a
1953 * problem because whatever we match first will be checked
1954 * against the correct length.
1955 */
1956 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001957 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958 if (*ptr == '\r') {
1959 bytes++;
1960 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001961 if (ptr >= buf->data + buf->size)
1962 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 }
1964
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001965 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001966 return 0;
1967
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001968 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001971 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972
1973 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02001974 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001975 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001976 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1977 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001978 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1980 return 1;
1981}
Willy Tarreau5b154472009-12-21 20:11:07 +01001982
William Lallemand82fe75c2012-10-23 10:25:10 +02001983
1984/*
1985 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001986 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001987int select_compression_request_header(struct session *s, struct buffer *req)
1988{
1989 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001990 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001991 struct hdr_ctx ctx;
1992 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001993 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001994
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001995 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1996 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001997 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1998 */
1999 ctx.idx = 0;
2000 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2001 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002002 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2003 (ctx.vlen < 31 ||
2004 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2005 ctx.line[ctx.val + 30] < '6' ||
2006 (ctx.line[ctx.val + 30] == '6' &&
2007 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2008 s->comp_algo = NULL;
2009 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002010 }
2011
William Lallemand82fe75c2012-10-23 10:25:10 +02002012 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002013 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 +02002014 ctx.idx = 0;
2015 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002016 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002017 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2018 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002019
2020 /* remove all occurrences of the header when "compression offload" is set */
2021
2022 if ((s->be->comp && s->be->comp->offload) ||
2023 (s->fe->comp && s->fe->comp->offload)) {
2024 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2025 ctx.idx = 0;
2026 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2027 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2028 }
2029 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002030 return 1;
2031 }
2032 }
2033 }
2034 }
2035
2036 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002037 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2038 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002039 if (comp_algo->add_data == identity_add_data) {
2040 s->comp_algo = comp_algo;
2041 return 1;
2042 }
2043 }
2044 }
2045
2046 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002047 return 0;
2048}
2049
2050/*
2051 * Selects a comression algorithm depending of the server response.
2052 */
2053int select_compression_response_header(struct session *s, struct buffer *res)
2054{
2055 struct http_txn *txn = &s->txn;
2056 struct http_msg *msg = &txn->rsp;
2057 struct hdr_ctx ctx;
2058 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002059
2060 /* no common compression algorithm was found in request header */
2061 if (s->comp_algo == NULL)
2062 goto fail;
2063
2064 /* HTTP < 1.1 should not be compressed */
2065 if (!(msg->flags & HTTP_MSGF_VER_11))
2066 goto fail;
2067
William Lallemandd3002612012-11-26 14:34:47 +01002068 /* 200 only */
2069 if (txn->status != 200)
2070 goto fail;
2071
William Lallemand82fe75c2012-10-23 10:25:10 +02002072 /* Content-Length is null */
2073 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2074 goto fail;
2075
2076 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002077 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002078 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2079 goto fail;
2080
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002081 /* no compression when Cache-Control: no-transform is present in the message */
2082 ctx.idx = 0;
2083 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2084 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2085 goto fail;
2086 }
2087
William Lallemand82fe75c2012-10-23 10:25:10 +02002088 comp_type = NULL;
2089
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002090 /* we don't want to compress multipart content-types, nor content-types that are
2091 * not listed in the "compression type" directive if any. If no content-type was
2092 * found but configuration requires one, we don't compress either. Backend has
2093 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002094 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002095 ctx.idx = 0;
2096 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2097 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2098 goto fail;
2099
2100 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2101 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002102 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002103 if (ctx.vlen >= comp_type->name_len &&
2104 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002105 /* this Content-Type should be compressed */
2106 break;
2107 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002108 /* this Content-Type should not be compressed */
2109 if (comp_type == NULL)
2110 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002111 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002112 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002113 else { /* no content-type header */
2114 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2115 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002116 }
2117
William Lallemandd85f9172012-11-09 17:05:39 +01002118 /* limit compression rate */
2119 if (global.comp_rate_lim > 0)
2120 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2121 goto fail;
2122
William Lallemand072a2bf2012-11-20 17:01:01 +01002123 /* limit cpu usage */
2124 if (idle_pct < compress_min_idle)
2125 goto fail;
2126
William Lallemand4c49fae2012-11-07 15:00:23 +01002127 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002128 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002129 goto fail;
2130
William Lallemandec3e3892012-11-12 17:02:18 +01002131 s->flags |= SN_COMP_READY;
2132
William Lallemand82fe75c2012-10-23 10:25:10 +02002133 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002134 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002135 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2136 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2137
2138 /* add Transfer-Encoding header */
2139 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2140 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2141
2142 /*
2143 * Add Content-Encoding header when it's not identity encoding.
2144 * RFC 2616 : Identity encoding: This content-coding is used only in the
2145 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2146 * header.
2147 */
2148 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002149 trash.len = 18;
2150 memcpy(trash.str, "Content-Encoding: ", trash.len);
2151 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2152 trash.len += s->comp_algo->name_len;
2153 trash.str[trash.len] = '\0';
2154 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002155 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002156 return 1;
2157
2158fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002159 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002160 return 0;
2161}
2162
2163
Willy Tarreaud787e662009-07-07 10:14:51 +02002164/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2165 * processing can continue on next analysers, or zero if it either needs more
2166 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2167 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2168 * when it has nothing left to do, and may remove any analyser when it wants to
2169 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002170 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002171int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002172{
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 /*
2174 * We will parse the partial (or complete) lines.
2175 * We will check the request syntax, and also join multi-line
2176 * headers. An index of all the lines will be elaborated while
2177 * parsing.
2178 *
2179 * For the parsing, we use a 28 states FSM.
2180 *
2181 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002182 * req->buf->p = beginning of request
2183 * req->buf->p + msg->eoh = end of processed headers / start of current one
2184 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002185 * msg->eol = end of current header or line (LF or CRLF)
2186 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002187 *
2188 * At end of parsing, we may perform a capture of the error (if any), and
2189 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2190 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2191 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002193
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002195 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002196 struct http_txn *txn = &s->txn;
2197 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002198 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002199
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002200 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 +01002201 now_ms, __FUNCTION__,
2202 s,
2203 req,
2204 req->rex, req->wex,
2205 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002206 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002207 req->analysers);
2208
Willy Tarreau52a0c602009-08-16 22:45:38 +02002209 /* we're speaking HTTP here, so let's speak HTTP to the client */
2210 s->srv_error = http_return_srv_error;
2211
Willy Tarreau83e3af02009-12-28 17:39:57 +01002212 /* There's a protected area at the end of the buffer for rewriting
2213 * purposes. We don't want to start to parse the request if the
2214 * protected area is affected, because we may have to move processed
2215 * data later, which is much more complicated.
2216 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002217 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002218 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002219 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002220 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2221 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2222 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002223 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002224 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002225 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002226 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002227 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002228 return 0;
2229 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002230 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2231 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2232 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002233 }
2234
Willy Tarreau065e8332010-01-08 00:30:20 +01002235 /* Note that we have the same problem with the response ; we
2236 * may want to send a redirect, error or anything which requires
2237 * some spare space. So we'll ensure that we have at least
2238 * maxrewrite bytes available in the response buffer before
2239 * processing that one. This will only affect pipelined
2240 * keep-alive requests.
2241 */
2242 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002243 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002244 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2245 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2246 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002247 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002248 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002249 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002250 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002251 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002252 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002253 return 0;
2254 }
2255 }
2256
Willy Tarreau9b28e032012-10-12 23:49:43 +02002257 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002258 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002259 }
2260
Willy Tarreau59234e92008-11-30 23:51:27 +01002261 /* 1: we might have to print this header in debug mode */
2262 if (unlikely((global.mode & MODE_DEBUG) &&
2263 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002264 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002266
Willy Tarreau9b28e032012-10-12 23:49:43 +02002267 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002268 /* this is a bit complex : in case of error on the request line,
2269 * we know that rq.l is still zero, so we display only the part
2270 * up to the end of the line (truncated by debug_hdr).
2271 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002272 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002274
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 sol += hdr_idx_first_pos(&txn->hdr_idx);
2276 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002277
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 while (cur_idx) {
2279 eol = sol + txn->hdr_idx.v[cur_idx].len;
2280 debug_hdr("clihdr", s, sol, eol);
2281 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2282 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002283 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002284 }
2285
Willy Tarreau58f10d72006-12-04 02:26:12 +01002286
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 /*
2288 * Now we quickly check if we have found a full valid request.
2289 * If not so, we check the FD and buffer states before leaving.
2290 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002291 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002292 * requests are checked first. When waiting for a second request
2293 * on a keep-alive session, if we encounter and error, close, t/o,
2294 * we note the error in the session flags but don't set any state.
2295 * Since the error will be noted there, it will not be counted by
2296 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002297 * Last, we may increase some tracked counters' http request errors on
2298 * the cases that are deliberately the client's fault. For instance,
2299 * a timeout or connection reset is not counted as an error. However
2300 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002301 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002302
Willy Tarreau655dce92009-11-08 13:10:58 +01002303 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002304 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002305 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002306 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002307 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002308 session_inc_http_req_ctr(s);
2309 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002310 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002312 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002313
Willy Tarreau59234e92008-11-30 23:51:27 +01002314 /* 1: Since we are in header mode, if there's no space
2315 * left for headers, we won't be able to free more
2316 * later, so the session will never terminate. We
2317 * must terminate it now.
2318 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002319 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 /* FIXME: check if URI is set and return Status
2321 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002322 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002323 session_inc_http_req_ctr(s);
2324 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002325 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002326 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002327 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002328 goto return_bad_req;
2329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002332 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002333 if (!(s->flags & SN_ERR_MASK))
2334 s->flags |= SN_ERR_CLICL;
2335
Willy Tarreaufcffa692010-01-10 14:21:19 +01002336 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002337 goto failed_keep_alive;
2338
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002340 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002341 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002342 session_inc_http_err_ctr(s);
2343 }
2344
Willy Tarreaudc979f22012-12-04 10:39:01 +01002345 txn->status = 400;
2346 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 msg->msg_state = HTTP_MSG_ERROR;
2348 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002349
Willy Tarreauda7ff642010-06-23 11:44:09 +02002350 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002351 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002352 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002353 if (s->listener->counters)
2354 s->listener->counters->failed_req++;
2355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 if (!(s->flags & SN_FINST_MASK))
2357 s->flags |= SN_FINST_R;
2358 return 0;
2359 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002362 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002363 if (!(s->flags & SN_ERR_MASK))
2364 s->flags |= SN_ERR_CLITO;
2365
Willy Tarreaufcffa692010-01-10 14:21:19 +01002366 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002367 goto failed_keep_alive;
2368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002370 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002371 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002372 session_inc_http_err_ctr(s);
2373 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002375 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 msg->msg_state = HTTP_MSG_ERROR;
2377 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002378
Willy Tarreauda7ff642010-06-23 11:44:09 +02002379 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002380 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002381 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002382 if (s->listener->counters)
2383 s->listener->counters->failed_req++;
2384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 if (!(s->flags & SN_FINST_MASK))
2386 s->flags |= SN_FINST_R;
2387 return 0;
2388 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002391 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002392 if (!(s->flags & SN_ERR_MASK))
2393 s->flags |= SN_ERR_CLICL;
2394
Willy Tarreaufcffa692010-01-10 14:21:19 +01002395 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002396 goto failed_keep_alive;
2397
Willy Tarreau4076a152009-04-02 15:18:36 +02002398 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002399 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002401 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 msg->msg_state = HTTP_MSG_ERROR;
2403 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002404
Willy Tarreauda7ff642010-06-23 11:44:09 +02002405 session_inc_http_err_ctr(s);
2406 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002407 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002408 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002409 if (s->listener->counters)
2410 s->listener->counters->failed_req++;
2411
Willy Tarreau59234e92008-11-30 23:51:27 +01002412 if (!(s->flags & SN_FINST_MASK))
2413 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002414 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002415 }
2416
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002417 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002418 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2419 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002420#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002421 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002422 /* We need more data, we have to re-enable quick-ack in case we
2423 * previously disabled it, otherwise we might cause the client
2424 * to delay next data.
2425 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002426 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002427 }
2428#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002429
Willy Tarreaufcffa692010-01-10 14:21:19 +01002430 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2431 /* If the client starts to talk, let's fall back to
2432 * request timeout processing.
2433 */
2434 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002435 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002436 }
2437
Willy Tarreau59234e92008-11-30 23:51:27 +01002438 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002439 if (!tick_isset(req->analyse_exp)) {
2440 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2441 (txn->flags & TX_WAIT_NEXT_RQ) &&
2442 tick_isset(s->be->timeout.httpka))
2443 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2444 else
2445 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002447
Willy Tarreau59234e92008-11-30 23:51:27 +01002448 /* we're not ready yet */
2449 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002450
2451 failed_keep_alive:
2452 /* Here we process low-level errors for keep-alive requests. In
2453 * short, if the request is not the first one and it experiences
2454 * a timeout, read error or shutdown, we just silently close so
2455 * that the client can try again.
2456 */
2457 txn->status = 0;
2458 msg->msg_state = HTTP_MSG_RQBEFORE;
2459 req->analysers = 0;
2460 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002461 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002462 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002463 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002464 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002465
Willy Tarreaud787e662009-07-07 10:14:51 +02002466 /* OK now we have a complete HTTP request with indexed headers. Let's
2467 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002468 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002469 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002470 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002471 * byte after the last LF. msg->sov points to the first byte of data.
2472 * msg->eol cannot be trusted because it may have been left uninitialized
2473 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002474 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002475
Willy Tarreauda7ff642010-06-23 11:44:09 +02002476 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002477 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2478
Willy Tarreaub16a5742010-01-10 14:46:16 +01002479 if (txn->flags & TX_WAIT_NEXT_RQ) {
2480 /* kill the pending keep-alive timeout */
2481 txn->flags &= ~TX_WAIT_NEXT_RQ;
2482 req->analyse_exp = TICK_ETERNITY;
2483 }
2484
2485
Willy Tarreaud787e662009-07-07 10:14:51 +02002486 /* Maybe we found in invalid header name while we were configured not
2487 * to block on that, so we have to capture it now.
2488 */
2489 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002490 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002491
Willy Tarreau59234e92008-11-30 23:51:27 +01002492 /*
2493 * 1: identify the method
2494 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002495 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002496
2497 /* we can make use of server redirect on GET and HEAD */
2498 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2499 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002500
Willy Tarreau59234e92008-11-30 23:51:27 +01002501 /*
2502 * 2: check if the URI matches the monitor_uri.
2503 * We have to do this for every request which gets in, because
2504 * the monitor-uri is defined by the frontend.
2505 */
2506 if (unlikely((s->fe->monitor_uri_len != 0) &&
2507 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002508 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 s->fe->monitor_uri,
2510 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002511 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002512 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002513 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002515
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002517 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002520 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002521 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 ret = acl_pass(ret);
2524 if (cond->pol == ACL_COND_UNLESS)
2525 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 if (ret) {
2528 /* we fail this request, let's return 503 service unavail */
2529 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002530 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002532 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002533 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002534
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 /* nothing to fail, let's reply normaly */
2536 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002537 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002538 goto return_prx_cond;
2539 }
2540
2541 /*
2542 * 3: Maybe we have to copy the original REQURI for the logs ?
2543 * Note: we cannot log anymore if the request has been
2544 * classified as invalid.
2545 */
2546 if (unlikely(s->logs.logwait & LW_REQ)) {
2547 /* we have a complete HTTP request that we must log */
2548 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2549 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002550
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 if (urilen >= REQURI_LEN)
2552 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002553 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002555
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002556 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 s->do_log(s);
2558 } else {
2559 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002560 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 }
Willy Tarreau06619262006-12-17 08:37:22 +01002562
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002563 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
2564 s->unique_id = pool_alloc2(pool2_uniqueid);
William Lallemanda73203e2012-03-12 12:48:57 +01002565
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002567 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002568 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002569
Willy Tarreau5b154472009-12-21 20:11:07 +01002570 /* ... and check if the request is HTTP/1.1 or above */
2571 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002572 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2573 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2574 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002575 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002576
2577 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002578 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 +01002579
Willy Tarreau88d349d2010-01-25 12:15:43 +01002580 /* if the frontend has "option http-use-proxy-header", we'll check if
2581 * we have what looks like a proxied connection instead of a connection,
2582 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2583 * Note that this is *not* RFC-compliant, however browsers and proxies
2584 * happen to do that despite being non-standard :-(
2585 * We consider that a request not beginning with either '/' or '*' is
2586 * a proxied connection, which covers both "scheme://location" and
2587 * CONNECT ip:port.
2588 */
2589 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002590 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002591 txn->flags |= TX_USE_PX_CONN;
2592
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002593 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002594 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002595
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002597 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002598 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002600
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002601 /* 6: determine the transfer-length.
2602 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2603 * the presence of a message-body in a REQUEST and its transfer length
2604 * must be determined that way (in order of precedence) :
2605 * 1. The presence of a message-body in a request is signaled by the
2606 * inclusion of a Content-Length or Transfer-Encoding header field
2607 * in the request's header fields. When a request message contains
2608 * both a message-body of non-zero length and a method that does
2609 * not define any semantics for that request message-body, then an
2610 * origin server SHOULD either ignore the message-body or respond
2611 * with an appropriate error message (e.g., 413). A proxy or
2612 * gateway, when presented the same request, SHOULD either forward
2613 * the request inbound with the message- body or ignore the
2614 * message-body when determining a response.
2615 *
2616 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2617 * and the "chunked" transfer-coding (Section 6.2) is used, the
2618 * transfer-length is defined by the use of this transfer-coding.
2619 * If a Transfer-Encoding header field is present and the "chunked"
2620 * transfer-coding is not present, the transfer-length is defined
2621 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002622 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002623 * 3. If a Content-Length header field is present, its decimal value in
2624 * OCTETs represents both the entity-length and the transfer-length.
2625 * If a message is received with both a Transfer-Encoding header
2626 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002627 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002628 * 4. By the server closing the connection. (Closing the connection
2629 * cannot be used to indicate the end of a request body, since that
2630 * would leave no possibility for the server to send back a response.)
2631 *
2632 * Whenever a transfer-coding is applied to a message-body, the set of
2633 * transfer-codings MUST include "chunked", unless the message indicates
2634 * it is terminated by closing the connection. When the "chunked"
2635 * transfer-coding is used, it MUST be the last transfer-coding applied
2636 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002637 */
2638
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002639 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002640 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002641 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002642 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002643 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002644 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002645 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2646 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002647 /* bad transfer-encoding (chunked followed by something else) */
2648 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002649 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002650 break;
2651 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002652 }
2653
Willy Tarreau32b47f42009-10-18 20:55:02 +02002654 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002655 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002656 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002657 signed long long cl;
2658
Willy Tarreauad14f752011-09-02 20:33:27 +02002659 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002660 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002661 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002662 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002663
Willy Tarreauad14f752011-09-02 20:33:27 +02002664 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002665 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002666 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002667 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002668
Willy Tarreauad14f752011-09-02 20:33:27 +02002669 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002670 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002671 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002672 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002673
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002674 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002675 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002676 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002677 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002678
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002679 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002680 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002681 }
2682
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002683 /* bodyless requests have a known length */
2684 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002685 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002686
Willy Tarreaud787e662009-07-07 10:14:51 +02002687 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002688 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002689 req->analyse_exp = TICK_ETERNITY;
2690 return 1;
2691
2692 return_bad_req:
2693 /* We centralize bad requests processing here */
2694 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2695 /* we detected a parsing error. We want to archive this request
2696 * in the dedicated proxy area for later troubleshooting.
2697 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002698 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002699 }
2700
2701 txn->req.msg_state = HTTP_MSG_ERROR;
2702 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002703 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002704
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002705 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002706 if (s->listener->counters)
2707 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002708
2709 return_prx_cond:
2710 if (!(s->flags & SN_ERR_MASK))
2711 s->flags |= SN_ERR_PRXCOND;
2712 if (!(s->flags & SN_FINST_MASK))
2713 s->flags |= SN_FINST_R;
2714
2715 req->analysers = 0;
2716 req->analyse_exp = TICK_ETERNITY;
2717 return 0;
2718}
2719
Cyril Bonté70be45d2010-10-12 00:14:35 +02002720/* We reached the stats page through a POST request.
2721 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002722 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002723 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002724int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002725{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002726 struct proxy *px = NULL;
2727 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002728
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002729 char key[LINESIZE];
2730 int action = ST_ADM_ACTION_NONE;
2731 int reprocess = 0;
2732
2733 int total_servers = 0;
2734 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002735
2736 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002737 char *st_cur_param = NULL;
2738 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739
Willy Tarreau9b28e032012-10-12 23:49:43 +02002740 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002741 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002742
2743 cur_param = next_param = end_params;
2744
Willy Tarreau9b28e032012-10-12 23:49:43 +02002745 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002746 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002747 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002748 return 1;
2749 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002750 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002751 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002752 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002753 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002754 }
2755
2756 *end_params = '\0';
2757
Willy Tarreau295a8372011-03-10 11:25:07 +01002758 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002759
2760 /*
2761 * Parse the parameters in reverse order to only store the last value.
2762 * From the html form, the backend and the action are at the end.
2763 */
2764 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002765 char *value;
2766 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002767
2768 cur_param--;
2769 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002770 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002771 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002772 poffset = (cur_param != first_param ? 1 : 0);
2773 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2774 if ((plen > 0) && (plen <= sizeof(key))) {
2775 strncpy(key, cur_param + poffset, plen);
2776 key[plen - 1] = '\0';
2777 } else {
2778 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2779 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002780 }
2781
2782 /* Parse the value */
2783 value = key;
2784 while (*value != '\0' && *value != '=') {
2785 value++;
2786 }
2787 if (*value == '=') {
2788 /* Ok, a value is found, we can mark the end of the key */
2789 *value++ = '\0';
2790 }
2791
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002792 if (!url_decode(key) || !url_decode(value))
2793 break;
2794
Cyril Bonté70be45d2010-10-12 00:14:35 +02002795 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002796 if (!px && (strcmp(key, "b") == 0)) {
2797 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2798 /* the backend name is unknown or ambiguous (duplicate names) */
2799 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2800 goto out;
2801 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002802 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002803 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002804 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002805 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002806 }
2807 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002808 action = ST_ADM_ACTION_ENABLE;
2809 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002810 else if (strcmp(value, "stop") == 0) {
2811 action = ST_ADM_ACTION_STOP;
2812 }
2813 else if (strcmp(value, "start") == 0) {
2814 action = ST_ADM_ACTION_START;
2815 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002816 else if (strcmp(value, "shutdown") == 0) {
2817 action = ST_ADM_ACTION_SHUTDOWN;
2818 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002819 else {
2820 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2821 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002822 }
2823 }
2824 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002825 if (!(px && action)) {
2826 /*
2827 * Indicates that we'll need to reprocess the parameters
2828 * as soon as backend and action are known
2829 */
2830 if (!reprocess) {
2831 st_cur_param = cur_param;
2832 st_next_param = next_param;
2833 }
2834 reprocess = 1;
2835 }
2836 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002837 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002838 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002839 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002840 /* Not already in maintenance, we can change the server state */
2841 sv->state |= SRV_MAINTAIN;
2842 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002843 altered_servers++;
2844 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002845 }
2846 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002847 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002848 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002849 /* Already in maintenance, we can change the server state */
2850 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002851 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002852 altered_servers++;
2853 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002854 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002855 break;
2856 case ST_ADM_ACTION_STOP:
2857 case ST_ADM_ACTION_START:
2858 if (action == ST_ADM_ACTION_START)
2859 sv->uweight = sv->iweight;
2860 else
2861 sv->uweight = 0;
2862
2863 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2864 /* we must take care of not pushing the server to full throttle during slow starts */
2865 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2866 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2867 else
2868 sv->eweight = BE_WEIGHT_SCALE;
2869 sv->eweight *= sv->uweight;
2870 } else {
2871 sv->eweight = sv->uweight;
2872 }
2873
2874 /* static LB algorithms are a bit harder to update */
2875 if (px->lbprm.update_server_eweight)
2876 px->lbprm.update_server_eweight(sv);
2877 else if (sv->eweight) {
2878 if (px->lbprm.set_server_status_up)
2879 px->lbprm.set_server_status_up(sv);
2880 }
2881 else {
2882 if (px->lbprm.set_server_status_down)
2883 px->lbprm.set_server_status_down(sv);
2884 }
2885 altered_servers++;
2886 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002887 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002888 case ST_ADM_ACTION_SHUTDOWN:
2889 if (px->state != PR_STSTOPPED) {
2890 struct session *sess, *sess_bck;
2891
2892 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2893 if (sess->srv_conn == sv)
2894 session_shutdown(sess, SN_ERR_KILLED);
2895
2896 altered_servers++;
2897 total_servers++;
2898 }
2899 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002900 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002901 } else {
2902 /* the server name is unknown or ambiguous (duplicate names) */
2903 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002904 }
2905 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002906 if (reprocess && px && action) {
2907 /* Now, we know the backend and the action chosen by the user.
2908 * We can safely restart from the first server parameter
2909 * to reprocess them
2910 */
2911 cur_param = st_cur_param;
2912 next_param = st_next_param;
2913 reprocess = 0;
2914 goto reprocess_servers;
2915 }
2916
Cyril Bonté70be45d2010-10-12 00:14:35 +02002917 next_param = cur_param;
2918 }
2919 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002920
2921 if (total_servers == 0) {
2922 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2923 }
2924 else if (altered_servers == 0) {
2925 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2926 }
2927 else if (altered_servers == total_servers) {
2928 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2929 }
2930 else {
2931 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2932 }
2933 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002934 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002935}
2936
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002937/* This function checks whether we need to enable a POST analyser to parse a
2938 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002939 * if it needs to come back again, otherwise non-zero if it finishes. In the
2940 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002941 */
2942int http_handle_stats(struct session *s, struct channel *req)
2943{
2944 struct stats_admin_rule *stats_admin_rule;
2945 struct stream_interface *si = s->rep->prod;
2946 struct http_txn *txn = &s->txn;
2947 struct http_msg *msg = &txn->req;
2948 struct uri_auth *uri = s->be->uri_auth;
2949
2950 /* now check whether we have some admin rules for this request */
2951 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2952 int ret = 1;
2953
2954 if (stats_admin_rule->cond) {
2955 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2956 ret = acl_pass(ret);
2957 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2958 ret = !ret;
2959 }
2960
2961 if (ret) {
2962 /* no rule, or the rule matches */
2963 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
2964 break;
2965 }
2966 }
2967
2968 /* Was the status page requested with a POST ? */
2969 if (unlikely(txn->meth == HTTP_METH_POST)) {
2970 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
2971 if (msg->msg_state < HTTP_MSG_100_SENT) {
2972 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2973 * send an HTTP/1.1 100 Continue intermediate response.
2974 */
2975 if (msg->flags & HTTP_MSGF_VER_11) {
2976 struct hdr_ctx ctx;
2977 ctx.idx = 0;
2978 /* Expect is allowed in 1.1, look for it */
2979 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2980 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2981 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2982 }
2983 }
2984 msg->msg_state = HTTP_MSG_100_SENT;
2985 s->logs.tv_request = now; /* update the request timer to reflect full request */
2986 }
2987 if (!http_process_req_stat_post(si, txn, req))
2988 return 0; /* we need more data */
2989 }
2990 else
2991 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
2992
2993 /* We don't want to land on the posted stats page because a refresh will
2994 * repost the data. We don't want this to happen on accident so we redirect
2995 * the browse to the stats page with a GET.
2996 */
2997 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04002998 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002999 "Cache-Control: no-cache\r\n"
3000 "Content-Type: text/plain\r\n"
3001 "Connection: close\r\n"
3002 "Location: %s;st=%s\r\n"
3003 "\r\n",
3004 uri->uri_prefix,
3005 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3006 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3007 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3008 stat_status_codes[si->applet.ctx.stats.st_code] :
3009 stat_status_codes[STAT_STATUS_UNKN]);
3010
3011 s->txn.status = 303;
3012 s->logs.tv_request = now;
3013 stream_int_retnclose(req->prod, &trash);
3014 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3015
3016 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3017 s->fe->fe_counters.intercepted_req++;
3018
3019 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3020 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3021 if (!(s->flags & SN_FINST_MASK))
3022 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003023 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003024 return 1;
3025 }
3026
3027 /* OK, let's go on now */
3028
3029 chunk_printf(&trash,
3030 "HTTP/1.0 200 OK\r\n"
3031 "Cache-Control: no-cache\r\n"
3032 "Connection: close\r\n"
3033 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003034 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003035
3036 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3037 chunk_appendf(&trash, "Refresh: %d\r\n",
3038 uri->refresh);
3039
3040 chunk_appendf(&trash, "\r\n");
3041
3042 s->txn.status = 200;
3043 s->logs.tv_request = now;
3044
3045 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3046 s->fe->fe_counters.intercepted_req++;
3047
3048 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3049 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3050 if (!(s->flags & SN_FINST_MASK))
3051 s->flags |= SN_FINST_R;
3052
3053 if (s->txn.meth == HTTP_METH_HEAD) {
3054 /* that's all we return in case of HEAD request, so let's immediately close. */
3055 stream_int_retnclose(req->prod, &trash);
3056 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003057 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003058 return 1;
3059 }
3060
3061 /* OK, push the response and hand over to the stats I/O handler */
3062 bi_putchk(s->rep, &trash);
3063
3064 s->task->nice = -32; /* small boost for HTTP statistics */
3065 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3066 s->target = s->rep->prod->conn->target; // for logging only
3067 s->rep->prod->conn->xprt_ctx = s;
3068 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3069 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003070 return 1;
3071}
3072
Willy Tarreau20b0de52012-12-24 15:45:22 +01003073/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003074 * transaction <txn>. Returns the first rule that prevents further processing
3075 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3076 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3077 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003078 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003079static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003080http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003081{
Willy Tarreauff011f22011-01-06 17:51:27 +01003082 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003083 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003084
Willy Tarreauff011f22011-01-06 17:51:27 +01003085 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003086 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003087 continue;
3088
Willy Tarreau96257ec2012-12-27 10:46:37 +01003089 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003090 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003091 int ret;
3092
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003093 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003094 ret = acl_pass(ret);
3095
Willy Tarreauff011f22011-01-06 17:51:27 +01003096 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003097 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003098
3099 if (!ret) /* condition not matched */
3100 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003101 }
3102
Willy Tarreau20b0de52012-12-24 15:45:22 +01003103
Willy Tarreau96257ec2012-12-27 10:46:37 +01003104 switch (rule->action) {
3105 case HTTP_REQ_ACT_ALLOW:
3106 return NULL; /* "allow" rules are OK */
3107
3108 case HTTP_REQ_ACT_DENY:
3109 txn->flags |= TX_CLDENY;
3110 return rule;
3111
Willy Tarreauccbcc372012-12-27 12:37:57 +01003112 case HTTP_REQ_ACT_TARPIT:
3113 txn->flags |= TX_CLTARPIT;
3114 return rule;
3115
Willy Tarreau96257ec2012-12-27 10:46:37 +01003116 case HTTP_REQ_ACT_AUTH:
3117 return rule;
3118
Willy Tarreau81499eb2012-12-27 12:19:02 +01003119 case HTTP_REQ_ACT_REDIR:
3120 return rule;
3121
Willy Tarreau96257ec2012-12-27 10:46:37 +01003122 case HTTP_REQ_ACT_SET_HDR:
3123 ctx.idx = 0;
3124 /* remove all occurrences of the header */
3125 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3126 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3127 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003128 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003129 /* now fall through to header addition */
3130
3131 case HTTP_REQ_ACT_ADD_HDR:
3132 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3133 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3134 trash.len = rule->arg.hdr_add.name_len;
3135 trash.str[trash.len++] = ':';
3136 trash.str[trash.len++] = ' ';
3137 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3138 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3139 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003140 }
3141 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003142
3143 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003144 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003145}
3146
Willy Tarreau71241ab2012-12-27 11:30:54 +01003147
3148/* Perform an HTTP redirect based on the information in <rule>. The function
3149 * returns non-zero on success, or zero in case of a, irrecoverable error such
3150 * as too large a request to build a valid response.
3151 */
3152static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3153{
3154 struct http_msg *msg = &txn->req;
3155 const char *msg_fmt;
3156
3157 /* build redirect message */
3158 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003159 case 308:
3160 msg_fmt = HTTP_308;
3161 break;
3162 case 307:
3163 msg_fmt = HTTP_307;
3164 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003165 case 303:
3166 msg_fmt = HTTP_303;
3167 break;
3168 case 301:
3169 msg_fmt = HTTP_301;
3170 break;
3171 case 302:
3172 default:
3173 msg_fmt = HTTP_302;
3174 break;
3175 }
3176
3177 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3178 return 0;
3179
3180 switch(rule->type) {
3181 case REDIRECT_TYPE_SCHEME: {
3182 const char *path;
3183 const char *host;
3184 struct hdr_ctx ctx;
3185 int pathlen;
3186 int hostlen;
3187
3188 host = "";
3189 hostlen = 0;
3190 ctx.idx = 0;
3191 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3192 host = ctx.line + ctx.val;
3193 hostlen = ctx.vlen;
3194 }
3195
3196 path = http_get_path(txn);
3197 /* build message using path */
3198 if (path) {
3199 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3200 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3201 int qs = 0;
3202 while (qs < pathlen) {
3203 if (path[qs] == '?') {
3204 pathlen = qs;
3205 break;
3206 }
3207 qs++;
3208 }
3209 }
3210 } else {
3211 path = "/";
3212 pathlen = 1;
3213 }
3214
3215 /* check if we can add scheme + "://" + host + path */
3216 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3217 return 0;
3218
3219 /* add scheme */
3220 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3221 trash.len += rule->rdr_len;
3222
3223 /* add "://" */
3224 memcpy(trash.str + trash.len, "://", 3);
3225 trash.len += 3;
3226
3227 /* add host */
3228 memcpy(trash.str + trash.len, host, hostlen);
3229 trash.len += hostlen;
3230
3231 /* add path */
3232 memcpy(trash.str + trash.len, path, pathlen);
3233 trash.len += pathlen;
3234
3235 /* append a slash at the end of the location is needed and missing */
3236 if (trash.len && trash.str[trash.len - 1] != '/' &&
3237 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3238 if (trash.len > trash.size - 5)
3239 return 0;
3240 trash.str[trash.len] = '/';
3241 trash.len++;
3242 }
3243
3244 break;
3245 }
3246 case REDIRECT_TYPE_PREFIX: {
3247 const char *path;
3248 int pathlen;
3249
3250 path = http_get_path(txn);
3251 /* build message using path */
3252 if (path) {
3253 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3254 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3255 int qs = 0;
3256 while (qs < pathlen) {
3257 if (path[qs] == '?') {
3258 pathlen = qs;
3259 break;
3260 }
3261 qs++;
3262 }
3263 }
3264 } else {
3265 path = "/";
3266 pathlen = 1;
3267 }
3268
3269 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3270 return 0;
3271
3272 /* add prefix. Note that if prefix == "/", we don't want to
3273 * add anything, otherwise it makes it hard for the user to
3274 * configure a self-redirection.
3275 */
3276 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3277 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3278 trash.len += rule->rdr_len;
3279 }
3280
3281 /* add path */
3282 memcpy(trash.str + trash.len, path, pathlen);
3283 trash.len += pathlen;
3284
3285 /* append a slash at the end of the location is needed and missing */
3286 if (trash.len && trash.str[trash.len - 1] != '/' &&
3287 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3288 if (trash.len > trash.size - 5)
3289 return 0;
3290 trash.str[trash.len] = '/';
3291 trash.len++;
3292 }
3293
3294 break;
3295 }
3296 case REDIRECT_TYPE_LOCATION:
3297 default:
3298 if (trash.len + rule->rdr_len > trash.size - 4)
3299 return 0;
3300
3301 /* add location */
3302 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3303 trash.len += rule->rdr_len;
3304 break;
3305 }
3306
3307 if (rule->cookie_len) {
3308 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3309 trash.len += 14;
3310 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3311 trash.len += rule->cookie_len;
3312 memcpy(trash.str + trash.len, "\r\n", 2);
3313 trash.len += 2;
3314 }
3315
3316 /* add end of headers and the keep-alive/close status.
3317 * We may choose to set keep-alive if the Location begins
3318 * with a slash, because the client will come back to the
3319 * same server.
3320 */
3321 txn->status = rule->code;
3322 /* let's log the request time */
3323 s->logs.tv_request = now;
3324
3325 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3326 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3327 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3328 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3329 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3330 /* keep-alive possible */
3331 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3332 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3333 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3334 trash.len += 30;
3335 } else {
3336 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3337 trash.len += 24;
3338 }
3339 }
3340 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3341 trash.len += 4;
3342 bo_inject(txn->rsp.chn, trash.str, trash.len);
3343 /* "eat" the request */
3344 bi_fast_delete(txn->req.chn->buf, msg->sov);
3345 msg->sov = 0;
3346 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3347 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3348 txn->req.msg_state = HTTP_MSG_CLOSED;
3349 txn->rsp.msg_state = HTTP_MSG_DONE;
3350 } else {
3351 /* keep-alive not possible */
3352 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3353 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3354 trash.len += 29;
3355 } else {
3356 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3357 trash.len += 23;
3358 }
3359 stream_int_retnclose(txn->req.chn->prod, &trash);
3360 txn->req.chn->analysers = 0;
3361 }
3362
3363 if (!(s->flags & SN_ERR_MASK))
3364 s->flags |= SN_ERR_PRXCOND;
3365 if (!(s->flags & SN_FINST_MASK))
3366 s->flags |= SN_FINST_R;
3367
3368 return 1;
3369}
3370
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003371/* This stream analyser runs all HTTP request processing which is common to
3372 * frontends and backends, which means blocking ACLs, filters, connection-close,
3373 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003374 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003375 * either needs more data or wants to immediately abort the request (eg: deny,
3376 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003377 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003378int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003379{
Willy Tarreaud787e662009-07-07 10:14:51 +02003380 struct http_txn *txn = &s->txn;
3381 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003382 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003383 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003384 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003385 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003386 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003387
Willy Tarreau655dce92009-11-08 13:10:58 +01003388 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003389 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003390 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003391 return 0;
3392 }
3393
Willy Tarreau3a816292009-07-07 10:55:49 +02003394 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003395 req->analyse_exp = TICK_ETERNITY;
3396
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003397 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 +02003398 now_ms, __FUNCTION__,
3399 s,
3400 req,
3401 req->rex, req->wex,
3402 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003403 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003404 req->analysers);
3405
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003406 /* first check whether we have some ACLs set to block this request */
3407 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003408 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003409
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003410 ret = acl_pass(ret);
3411 if (cond->pol == ACL_COND_UNLESS)
3412 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003413
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003414 if (ret) {
3415 txn->status = 403;
3416 /* let's log the request time */
3417 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003418 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003419 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003420 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003421 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003422 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003423
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003424 /* just in case we have some per-backend tracking */
3425 session_inc_be_http_req_ctr(s);
3426
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003427 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003428 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003429
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003430 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003431 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003432 do_stats = stats_check_uri(s->rep->prod, txn, px);
3433 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003434 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 +01003435 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003436 else
3437 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003438
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003439 /* return a 403 if either rule has blocked */
Willy Tarreauccbcc372012-12-27 12:37:57 +01003440 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3441 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003442 txn->status = 403;
3443 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003444 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003445 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003446 s->fe->fe_counters.denied_req++;
3447 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3448 s->be->be_counters.denied_req++;
3449 if (s->listener->counters)
3450 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003451 goto return_prx_cond;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003452 }
3453 /* When a connection is tarpitted, we use the tarpit timeout,
3454 * which may be the same as the connect timeout if unspecified.
3455 * If unset, then set it to zero because we really want it to
3456 * eventually expire. We build the tarpit as an analyser.
3457 */
3458 if (txn->flags & TX_CLTARPIT) {
3459 channel_erase(s->req);
3460 /* wipe the request out so that we can drop the connection early
3461 * if the client closes first.
3462 */
3463 channel_dont_connect(req);
3464 req->analysers = 0; /* remove switching rules etc... */
3465 req->analysers |= AN_REQ_HTTP_TARPIT;
3466 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3467 if (!req->analyse_exp)
3468 req->analyse_exp = tick_add(now_ms, 0);
3469 session_inc_http_err_ctr(s);
3470 s->fe->fe_counters.denied_req++;
3471 if (s->fe != s->be)
3472 s->be->be_counters.denied_req++;
3473 if (s->listener->counters)
3474 s->listener->counters->denied_req++;
3475 return 1;
3476 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003477 }
3478
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003479 /* try headers filters */
3480 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003481 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003482 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003483
Willy Tarreau59234e92008-11-30 23:51:27 +01003484 /* has the request been denied ? */
3485 if (txn->flags & TX_CLDENY) {
3486 /* no need to go further */
3487 txn->status = 403;
3488 /* let's log the request time */
3489 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003490 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003491 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003492 goto return_prx_cond;
3493 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003494
3495 /* When a connection is tarpitted, we use the tarpit timeout,
3496 * which may be the same as the connect timeout if unspecified.
3497 * If unset, then set it to zero because we really want it to
3498 * eventually expire. We build the tarpit as an analyser.
3499 */
3500 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003501 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003502 /* wipe the request out so that we can drop the connection early
3503 * if the client closes first.
3504 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003505 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003506 req->analysers = 0; /* remove switching rules etc... */
3507 req->analysers |= AN_REQ_HTTP_TARPIT;
3508 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3509 if (!req->analyse_exp)
3510 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003511 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003512 return 1;
3513 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003514 }
Willy Tarreau06619262006-12-17 08:37:22 +01003515
Willy Tarreau5b154472009-12-21 20:11:07 +01003516 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3517 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003518 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3519 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003520 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3521 * avoid to redo the same work if FE and BE have the same settings (common).
3522 * The method consists in checking if options changed between the two calls
3523 * (implying that either one is non-null, or one of them is non-null and we
3524 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003525 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003526
Willy Tarreaudc008c52010-02-01 16:20:08 +01003527 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3528 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3529 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3530 (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 +01003531 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003532
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003533 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3534 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003535 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003536 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3537 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003538 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003539 tmp = TX_CON_WANT_CLO;
3540
Willy Tarreau5b154472009-12-21 20:11:07 +01003541 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3542 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003543
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003544 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3545 /* parse the Connection header and possibly clean it */
3546 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003547 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003548 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3549 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003550 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003551 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003552 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003553 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003554 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003555
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003556 /* check if client or config asks for explicit close in KAL/SCL */
3557 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3558 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3559 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003560 (!(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 +01003561 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003562 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003563 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003564 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3565 }
Willy Tarreau78599912009-10-17 20:12:21 +02003566
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003567 /* we can be blocked here because the request needs to be authenticated,
3568 * either to pass or to access stats.
3569 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003570 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003571 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003572
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003573 if (!realm)
3574 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3575
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003576 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003577 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003578 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003579 /* on 401 we still count one error, because normal browsing
3580 * won't significantly increase the counter but brute force
3581 * attempts will.
3582 */
3583 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003584 goto return_prx_cond;
3585 }
3586
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003587 /* add request headers from the rule sets in the same order */
3588 list_for_each_entry(wl, &px->req_add, list) {
3589 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003590 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003591 ret = acl_pass(ret);
3592 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3593 ret = !ret;
3594 if (!ret)
3595 continue;
3596 }
3597
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003598 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003599 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003600 }
3601
3602 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3603 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3604 goto return_bad_req;
3605 req->analyse_exp = TICK_ETERNITY;
3606 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003607 }
3608
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003609 if (unlikely(do_stats)) {
3610 /* process the stats request now */
3611 if (!http_handle_stats(s, req)) {
3612 /* we need more data, let's come back here later */
3613 req->analysers |= an_bit;
3614 channel_dont_connect(req);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003615 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003616 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003617 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003618
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003619 /* check whether we have some ACLs set to redirect this request */
3620 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003621 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003622 int ret;
3623
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003624 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003625 ret = acl_pass(ret);
3626 if (rule->cond->pol == ACL_COND_UNLESS)
3627 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003628 if (!ret)
3629 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003630 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003631 if (!http_apply_redirect_rule(rule, s, txn))
3632 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003633
Willy Tarreau71241ab2012-12-27 11:30:54 +01003634 req->analyse_exp = TICK_ETERNITY;
3635 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003636 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003637
Willy Tarreau2be39392010-01-03 17:24:51 +01003638 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3639 * If this happens, then the data will not come immediately, so we must
3640 * send all what we have without waiting. Note that due to the small gain
3641 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003642 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003643 * itself once used.
3644 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003645 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003646
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003647 /* that's OK for us now, let's move on to next analysers */
3648 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003649
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003650 return_bad_req:
3651 /* We centralize bad requests processing here */
3652 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3653 /* we detected a parsing error. We want to archive this request
3654 * in the dedicated proxy area for later troubleshooting.
3655 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003656 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003657 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003658
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003659 txn->req.msg_state = HTTP_MSG_ERROR;
3660 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003661 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003662
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003663 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003664 if (s->listener->counters)
3665 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003666
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003667 return_prx_cond:
3668 if (!(s->flags & SN_ERR_MASK))
3669 s->flags |= SN_ERR_PRXCOND;
3670 if (!(s->flags & SN_FINST_MASK))
3671 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003672
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003673 req->analysers = 0;
3674 req->analyse_exp = TICK_ETERNITY;
3675 return 0;
3676}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003677
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003678/* This function performs all the processing enabled for the current request.
3679 * It returns 1 if the processing can continue on next analysers, or zero if it
3680 * needs more data, encounters an error, or wants to immediately abort the
3681 * request. It relies on buffers flags, and updates s->req->analysers.
3682 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003683int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003684{
3685 struct http_txn *txn = &s->txn;
3686 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003687
Willy Tarreau655dce92009-11-08 13:10:58 +01003688 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003689 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003690 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003691 return 0;
3692 }
3693
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003694 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 +02003695 now_ms, __FUNCTION__,
3696 s,
3697 req,
3698 req->rex, req->wex,
3699 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003700 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003701 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003702
William Lallemand82fe75c2012-10-23 10:25:10 +02003703 if (s->fe->comp || s->be->comp)
3704 select_compression_request_header(s, req->buf);
3705
Willy Tarreau59234e92008-11-30 23:51:27 +01003706 /*
3707 * Right now, we know that we have processed the entire headers
3708 * and that unwanted requests have been filtered out. We can do
3709 * whatever we want with the remaining request. Also, now we
3710 * may have separate values for ->fe, ->be.
3711 */
Willy Tarreau06619262006-12-17 08:37:22 +01003712
Willy Tarreau59234e92008-11-30 23:51:27 +01003713 /*
3714 * If HTTP PROXY is set we simply get remote server address
3715 * parsing incoming request.
3716 */
3717 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003718 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 +01003719 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003720
Willy Tarreau59234e92008-11-30 23:51:27 +01003721 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003722 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003723 * Note that doing so might move headers in the request, but
3724 * the fields will stay coherent and the URI will not move.
3725 * This should only be performed in the backend.
3726 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003727 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003728 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3729 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003730
Willy Tarreau59234e92008-11-30 23:51:27 +01003731 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003732 * 8: the appsession cookie was looked up very early in 1.2,
3733 * so let's do the same now.
3734 */
3735
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003736 /* It needs to look into the URI unless persistence must be ignored */
3737 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003738 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 +01003739 }
3740
William Lallemanda73203e2012-03-12 12:48:57 +01003741 /* add unique-id if "header-unique-id" is specified */
3742
3743 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3744 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3745
3746 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003747 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3748 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003749 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003750 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003751 goto return_bad_req;
3752 }
3753
Cyril Bontéb21570a2009-11-29 20:04:48 +01003754 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003755 * 9: add X-Forwarded-For if either the frontend or the backend
3756 * asks for it.
3757 */
3758 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003759 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003760 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003761 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3762 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003763 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003764 /* The header is set to be added only if none is present
3765 * and we found it, so don't do anything.
3766 */
3767 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003768 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 /* Add an X-Forwarded-For header unless the source IP is
3770 * in the 'except' network range.
3771 */
3772 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003773 (((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 +01003774 != s->fe->except_net.s_addr) &&
3775 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003776 (((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 +01003777 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003778 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003779 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003780 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003781
3782 /* Note: we rely on the backend to get the header name to be used for
3783 * x-forwarded-for, because the header is really meant for the backends.
3784 * However, if the backend did not specify any option, we have to rely
3785 * on the frontend's header name.
3786 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003787 if (s->be->fwdfor_hdr_len) {
3788 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003789 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003790 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003791 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003792 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003793 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003794 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003795
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003796 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003797 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003798 }
3799 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003800 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003801 /* FIXME: for the sake of completeness, we should also support
3802 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003803 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003804 int len;
3805 char pn[INET6_ADDRSTRLEN];
3806 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003807 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003808 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003809
Willy Tarreau59234e92008-11-30 23:51:27 +01003810 /* Note: we rely on the backend to get the header name to be used for
3811 * x-forwarded-for, because the header is really meant for the backends.
3812 * However, if the backend did not specify any option, we have to rely
3813 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003814 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003815 if (s->be->fwdfor_hdr_len) {
3816 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003817 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003818 } else {
3819 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003820 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003821 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003822 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003823
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003824 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003825 goto return_bad_req;
3826 }
3827 }
3828
3829 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003830 * 10: add X-Original-To if either the frontend or the backend
3831 * asks for it.
3832 */
3833 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3834
3835 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003836 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003837 /* Add an X-Original-To header unless the destination IP is
3838 * in the 'except' network range.
3839 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003840 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003841
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003842 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003843 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003844 (((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 +02003845 != s->fe->except_to.s_addr) &&
3846 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003847 (((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 +02003848 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003849 int len;
3850 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003851 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003852
3853 /* Note: we rely on the backend to get the header name to be used for
3854 * x-original-to, because the header is really meant for the backends.
3855 * However, if the backend did not specify any option, we have to rely
3856 * on the frontend's header name.
3857 */
3858 if (s->be->orgto_hdr_len) {
3859 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003860 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003861 } else {
3862 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003863 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003864 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003865 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003866
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003867 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003868 goto return_bad_req;
3869 }
3870 }
3871 }
3872
Willy Tarreau50fc7772012-11-11 22:19:57 +01003873 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3874 * If an "Upgrade" token is found, the header is left untouched in order not to have
3875 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3876 * "Upgrade" is present in the Connection header.
3877 */
3878 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3879 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3880 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003881 unsigned int want_flags = 0;
3882
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003883 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003884 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3885 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3886 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003887 want_flags |= TX_CON_CLO_SET;
3888 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003889 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3890 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3891 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003892 want_flags |= TX_CON_KAL_SET;
3893 }
3894
3895 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003896 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003897 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003898
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003899
Willy Tarreau522d6c02009-12-06 18:49:18 +01003900 /* If we have no server assigned yet and we're balancing on url_param
3901 * with a POST request, we may be interested in checking the body for
3902 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003903 */
3904 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3905 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003906 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003907 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003908 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003909 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003910 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003911
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003912 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003913 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003914#ifdef TCP_QUICKACK
3915 /* We expect some data from the client. Unless we know for sure
3916 * we already have a full request, we have to re-enable quick-ack
3917 * in case we previously disabled it, otherwise we might cause
3918 * the client to delay further data.
3919 */
3920 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003921 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003922 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003923 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003924#endif
3925 }
Willy Tarreau03945942009-12-22 16:50:27 +01003926
Willy Tarreau59234e92008-11-30 23:51:27 +01003927 /*************************************************************
3928 * OK, that's finished for the headers. We have done what we *
3929 * could. Let's switch to the DATA state. *
3930 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003931 req->analyse_exp = TICK_ETERNITY;
3932 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003933
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003934 /* if the server closes the connection, we want to immediately react
3935 * and close the socket to save packets and syscalls.
3936 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003937 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3938 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003939
Willy Tarreau59234e92008-11-30 23:51:27 +01003940 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003941 /* OK let's go on with the BODY now */
3942 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003943
Willy Tarreau59234e92008-11-30 23:51:27 +01003944 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003945 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003946 /* we detected a parsing error. We want to archive this request
3947 * in the dedicated proxy area for later troubleshooting.
3948 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003949 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003950 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003951
Willy Tarreau59234e92008-11-30 23:51:27 +01003952 txn->req.msg_state = HTTP_MSG_ERROR;
3953 txn->status = 400;
3954 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003955 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003956
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003957 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003958 if (s->listener->counters)
3959 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003960
Willy Tarreau59234e92008-11-30 23:51:27 +01003961 if (!(s->flags & SN_ERR_MASK))
3962 s->flags |= SN_ERR_PRXCOND;
3963 if (!(s->flags & SN_FINST_MASK))
3964 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003965 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003966}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003967
Willy Tarreau60b85b02008-11-30 23:28:40 +01003968/* This function is an analyser which processes the HTTP tarpit. It always
3969 * returns zero, at the beginning because it prevents any other processing
3970 * from occurring, and at the end because it terminates the request.
3971 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003972int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003973{
3974 struct http_txn *txn = &s->txn;
3975
3976 /* This connection is being tarpitted. The CLIENT side has
3977 * already set the connect expiration date to the right
3978 * timeout. We just have to check that the client is still
3979 * there and that the timeout has not expired.
3980 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003981 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003982 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003983 !tick_is_expired(req->analyse_exp, now_ms))
3984 return 0;
3985
3986 /* We will set the queue timer to the time spent, just for
3987 * logging purposes. We fake a 500 server error, so that the
3988 * attacker will not suspect his connection has been tarpitted.
3989 * It will not cause trouble to the logs because we can exclude
3990 * the tarpitted connections by filtering on the 'PT' status flags.
3991 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003992 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3993
3994 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003995 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003996 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003997
3998 req->analysers = 0;
3999 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004000
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004001 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004002 if (s->listener->counters)
4003 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004004
Willy Tarreau60b85b02008-11-30 23:28:40 +01004005 if (!(s->flags & SN_ERR_MASK))
4006 s->flags |= SN_ERR_PRXCOND;
4007 if (!(s->flags & SN_FINST_MASK))
4008 s->flags |= SN_FINST_T;
4009 return 0;
4010}
4011
Willy Tarreaud34af782008-11-30 23:36:37 +01004012/* This function is an analyser which processes the HTTP request body. It looks
4013 * for parameters to be used for the load balancing algorithm (url_param). It
4014 * must only be called after the standard HTTP request processing has occurred,
4015 * because it expects the request to be parsed. It returns zero if it needs to
4016 * read more data, or 1 once it has completed its analysis.
4017 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004018int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004019{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004020 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004021 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004022 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004023
4024 /* We have to parse the HTTP request body to find any required data.
4025 * "balance url_param check_post" should have been the only way to get
4026 * into this. We were brought here after HTTP header analysis, so all
4027 * related structures are ready.
4028 */
4029
Willy Tarreau522d6c02009-12-06 18:49:18 +01004030 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4031 goto missing_data;
4032
4033 if (msg->msg_state < HTTP_MSG_100_SENT) {
4034 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4035 * send an HTTP/1.1 100 Continue intermediate response.
4036 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004037 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004038 struct hdr_ctx ctx;
4039 ctx.idx = 0;
4040 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004041 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004042 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004043 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004044 }
4045 }
4046 msg->msg_state = HTTP_MSG_100_SENT;
4047 }
4048
4049 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004050 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004051 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004052 * is still null. We must save the body in msg->next because it
4053 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004054 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004055 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004056
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004057 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004058 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4059 else
4060 msg->msg_state = HTTP_MSG_DATA;
4061 }
4062
4063 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004064 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004065 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004066 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004067 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004068 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004069
Willy Tarreau115acb92009-12-26 13:56:06 +01004070 if (!ret)
4071 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004072 else if (ret < 0) {
4073 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004074 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004075 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004076 }
4077
Willy Tarreaud98cf932009-12-27 22:54:55 +01004078 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004079 * We have the first data byte is in msg->sov. We're waiting for at
4080 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004081 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004082
Willy Tarreau124d9912011-03-01 20:30:48 +01004083 if (msg->body_len < limit)
4084 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004085
Willy Tarreau9b28e032012-10-12 23:49:43 +02004086 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004087 goto http_end;
4088
4089 missing_data:
4090 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004091 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004092 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004093 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004094 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004095
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004096 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004097 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004098 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004099
4100 if (!(s->flags & SN_ERR_MASK))
4101 s->flags |= SN_ERR_CLITO;
4102 if (!(s->flags & SN_FINST_MASK))
4103 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004104 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004105 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004106
4107 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004108 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004109 /* Not enough data. We'll re-use the http-request
4110 * timeout here. Ideally, we should set the timeout
4111 * relative to the accept() date. We just set the
4112 * request timeout once at the beginning of the
4113 * request.
4114 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004115 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004116 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004117 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004118 return 0;
4119 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004120
4121 http_end:
4122 /* The situation will not evolve, so let's give up on the analysis. */
4123 s->logs.tv_request = now; /* update the request timer to reflect full request */
4124 req->analysers &= ~an_bit;
4125 req->analyse_exp = TICK_ETERNITY;
4126 return 1;
4127
4128 return_bad_req: /* let's centralize all bad requests */
4129 txn->req.msg_state = HTTP_MSG_ERROR;
4130 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004131 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004132
Willy Tarreau79ebac62010-06-07 13:47:49 +02004133 if (!(s->flags & SN_ERR_MASK))
4134 s->flags |= SN_ERR_PRXCOND;
4135 if (!(s->flags & SN_FINST_MASK))
4136 s->flags |= SN_FINST_R;
4137
Willy Tarreau522d6c02009-12-06 18:49:18 +01004138 return_err_msg:
4139 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004140 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004141 if (s->listener->counters)
4142 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004143 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004144}
4145
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004146/* send a server's name with an outgoing request over an established connection.
4147 * Note: this function is designed to be called once the request has been scheduled
4148 * for being forwarded. This is the reason why it rewinds the buffer before
4149 * proceeding.
4150 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004151int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004152
4153 struct hdr_ctx ctx;
4154
Mark Lamourinec2247f02012-01-04 13:02:01 -05004155 char *hdr_name = be->server_id_hdr_name;
4156 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004157 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004158 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004159 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004160
William Lallemandd9e90662012-01-30 17:27:17 +01004161 ctx.idx = 0;
4162
Willy Tarreau9b28e032012-10-12 23:49:43 +02004163 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004164 if (old_o) {
4165 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004166 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004167 }
4168
Willy Tarreau9b28e032012-10-12 23:49:43 +02004169 old_i = chn->buf->i;
4170 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 -05004171 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004172 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004173 }
4174
4175 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004176 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004177 memcpy(hdr_val, hdr_name, hdr_name_len);
4178 hdr_val += hdr_name_len;
4179 *hdr_val++ = ':';
4180 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004181 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4182 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004183
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004184 if (old_o) {
4185 /* If this was a forwarded request, we must readjust the amount of
4186 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004187 * variations. Note that if the request was already scheduled for
4188 * forwarding, it had its req->sol pointing to the body, which
4189 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004190 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004191 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004192 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004193 }
4194
Mark Lamourinec2247f02012-01-04 13:02:01 -05004195 return 0;
4196}
4197
Willy Tarreau610ecce2010-01-04 21:15:02 +01004198/* Terminate current transaction and prepare a new one. This is very tricky
4199 * right now but it works.
4200 */
4201void http_end_txn_clean_session(struct session *s)
4202{
4203 /* FIXME: We need a more portable way of releasing a backend's and a
4204 * server's connections. We need a safer way to reinitialize buffer
4205 * flags. We also need a more accurate method for computing per-request
4206 * data.
4207 */
4208 http_silent_debug(__LINE__, s);
4209
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004210 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004211 si_shutr(s->req->cons);
4212 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004213
4214 http_silent_debug(__LINE__, s);
4215
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004216 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004218 if (unlikely(s->srv_conn))
4219 sess_change_server(s, NULL);
4220 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221
4222 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4223 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004224 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004225
4226 if (s->txn.status) {
4227 int n;
4228
4229 n = s->txn.status / 100;
4230 if (n < 1 || n > 5)
4231 n = 0;
4232
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004233 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004234 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004235 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004236 s->fe->fe_counters.p.http.comp_rsp++;
4237 }
Willy Tarreau24657792010-02-26 10:30:28 +01004238 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004239 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004240 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004241 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004242 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004243 s->be->be_counters.p.http.comp_rsp++;
4244 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245 }
4246
4247 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004248 s->logs.bytes_in -= s->req->buf->i;
4249 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250
4251 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004252 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253 !(s->flags & SN_MONITOR) &&
4254 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4255 s->do_log(s);
4256 }
4257
4258 s->logs.accept_date = date; /* user-visible date for logging */
4259 s->logs.tv_accept = now; /* corrected date for internal use */
4260 tv_zero(&s->logs.tv_request);
4261 s->logs.t_queue = -1;
4262 s->logs.t_connect = -1;
4263 s->logs.t_data = -1;
4264 s->logs.t_close = 0;
4265 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4266 s->logs.srv_queue_size = 0; /* we will get this number soon */
4267
Willy Tarreau9b28e032012-10-12 23:49:43 +02004268 s->logs.bytes_in = s->req->total = s->req->buf->i;
4269 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270
4271 if (s->pend_pos)
4272 pendconn_free(s->pend_pos);
4273
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004274 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004275 if (s->flags & SN_CURR_SESS) {
4276 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004277 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004279 if (may_dequeue_tasks(objt_server(s->target), s->be))
4280 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281 }
4282
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004283 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284
4285 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004286 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4287 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004288 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004290 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291 s->req->cons->err_loc = NULL;
4292 s->req->cons->exp = TICK_ETERNITY;
4293 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004294 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4295 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 +02004296 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 +01004297 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004298
4299 if (s->flags & SN_COMP_READY)
4300 s->comp_algo->end(&s->comp_ctx);
4301 s->comp_algo = NULL;
4302 s->flags &= ~SN_COMP_READY;
4303
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 s->txn.meth = 0;
4305 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004306 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004307 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 s->req->cons->flags |= SI_FL_INDEP_STR;
4309
Willy Tarreau96e31212011-05-30 18:10:30 +02004310 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004311 s->req->flags |= CF_NEVER_WAIT;
4312 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004313 }
4314
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315 /* if the request buffer is not empty, it means we're
4316 * about to process another request, so send pending
4317 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004318 * Just don't do this if the buffer is close to be full,
4319 * because the request will wait for it to flush a little
4320 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004321 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004322 if (s->req->buf->i) {
4323 if (s->rep->buf->o &&
4324 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4325 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004326 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004327 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004328
4329 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004330 channel_auto_read(s->req);
4331 channel_auto_close(s->req);
4332 channel_auto_read(s->rep);
4333 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004334
Willy Tarreau342b11c2010-11-24 16:22:09 +01004335 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336 s->rep->analysers = 0;
4337
4338 http_silent_debug(__LINE__, s);
4339}
4340
4341
4342/* This function updates the request state machine according to the response
4343 * state machine and buffer flags. It returns 1 if it changes anything (flag
4344 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4345 * it is only used to find when a request/response couple is complete. Both
4346 * this function and its equivalent should loop until both return zero. It
4347 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4348 */
4349int http_sync_req_state(struct session *s)
4350{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004351 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004353 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004354 unsigned int old_state = txn->req.msg_state;
4355
4356 http_silent_debug(__LINE__, s);
4357 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4358 return 0;
4359
4360 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004361 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004362 * We can shut the read side unless we want to abort_on_close,
4363 * or we have a POST request. The issue with POST requests is
4364 * that some browsers still send a CRLF after the request, and
4365 * this CRLF must be read so that it does not remain in the kernel
4366 * buffers, otherwise a close could cause an RST on some systems
4367 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004368 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004369 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004370 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004371
Willy Tarreau40f151a2012-12-20 12:10:09 +01004372 /* if the server closes the connection, we want to immediately react
4373 * and close the socket to save packets and syscalls.
4374 */
4375 chn->cons->flags |= SI_FL_NOHALF;
4376
Willy Tarreau610ecce2010-01-04 21:15:02 +01004377 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4378 goto wait_other_side;
4379
4380 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4381 /* The server has not finished to respond, so we
4382 * don't want to move in order not to upset it.
4383 */
4384 goto wait_other_side;
4385 }
4386
4387 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4388 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004389 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004390 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004391 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 goto wait_other_side;
4393 }
4394
4395 /* When we get here, it means that both the request and the
4396 * response have finished receiving. Depending on the connection
4397 * mode, we'll have to wait for the last bytes to leave in either
4398 * direction, and sometimes for a close to be effective.
4399 */
4400
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004401 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4402 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004403 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4404 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004405 }
4406 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4407 /* Option forceclose is set, or either side wants to close,
4408 * let's enforce it now that we're not expecting any new
4409 * data to come. The caller knows the session is complete
4410 * once both states are CLOSED.
4411 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004412 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4413 channel_shutr_now(chn);
4414 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004415 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004416 }
4417 else {
4418 /* The last possible modes are keep-alive and tunnel. Since tunnel
4419 * mode does not set the body analyser, we can't reach this place
4420 * in tunnel mode, so we're left with keep-alive only.
4421 * This mode is currently not implemented, we switch to tunnel mode.
4422 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004423 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004424 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004425 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004426 }
4427
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004428 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004430 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004431
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004432 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004433 txn->req.msg_state = HTTP_MSG_CLOSING;
4434 goto http_msg_closing;
4435 }
4436 else {
4437 txn->req.msg_state = HTTP_MSG_CLOSED;
4438 goto http_msg_closed;
4439 }
4440 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004441 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442 }
4443
4444 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4445 http_msg_closing:
4446 /* nothing else to forward, just waiting for the output buffer
4447 * to be empty and for the shutw_now to take effect.
4448 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004449 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004450 txn->req.msg_state = HTTP_MSG_CLOSED;
4451 goto http_msg_closed;
4452 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004453 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004454 txn->req.msg_state = HTTP_MSG_ERROR;
4455 goto wait_other_side;
4456 }
4457 }
4458
4459 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4460 http_msg_closed:
4461 goto wait_other_side;
4462 }
4463
4464 wait_other_side:
4465 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004466 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467}
4468
4469
4470/* This function updates the response state machine according to the request
4471 * state machine and buffer flags. It returns 1 if it changes anything (flag
4472 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4473 * it is only used to find when a request/response couple is complete. Both
4474 * this function and its equivalent should loop until both return zero. It
4475 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4476 */
4477int http_sync_res_state(struct session *s)
4478{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004479 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004481 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482 unsigned int old_state = txn->rsp.msg_state;
4483
4484 http_silent_debug(__LINE__, s);
4485 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4486 return 0;
4487
4488 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4489 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004490 * still monitor the server connection for a possible close
4491 * while the request is being uploaded, so we don't disable
4492 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004494 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495
4496 if (txn->req.msg_state == HTTP_MSG_ERROR)
4497 goto wait_other_side;
4498
4499 if (txn->req.msg_state < HTTP_MSG_DONE) {
4500 /* The client seems to still be sending data, probably
4501 * because we got an error response during an upload.
4502 * We have the choice of either breaking the connection
4503 * or letting it pass through. Let's do the later.
4504 */
4505 goto wait_other_side;
4506 }
4507
4508 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4509 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004510 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004512 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513 goto wait_other_side;
4514 }
4515
4516 /* When we get here, it means that both the request and the
4517 * response have finished receiving. Depending on the connection
4518 * mode, we'll have to wait for the last bytes to leave in either
4519 * direction, and sometimes for a close to be effective.
4520 */
4521
4522 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4523 /* Server-close mode : shut read and wait for the request
4524 * side to close its output buffer. The caller will detect
4525 * when we're in DONE and the other is in CLOSED and will
4526 * catch that for the final cleanup.
4527 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004528 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4529 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004530 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004531 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4532 /* Option forceclose is set, or either side wants to close,
4533 * let's enforce it now that we're not expecting any new
4534 * data to come. The caller knows the session is complete
4535 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004537 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4538 channel_shutr_now(chn);
4539 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004540 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004541 }
4542 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004543 /* The last possible modes are keep-alive and tunnel. Since tunnel
4544 * mode does not set the body analyser, we can't reach this place
4545 * in tunnel mode, so we're left with keep-alive only.
4546 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004548 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004549 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004550 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 }
4552
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004553 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004555 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004556 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4557 goto http_msg_closing;
4558 }
4559 else {
4560 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4561 goto http_msg_closed;
4562 }
4563 }
4564 goto wait_other_side;
4565 }
4566
4567 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4568 http_msg_closing:
4569 /* nothing else to forward, just waiting for the output buffer
4570 * to be empty and for the shutw_now to take effect.
4571 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004572 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4574 goto http_msg_closed;
4575 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004576 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004577 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004578 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004579 if (objt_server(s->target))
4580 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004581 goto wait_other_side;
4582 }
4583 }
4584
4585 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4586 http_msg_closed:
4587 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004588 bi_erase(chn);
4589 channel_auto_close(chn);
4590 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004591 goto wait_other_side;
4592 }
4593
4594 wait_other_side:
4595 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004596 /* We force the response to leave immediately if we're waiting for the
4597 * other side, since there is no pending shutdown to push it out.
4598 */
4599 if (!channel_is_empty(chn))
4600 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602}
4603
4604
4605/* Resync the request and response state machines. Return 1 if either state
4606 * changes.
4607 */
4608int http_resync_states(struct session *s)
4609{
4610 struct http_txn *txn = &s->txn;
4611 int old_req_state = txn->req.msg_state;
4612 int old_res_state = txn->rsp.msg_state;
4613
4614 http_silent_debug(__LINE__, s);
4615 http_sync_req_state(s);
4616 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004617 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 if (!http_sync_res_state(s))
4619 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004620 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621 if (!http_sync_req_state(s))
4622 break;
4623 }
4624 http_silent_debug(__LINE__, s);
4625 /* OK, both state machines agree on a compatible state.
4626 * There are a few cases we're interested in :
4627 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4628 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4629 * directions, so let's simply disable both analysers.
4630 * - HTTP_MSG_CLOSED on the response only means we must abort the
4631 * request.
4632 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4633 * with server-close mode means we've completed one request and we
4634 * must re-initialize the server connection.
4635 */
4636
4637 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4638 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4639 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4640 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4641 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004642 channel_auto_close(s->req);
4643 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004644 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004645 channel_auto_close(s->rep);
4646 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004647 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004648 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4649 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004650 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004651 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004652 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004653 channel_auto_close(s->rep);
4654 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004655 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004656 channel_abort(s->req);
4657 channel_auto_close(s->req);
4658 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004659 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 }
4661 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4662 txn->rsp.msg_state == HTTP_MSG_DONE &&
4663 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4664 /* server-close: terminate this server connection and
4665 * reinitialize a fresh-new transaction.
4666 */
4667 http_end_txn_clean_session(s);
4668 }
4669
4670 http_silent_debug(__LINE__, s);
4671 return txn->req.msg_state != old_req_state ||
4672 txn->rsp.msg_state != old_res_state;
4673}
4674
Willy Tarreaud98cf932009-12-27 22:54:55 +01004675/* This function is an analyser which forwards request body (including chunk
4676 * sizes if any). It is called as soon as we must forward, even if we forward
4677 * zero byte. The only situation where it must not be called is when we're in
4678 * tunnel mode and we want to forward till the close. It's used both to forward
4679 * remaining data and to resync after end of body. It expects the msg_state to
4680 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4681 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004682 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004683 * bytes of pending data + the headers if not already done (between sol and sov).
4684 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004685 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004686int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687{
4688 struct http_txn *txn = &s->txn;
4689 struct http_msg *msg = &s->txn.req;
4690
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004691 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4692 return 0;
4693
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004694 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004695 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004696 /* Output closed while we were sending data. We must abort and
4697 * wake the other side up.
4698 */
4699 msg->msg_state = HTTP_MSG_ERROR;
4700 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004701 return 1;
4702 }
4703
Willy Tarreau4fe41902010-06-07 22:27:41 +02004704 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004705 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004706
4707 /* Note that we don't have to send 100-continue back because we don't
4708 * need the data to complete our job, and it's up to the server to
4709 * decide whether to return 100, 417 or anything else in return of
4710 * an "Expect: 100-continue" header.
4711 */
4712
4713 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004714 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004715 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004716 * is still null. We must save the body in msg->next because it
4717 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004718 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004719 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004720
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004721 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004722 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004723 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004724 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004725 }
4726
Willy Tarreaud98cf932009-12-27 22:54:55 +01004727 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004728 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004729
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004731 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004732 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004733 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004734 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004735 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004736 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004737 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004738 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004739
Willy Tarreaucaabe412010-01-03 23:08:28 +01004740 if (msg->msg_state == HTTP_MSG_DATA) {
4741 /* must still forward */
4742 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004743 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004744
4745 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004746 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004747 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004749 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004750 }
4751 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004752 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004753 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004754 * TRAILERS state.
4755 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004756 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004757
Willy Tarreau54d23df2012-10-25 19:04:45 +02004758 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004759 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004760 else if (ret < 0) {
4761 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004762 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004763 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004765 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004766 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004767 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004768 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004769 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004770 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004771
4772 if (ret == 0)
4773 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004774 else if (ret < 0) {
4775 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004776 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004777 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004778 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004779 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004780 /* we're in MSG_CHUNK_SIZE now */
4781 }
4782 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004783 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784
4785 if (ret == 0)
4786 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004787 else if (ret < 0) {
4788 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004789 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004790 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004791 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004792 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004793 /* we're in HTTP_MSG_DONE now */
4794 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004795 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004796 int old_state = msg->msg_state;
4797
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004799 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004800 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4801 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004802 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004803 if (http_resync_states(s)) {
4804 /* some state changes occurred, maybe the analyser
4805 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004806 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004807 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004808 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004809 /* request errors are most likely due to
4810 * the server aborting the transfer.
4811 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004812 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004813 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004814 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004815 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004816 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004817 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004818 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004819 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004820
4821 /* If "option abortonclose" is set on the backend, we
4822 * want to monitor the client's connection and forward
4823 * any shutdown notification to the server, which will
4824 * decide whether to close or to go on processing the
4825 * request.
4826 */
4827 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004828 channel_auto_read(req);
4829 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004830 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004831 else if (s->txn.meth == HTTP_METH_POST) {
4832 /* POST requests may require to read extra CRLF
4833 * sent by broken browsers and which could cause
4834 * an RST to be sent upon close on some systems
4835 * (eg: Linux).
4836 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004837 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004838 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004839
Willy Tarreau610ecce2010-01-04 21:15:02 +01004840 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004841 }
4842 }
4843
Willy Tarreaud98cf932009-12-27 22:54:55 +01004844 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004845 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004846 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004847 if (!(s->flags & SN_ERR_MASK))
4848 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004849 if (!(s->flags & SN_FINST_MASK)) {
4850 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4851 s->flags |= SN_FINST_H;
4852 else
4853 s->flags |= SN_FINST_D;
4854 }
4855
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004856 s->fe->fe_counters.cli_aborts++;
4857 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004858 if (objt_server(s->target))
4859 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004860
4861 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004862 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004863
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004864 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004865 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004866 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004868 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004869 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004870 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004871 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004872 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004873
Willy Tarreau5c620922011-05-11 19:56:11 +02004874 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004875 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004876 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004877 * modes are already handled by the stream sock layer. We must not do
4878 * this in content-length mode because it could present the MSG_MORE
4879 * flag with the last block of forwarded data, which would cause an
4880 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004881 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004882 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004883 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004884
Willy Tarreau610ecce2010-01-04 21:15:02 +01004885 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004886 return 0;
4887
Willy Tarreaud98cf932009-12-27 22:54:55 +01004888 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004889 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004890 if (s->listener->counters)
4891 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004892 return_bad_req_stats_ok:
4893 txn->req.msg_state = HTTP_MSG_ERROR;
4894 if (txn->status) {
4895 /* Note: we don't send any error if some data were already sent */
4896 stream_int_retnclose(req->prod, NULL);
4897 } else {
4898 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004899 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004900 }
4901 req->analysers = 0;
4902 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004903
4904 if (!(s->flags & SN_ERR_MASK))
4905 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004906 if (!(s->flags & SN_FINST_MASK)) {
4907 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4908 s->flags |= SN_FINST_H;
4909 else
4910 s->flags |= SN_FINST_D;
4911 }
4912 return 0;
4913
4914 aborted_xfer:
4915 txn->req.msg_state = HTTP_MSG_ERROR;
4916 if (txn->status) {
4917 /* Note: we don't send any error if some data were already sent */
4918 stream_int_retnclose(req->prod, NULL);
4919 } else {
4920 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004921 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004922 }
4923 req->analysers = 0;
4924 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4925
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004926 s->fe->fe_counters.srv_aborts++;
4927 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004928 if (objt_server(s->target))
4929 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004930
4931 if (!(s->flags & SN_ERR_MASK))
4932 s->flags |= SN_ERR_SRVCL;
4933 if (!(s->flags & SN_FINST_MASK)) {
4934 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4935 s->flags |= SN_FINST_H;
4936 else
4937 s->flags |= SN_FINST_D;
4938 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004939 return 0;
4940}
4941
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004942/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4943 * processing can continue on next analysers, or zero if it either needs more
4944 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4945 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4946 * when it has nothing left to do, and may remove any analyser when it wants to
4947 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004948 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004949int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004950{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004951 struct http_txn *txn = &s->txn;
4952 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004953 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004954 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004955 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004956 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004957
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004958 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 +02004959 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004960 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004961 rep,
4962 rep->rex, rep->wex,
4963 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004964 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004965 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004966
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004967 /*
4968 * Now parse the partial (or complete) lines.
4969 * We will check the response syntax, and also join multi-line
4970 * headers. An index of all the lines will be elaborated while
4971 * parsing.
4972 *
4973 * For the parsing, we use a 28 states FSM.
4974 *
4975 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004976 * rep->buf->p = beginning of response
4977 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4978 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004979 * msg->eol = end of current header or line (LF or CRLF)
4980 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004981 */
4982
Willy Tarreau83e3af02009-12-28 17:39:57 +01004983 /* There's a protected area at the end of the buffer for rewriting
4984 * purposes. We don't want to start to parse the request if the
4985 * protected area is affected, because we may have to move processed
4986 * data later, which is much more complicated.
4987 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004988 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004989 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004990 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4991 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4992 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004993 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004994 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004995 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004996 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004997 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004998 return 0;
4999 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02005000 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
5001 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005002 }
5003
Willy Tarreau9b28e032012-10-12 23:49:43 +02005004 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005005 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005006 }
5007
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005008 /* 1: we might have to print this header in debug mode */
5009 if (unlikely((global.mode & MODE_DEBUG) &&
5010 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005011 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005012 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005013
Willy Tarreau9b28e032012-10-12 23:49:43 +02005014 sol = rep->buf->p;
5015 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005016 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005017
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005018 sol += hdr_idx_first_pos(&txn->hdr_idx);
5019 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005021 while (cur_idx) {
5022 eol = sol + txn->hdr_idx.v[cur_idx].len;
5023 debug_hdr("srvhdr", s, sol, eol);
5024 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5025 cur_idx = txn->hdr_idx.v[cur_idx].next;
5026 }
5027 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005028
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005029 /*
5030 * Now we quickly check if we have found a full valid response.
5031 * If not so, we check the FD and buffer states before leaving.
5032 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005033 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005034 * responses are checked first.
5035 *
5036 * Depending on whether the client is still there or not, we
5037 * may send an error response back or not. Note that normally
5038 * we should only check for HTTP status there, and check I/O
5039 * errors somewhere else.
5040 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041
Willy Tarreau655dce92009-11-08 13:10:58 +01005042 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005043 /* Invalid response */
5044 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5045 /* we detected a parsing error. We want to archive this response
5046 * in the dedicated proxy area for later troubleshooting.
5047 */
5048 hdr_response_bad:
5049 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005050 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005051
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005052 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005053 if (objt_server(s->target)) {
5054 objt_server(s->target)->counters.failed_resp++;
5055 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005056 }
Willy Tarreau64648412010-03-05 10:41:54 +01005057 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005058 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005059 rep->analysers = 0;
5060 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005061 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005062 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005063 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005064
5065 if (!(s->flags & SN_ERR_MASK))
5066 s->flags |= SN_ERR_PRXCOND;
5067 if (!(s->flags & SN_FINST_MASK))
5068 s->flags |= SN_FINST_H;
5069
5070 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005072
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005074 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005075 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005076 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005077 goto hdr_response_bad;
5078 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005080 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005081 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005083 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005084
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005085 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005086 if (objt_server(s->target)) {
5087 objt_server(s->target)->counters.failed_resp++;
5088 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005089 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005090
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005091 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092 rep->analysers = 0;
5093 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005094 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005095 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005096 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005097
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005098 if (!(s->flags & SN_ERR_MASK))
5099 s->flags |= SN_ERR_SRVCL;
5100 if (!(s->flags & SN_FINST_MASK))
5101 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005102 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005104
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005105 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005106 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005107 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005108 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005109
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005110 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005111 if (objt_server(s->target)) {
5112 objt_server(s->target)->counters.failed_resp++;
5113 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005114 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005115
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005116 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005117 rep->analysers = 0;
5118 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005119 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005120 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005121 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005122
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123 if (!(s->flags & SN_ERR_MASK))
5124 s->flags |= SN_ERR_SRVTO;
5125 if (!(s->flags & SN_FINST_MASK))
5126 s->flags |= SN_FINST_H;
5127 return 0;
5128 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005129
Willy Tarreauf003d372012-11-26 13:35:37 +01005130 /* client abort with an abortonclose */
5131 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5132 s->fe->fe_counters.cli_aborts++;
5133 s->be->be_counters.cli_aborts++;
5134 if (objt_server(s->target))
5135 objt_server(s->target)->counters.cli_aborts++;
5136
5137 rep->analysers = 0;
5138 channel_auto_close(rep);
5139
5140 txn->status = 400;
5141 bi_erase(rep);
5142 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5143
5144 if (!(s->flags & SN_ERR_MASK))
5145 s->flags |= SN_ERR_CLICL;
5146 if (!(s->flags & SN_FINST_MASK))
5147 s->flags |= SN_FINST_H;
5148
5149 /* process_session() will take care of the error */
5150 return 0;
5151 }
5152
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005153 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005154 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005155 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005156 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005157
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005158 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005159 if (objt_server(s->target)) {
5160 objt_server(s->target)->counters.failed_resp++;
5161 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005162 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005163
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005164 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005165 rep->analysers = 0;
5166 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005167 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005168 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005169 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005170
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005171 if (!(s->flags & SN_ERR_MASK))
5172 s->flags |= SN_ERR_SRVCL;
5173 if (!(s->flags & SN_FINST_MASK))
5174 s->flags |= SN_FINST_H;
5175 return 0;
5176 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005177
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005179 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005180 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005181 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005182
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005183 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005184 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005185 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005186
5187 if (!(s->flags & SN_ERR_MASK))
5188 s->flags |= SN_ERR_CLICL;
5189 if (!(s->flags & SN_FINST_MASK))
5190 s->flags |= SN_FINST_H;
5191
5192 /* process_session() will take care of the error */
5193 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005194 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005195
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005196 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005197 return 0;
5198 }
5199
5200 /* More interesting part now : we know that we have a complete
5201 * response which at least looks like HTTP. We have an indicator
5202 * of each header's length, so we can parse them quickly.
5203 */
5204
5205 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005206 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005207
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005208 /*
5209 * 1: get the status code
5210 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005211 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005212 if (n < 1 || n > 5)
5213 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005214 /* when the client triggers a 4xx from the server, it's most often due
5215 * to a missing object or permission. These events should be tracked
5216 * because if they happen often, it may indicate a brute force or a
5217 * vulnerability scan.
5218 */
5219 if (n == 4)
5220 session_inc_http_err_ctr(s);
5221
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005222 if (objt_server(s->target))
5223 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005224
Willy Tarreau5b154472009-12-21 20:11:07 +01005225 /* check if the response is HTTP/1.1 or above */
5226 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005227 ((rep->buf->p[5] > '1') ||
5228 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005229 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005230
5231 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005232 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 +01005233
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005234 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005235 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005236
Willy Tarreau9b28e032012-10-12 23:49:43 +02005237 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005238
Willy Tarreau39650402010-03-15 19:44:39 +01005239 /* Adjust server's health based on status code. Note: status codes 501
5240 * and 505 are triggered on demand by client request, so we must not
5241 * count them as server failures.
5242 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005243 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005244 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005245 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005246 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005247 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005248 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005249
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005250 /*
5251 * 2: check for cacheability.
5252 */
5253
5254 switch (txn->status) {
5255 case 200:
5256 case 203:
5257 case 206:
5258 case 300:
5259 case 301:
5260 case 410:
5261 /* RFC2616 @13.4:
5262 * "A response received with a status code of
5263 * 200, 203, 206, 300, 301 or 410 MAY be stored
5264 * by a cache (...) unless a cache-control
5265 * directive prohibits caching."
5266 *
5267 * RFC2616 @9.5: POST method :
5268 * "Responses to this method are not cacheable,
5269 * unless the response includes appropriate
5270 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005271 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005272 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005273 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005274 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5275 break;
5276 default:
5277 break;
5278 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005279
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005280 /*
5281 * 3: we may need to capture headers
5282 */
5283 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005284 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005285 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005286 txn->rsp.cap, s->fe->rsp_cap);
5287
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005288 /* 4: determine the transfer-length.
5289 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5290 * the presence of a message-body in a RESPONSE and its transfer length
5291 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005292 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005293 * All responses to the HEAD request method MUST NOT include a
5294 * message-body, even though the presence of entity-header fields
5295 * might lead one to believe they do. All 1xx (informational), 204
5296 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5297 * message-body. All other responses do include a message-body,
5298 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005299 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005300 * 1. Any response which "MUST NOT" include a message-body (such as the
5301 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5302 * always terminated by the first empty line after the header fields,
5303 * regardless of the entity-header fields present in the message.
5304 *
5305 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5306 * the "chunked" transfer-coding (Section 6.2) is used, the
5307 * transfer-length is defined by the use of this transfer-coding.
5308 * If a Transfer-Encoding header field is present and the "chunked"
5309 * transfer-coding is not present, the transfer-length is defined by
5310 * the sender closing the connection.
5311 *
5312 * 3. If a Content-Length header field is present, its decimal value in
5313 * OCTETs represents both the entity-length and the transfer-length.
5314 * If a message is received with both a Transfer-Encoding header
5315 * field and a Content-Length header field, the latter MUST be ignored.
5316 *
5317 * 4. If the message uses the media type "multipart/byteranges", and
5318 * the transfer-length is not otherwise specified, then this self-
5319 * delimiting media type defines the transfer-length. This media
5320 * type MUST NOT be used unless the sender knows that the recipient
5321 * can parse it; the presence in a request of a Range header with
5322 * multiple byte-range specifiers from a 1.1 client implies that the
5323 * client can parse multipart/byteranges responses.
5324 *
5325 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005326 */
5327
5328 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005329 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005330 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005331 * FIXME: should we parse anyway and return an error on chunked encoding ?
5332 */
5333 if (txn->meth == HTTP_METH_HEAD ||
5334 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005335 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005336 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005337 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005338 goto skip_content_length;
5339 }
5340
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005341 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005342 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005343 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005344 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005345 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005346 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5347 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005348 /* bad transfer-encoding (chunked followed by something else) */
5349 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005350 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005351 break;
5352 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005353 }
5354
5355 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5356 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005357 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005358 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005359 signed long long cl;
5360
Willy Tarreauad14f752011-09-02 20:33:27 +02005361 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005362 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005363 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005364 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005365
Willy Tarreauad14f752011-09-02 20:33:27 +02005366 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005367 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005368 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005369 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005370
Willy Tarreauad14f752011-09-02 20:33:27 +02005371 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005372 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005373 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005374 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005375
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005376 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005377 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005378 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005379 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005380
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005381 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005382 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005383 }
5384
William Lallemand82fe75c2012-10-23 10:25:10 +02005385 if (s->fe->comp || s->be->comp)
5386 select_compression_response_header(s, rep->buf);
5387
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005388 /* FIXME: we should also implement the multipart/byterange method.
5389 * For now on, we resort to close mode in this case (unknown length).
5390 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005391skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005392
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005393 /* end of job, return OK */
5394 rep->analysers &= ~an_bit;
5395 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005396 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005397 return 1;
5398}
5399
5400/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005401 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5402 * and updates t->rep->analysers. It might make sense to explode it into several
5403 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005404 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005405int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005406{
5407 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005408 struct http_msg *msg = &txn->rsp;
5409 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005410 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005411
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005412 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 +02005413 now_ms, __FUNCTION__,
5414 t,
5415 rep,
5416 rep->rex, rep->wex,
5417 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005418 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419 rep->analysers);
5420
Willy Tarreau655dce92009-11-08 13:10:58 +01005421 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005422 return 0;
5423
5424 rep->analysers &= ~an_bit;
5425 rep->analyse_exp = TICK_ETERNITY;
5426
Willy Tarreau5b154472009-12-21 20:11:07 +01005427 /* Now we have to check if we need to modify the Connection header.
5428 * This is more difficult on the response than it is on the request,
5429 * because we can have two different HTTP versions and we don't know
5430 * how the client will interprete a response. For instance, let's say
5431 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5432 * HTTP/1.1 response without any header. Maybe it will bound itself to
5433 * HTTP/1.0 because it only knows about it, and will consider the lack
5434 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5435 * the lack of header as a keep-alive. Thus we will use two flags
5436 * indicating how a request MAY be understood by the client. In case
5437 * of multiple possibilities, we'll fix the header to be explicit. If
5438 * ambiguous cases such as both close and keepalive are seen, then we
5439 * will fall back to explicit close. Note that we won't take risks with
5440 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005441 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005442 */
5443
Willy Tarreaudc008c52010-02-01 16:20:08 +01005444 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5445 txn->status == 101)) {
5446 /* Either we've established an explicit tunnel, or we're
5447 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005448 * to understand the next protocols. We have to switch to tunnel
5449 * mode, so that we transfer the request and responses then let
5450 * this protocol pass unmodified. When we later implement specific
5451 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005452 * header which contains information about that protocol for
5453 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005454 */
5455 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5456 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005457 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5458 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5459 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005460 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005461
Willy Tarreau60466522010-01-18 19:08:45 +01005462 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005463 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005464 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5465 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005466
Willy Tarreau60466522010-01-18 19:08:45 +01005467 /* now adjust header transformations depending on current state */
5468 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5469 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5470 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005471 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005472 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005473 }
Willy Tarreau60466522010-01-18 19:08:45 +01005474 else { /* SCL / KAL */
5475 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005476 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005477 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005478 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005479
Willy Tarreau60466522010-01-18 19:08:45 +01005480 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005481 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005482
Willy Tarreau60466522010-01-18 19:08:45 +01005483 /* Some keep-alive responses are converted to Server-close if
5484 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005485 */
Willy Tarreau60466522010-01-18 19:08:45 +01005486 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5487 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005488 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005489 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005490 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005491 }
5492
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005493 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005494 /*
5495 * 3: we will have to evaluate the filters.
5496 * As opposed to version 1.2, now they will be evaluated in the
5497 * filters order and not in the header order. This means that
5498 * each filter has to be validated among all headers.
5499 *
5500 * Filters are tried with ->be first, then with ->fe if it is
5501 * different from ->be.
5502 */
5503
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005504 cur_proxy = t->be;
5505 while (1) {
5506 struct proxy *rule_set = cur_proxy;
5507
5508 /* try headers filters */
5509 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005510 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005511 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005512 if (objt_server(t->target)) {
5513 objt_server(t->target)->counters.failed_resp++;
5514 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005515 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005516 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005517 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005518 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005519 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005520 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005521 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005522 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005523 if (!(t->flags & SN_ERR_MASK))
5524 t->flags |= SN_ERR_PRXCOND;
5525 if (!(t->flags & SN_FINST_MASK))
5526 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005527 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005528 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005529 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005530
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005531 /* has the response been denied ? */
5532 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005533 if (objt_server(t->target))
5534 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005536 t->be->be_counters.denied_resp++;
5537 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005538 if (t->listener->counters)
5539 t->listener->counters->denied_resp++;
5540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005541 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005542 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005543
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005544 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005545 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005546 if (txn->status < 200)
5547 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005548 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005549 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005550 ret = acl_pass(ret);
5551 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5552 ret = !ret;
5553 if (!ret)
5554 continue;
5555 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005556 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005557 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005558 }
5559
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005560 /* check whether we're already working on the frontend */
5561 if (cur_proxy == t->fe)
5562 break;
5563 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005564 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005565
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005566 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005567 * We may be facing a 100-continue response, in which case this
5568 * is not the right response, and we're waiting for the next one.
5569 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005570 * next one.
5571 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005572 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005573 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005574 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005575 msg->msg_state = HTTP_MSG_RPBEFORE;
5576 txn->status = 0;
5577 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5578 return 1;
5579 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005580 else if (unlikely(txn->status < 200))
5581 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005582
5583 /* we don't have any 1xx status code now */
5584
5585 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005586 * 4: check for server cookie.
5587 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005588 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5589 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005590 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005591
Willy Tarreaubaaee002006-06-26 02:48:02 +02005592
Willy Tarreaua15645d2007-03-18 16:22:39 +01005593 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005594 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005595 */
Willy Tarreau67402132012-05-31 20:40:20 +02005596 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005597 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005598
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005599 /*
5600 * 6: add server cookie in the response if needed
5601 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005602 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005603 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005604 (!(t->flags & SN_DIRECT) ||
5605 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5606 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5607 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5608 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005609 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005610 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005611 /* the server is known, it's not the one the client requested, or the
5612 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005613 * insert a set-cookie here, except if we want to insert only on POST
5614 * requests and this one isn't. Note that servers which don't have cookies
5615 * (eg: some backup servers) will return a full cookie removal request.
5616 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005617 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005618 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005619 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5620 t->be->cookie_name);
5621 }
5622 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005623 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005624
5625 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5626 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005627 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5628 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5629 trash.len += 5;
5630
Willy Tarreauef4f3912010-10-07 21:00:29 +02005631 if (t->be->cookie_maxlife) {
5632 /* emit first_date, which is either the original one or
5633 * the current date.
5634 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005635 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005636 s30tob64(txn->cookie_first_date ?
5637 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005638 (date.tv_sec+3) >> 2, trash.str + trash.len);
5639 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005640 }
5641 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005642 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005643 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005644
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005645 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005646 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005647
Willy Tarreau4992dd22012-05-31 21:02:17 +02005648 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005649 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005650
5651 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005652 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005653
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005654 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005655 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005656
Willy Tarreauf1348312010-10-07 15:54:11 +02005657 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005658 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005659 /* the server did not change, only the date was updated */
5660 txn->flags |= TX_SCK_UPDATED;
5661 else
5662 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005663
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005664 /* Here, we will tell an eventual cache on the client side that we don't
5665 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5666 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5667 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5668 */
Willy Tarreau67402132012-05-31 20:40:20 +02005669 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005670
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005671 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5672
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005673 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005674 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005675 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005676 }
5677 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005678
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005679 /*
5680 * 7: check if result will be cacheable with a cookie.
5681 * We'll block the response if security checks have caught
5682 * nasty things such as a cacheable cookie.
5683 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005684 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5685 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005686 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005687
5688 /* we're in presence of a cacheable response containing
5689 * a set-cookie header. We'll block it as requested by
5690 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005691 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005692 if (objt_server(t->target))
5693 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005694
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005695 t->be->be_counters.denied_resp++;
5696 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005697 if (t->listener->counters)
5698 t->listener->counters->denied_resp++;
5699
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005700 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005701 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005702 send_log(t->be, LOG_ALERT,
5703 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005704 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005705 goto return_srv_prx_502;
5706 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005707
5708 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005709 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005710 * If an "Upgrade" token is found, the header is left untouched in order
5711 * not to have to deal with some client bugs : some of them fail an upgrade
5712 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005713 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005714 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5715 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5716 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005717 unsigned int want_flags = 0;
5718
5719 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5720 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5721 /* we want a keep-alive response here. Keep-alive header
5722 * required if either side is not 1.1.
5723 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005724 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005725 want_flags |= TX_CON_KAL_SET;
5726 }
5727 else {
5728 /* we want a close response here. Close header required if
5729 * the server is 1.1, regardless of the client.
5730 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005731 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005732 want_flags |= TX_CON_CLO_SET;
5733 }
5734
5735 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005736 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005737 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005738
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005739 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005740 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005741 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005742 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005743
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005744 /*************************************************************
5745 * OK, that's finished for the headers. We have done what we *
5746 * could. Let's switch to the DATA state. *
5747 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005748
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005749 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005750
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005751 /* if the user wants to log as soon as possible, without counting
5752 * bytes from the server, then this is the right moment. We have
5753 * to temporarily assign bytes_out to log what we currently have.
5754 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005755 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005756 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5757 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005758 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005759 t->logs.bytes_out = 0;
5760 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005761
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005762 /* Note: we must not try to cheat by jumping directly to DATA,
5763 * otherwise we would not let the client side wake up.
5764 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005765
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005766 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005767 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005768 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005769}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005770
Willy Tarreaud98cf932009-12-27 22:54:55 +01005771/* This function is an analyser which forwards response body (including chunk
5772 * sizes if any). It is called as soon as we must forward, even if we forward
5773 * zero byte. The only situation where it must not be called is when we're in
5774 * tunnel mode and we want to forward till the close. It's used both to forward
5775 * remaining data and to resync after end of body. It expects the msg_state to
5776 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5777 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005778 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005779 * bytes of pending data + the headers if not already done (between sol and sov).
5780 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005781 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005782int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005783{
5784 struct http_txn *txn = &s->txn;
5785 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005786 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005787 static struct buffer *tmpbuf = NULL;
5788 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005789 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005790 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005791
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005792 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5793 return 0;
5794
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005795 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005796 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005797 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005798 /* Output closed while we were sending data. We must abort and
5799 * wake the other side up.
5800 */
5801 msg->msg_state = HTTP_MSG_ERROR;
5802 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005803 return 1;
5804 }
5805
Willy Tarreau4fe41902010-06-07 22:27:41 +02005806 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005807 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005808
William Lallemand82fe75c2012-10-23 10:25:10 +02005809 /* this is the first time we need the compression buffer */
5810 if (s->comp_algo != NULL && tmpbuf == NULL) {
5811 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5812 goto aborted_xfer; /* no memory */
5813 }
5814
Willy Tarreaud98cf932009-12-27 22:54:55 +01005815 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005816 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005817 * rep->buf.p still points to the beginning of the message and msg->sol
5818 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005819 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005820 channel_forward(res, msg->sov);
5821 msg->next = 0;
5822 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005823
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005824 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005825 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005826 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005827 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005828 }
5829
William Lallemand82fe75c2012-10-23 10:25:10 +02005830 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005831 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005832 if (ret < 0)
5833 goto missing_data; /* not enough spaces in buffers */
5834 compressing = 1;
5835 }
5836
Willy Tarreaud98cf932009-12-27 22:54:55 +01005837 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005838 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005839 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005840 if (s->comp_algo == NULL) {
5841 bytes = msg->sov - msg->sol;
5842 if (msg->chunk_len || bytes) {
5843 msg->sol = msg->sov;
5844 msg->next -= bytes; /* will be forwarded */
5845 msg->chunk_len += bytes;
5846 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5847 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005848 }
5849
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005850 switch (msg->msg_state - HTTP_MSG_DATA) {
5851 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005852 if (compressing) {
5853 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5854 if (ret < 0)
5855 goto aborted_xfer;
5856 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005857
5858 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005859 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005860
5861 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005862 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005863 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005864 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005865 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005866 if (compressing && consumed_data) {
5867 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5868 compressing = 0;
5869 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005870 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005871 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005872 /* fall through for HTTP_MSG_CHUNK_CRLF */
5873
5874 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5875 /* we want the CRLF after the data */
5876
5877 ret = http_skip_chunk_crlf(msg);
5878 if (ret == 0)
5879 goto missing_data;
5880 else if (ret < 0) {
5881 if (msg->err_pos >= 0)
5882 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5883 goto return_bad_res;
5884 }
5885 /* skipping data in buffer for compression */
5886 if (compressing) {
5887 b_adv(res->buf, msg->next);
5888 msg->next = 0;
5889 msg->sov = 0;
5890 msg->sol = 0;
5891 }
5892 /* we're in MSG_CHUNK_SIZE now, fall through */
5893
5894 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005895 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005896 * set ->sov and ->next to point to the body and switch to DATA or
5897 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005898 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005899
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005900 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005901 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005902 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005903 else if (ret < 0) {
5904 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005905 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005906 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005907 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005908 if (compressing) {
5909 if (likely(msg->chunk_len > 0)) {
5910 /* skipping data if we are in compression mode */
5911 b_adv(res->buf, msg->next);
5912 msg->next = 0;
5913 msg->sov = 0;
5914 msg->sol = 0;
5915 } else {
5916 if (consumed_data) {
5917 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5918 compressing = 0;
5919 }
5920 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005921 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005922 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005923 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005924
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005925 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5926 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005927 if (ret == 0)
5928 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005929 else if (ret < 0) {
5930 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005931 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005932 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005933 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005934 if (s->comp_algo != NULL) {
5935 /* forwarding trailers */
5936 channel_forward(res, msg->next);
5937 msg->next = 0;
5938 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02005939 /* we're in HTTP_MSG_DONE now, but we might still have
5940 * some data pending, so let's loop over once.
5941 */
5942 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005943
5944 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005945 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005946
5947 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005948 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005949 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5950 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005951 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005952 if (http_resync_states(s)) {
5953 http_silent_debug(__LINE__, s);
5954 /* some state changes occurred, maybe the analyser
5955 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005956 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005957 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005958 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005959 /* response errors are most likely due to
5960 * the client aborting the transfer.
5961 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005962 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005963 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005964 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005965 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005966 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005967 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005968 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005969 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005970 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005971 }
5972 }
5973
Willy Tarreaud98cf932009-12-27 22:54:55 +01005974 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005975 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005976 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5977 compressing = 0;
5978 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005979
5980 if (res->flags & CF_SHUTW)
5981 goto aborted_xfer;
5982
5983 /* stop waiting for data if the input is closed before the end. If the
5984 * client side was already closed, it means that the client has aborted,
5985 * so we don't want to count this as a server abort. Otherwise it's a
5986 * server abort.
5987 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005988 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005989 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
5990 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01005991 if (!(s->flags & SN_ERR_MASK))
5992 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005993 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005994 if (objt_server(s->target))
5995 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005996 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005997 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005998
Willy Tarreau40dba092010-03-04 18:14:51 +01005999 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006000 if (!s->req->analysers)
6001 goto return_bad_res;
6002
Willy Tarreauea953162012-05-18 23:41:28 +02006003 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006004 if (s->comp_algo == NULL) {
6005 bytes = msg->sov - msg->sol;
6006 if (msg->chunk_len || bytes) {
6007 msg->sol = msg->sov;
6008 msg->next -= bytes; /* will be forwarded */
6009 msg->chunk_len += bytes;
6010 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6011 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006012 }
6013
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006014 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006015 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006016 * Similarly, with keep-alive on the client side, we don't want to forward a
6017 * close.
6018 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006019 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006020 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6021 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006022 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006023
Willy Tarreau5c620922011-05-11 19:56:11 +02006024 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006025 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006026 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006027 * modes are already handled by the stream sock layer. We must not do
6028 * this in content-length mode because it could present the MSG_MORE
6029 * flag with the last block of forwarded data, which would cause an
6030 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006031 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006032 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006033 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006034
Willy Tarreaud98cf932009-12-27 22:54:55 +01006035 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006036 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006037 return 0;
6038
Willy Tarreau40dba092010-03-04 18:14:51 +01006039 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006040 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006041 if (objt_server(s->target))
6042 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006043
6044 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006045 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006046 /* don't send any error message as we're in the body */
6047 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006048 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006049 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006050 if (objt_server(s->target))
6051 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006052
6053 if (!(s->flags & SN_ERR_MASK))
6054 s->flags |= SN_ERR_PRXCOND;
6055 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006056 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006057 return 0;
6058
6059 aborted_xfer:
6060 txn->rsp.msg_state = HTTP_MSG_ERROR;
6061 /* don't send any error message as we're in the body */
6062 stream_int_retnclose(res->cons, NULL);
6063 res->analysers = 0;
6064 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6065
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006066 s->fe->fe_counters.cli_aborts++;
6067 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006068 if (objt_server(s->target))
6069 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006070
6071 if (!(s->flags & SN_ERR_MASK))
6072 s->flags |= SN_ERR_CLICL;
6073 if (!(s->flags & SN_FINST_MASK))
6074 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006075 return 0;
6076}
6077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006078/* Iterate the same filter through all request headers.
6079 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006080 * Since it can manage the switch to another backend, it updates the per-proxy
6081 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006083int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006085 char term;
6086 char *cur_ptr, *cur_end, *cur_next;
6087 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006088 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006090 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006092 last_hdr = 0;
6093
Willy Tarreau9b28e032012-10-12 23:49:43 +02006094 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006095 old_idx = 0;
6096
6097 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006098 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006099 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006100 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006101 (exp->action == ACT_ALLOW ||
6102 exp->action == ACT_DENY ||
6103 exp->action == ACT_TARPIT))
6104 return 0;
6105
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006106 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006107 if (!cur_idx)
6108 break;
6109
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006110 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006111 cur_ptr = cur_next;
6112 cur_end = cur_ptr + cur_hdr->len;
6113 cur_next = cur_end + cur_hdr->cr + 1;
6114
6115 /* Now we have one header between cur_ptr and cur_end,
6116 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006117 */
6118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119 /* The annoying part is that pattern matching needs
6120 * that we modify the contents to null-terminate all
6121 * strings before testing them.
6122 */
6123
6124 term = *cur_end;
6125 *cur_end = '\0';
6126
6127 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6128 switch (exp->action) {
6129 case ACT_SETBE:
6130 /* It is not possible to jump a second time.
6131 * FIXME: should we return an HTTP/500 here so that
6132 * the admin knows there's a problem ?
6133 */
6134 if (t->be != t->fe)
6135 break;
6136
6137 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006138 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 last_hdr = 1;
6140 break;
6141
6142 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006143 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006144 last_hdr = 1;
6145 break;
6146
6147 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006148 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006149 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006150
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006151 t->fe->fe_counters.denied_req++;
6152 if (t->fe != t->be)
6153 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006154 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006155 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006157 break;
6158
6159 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006160 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006161 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006162
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006163 t->fe->fe_counters.denied_req++;
6164 if (t->fe != t->be)
6165 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006166 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006167 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006169 break;
6170
6171 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006172 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6173 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006174 /* FIXME: if the user adds a newline in the replacement, the
6175 * index will not be recalculated for now, and the new line
6176 * will not be counted as a new header.
6177 */
6178
6179 cur_end += delta;
6180 cur_next += delta;
6181 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006182 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006183 break;
6184
6185 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006186 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006187 cur_next += delta;
6188
Willy Tarreaufa355d42009-11-29 18:12:29 +01006189 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006190 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6191 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006192 cur_hdr->len = 0;
6193 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006194 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006195 break;
6196
6197 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006198 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006199 if (cur_end)
6200 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006202 /* keep the link from this header to next one in case of later
6203 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006204 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006205 old_idx = cur_idx;
6206 }
6207 return 0;
6208}
6209
6210
6211/* Apply the filter to the request line.
6212 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6213 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006214 * Since it can manage the switch to another backend, it updates the per-proxy
6215 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006216 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006217int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006218{
6219 char term;
6220 char *cur_ptr, *cur_end;
6221 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006222 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006223 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224
Willy Tarreau3d300592007-03-18 18:34:41 +01006225 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006226 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006227 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006228 (exp->action == ACT_ALLOW ||
6229 exp->action == ACT_DENY ||
6230 exp->action == ACT_TARPIT))
6231 return 0;
6232 else if (exp->action == ACT_REMOVE)
6233 return 0;
6234
6235 done = 0;
6236
Willy Tarreau9b28e032012-10-12 23:49:43 +02006237 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006238 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006239
6240 /* Now we have the request line between cur_ptr and cur_end */
6241
6242 /* The annoying part is that pattern matching needs
6243 * that we modify the contents to null-terminate all
6244 * strings before testing them.
6245 */
6246
6247 term = *cur_end;
6248 *cur_end = '\0';
6249
6250 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6251 switch (exp->action) {
6252 case ACT_SETBE:
6253 /* It is not possible to jump a second time.
6254 * FIXME: should we return an HTTP/500 here so that
6255 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006257 if (t->be != t->fe)
6258 break;
6259
6260 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006261 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006262 done = 1;
6263 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006264
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006265 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006266 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006267 done = 1;
6268 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006270 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006271 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006272
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006273 t->fe->fe_counters.denied_req++;
6274 if (t->fe != t->be)
6275 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006276 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006277 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006278
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006279 done = 1;
6280 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006282 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006283 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006284
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006285 t->fe->fe_counters.denied_req++;
6286 if (t->fe != t->be)
6287 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006288 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006289 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006291 done = 1;
6292 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006294 case ACT_REPLACE:
6295 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006296 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6297 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006298 /* FIXME: if the user adds a newline in the replacement, the
6299 * index will not be recalculated for now, and the new line
6300 * will not be counted as a new header.
6301 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006302
Willy Tarreaufa355d42009-11-29 18:12:29 +01006303 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006304 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006305 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006306 HTTP_MSG_RQMETH,
6307 cur_ptr, cur_end + 1,
6308 NULL, NULL);
6309 if (unlikely(!cur_end))
6310 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006312 /* we have a full request and we know that we have either a CR
6313 * or an LF at <ptr>.
6314 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006315 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6316 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 /* there is no point trying this regex on headers */
6318 return 1;
6319 }
6320 }
6321 *cur_end = term; /* restore the string terminator */
6322 return done;
6323}
Willy Tarreau97de6242006-12-27 17:18:38 +01006324
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006327/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006328 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006329 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006330 * unparsable request. Since it can manage the switch to another backend, it
6331 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006332 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006333int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006334{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006335 struct http_txn *txn = &s->txn;
6336 struct hdr_exp *exp;
6337
6338 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006339 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006341 /*
6342 * The interleaving of transformations and verdicts
6343 * makes it difficult to decide to continue or stop
6344 * the evaluation.
6345 */
6346
Willy Tarreau6c123b12010-01-28 20:22:06 +01006347 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6348 break;
6349
Willy Tarreau3d300592007-03-18 18:34:41 +01006350 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006351 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006352 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006354
6355 /* if this filter had a condition, evaluate it now and skip to
6356 * next filter if the condition does not match.
6357 */
6358 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006359 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006360 ret = acl_pass(ret);
6361 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6362 ret = !ret;
6363
6364 if (!ret)
6365 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006366 }
6367
6368 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006369 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 if (unlikely(ret < 0))
6371 return -1;
6372
6373 if (likely(ret == 0)) {
6374 /* The filter did not match the request, it can be
6375 * iterated through all headers.
6376 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006377 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381}
6382
6383
Willy Tarreaua15645d2007-03-18 16:22:39 +01006384
Willy Tarreau58f10d72006-12-04 02:26:12 +01006385/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006386 * Try to retrieve the server associated to the appsession.
6387 * If the server is found, it's assigned to the session.
6388 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006389void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006390 struct http_txn *txn = &t->txn;
6391 appsess *asession = NULL;
6392 char *sessid_temp = NULL;
6393
Cyril Bontéb21570a2009-11-29 20:04:48 +01006394 if (len > t->be->appsession_len) {
6395 len = t->be->appsession_len;
6396 }
6397
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006398 if (t->be->options2 & PR_O2_AS_REQL) {
6399 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006400 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006401 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006402 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006403 }
6404
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006405 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006406 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6407 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6408 return;
6409 }
6410
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006411 memcpy(txn->sessid, buf, len);
6412 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006413 }
6414
6415 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6416 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6417 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6418 return;
6419 }
6420
Cyril Bontéb21570a2009-11-29 20:04:48 +01006421 memcpy(sessid_temp, buf, len);
6422 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006423
6424 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6425 /* free previously allocated memory */
6426 pool_free2(apools.sessid, sessid_temp);
6427
6428 if (asession != NULL) {
6429 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6430 if (!(t->be->options2 & PR_O2_AS_REQL))
6431 asession->request_count++;
6432
6433 if (asession->serverid != NULL) {
6434 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006435
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006436 while (srv) {
6437 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006438 if ((srv->state & SRV_RUNNING) ||
6439 (t->be->options & PR_O_PERSIST) ||
6440 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006441 /* we found the server and it's usable */
6442 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006443 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006444 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006445 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006446
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006447 break;
6448 } else {
6449 txn->flags &= ~TX_CK_MASK;
6450 txn->flags |= TX_CK_DOWN;
6451 }
6452 }
6453 srv = srv->next;
6454 }
6455 }
6456 }
6457}
6458
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006459/* Find the end of a cookie value contained between <s> and <e>. It works the
6460 * same way as with headers above except that the semi-colon also ends a token.
6461 * See RFC2965 for more information. Note that it requires a valid header to
6462 * return a valid result.
6463 */
6464char *find_cookie_value_end(char *s, const char *e)
6465{
6466 int quoted, qdpair;
6467
6468 quoted = qdpair = 0;
6469 for (; s < e; s++) {
6470 if (qdpair) qdpair = 0;
6471 else if (quoted) {
6472 if (*s == '\\') qdpair = 1;
6473 else if (*s == '"') quoted = 0;
6474 }
6475 else if (*s == '"') quoted = 1;
6476 else if (*s == ',' || *s == ';') return s;
6477 }
6478 return s;
6479}
6480
6481/* Delete a value in a header between delimiters <from> and <next> in buffer
6482 * <buf>. The number of characters displaced is returned, and the pointer to
6483 * the first delimiter is updated if required. The function tries as much as
6484 * possible to respect the following principles :
6485 * - replace <from> delimiter by the <next> one unless <from> points to a
6486 * colon, in which case <next> is simply removed
6487 * - set exactly one space character after the new first delimiter, unless
6488 * there are not enough characters in the block being moved to do so.
6489 * - remove unneeded spaces before the previous delimiter and after the new
6490 * one.
6491 *
6492 * It is the caller's responsibility to ensure that :
6493 * - <from> points to a valid delimiter or the colon ;
6494 * - <next> points to a valid delimiter or the final CR/LF ;
6495 * - there are non-space chars before <from> ;
6496 * - there is a CR/LF at or after <next>.
6497 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006498int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006499{
6500 char *prev = *from;
6501
6502 if (*prev == ':') {
6503 /* We're removing the first value, preserve the colon and add a
6504 * space if possible.
6505 */
6506 if (!http_is_crlf[(unsigned char)*next])
6507 next++;
6508 prev++;
6509 if (prev < next)
6510 *prev++ = ' ';
6511
6512 while (http_is_spht[(unsigned char)*next])
6513 next++;
6514 } else {
6515 /* Remove useless spaces before the old delimiter. */
6516 while (http_is_spht[(unsigned char)*(prev-1)])
6517 prev--;
6518 *from = prev;
6519
6520 /* copy the delimiter and if possible a space if we're
6521 * not at the end of the line.
6522 */
6523 if (!http_is_crlf[(unsigned char)*next]) {
6524 *prev++ = *next++;
6525 if (prev + 1 < next)
6526 *prev++ = ' ';
6527 while (http_is_spht[(unsigned char)*next])
6528 next++;
6529 }
6530 }
6531 return buffer_replace2(buf, prev, next, NULL, 0);
6532}
6533
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006534/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006535 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006536 * desirable to call it only when needed. This code is quite complex because
6537 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6538 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006539 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006540void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006541{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006542 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006543 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006544 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006545 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6546 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006549 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006550 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006552 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006553 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006554 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006555
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006556 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006557 hdr_beg = hdr_next;
6558 hdr_end = hdr_beg + cur_hdr->len;
6559 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006561 /* We have one full header between hdr_beg and hdr_end, and the
6562 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563 * "Cookie:" headers.
6564 */
6565
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006566 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006567 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568 old_idx = cur_idx;
6569 continue;
6570 }
6571
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006572 del_from = NULL; /* nothing to be deleted */
6573 preserve_hdr = 0; /* assume we may kill the whole header */
6574
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575 /* Now look for cookies. Conforming to RFC2109, we have to support
6576 * attributes whose name begin with a '$', and associate them with
6577 * the right cookie, if we want to delete this cookie.
6578 * So there are 3 cases for each cookie read :
6579 * 1) it's a special attribute, beginning with a '$' : ignore it.
6580 * 2) it's a server id cookie that we *MAY* want to delete : save
6581 * some pointers on it (last semi-colon, beginning of cookie...)
6582 * 3) it's an application cookie : we *MAY* have to delete a previous
6583 * "special" cookie.
6584 * At the end of loop, if a "special" cookie remains, we may have to
6585 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 * *MUST* delete it.
6587 *
6588 * Note: RFC2965 is unclear about the processing of spaces around
6589 * the equal sign in the ATTR=VALUE form. A careful inspection of
6590 * the RFC explicitly allows spaces before it, and not within the
6591 * tokens (attrs or values). An inspection of RFC2109 allows that
6592 * too but section 10.1.3 lets one think that spaces may be allowed
6593 * after the equal sign too, resulting in some (rare) buggy
6594 * implementations trying to do that. So let's do what servers do.
6595 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6596 * allowed quoted strings in values, with any possible character
6597 * after a backslash, including control chars and delimitors, which
6598 * causes parsing to become ambiguous. Browsers also allow spaces
6599 * within values even without quotes.
6600 *
6601 * We have to keep multiple pointers in order to support cookie
6602 * removal at the beginning, middle or end of header without
6603 * corrupting the header. All of these headers are valid :
6604 *
6605 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6606 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6607 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6608 * | | | | | | | | |
6609 * | | | | | | | | hdr_end <--+
6610 * | | | | | | | +--> next
6611 * | | | | | | +----> val_end
6612 * | | | | | +-----------> val_beg
6613 * | | | | +--------------> equal
6614 * | | | +----------------> att_end
6615 * | | +---------------------> att_beg
6616 * | +--------------------------> prev
6617 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006618 */
6619
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006620 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6621 /* Iterate through all cookies on this line */
6622
6623 /* find att_beg */
6624 att_beg = prev + 1;
6625 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6626 att_beg++;
6627
6628 /* find att_end : this is the first character after the last non
6629 * space before the equal. It may be equal to hdr_end.
6630 */
6631 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006632
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006633 while (equal < hdr_end) {
6634 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006635 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006636 if (http_is_spht[(unsigned char)*equal++])
6637 continue;
6638 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006639 }
6640
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006641 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6642 * is between <att_beg> and <equal>, both may be identical.
6643 */
6644
6645 /* look for end of cookie if there is an equal sign */
6646 if (equal < hdr_end && *equal == '=') {
6647 /* look for the beginning of the value */
6648 val_beg = equal + 1;
6649 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6650 val_beg++;
6651
6652 /* find the end of the value, respecting quotes */
6653 next = find_cookie_value_end(val_beg, hdr_end);
6654
6655 /* make val_end point to the first white space or delimitor after the value */
6656 val_end = next;
6657 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6658 val_end--;
6659 } else {
6660 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006661 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006662
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006663 /* We have nothing to do with attributes beginning with '$'. However,
6664 * they will automatically be removed if a header before them is removed,
6665 * since they're supposed to be linked together.
6666 */
6667 if (*att_beg == '$')
6668 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006669
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006670 /* Ignore cookies with no equal sign */
6671 if (equal == next) {
6672 /* This is not our cookie, so we must preserve it. But if we already
6673 * scheduled another cookie for removal, we cannot remove the
6674 * complete header, but we can remove the previous block itself.
6675 */
6676 preserve_hdr = 1;
6677 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006678 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006679 val_end += delta;
6680 next += delta;
6681 hdr_end += delta;
6682 hdr_next += delta;
6683 cur_hdr->len += delta;
6684 http_msg_move_end(&txn->req, delta);
6685 prev = del_from;
6686 del_from = NULL;
6687 }
6688 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006689 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006690
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006691 /* if there are spaces around the equal sign, we need to
6692 * strip them otherwise we'll get trouble for cookie captures,
6693 * or even for rewrites. Since this happens extremely rarely,
6694 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006695 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006696 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6697 int stripped_before = 0;
6698 int stripped_after = 0;
6699
6700 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006701 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006702 equal += stripped_before;
6703 val_beg += stripped_before;
6704 }
6705
6706 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006707 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006708 val_beg += stripped_after;
6709 stripped_before += stripped_after;
6710 }
6711
6712 val_end += stripped_before;
6713 next += stripped_before;
6714 hdr_end += stripped_before;
6715 hdr_next += stripped_before;
6716 cur_hdr->len += stripped_before;
6717 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006718 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006720
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 /* First, let's see if we want to capture this cookie. We check
6722 * that we don't already have a client side cookie, because we
6723 * can only capture one. Also as an optimisation, we ignore
6724 * cookies shorter than the declared name.
6725 */
6726 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6727 (val_end - att_beg >= t->fe->capture_namelen) &&
6728 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6729 int log_len = val_end - att_beg;
6730
6731 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6732 Alert("HTTP logging : out of memory.\n");
6733 } else {
6734 if (log_len > t->fe->capture_len)
6735 log_len = t->fe->capture_len;
6736 memcpy(txn->cli_cookie, att_beg, log_len);
6737 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006739 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006740
Willy Tarreaubca99692010-10-06 19:25:55 +02006741 /* Persistence cookies in passive, rewrite or insert mode have the
6742 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006744 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006745 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006746 * For cookies in prefix mode, the form is :
6747 *
6748 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006749 */
6750 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6751 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6752 struct server *srv = t->be->srv;
6753 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6756 * have the server ID between val_beg and delim, and the original cookie between
6757 * delim+1 and val_end. Otherwise, delim==val_end :
6758 *
6759 * Cookie: NAME=SRV; # in all but prefix modes
6760 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6761 * | || || | |+-> next
6762 * | || || | +--> val_end
6763 * | || || +---------> delim
6764 * | || |+------------> val_beg
6765 * | || +-------------> att_end = equal
6766 * | |+-----------------> att_beg
6767 * | +------------------> prev
6768 * +-------------------------> hdr_beg
6769 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770
Willy Tarreau67402132012-05-31 20:40:20 +02006771 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006772 for (delim = val_beg; delim < val_end; delim++)
6773 if (*delim == COOKIE_DELIM)
6774 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006775 } else {
6776 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006777 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006778 /* Now check if the cookie contains a date field, which would
6779 * appear after a vertical bar ('|') just after the server name
6780 * and before the delimiter.
6781 */
6782 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6783 if (vbar1) {
6784 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006785 * right is the last seen date. It is a base64 encoded
6786 * 30-bit value representing the UNIX date since the
6787 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006788 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006789 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006790 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006791 if (val_end - vbar1 >= 5) {
6792 val = b64tos30(vbar1);
6793 if (val > 0)
6794 txn->cookie_last_date = val << 2;
6795 }
6796 /* look for a second vertical bar */
6797 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6798 if (vbar1 && (val_end - vbar1 > 5)) {
6799 val = b64tos30(vbar1 + 1);
6800 if (val > 0)
6801 txn->cookie_first_date = val << 2;
6802 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006803 }
6804 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006805
Willy Tarreauf64d1412010-10-07 20:06:11 +02006806 /* if the cookie has an expiration date and the proxy wants to check
6807 * it, then we do that now. We first check if the cookie is too old,
6808 * then only if it has expired. We detect strict overflow because the
6809 * time resolution here is not great (4 seconds). Cookies with dates
6810 * in the future are ignored if their offset is beyond one day. This
6811 * allows an admin to fix timezone issues without expiring everyone
6812 * and at the same time avoids keeping unwanted side effects for too
6813 * long.
6814 */
6815 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006816 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6817 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006818 txn->flags &= ~TX_CK_MASK;
6819 txn->flags |= TX_CK_OLD;
6820 delim = val_beg; // let's pretend we have not found the cookie
6821 txn->cookie_first_date = 0;
6822 txn->cookie_last_date = 0;
6823 }
6824 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006825 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6826 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006827 txn->flags &= ~TX_CK_MASK;
6828 txn->flags |= TX_CK_EXPIRED;
6829 delim = val_beg; // let's pretend we have not found the cookie
6830 txn->cookie_first_date = 0;
6831 txn->cookie_last_date = 0;
6832 }
6833
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834 /* Here, we'll look for the first running server which supports the cookie.
6835 * This allows to share a same cookie between several servers, for example
6836 * to dedicate backup servers to specific servers only.
6837 * However, to prevent clients from sticking to cookie-less backup server
6838 * when they have incidentely learned an empty cookie, we simply ignore
6839 * empty cookies and mark them as invalid.
6840 * The same behaviour is applied when persistence must be ignored.
6841 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006842 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006843 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006844
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006845 while (srv) {
6846 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6847 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6848 if ((srv->state & SRV_RUNNING) ||
6849 (t->be->options & PR_O_PERSIST) ||
6850 (t->flags & SN_FORCE_PRST)) {
6851 /* we found the server and we can use it */
6852 txn->flags &= ~TX_CK_MASK;
6853 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6854 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006855 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 break;
6857 } else {
6858 /* we found a server, but it's down,
6859 * mark it as such and go on in case
6860 * another one is available.
6861 */
6862 txn->flags &= ~TX_CK_MASK;
6863 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006864 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006865 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006866 srv = srv->next;
6867 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868
Willy Tarreauf64d1412010-10-07 20:06:11 +02006869 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006870 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006872 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6873 txn->flags |= TX_CK_UNUSED;
6874 else
6875 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006876 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 /* depending on the cookie mode, we may have to either :
6879 * - delete the complete cookie if we're in insert+indirect mode, so that
6880 * the server never sees it ;
6881 * - remove the server id from the cookie value, and tag the cookie as an
6882 * application cookie so that it does not get accidentely removed later,
6883 * if we're in cookie prefix mode
6884 */
Willy Tarreau67402132012-05-31 20:40:20 +02006885 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006887
Willy Tarreau9b28e032012-10-12 23:49:43 +02006888 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 val_end += delta;
6890 next += delta;
6891 hdr_end += delta;
6892 hdr_next += delta;
6893 cur_hdr->len += delta;
6894 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006895
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 del_from = NULL;
6897 preserve_hdr = 1; /* we want to keep this cookie */
6898 }
6899 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006900 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 del_from = prev;
6902 }
6903 } else {
6904 /* This is not our cookie, so we must preserve it. But if we already
6905 * scheduled another cookie for removal, we cannot remove the
6906 * complete header, but we can remove the previous block itself.
6907 */
6908 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006909
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006910 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006911 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006912 if (att_beg >= del_from)
6913 att_beg += delta;
6914 if (att_end >= del_from)
6915 att_end += delta;
6916 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006917 val_end += delta;
6918 next += delta;
6919 hdr_end += delta;
6920 hdr_next += delta;
6921 cur_hdr->len += delta;
6922 http_msg_move_end(&txn->req, delta);
6923 prev = del_from;
6924 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006928 /* Look for the appsession cookie unless persistence must be ignored */
6929 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6930 int cmp_len, value_len;
6931 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006932
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006933 if (t->be->options2 & PR_O2_AS_PFX) {
6934 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6935 value_begin = att_beg + t->be->appsession_name_len;
6936 value_len = val_end - att_beg - t->be->appsession_name_len;
6937 } else {
6938 cmp_len = att_end - att_beg;
6939 value_begin = val_beg;
6940 value_len = val_end - val_beg;
6941 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006942
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006943 /* let's see if the cookie is our appcookie */
6944 if (cmp_len == t->be->appsession_name_len &&
6945 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6946 manage_client_side_appsession(t, value_begin, value_len);
6947 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006948 }
6949
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 /* continue with next cookie on this header line */
6951 att_beg = next;
6952 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006954 /* There are no more cookies on this line.
6955 * We may still have one (or several) marked for deletion at the
6956 * end of the line. We must do this now in two ways :
6957 * - if some cookies must be preserved, we only delete from the
6958 * mark to the end of line ;
6959 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006960 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006961 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006962 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006963 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006964 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006965 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006966 cur_hdr->len += delta;
6967 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006968 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006969
6970 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006971 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6972 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006974 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006977 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006978 }
6979
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006981 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006982 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006983}
6984
6985
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986/* Iterate the same filter through all response headers contained in <rtr>.
6987 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6988 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006989int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006990{
6991 char term;
6992 char *cur_ptr, *cur_end, *cur_next;
6993 int cur_idx, old_idx, last_hdr;
6994 struct http_txn *txn = &t->txn;
6995 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006996 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997
6998 last_hdr = 0;
6999
Willy Tarreau9b28e032012-10-12 23:49:43 +02007000 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001 old_idx = 0;
7002
7003 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007004 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007006 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007 (exp->action == ACT_ALLOW ||
7008 exp->action == ACT_DENY))
7009 return 0;
7010
7011 cur_idx = txn->hdr_idx.v[old_idx].next;
7012 if (!cur_idx)
7013 break;
7014
7015 cur_hdr = &txn->hdr_idx.v[cur_idx];
7016 cur_ptr = cur_next;
7017 cur_end = cur_ptr + cur_hdr->len;
7018 cur_next = cur_end + cur_hdr->cr + 1;
7019
7020 /* Now we have one header between cur_ptr and cur_end,
7021 * and the next header starts at cur_next.
7022 */
7023
7024 /* The annoying part is that pattern matching needs
7025 * that we modify the contents to null-terminate all
7026 * strings before testing them.
7027 */
7028
7029 term = *cur_end;
7030 *cur_end = '\0';
7031
7032 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7033 switch (exp->action) {
7034 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007035 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 last_hdr = 1;
7037 break;
7038
7039 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007040 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007041 last_hdr = 1;
7042 break;
7043
7044 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007045 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7046 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 /* FIXME: if the user adds a newline in the replacement, the
7048 * index will not be recalculated for now, and the new line
7049 * will not be counted as a new header.
7050 */
7051
7052 cur_end += delta;
7053 cur_next += delta;
7054 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007055 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 break;
7057
7058 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007059 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007060 cur_next += delta;
7061
Willy Tarreaufa355d42009-11-29 18:12:29 +01007062 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7064 txn->hdr_idx.used--;
7065 cur_hdr->len = 0;
7066 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007067 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007068 break;
7069
7070 }
7071 }
7072 if (cur_end)
7073 *cur_end = term; /* restore the string terminator */
7074
7075 /* keep the link from this header to next one in case of later
7076 * removal of next header.
7077 */
7078 old_idx = cur_idx;
7079 }
7080 return 0;
7081}
7082
7083
7084/* Apply the filter to the status line in the response buffer <rtr>.
7085 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7086 * or -1 if a replacement resulted in an invalid status line.
7087 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007088int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007089{
7090 char term;
7091 char *cur_ptr, *cur_end;
7092 int done;
7093 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007094 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007095
7096
Willy Tarreau3d300592007-03-18 18:34:41 +01007097 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007098 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007099 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 (exp->action == ACT_ALLOW ||
7101 exp->action == ACT_DENY))
7102 return 0;
7103 else if (exp->action == ACT_REMOVE)
7104 return 0;
7105
7106 done = 0;
7107
Willy Tarreau9b28e032012-10-12 23:49:43 +02007108 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007109 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007110
7111 /* Now we have the status line between cur_ptr and cur_end */
7112
7113 /* The annoying part is that pattern matching needs
7114 * that we modify the contents to null-terminate all
7115 * strings before testing them.
7116 */
7117
7118 term = *cur_end;
7119 *cur_end = '\0';
7120
7121 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7122 switch (exp->action) {
7123 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007124 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125 done = 1;
7126 break;
7127
7128 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007129 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130 done = 1;
7131 break;
7132
7133 case ACT_REPLACE:
7134 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007135 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7136 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137 /* FIXME: if the user adds a newline in the replacement, the
7138 * index will not be recalculated for now, and the new line
7139 * will not be counted as a new header.
7140 */
7141
Willy Tarreaufa355d42009-11-29 18:12:29 +01007142 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007144 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007145 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146 cur_ptr, cur_end + 1,
7147 NULL, NULL);
7148 if (unlikely(!cur_end))
7149 return -1;
7150
7151 /* we have a full respnse and we know that we have either a CR
7152 * or an LF at <ptr>.
7153 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007154 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007155 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 /* there is no point trying this regex on headers */
7157 return 1;
7158 }
7159 }
7160 *cur_end = term; /* restore the string terminator */
7161 return done;
7162}
7163
7164
7165
7166/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007167 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007168 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7169 * unparsable response.
7170 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007171int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007173 struct http_txn *txn = &s->txn;
7174 struct hdr_exp *exp;
7175
7176 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 int ret;
7178
7179 /*
7180 * The interleaving of transformations and verdicts
7181 * makes it difficult to decide to continue or stop
7182 * the evaluation.
7183 */
7184
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007185 if (txn->flags & TX_SVDENY)
7186 break;
7187
Willy Tarreau3d300592007-03-18 18:34:41 +01007188 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7190 exp->action == ACT_PASS)) {
7191 exp = exp->next;
7192 continue;
7193 }
7194
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007195 /* if this filter had a condition, evaluate it now and skip to
7196 * next filter if the condition does not match.
7197 */
7198 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007199 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007200 ret = acl_pass(ret);
7201 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7202 ret = !ret;
7203 if (!ret)
7204 continue;
7205 }
7206
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007208 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 if (unlikely(ret < 0))
7210 return -1;
7211
7212 if (likely(ret == 0)) {
7213 /* The filter did not match the response, it can be
7214 * iterated through all headers.
7215 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007216 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218 }
7219 return 0;
7220}
7221
7222
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007224 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007225 * desirable to call it only when needed. This function is also used when we
7226 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007228void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229{
7230 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007231 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007232 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007234 char *hdr_beg, *hdr_end, *hdr_next;
7235 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 /* Iterate through the headers.
7238 * we start with the start line.
7239 */
7240 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007241 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242
7243 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7244 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007245 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246
7247 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007248 hdr_beg = hdr_next;
7249 hdr_end = hdr_beg + cur_hdr->len;
7250 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251
Willy Tarreau24581ba2010-08-31 22:39:35 +02007252 /* We have one full header between hdr_beg and hdr_end, and the
7253 * next header starts at hdr_next. We're only interested in
7254 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 */
7256
Willy Tarreau24581ba2010-08-31 22:39:35 +02007257 is_cookie2 = 0;
7258 prev = hdr_beg + 10;
7259 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007260 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007261 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7262 if (!val) {
7263 old_idx = cur_idx;
7264 continue;
7265 }
7266 is_cookie2 = 1;
7267 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 }
7269
Willy Tarreau24581ba2010-08-31 22:39:35 +02007270 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7271 * <prev> points to the colon.
7272 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007273 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274
Willy Tarreau24581ba2010-08-31 22:39:35 +02007275 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7276 * check-cache is enabled) and we are not interested in checking
7277 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007278 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007279 if (t->be->cookie_name == NULL &&
7280 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007281 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007282 return;
7283
Willy Tarreau24581ba2010-08-31 22:39:35 +02007284 /* OK so now we know we have to process this response cookie.
7285 * The format of the Set-Cookie header is slightly different
7286 * from the format of the Cookie header in that it does not
7287 * support the comma as a cookie delimiter (thus the header
7288 * cannot be folded) because the Expires attribute described in
7289 * the original Netscape's spec may contain an unquoted date
7290 * with a comma inside. We have to live with this because
7291 * many browsers don't support Max-Age and some browsers don't
7292 * support quoted strings. However the Set-Cookie2 header is
7293 * clean.
7294 *
7295 * We have to keep multiple pointers in order to support cookie
7296 * removal at the beginning, middle or end of header without
7297 * corrupting the header (in case of set-cookie2). A special
7298 * pointer, <scav> points to the beginning of the set-cookie-av
7299 * fields after the first semi-colon. The <next> pointer points
7300 * either to the end of line (set-cookie) or next unquoted comma
7301 * (set-cookie2). All of these headers are valid :
7302 *
7303 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7304 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7305 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7306 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7307 * | | | | | | | | | |
7308 * | | | | | | | | +-> next hdr_end <--+
7309 * | | | | | | | +------------> scav
7310 * | | | | | | +--------------> val_end
7311 * | | | | | +--------------------> val_beg
7312 * | | | | +----------------------> equal
7313 * | | | +------------------------> att_end
7314 * | | +----------------------------> att_beg
7315 * | +------------------------------> prev
7316 * +-----------------------------------------> hdr_beg
7317 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318
Willy Tarreau24581ba2010-08-31 22:39:35 +02007319 for (; prev < hdr_end; prev = next) {
7320 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007321
Willy Tarreau24581ba2010-08-31 22:39:35 +02007322 /* find att_beg */
7323 att_beg = prev + 1;
7324 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7325 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326
Willy Tarreau24581ba2010-08-31 22:39:35 +02007327 /* find att_end : this is the first character after the last non
7328 * space before the equal. It may be equal to hdr_end.
7329 */
7330 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331
Willy Tarreau24581ba2010-08-31 22:39:35 +02007332 while (equal < hdr_end) {
7333 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7334 break;
7335 if (http_is_spht[(unsigned char)*equal++])
7336 continue;
7337 att_end = equal;
7338 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339
Willy Tarreau24581ba2010-08-31 22:39:35 +02007340 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7341 * is between <att_beg> and <equal>, both may be identical.
7342 */
7343
7344 /* look for end of cookie if there is an equal sign */
7345 if (equal < hdr_end && *equal == '=') {
7346 /* look for the beginning of the value */
7347 val_beg = equal + 1;
7348 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7349 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350
Willy Tarreau24581ba2010-08-31 22:39:35 +02007351 /* find the end of the value, respecting quotes */
7352 next = find_cookie_value_end(val_beg, hdr_end);
7353
7354 /* make val_end point to the first white space or delimitor after the value */
7355 val_end = next;
7356 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7357 val_end--;
7358 } else {
7359 /* <equal> points to next comma, semi-colon or EOL */
7360 val_beg = val_end = next = equal;
7361 }
7362
7363 if (next < hdr_end) {
7364 /* Set-Cookie2 supports multiple cookies, and <next> points to
7365 * a colon or semi-colon before the end. So skip all attr-value
7366 * pairs and look for the next comma. For Set-Cookie, since
7367 * commas are permitted in values, skip to the end.
7368 */
7369 if (is_cookie2)
7370 next = find_hdr_value_end(next, hdr_end);
7371 else
7372 next = hdr_end;
7373 }
7374
7375 /* Now everything is as on the diagram above */
7376
7377 /* Ignore cookies with no equal sign */
7378 if (equal == val_end)
7379 continue;
7380
7381 /* If there are spaces around the equal sign, we need to
7382 * strip them otherwise we'll get trouble for cookie captures,
7383 * or even for rewrites. Since this happens extremely rarely,
7384 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007386 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7387 int stripped_before = 0;
7388 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389
Willy Tarreau24581ba2010-08-31 22:39:35 +02007390 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007391 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007392 equal += stripped_before;
7393 val_beg += stripped_before;
7394 }
7395
7396 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007397 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007398 val_beg += stripped_after;
7399 stripped_before += stripped_after;
7400 }
7401
7402 val_end += stripped_before;
7403 next += stripped_before;
7404 hdr_end += stripped_before;
7405 hdr_next += stripped_before;
7406 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007407 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007408 }
7409
7410 /* First, let's see if we want to capture this cookie. We check
7411 * that we don't already have a server side cookie, because we
7412 * can only capture one. Also as an optimisation, we ignore
7413 * cookies shorter than the declared name.
7414 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007415 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007416 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007417 (val_end - att_beg >= t->fe->capture_namelen) &&
7418 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7419 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007420 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421 Alert("HTTP logging : out of memory.\n");
7422 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007423 else {
7424 if (log_len > t->fe->capture_len)
7425 log_len = t->fe->capture_len;
7426 memcpy(txn->srv_cookie, att_beg, log_len);
7427 txn->srv_cookie[log_len] = 0;
7428 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007429 }
7430
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007431 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007432 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007433 if (!(t->flags & SN_IGNORE_PRST) &&
7434 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7435 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007436 /* assume passive cookie by default */
7437 txn->flags &= ~TX_SCK_MASK;
7438 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007439
7440 /* If the cookie is in insert mode on a known server, we'll delete
7441 * this occurrence because we'll insert another one later.
7442 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007443 * a direct access.
7444 */
Willy Tarreau67402132012-05-31 20:40:20 +02007445 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007446 /* The "preserve" flag was set, we don't want to touch the
7447 * server's cookie.
7448 */
7449 }
Willy Tarreau67402132012-05-31 20:40:20 +02007450 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7451 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007452 /* this cookie must be deleted */
7453 if (*prev == ':' && next == hdr_end) {
7454 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007455 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7457 txn->hdr_idx.used--;
7458 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007459 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007460 hdr_next += delta;
7461 http_msg_move_end(&txn->rsp, delta);
7462 /* note: while both invalid now, <next> and <hdr_end>
7463 * are still equal, so the for() will stop as expected.
7464 */
7465 } else {
7466 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007467 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007468 next = prev;
7469 hdr_end += delta;
7470 hdr_next += delta;
7471 cur_hdr->len += delta;
7472 http_msg_move_end(&txn->rsp, delta);
7473 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007474 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007475 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007476 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477 }
Willy Tarreau67402132012-05-31 20:40:20 +02007478 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007479 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007480 * with this server since we know it.
7481 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007482 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 next += delta;
7484 hdr_end += delta;
7485 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007486 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007487 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488
Willy Tarreauf1348312010-10-07 15:54:11 +02007489 txn->flags &= ~TX_SCK_MASK;
7490 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007491 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007492 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007493 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007494 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007495 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007496 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007497 next += delta;
7498 hdr_end += delta;
7499 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007500 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007501 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007502
Willy Tarreau827aee92011-03-10 16:55:02 +01007503 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007504 txn->flags &= ~TX_SCK_MASK;
7505 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506 }
7507 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007508 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7509 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007510 int cmp_len, value_len;
7511 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512
Cyril Bontéb21570a2009-11-29 20:04:48 +01007513 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007514 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7515 value_begin = att_beg + t->be->appsession_name_len;
7516 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007517 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007518 cmp_len = att_end - att_beg;
7519 value_begin = val_beg;
7520 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007521 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007522
Cyril Bonté17530c32010-04-06 21:11:10 +02007523 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007524 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7525 /* free a possibly previously allocated memory */
7526 pool_free2(apools.sessid, txn->sessid);
7527
Cyril Bontéb21570a2009-11-29 20:04:48 +01007528 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007529 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007530 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7531 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7532 return;
7533 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007534 memcpy(txn->sessid, value_begin, value_len);
7535 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007536 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007537 }
7538 /* that's done for this cookie, check the next one on the same
7539 * line when next != hdr_end (only if is_cookie2).
7540 */
7541 }
7542 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007543 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007544 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007545
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007546 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007547 appsess *asession = NULL;
7548 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007549 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007550 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007551 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007552 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7553 Alert("Not enough Memory process_srv():asession:calloc().\n");
7554 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7555 return;
7556 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007557 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7558
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007559 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7560 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7561 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007562 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007563 return;
7564 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007565 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007566 asession->sessid[t->be->appsession_len] = 0;
7567
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007568 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007569 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007570 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007571 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007572 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007573 return;
7574 }
7575 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007576 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007577
7578 asession->request_count = 0;
7579 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7580 }
7581
7582 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7583 asession->request_count++;
7584 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007585}
7586
7587
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588/*
7589 * Check if response is cacheable or not. Updates t->flags.
7590 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007591void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007592{
7593 struct http_txn *txn = &t->txn;
7594 char *p1, *p2;
7595
7596 char *cur_ptr, *cur_end, *cur_next;
7597 int cur_idx;
7598
Willy Tarreau5df51872007-11-25 16:20:08 +01007599 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 return;
7601
7602 /* Iterate through the headers.
7603 * we start with the start line.
7604 */
7605 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007606 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007607
7608 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7609 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007610 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611
7612 cur_hdr = &txn->hdr_idx.v[cur_idx];
7613 cur_ptr = cur_next;
7614 cur_end = cur_ptr + cur_hdr->len;
7615 cur_next = cur_end + cur_hdr->cr + 1;
7616
7617 /* We have one full header between cur_ptr and cur_end, and the
7618 * next header starts at cur_next. We're only interested in
7619 * "Cookie:" headers.
7620 */
7621
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007622 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7623 if (val) {
7624 if ((cur_end - (cur_ptr + val) >= 8) &&
7625 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7626 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7627 return;
7628 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629 }
7630
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007631 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7632 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007633 continue;
7634
7635 /* OK, right now we know we have a cache-control header at cur_ptr */
7636
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007637 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638
7639 if (p1 >= cur_end) /* no more info */
7640 continue;
7641
7642 /* p1 is at the beginning of the value */
7643 p2 = p1;
7644
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007645 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007646 p2++;
7647
7648 /* we have a complete value between p1 and p2 */
7649 if (p2 < cur_end && *p2 == '=') {
7650 /* we have something of the form no-cache="set-cookie" */
7651 if ((cur_end - p1 >= 21) &&
7652 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7653 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007654 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655 continue;
7656 }
7657
7658 /* OK, so we know that either p2 points to the end of string or to a comma */
7659 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7660 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7661 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7662 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007663 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 return;
7665 }
7666
7667 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007668 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007669 continue;
7670 }
7671 }
7672}
7673
7674
Willy Tarreau58f10d72006-12-04 02:26:12 +01007675/*
7676 * Try to retrieve a known appsession in the URI, then the associated server.
7677 * If the server is found, it's assigned to the session.
7678 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007679void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007681 char *end_params, *first_param, *cur_param, *next_param;
7682 char separator;
7683 int value_len;
7684
7685 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007686
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007687 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007688 (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 +01007689 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007690 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Cyril Bontéb21570a2009-11-29 20:04:48 +01007692 first_param = NULL;
7693 switch (mode) {
7694 case PR_O2_AS_M_PP:
7695 first_param = memchr(begin, ';', len);
7696 break;
7697 case PR_O2_AS_M_QS:
7698 first_param = memchr(begin, '?', len);
7699 break;
7700 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701
Cyril Bontéb21570a2009-11-29 20:04:48 +01007702 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007703 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007704 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705
Cyril Bontéb21570a2009-11-29 20:04:48 +01007706 switch (mode) {
7707 case PR_O2_AS_M_PP:
7708 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7709 end_params = (char *) begin + len;
7710 }
7711 separator = ';';
7712 break;
7713 case PR_O2_AS_M_QS:
7714 end_params = (char *) begin + len;
7715 separator = '&';
7716 break;
7717 default:
7718 /* unknown mode, shouldn't happen */
7719 return;
7720 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721
Cyril Bontéb21570a2009-11-29 20:04:48 +01007722 cur_param = next_param = end_params;
7723 while (cur_param > first_param) {
7724 cur_param--;
7725 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7726 /* let's see if this is the appsession parameter */
7727 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7728 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7729 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7730 /* Cool... it's the right one */
7731 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7732 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7733 if (value_len > 0) {
7734 manage_client_side_appsession(t, cur_param, value_len);
7735 }
7736 break;
7737 }
7738 next_param = cur_param;
7739 }
7740 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007741#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007742 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007743 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007745}
7746
Willy Tarreaub2513902006-12-17 14:52:38 +01007747/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007748 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007749 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007750 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007751 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007752 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007753 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007754 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007755 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007756int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007757{
7758 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007759 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007760 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007761 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007762
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007763 if (!uri_auth)
7764 return 0;
7765
Cyril Bonté70be45d2010-10-12 00:14:35 +02007766 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007767 return 0;
7768
Willy Tarreau295a8372011-03-10 11:25:07 +01007769 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007770 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007771 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007772
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007773 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007774 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007775 return 0;
7776
Willy Tarreau3a215be2012-03-09 21:39:51 +01007777 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007778 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007779 return 0;
7780
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007781 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007782 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007783 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007784 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007785 break;
7786 }
7787 h++;
7788 }
7789
7790 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007791 h = uri + uri_auth->uri_len;
7792 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007793 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007794 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007795 break;
7796 }
7797 h++;
7798 }
7799 }
7800
Willy Tarreau3a215be2012-03-09 21:39:51 +01007801 h = uri + uri_auth->uri_len;
7802 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007803 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007804 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007805 break;
7806 }
7807 h++;
7808 }
7809
Willy Tarreau3a215be2012-03-09 21:39:51 +01007810 h = uri + uri_auth->uri_len;
7811 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007812 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007813 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007814 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007815 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007816 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7817 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7818 si->applet.ctx.stats.st_code = i;
7819 break;
7820 }
7821 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007822 break;
7823 }
7824 h++;
7825 }
Willy Tarreaub2513902006-12-17 14:52:38 +01007826 return 1;
7827}
7828
Willy Tarreau4076a152009-04-02 15:18:36 +02007829/*
7830 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007831 * By default it tries to report the error position as msg->err_pos. However if
7832 * this one is not set, it will then report msg->next, which is the last known
7833 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007834 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007835 */
7836void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007837 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007838 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007839{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007840 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007841 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007842
Willy Tarreau9b28e032012-10-12 23:49:43 +02007843 es->len = MIN(chn->buf->i, sizeof(es->buf));
7844 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007845 len1 = MIN(len1, es->len);
7846 len2 = es->len - len1; /* remaining data if buffer wraps */
7847
Willy Tarreau9b28e032012-10-12 23:49:43 +02007848 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007849 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007850 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007851
Willy Tarreau4076a152009-04-02 15:18:36 +02007852 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007853 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007854 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007855 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007856
Willy Tarreau4076a152009-04-02 15:18:36 +02007857 es->when = date; // user-visible date
7858 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007859 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007860 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007861 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007862 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007863 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007864 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007865 es->s_flags = s->flags;
7866 es->t_flags = s->txn.flags;
7867 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007868 es->b_out = chn->buf->o;
7869 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007870 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007871 es->m_clen = msg->chunk_len;
7872 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007873}
Willy Tarreaub2513902006-12-17 14:52:38 +01007874
Willy Tarreau294c4732011-12-16 21:35:50 +01007875/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7876 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7877 * performed over the whole headers. Otherwise it must contain a valid header
7878 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7879 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7880 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7881 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7882 * -1.
7883 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007884 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007885unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007886 struct hdr_idx *idx, int occ,
7887 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007888{
Willy Tarreau294c4732011-12-16 21:35:50 +01007889 struct hdr_ctx local_ctx;
7890 char *ptr_hist[MAX_HDR_HISTORY];
7891 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007892 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007893 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007894
Willy Tarreau294c4732011-12-16 21:35:50 +01007895 if (!ctx) {
7896 local_ctx.idx = 0;
7897 ctx = &local_ctx;
7898 }
7899
Willy Tarreaubce70882009-09-07 11:51:47 +02007900 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007901 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007902 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007903 occ--;
7904 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007905 *vptr = ctx->line + ctx->val;
7906 *vlen = ctx->vlen;
7907 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007908 }
7909 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007910 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007911 }
7912
7913 /* negative occurrence, we scan all the list then walk back */
7914 if (-occ > MAX_HDR_HISTORY)
7915 return 0;
7916
Willy Tarreau294c4732011-12-16 21:35:50 +01007917 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007918 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007919 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7920 len_hist[hist_ptr] = ctx->vlen;
7921 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007922 hist_ptr = 0;
7923 found++;
7924 }
7925 if (-occ > found)
7926 return 0;
7927 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7928 * find occurrence -occ, so we have to check [hist_ptr+occ].
7929 */
7930 hist_ptr += occ;
7931 if (hist_ptr >= MAX_HDR_HISTORY)
7932 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007933 *vptr = ptr_hist[hist_ptr];
7934 *vlen = len_hist[hist_ptr];
7935 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007936}
7937
Willy Tarreaubaaee002006-06-26 02:48:02 +02007938/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007939 * Print a debug line with a header. Always stop at the first CR or LF char,
7940 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7941 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007942 */
7943void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7944{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007945 int max;
7946 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007947 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7948 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007949
7950 for (max = 0; start + max < end; max++)
7951 if (start[max] == '\r' || start[max] == '\n')
7952 break;
7953
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007954 UBOUND(max, trash.size - trash.len - 3);
7955 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7956 trash.str[trash.len++] = '\n';
7957 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007958}
7959
Willy Tarreau0937bc42009-12-22 15:03:09 +01007960/*
7961 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7962 * the required fields are properly allocated and that we only need to (re)init
7963 * them. This should be used before processing any new request.
7964 */
7965void http_init_txn(struct session *s)
7966{
7967 struct http_txn *txn = &s->txn;
7968 struct proxy *fe = s->fe;
7969
7970 txn->flags = 0;
7971 txn->status = -1;
7972
William Lallemand5f232402012-04-05 18:02:55 +02007973 global.req_count++;
7974
Willy Tarreauf64d1412010-10-07 20:06:11 +02007975 txn->cookie_first_date = 0;
7976 txn->cookie_last_date = 0;
7977
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007978 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007979 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007980 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007981 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007982 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007983 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007984 txn->req.chunk_len = 0LL;
7985 txn->req.body_len = 0LL;
7986 txn->rsp.chunk_len = 0LL;
7987 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007988 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7989 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007990 txn->req.chn = s->req;
7991 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007992
7993 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007994
7995 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7996 if (fe->options2 & PR_O2_REQBUG_OK)
7997 txn->req.err_pos = -1; /* let buggy requests pass */
7998
Willy Tarreau46023632010-01-07 22:51:47 +01007999 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008000 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8001
Willy Tarreau46023632010-01-07 22:51:47 +01008002 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008003 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8004
8005 if (txn->hdr_idx.v)
8006 hdr_idx_init(&txn->hdr_idx);
8007}
8008
8009/* to be used at the end of a transaction */
8010void http_end_txn(struct session *s)
8011{
8012 struct http_txn *txn = &s->txn;
8013
8014 /* these ones will have been dynamically allocated */
8015 pool_free2(pool2_requri, txn->uri);
8016 pool_free2(pool2_capture, txn->cli_cookie);
8017 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008018 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008019 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008020
William Lallemanda73203e2012-03-12 12:48:57 +01008021 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008022 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008023 txn->uri = NULL;
8024 txn->srv_cookie = NULL;
8025 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008026
8027 if (txn->req.cap) {
8028 struct cap_hdr *h;
8029 for (h = s->fe->req_cap; h; h = h->next)
8030 pool_free2(h->pool, txn->req.cap[h->index]);
8031 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8032 }
8033
8034 if (txn->rsp.cap) {
8035 struct cap_hdr *h;
8036 for (h = s->fe->rsp_cap; h; h = h->next)
8037 pool_free2(h->pool, txn->rsp.cap[h->index]);
8038 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8039 }
8040
Willy Tarreau0937bc42009-12-22 15:03:09 +01008041}
8042
8043/* to be used at the end of a transaction to prepare a new one */
8044void http_reset_txn(struct session *s)
8045{
8046 http_end_txn(s);
8047 http_init_txn(s);
8048
8049 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008050 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09008051 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008052 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008053 /* re-init store persistence */
8054 s->store_count = 0;
8055
Willy Tarreau0937bc42009-12-22 15:03:09 +01008056 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008057
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008058 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008059
Willy Tarreau739cfba2010-01-25 23:11:14 +01008060 /* We must trim any excess data from the response buffer, because we
8061 * may have blocked an invalid response from a server that we don't
8062 * want to accidentely forward once we disable the analysers, nor do
8063 * we want those data to come along with next response. A typical
8064 * example of such data would be from a buggy server responding to
8065 * a HEAD with some data, or sending more than the advertised
8066 * content-length.
8067 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008068 if (unlikely(s->rep->buf->i))
8069 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008070
Willy Tarreau0937bc42009-12-22 15:03:09 +01008071 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008072 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008073
Willy Tarreaud04e8582010-05-31 12:31:35 +02008074 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008075 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008076
8077 s->req->rex = TICK_ETERNITY;
8078 s->req->wex = TICK_ETERNITY;
8079 s->req->analyse_exp = TICK_ETERNITY;
8080 s->rep->rex = TICK_ETERNITY;
8081 s->rep->wex = TICK_ETERNITY;
8082 s->rep->analyse_exp = TICK_ETERNITY;
8083}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008084
Willy Tarreauff011f22011-01-06 17:51:27 +01008085void free_http_req_rules(struct list *r) {
8086 struct http_req_rule *tr, *pr;
8087
8088 list_for_each_entry_safe(pr, tr, r, list) {
8089 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008090 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008091 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008092
8093 free(pr);
8094 }
8095}
8096
8097struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8098{
8099 struct http_req_rule *rule;
8100 int cur_arg;
8101
8102 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8103 if (!rule) {
8104 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008105 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008106 }
8107
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008108 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008109 rule->action = HTTP_REQ_ACT_ALLOW;
8110 cur_arg = 1;
8111 } else if (!strcmp(args[0], "deny")) {
8112 rule->action = HTTP_REQ_ACT_DENY;
8113 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008114 } else if (!strcmp(args[0], "tarpit")) {
8115 rule->action = HTTP_REQ_ACT_TARPIT;
8116 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008117 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008118 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008119 cur_arg = 1;
8120
8121 while(*args[cur_arg]) {
8122 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008123 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008124 cur_arg+=2;
8125 continue;
8126 } else
8127 break;
8128 }
Willy Tarreau20b0de52012-12-24 15:45:22 +01008129 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8130 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8131 cur_arg = 1;
8132
8133 if (!*args[cur_arg] || !*args[cur_arg+1] || *args[cur_arg+2]) {
8134 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8135 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008136 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008137 }
8138
8139 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8140 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8141 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreau3bfeadb2013-03-24 07:33:22 +01008142 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008143 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008144 } else if (strcmp(args[0], "redirect") == 0) {
8145 struct redirect_rule *redir;
8146 char *errmsg;
8147
8148 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8149 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8150 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8151 goto out_err;
8152 }
8153
8154 /* this redirect rule might already contain a parsed condition which
8155 * we'll pass to the http-request rule.
8156 */
8157 rule->action = HTTP_REQ_ACT_REDIR;
8158 rule->arg.redir = redir;
8159 rule->cond = redir->cond;
8160 redir->cond = NULL;
8161 cur_arg = 2;
8162 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008163 } else {
Baptiste Assmann116eefe2013-01-05 16:02:07 +01008164 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008165 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008166 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008167 }
8168
8169 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8170 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008171 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008172
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008173 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8174 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8175 file, linenum, args[0], errmsg);
8176 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008177 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008178 }
8179 rule->cond = cond;
8180 }
8181 else if (*args[cur_arg]) {
8182 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8183 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8184 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008185 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008186 }
8187
8188 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008189 out_err:
8190 free(rule);
8191 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008192}
8193
Willy Tarreau4baae242012-12-27 12:00:31 +01008194/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8195 * with <err> filled with the error message.
8196 */
8197struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8198 const char **args, char **errmsg)
8199{
8200 struct redirect_rule *rule;
8201 int cur_arg;
8202 int type = REDIRECT_TYPE_NONE;
8203 int code = 302;
8204 const char *destination = NULL;
8205 const char *cookie = NULL;
8206 int cookie_set = 0;
8207 unsigned int flags = REDIRECT_FLAG_NONE;
8208 struct acl_cond *cond = NULL;
8209
8210 cur_arg = 0;
8211 while (*(args[cur_arg])) {
8212 if (strcmp(args[cur_arg], "location") == 0) {
8213 if (!*args[cur_arg + 1])
8214 goto missing_arg;
8215
8216 type = REDIRECT_TYPE_LOCATION;
8217 cur_arg++;
8218 destination = args[cur_arg];
8219 }
8220 else if (strcmp(args[cur_arg], "prefix") == 0) {
8221 if (!*args[cur_arg + 1])
8222 goto missing_arg;
8223
8224 type = REDIRECT_TYPE_PREFIX;
8225 cur_arg++;
8226 destination = args[cur_arg];
8227 }
8228 else if (strcmp(args[cur_arg], "scheme") == 0) {
8229 if (!*args[cur_arg + 1])
8230 goto missing_arg;
8231
8232 type = REDIRECT_TYPE_SCHEME;
8233 cur_arg++;
8234 destination = args[cur_arg];
8235 }
8236 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8237 if (!*args[cur_arg + 1])
8238 goto missing_arg;
8239
8240 cur_arg++;
8241 cookie = args[cur_arg];
8242 cookie_set = 1;
8243 }
8244 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8245 if (!*args[cur_arg + 1])
8246 goto missing_arg;
8247
8248 cur_arg++;
8249 cookie = args[cur_arg];
8250 cookie_set = 0;
8251 }
8252 else if (strcmp(args[cur_arg], "code") == 0) {
8253 if (!*args[cur_arg + 1])
8254 goto missing_arg;
8255
8256 cur_arg++;
8257 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008258 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008259 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008260 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008261 args[cur_arg - 1], args[cur_arg]);
8262 return NULL;
8263 }
8264 }
8265 else if (!strcmp(args[cur_arg],"drop-query")) {
8266 flags |= REDIRECT_FLAG_DROP_QS;
8267 }
8268 else if (!strcmp(args[cur_arg],"append-slash")) {
8269 flags |= REDIRECT_FLAG_APPEND_SLASH;
8270 }
8271 else if (strcmp(args[cur_arg], "if") == 0 ||
8272 strcmp(args[cur_arg], "unless") == 0) {
8273 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8274 if (!cond) {
8275 memprintf(errmsg, "error in condition: %s", *errmsg);
8276 return NULL;
8277 }
8278 break;
8279 }
8280 else {
8281 memprintf(errmsg,
8282 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8283 args[cur_arg]);
8284 return NULL;
8285 }
8286 cur_arg++;
8287 }
8288
8289 if (type == REDIRECT_TYPE_NONE) {
8290 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8291 return NULL;
8292 }
8293
8294 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8295 rule->cond = cond;
8296 rule->rdr_str = strdup(destination);
8297 rule->rdr_len = strlen(destination);
8298 if (cookie) {
8299 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8300 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8301 */
8302 rule->cookie_len = strlen(cookie);
8303 if (cookie_set) {
8304 rule->cookie_str = malloc(rule->cookie_len + 10);
8305 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8306 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8307 rule->cookie_len += 9;
8308 } else {
8309 rule->cookie_str = malloc(rule->cookie_len + 21);
8310 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8311 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8312 rule->cookie_len += 20;
8313 }
8314 }
8315 rule->type = type;
8316 rule->code = code;
8317 rule->flags = flags;
8318 LIST_INIT(&rule->list);
8319 return rule;
8320
8321 missing_arg:
8322 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8323 return NULL;
8324}
8325
Willy Tarreau8797c062007-05-07 00:55:35 +02008326/************************************************************************/
8327/* The code below is dedicated to ACL parsing and matching */
8328/************************************************************************/
8329
8330
Willy Tarreau14174bc2012-04-16 14:34:04 +02008331/* This function ensures that the prerequisites for an L7 fetch are ready,
8332 * which means that a request or response is ready. If some data is missing,
8333 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008334 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8335 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008336 *
8337 * The function returns :
8338 * 0 if some data is missing or if the requested data cannot be fetched
8339 * -1 if it is certain that we'll never have any HTTP message there
8340 * 1 if an HTTP message is ready
8341 */
8342static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008343acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008344 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008345{
8346 struct http_txn *txn = l7;
8347 struct http_msg *msg = &txn->req;
8348
8349 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8350 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8351 */
8352
8353 if (unlikely(!s || !txn))
8354 return 0;
8355
8356 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008357 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008358
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008359 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008360 if (unlikely(!s->req))
8361 return 0;
8362
Willy Tarreauaae75e32013-03-29 12:31:49 +01008363 /* If the buffer does not leave enough free space at the end,
8364 * we must first realign it.
8365 */
8366 if (s->req->buf->p > s->req->buf->data &&
8367 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8368 buffer_slow_realign(s->req->buf);
8369
Willy Tarreau14174bc2012-04-16 14:34:04 +02008370 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008371 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008372 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008373 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008374 return -1;
8375 }
8376
8377 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008378 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008379 http_msg_analyzer(msg, &txn->hdr_idx);
8380
8381 /* Still no valid request ? */
8382 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008383 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008384 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008385 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008386 return -1;
8387 }
8388 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008389 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008390 return 0;
8391 }
8392
8393 /* OK we just got a valid HTTP request. We have some minor
8394 * preparation to perform so that further checks can rely
8395 * on HTTP tests.
8396 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008397
8398 /* If the request was parsed but was too large, we must absolutely
8399 * return an error so that it is not processed. At the moment this
8400 * cannot happen, but if the parsers are to change in the future,
8401 * we want this check to be maintained.
8402 */
8403 if (unlikely(s->req->buf->i + s->req->buf->p >
8404 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8405 msg->msg_state = HTTP_MSG_ERROR;
8406 return 1;
8407 }
8408
Willy Tarreau9b28e032012-10-12 23:49:43 +02008409 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008410 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8411 s->flags |= SN_REDIRECTABLE;
8412
8413 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008414 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008415 return -1;
8416 }
8417 }
8418
Willy Tarreau24e32d82012-04-23 23:55:44 +02008419 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008420 return 0; /* data might have moved and indexes changed */
8421
8422 /* otherwise everything's ready for the request */
8423 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008424 else {
8425 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008426 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8427 return 0;
8428 }
8429
8430 /* everything's OK */
8431 return 1;
8432}
Willy Tarreau8797c062007-05-07 00:55:35 +02008433
Willy Tarreauc0239e02012-04-16 14:42:55 +02008434#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008435 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02008436
Willy Tarreau24e32d82012-04-23 23:55:44 +02008437#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008438 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02008439
Willy Tarreau8797c062007-05-07 00:55:35 +02008440
8441/* 1. Check on METHOD
8442 * We use the pre-parsed method if it is known, and store its number as an
8443 * integer. If it is unknown, we use the pointer and the length.
8444 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008445static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008446{
8447 int len, meth;
8448
Willy Tarreauae8b7962007-06-09 23:10:04 +02008449 len = strlen(*text);
8450 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008451
8452 pattern->val.i = meth;
8453 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008454 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008455 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008456 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008457 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008458 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008459 pattern->len = len;
8460 }
8461 return 1;
8462}
8463
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008464/* This function fetches the method of current HTTP request and stores
8465 * it in the global pattern struct as a chunk. There are two possibilities :
8466 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8467 * in <len> and <ptr> is NULL ;
8468 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8469 * <len> to its length.
8470 * This is intended to be used with acl_match_meth() only.
8471 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008472static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008473acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008474 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008475{
8476 int meth;
8477 struct http_txn *txn = l7;
8478
Willy Tarreau24e32d82012-04-23 23:55:44 +02008479 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008480
Willy Tarreau8797c062007-05-07 00:55:35 +02008481 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008482 smp->type = SMP_T_UINT;
8483 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008484 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008485 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8486 /* ensure the indexes are not affected */
8487 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008488 smp->type = SMP_T_CSTR;
8489 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008490 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008491 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008492 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008493 return 1;
8494}
8495
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008496/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008497static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008498{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008499 int icase;
8500
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008501
Willy Tarreauf853c462012-04-23 18:53:56 +02008502 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008503 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008504 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008505 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008506 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008507 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008508
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008509 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8510 if (pattern->val.i != HTTP_METH_OTHER)
8511 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008512
8513 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008514 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008515 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008516
8517 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008518 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8519 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008520 return ACL_PAT_FAIL;
8521 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008522}
8523
8524/* 2. Check on Request/Status Version
8525 * We simply compare strings here.
8526 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008527static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008528{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008529 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008530 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008531 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008532 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008533 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008534 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008535 return 1;
8536}
8537
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008538static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008539acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008540 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008541{
8542 struct http_txn *txn = l7;
8543 char *ptr;
8544 int len;
8545
Willy Tarreauc0239e02012-04-16 14:42:55 +02008546 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008547
Willy Tarreau8797c062007-05-07 00:55:35 +02008548 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008549 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008550
8551 while ((len-- > 0) && (*ptr++ != '/'));
8552 if (len <= 0)
8553 return 0;
8554
Willy Tarreauf853c462012-04-23 18:53:56 +02008555 smp->type = SMP_T_CSTR;
8556 smp->data.str.str = ptr;
8557 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008558
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008559 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008560 return 1;
8561}
8562
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008563static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008564acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008565 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008566{
8567 struct http_txn *txn = l7;
8568 char *ptr;
8569 int len;
8570
Willy Tarreauc0239e02012-04-16 14:42:55 +02008571 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008572
Willy Tarreauf26b2522012-12-14 08:33:14 +01008573 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8574 return 0;
8575
Willy Tarreau8797c062007-05-07 00:55:35 +02008576 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008577 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008578
8579 while ((len-- > 0) && (*ptr++ != '/'));
8580 if (len <= 0)
8581 return 0;
8582
Willy Tarreauf853c462012-04-23 18:53:56 +02008583 smp->type = SMP_T_CSTR;
8584 smp->data.str.str = ptr;
8585 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008586
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008587 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008588 return 1;
8589}
8590
8591/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008592static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008593acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008594 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008595{
8596 struct http_txn *txn = l7;
8597 char *ptr;
8598 int len;
8599
Willy Tarreauc0239e02012-04-16 14:42:55 +02008600 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008601
Willy Tarreauf26b2522012-12-14 08:33:14 +01008602 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8603 return 0;
8604
Willy Tarreau8797c062007-05-07 00:55:35 +02008605 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008606 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008607
Willy Tarreauf853c462012-04-23 18:53:56 +02008608 smp->type = SMP_T_UINT;
8609 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008610 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008611 return 1;
8612}
8613
8614/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008615static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008616smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008617 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008618{
8619 struct http_txn *txn = l7;
8620
Willy Tarreauc0239e02012-04-16 14:42:55 +02008621 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008622
Willy Tarreauf853c462012-04-23 18:53:56 +02008623 smp->type = SMP_T_CSTR;
8624 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008625 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008626 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008627 return 1;
8628}
8629
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008630static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008631smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008632 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008633{
8634 struct http_txn *txn = l7;
8635
Willy Tarreauc0239e02012-04-16 14:42:55 +02008636 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008637
8638 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008639 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8640 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008641 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008642 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008643 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008644
8645 /*
8646 * If we are parsing url in frontend space, we prepare backend stage
8647 * to not parse again the same url ! optimization lazyness...
8648 */
8649 if (px->options & PR_O_HTTP_PROXY)
8650 l4->flags |= SN_ADDR_SET;
8651
Willy Tarreau37406352012-04-23 16:16:37 +02008652 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008653 return 1;
8654}
8655
8656static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008657smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008658 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008659{
8660 struct http_txn *txn = l7;
8661
Willy Tarreauc0239e02012-04-16 14:42:55 +02008662 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008663
8664 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008665 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 +02008666 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008667 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008668
8669 if (px->options & PR_O_HTTP_PROXY)
8670 l4->flags |= SN_ADDR_SET;
8671
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008672 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008673 return 1;
8674}
8675
Willy Tarreau185b5c42012-04-26 15:11:51 +02008676/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8677 * Accepts an optional argument of type string containing the header field name,
8678 * and an optional argument of type signed or unsigned integer to request an
8679 * explicit occurrence of the header. Note that in the event of a missing name,
8680 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008681 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008682static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008683smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008684 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008685{
8686 struct http_txn *txn = l7;
8687 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02008688 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008689 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008690 int occ = 0;
8691 const char *name_str = NULL;
8692 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008693
Willy Tarreaua890d072013-04-02 12:01:06 +02008694 if (!ctx) {
8695 /* first call */
8696 ctx = &static_hdr_ctx;
8697 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02008698 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02008699 }
8700
Willy Tarreau185b5c42012-04-26 15:11:51 +02008701 if (args) {
8702 if (args[0].type != ARGT_STR)
8703 return 0;
8704 name_str = args[0].data.str.str;
8705 name_len = args[0].data.str.len;
8706
8707 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8708 occ = args[1].data.uint;
8709 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008710
Willy Tarreaue333ec92012-04-16 16:26:40 +02008711 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008712
Willy Tarreau185b5c42012-04-26 15:11:51 +02008713 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008714 /* search for header from the beginning */
8715 ctx->idx = 0;
8716
Willy Tarreau185b5c42012-04-26 15:11:51 +02008717 if (!occ && !(opt & SMP_OPT_ITERATE))
8718 /* no explicit occurrence and single fetch => last header by default */
8719 occ = -1;
8720
8721 if (!occ)
8722 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008723 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008724
Willy Tarreau185b5c42012-04-26 15:11:51 +02008725 smp->type = SMP_T_CSTR;
8726 smp->flags |= SMP_F_VOL_HDR;
8727 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 +02008728 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008729
Willy Tarreau37406352012-04-23 16:16:37 +02008730 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008731 return 0;
8732}
8733
Willy Tarreauc11416f2007-06-17 16:58:38 +02008734/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008735 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008736 */
8737static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008738smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008739 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008740{
8741 struct http_txn *txn = l7;
8742 struct hdr_idx *idx = &txn->hdr_idx;
8743 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008744 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008745 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008746
Willy Tarreau24e32d82012-04-23 23:55:44 +02008747 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008748 return 0;
8749
Willy Tarreaue333ec92012-04-16 16:26:40 +02008750 CHECK_HTTP_MESSAGE_FIRST();
8751
Willy Tarreau33a7e692007-06-10 19:45:56 +02008752 ctx.idx = 0;
8753 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008754 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 +02008755 cnt++;
8756
Willy Tarreauf853c462012-04-23 18:53:56 +02008757 smp->type = SMP_T_UINT;
8758 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008759 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008760 return 1;
8761}
8762
Willy Tarreau185b5c42012-04-26 15:11:51 +02008763/* Fetch an HTTP header's integer value. The integer value is returned. It
8764 * takes a mandatory argument of type string and an optional one of type int
8765 * to designate a specific occurrence. It returns an unsigned integer, which
8766 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008767 */
8768static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008769smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008770 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008771{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008772 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008773
Willy Tarreauf853c462012-04-23 18:53:56 +02008774 if (ret > 0) {
8775 smp->type = SMP_T_UINT;
8776 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8777 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008778
Willy Tarreaud53e2422012-04-16 17:21:11 +02008779 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008780}
8781
Cyril Bonté69fa9922012-10-25 00:01:06 +02008782/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8783 * and an optional one of type int to designate a specific occurrence.
8784 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008785 */
8786static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008787smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008788 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008789{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008790 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008791
Willy Tarreau185b5c42012-04-26 15:11:51 +02008792 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008793 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8794 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008795 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008796 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01008797 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02008798 if (smp->data.str.len < temp->size - 1) {
8799 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8800 temp->str[smp->data.str.len] = '\0';
8801 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8802 smp->type = SMP_T_IPV6;
8803 break;
8804 }
8805 }
8806 }
8807
Willy Tarreaud53e2422012-04-16 17:21:11 +02008808 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008809 if (!(smp->flags & SMP_F_NOT_LAST))
8810 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008811 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008812 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008813}
8814
Willy Tarreau737b0c12007-06-10 21:28:46 +02008815/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8816 * the first '/' after the possible hostname, and ends before the possible '?'.
8817 */
8818static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008819smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008820 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008821{
8822 struct http_txn *txn = l7;
8823 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008824
Willy Tarreauc0239e02012-04-16 14:42:55 +02008825 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008826
Willy Tarreau9b28e032012-10-12 23:49:43 +02008827 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008828 ptr = http_get_path(txn);
8829 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008830 return 0;
8831
8832 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008833 smp->type = SMP_T_CSTR;
8834 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008835
8836 while (ptr < end && *ptr != '?')
8837 ptr++;
8838
Willy Tarreauf853c462012-04-23 18:53:56 +02008839 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008840 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008841 return 1;
8842}
8843
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008844/* This produces a concatenation of the first occurrence of the Host header
8845 * followed by the path component if it begins with a slash ('/'). This means
8846 * that '*' will not be added, resulting in exactly the first Host entry.
8847 * If no Host header is found, then the path is returned as-is. The returned
8848 * value is stored in the trash so it does not need to be marked constant.
8849 */
8850static int
8851smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8852 const struct arg *args, struct sample *smp)
8853{
8854 struct http_txn *txn = l7;
8855 char *ptr, *end, *beg;
8856 struct hdr_ctx ctx;
8857
8858 CHECK_HTTP_MESSAGE_FIRST();
8859
8860 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008861 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 +02008862 !ctx.vlen)
8863 return smp_fetch_path(px, l4, l7, opt, args, smp);
8864
8865 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008866 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008867 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008868 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008869 smp->data.str.len = ctx.vlen;
8870
8871 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008872 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 +02008873 beg = http_get_path(txn);
8874 if (!beg)
8875 beg = end;
8876
8877 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8878
8879 if (beg < ptr && *beg == '/') {
8880 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8881 smp->data.str.len += ptr - beg;
8882 }
8883
8884 smp->flags = SMP_F_VOL_1ST;
8885 return 1;
8886}
8887
Willy Tarreauab1f7b72012-12-09 13:38:54 +01008888/* This produces a 32-bit hash of the concatenation of the first occurrence of
8889 * the Host header followed by the path component if it begins with a slash ('/').
8890 * This means that '*' will not be added, resulting in exactly the first Host
8891 * entry. If no Host header is found, then the path is used. The resulting value
8892 * is hashed using the url hash followed by a full avalanche hash and provides a
8893 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
8894 * high-traffic sites without having to store whole paths.
8895 */
8896static int
8897smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8898 const struct arg *args, struct sample *smp)
8899{
8900 struct http_txn *txn = l7;
8901 struct hdr_ctx ctx;
8902 unsigned int hash = 0;
8903 char *ptr, *beg, *end;
8904 int len;
8905
8906 CHECK_HTTP_MESSAGE_FIRST();
8907
8908 ctx.idx = 0;
8909 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
8910 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8911 ptr = ctx.line + ctx.val;
8912 len = ctx.vlen;
8913 while (len--)
8914 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
8915 }
8916
8917 /* now retrieve the path */
8918 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
8919 beg = http_get_path(txn);
8920 if (!beg)
8921 beg = end;
8922
8923 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8924
8925 if (beg < ptr && *beg == '/') {
8926 while (beg < ptr)
8927 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
8928 }
8929 hash = full_hash(hash);
8930
8931 smp->type = SMP_T_UINT;
8932 smp->data.uint = hash;
8933 smp->flags = SMP_F_VOL_1ST;
8934 return 1;
8935}
8936
Willy Tarreau4a550602012-12-09 14:53:32 +01008937/* This concatenates the source address with the 32-bit hash of the Host and
8938 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
8939 * per-url counters. The result is a binary block from 8 to 20 bytes depending
8940 * on the source address length. The URL hash is stored before the address so
8941 * that in environments where IPv6 is insignificant, truncating the output to
8942 * 8 bytes would still work.
8943 */
8944static int
8945smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8946 const struct arg *args, struct sample *smp)
8947{
Willy Tarreau47ca5452012-12-23 20:22:19 +01008948 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01008949
8950 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
8951 return 0;
8952
Willy Tarreau47ca5452012-12-23 20:22:19 +01008953 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01008954 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
8955 temp->len += sizeof(smp->data.uint);
8956
8957 switch (l4->si[0].conn->addr.from.ss_family) {
8958 case AF_INET:
8959 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
8960 temp->len += 4;
8961 break;
8962 case AF_INET6:
8963 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
8964 temp->len += 16;
8965 break;
8966 default:
8967 return 0;
8968 }
8969
8970 smp->data.str = *temp;
8971 smp->type = SMP_T_BIN;
8972 return 1;
8973}
8974
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008975static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008976acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008977 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008978{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008979 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8980 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8981 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008982
Willy Tarreau24e32d82012-04-23 23:55:44 +02008983 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008984
Willy Tarreauf853c462012-04-23 18:53:56 +02008985 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008986 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008987 return 1;
8988}
8989
Willy Tarreau7f18e522010-10-22 20:04:13 +02008990/* return a valid test if the current request is the first one on the connection */
8991static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008992acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008993 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008994{
8995 if (!s)
8996 return 0;
8997
Willy Tarreauf853c462012-04-23 18:53:56 +02008998 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008999 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009000 return 1;
9001}
9002
Willy Tarreau34db1082012-04-19 17:16:54 +02009003/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009004static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009005acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009006 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009007{
9008
Willy Tarreau24e32d82012-04-23 23:55:44 +02009009 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009010 return 0;
9011
Willy Tarreauc0239e02012-04-16 14:42:55 +02009012 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009013
Willy Tarreauc0239e02012-04-16 14:42:55 +02009014 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009015 return 0;
9016
Willy Tarreauf853c462012-04-23 18:53:56 +02009017 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009018 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 +01009019 return 1;
9020}
Willy Tarreau8797c062007-05-07 00:55:35 +02009021
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009022/* Accepts exactly 1 argument of type userlist */
9023static int
9024acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9025 const struct arg *args, struct sample *smp)
9026{
9027
9028 if (!args || args->type != ARGT_USR)
9029 return 0;
9030
9031 CHECK_HTTP_MESSAGE_FIRST();
9032
9033 if (!get_http_auth(l4))
9034 return 0;
9035
9036 /* acl_match_auth() will need several information at once */
9037 smp->ctx.a[0] = args->data.usr; /* user list */
9038 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9039 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9040
9041 /* if the user does not belong to the userlist or has a wrong password,
9042 * report that it unconditionally does not match. Otherwise we return
9043 * a non-zero integer which will be ignored anyway since all the params
9044 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
9045 */
9046 smp->type = SMP_T_BOOL;
9047 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9048 if (smp->data.uint)
9049 smp->type = SMP_T_UINT;
9050
9051 return 1;
9052}
9053
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009054/* Try to find the next occurrence of a cookie name in a cookie header value.
9055 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9056 * the cookie value is returned into *value and *value_l, and the function
9057 * returns a pointer to the next pointer to search from if the value was found.
9058 * Otherwise if the cookie was not found, NULL is returned and neither value
9059 * nor value_l are touched. The input <hdr> string should first point to the
9060 * header's value, and the <hdr_end> pointer must point to the first character
9061 * not part of the value. <list> must be non-zero if value may represent a list
9062 * of values (cookie headers). This makes it faster to abort parsing when no
9063 * list is expected.
9064 */
9065static char *
9066extract_cookie_value(char *hdr, const char *hdr_end,
9067 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009068 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009069{
9070 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9071 char *next;
9072
9073 /* we search at least a cookie name followed by an equal, and more
9074 * generally something like this :
9075 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9076 */
9077 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9078 /* Iterate through all cookies on this line */
9079
9080 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9081 att_beg++;
9082
9083 /* find att_end : this is the first character after the last non
9084 * space before the equal. It may be equal to hdr_end.
9085 */
9086 equal = att_end = att_beg;
9087
9088 while (equal < hdr_end) {
9089 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9090 break;
9091 if (http_is_spht[(unsigned char)*equal++])
9092 continue;
9093 att_end = equal;
9094 }
9095
9096 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9097 * is between <att_beg> and <equal>, both may be identical.
9098 */
9099
9100 /* look for end of cookie if there is an equal sign */
9101 if (equal < hdr_end && *equal == '=') {
9102 /* look for the beginning of the value */
9103 val_beg = equal + 1;
9104 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9105 val_beg++;
9106
9107 /* find the end of the value, respecting quotes */
9108 next = find_cookie_value_end(val_beg, hdr_end);
9109
9110 /* make val_end point to the first white space or delimitor after the value */
9111 val_end = next;
9112 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9113 val_end--;
9114 } else {
9115 val_beg = val_end = next = equal;
9116 }
9117
9118 /* We have nothing to do with attributes beginning with '$'. However,
9119 * they will automatically be removed if a header before them is removed,
9120 * since they're supposed to be linked together.
9121 */
9122 if (*att_beg == '$')
9123 continue;
9124
9125 /* Ignore cookies with no equal sign */
9126 if (equal == next)
9127 continue;
9128
9129 /* Now we have the cookie name between att_beg and att_end, and
9130 * its value between val_beg and val_end.
9131 */
9132
9133 if (att_end - att_beg == cookie_name_l &&
9134 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9135 /* let's return this value and indicate where to go on from */
9136 *value = val_beg;
9137 *value_l = val_end - val_beg;
9138 return next + 1;
9139 }
9140
9141 /* Set-Cookie headers only have the name in the first attr=value part */
9142 if (!list)
9143 break;
9144 }
9145
9146 return NULL;
9147}
9148
Willy Tarreaue333ec92012-04-16 16:26:40 +02009149/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009150 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009151 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009152 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009153 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009154 * Accepts exactly 1 argument of type string. If the input options indicate
9155 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009156 */
9157static int
Willy Tarreau51539362012-05-08 12:46:28 +02009158smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9159 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009160{
9161 struct http_txn *txn = l7;
9162 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009163 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009164 const struct http_msg *msg;
9165 const char *hdr_name;
9166 int hdr_name_len;
9167 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009168 int occ = 0;
9169 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009170
Willy Tarreau24e32d82012-04-23 23:55:44 +02009171 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009172 return 0;
9173
Willy Tarreaua890d072013-04-02 12:01:06 +02009174 if (!ctx) {
9175 /* first call */
9176 ctx = &static_hdr_ctx;
9177 ctx->idx = 0;
9178 smp->ctx.a[2] = ctx;
9179 }
9180
Willy Tarreaue333ec92012-04-16 16:26:40 +02009181 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009182
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009183 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009184 msg = &txn->req;
9185 hdr_name = "Cookie";
9186 hdr_name_len = 6;
9187 } else {
9188 msg = &txn->rsp;
9189 hdr_name = "Set-Cookie";
9190 hdr_name_len = 10;
9191 }
9192
Willy Tarreau28376d62012-04-26 21:26:10 +02009193 if (!occ && !(opt & SMP_OPT_ITERATE))
9194 /* no explicit occurrence and single fetch => last cookie by default */
9195 occ = -1;
9196
9197 /* OK so basically here, either we want only one value and it's the
9198 * last one, or we want to iterate over all of them and we fetch the
9199 * next one.
9200 */
9201
Willy Tarreau9b28e032012-10-12 23:49:43 +02009202 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009203 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009204 /* search for the header from the beginning, we must first initialize
9205 * the search parameters.
9206 */
Willy Tarreau37406352012-04-23 16:16:37 +02009207 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009208 ctx->idx = 0;
9209 }
9210
Willy Tarreau28376d62012-04-26 21:26:10 +02009211 smp->flags |= SMP_F_VOL_HDR;
9212
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009213 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009214 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9215 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009216 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9217 goto out;
9218
Willy Tarreau24e32d82012-04-23 23:55:44 +02009219 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009220 continue;
9221
Willy Tarreau37406352012-04-23 16:16:37 +02009222 smp->ctx.a[0] = ctx->line + ctx->val;
9223 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009224 }
9225
Willy Tarreauf853c462012-04-23 18:53:56 +02009226 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009227 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009228 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009229 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009230 &smp->data.str.str,
9231 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009232 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009233 found = 1;
9234 if (occ >= 0) {
9235 /* one value was returned into smp->data.str.{str,len} */
9236 smp->flags |= SMP_F_NOT_LAST;
9237 return 1;
9238 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009239 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009240 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009241 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009242 /* all cookie headers and values were scanned. If we're looking for the
9243 * last occurrence, we may return it now.
9244 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009245 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009246 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009247 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009248}
9249
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009250/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009251 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009252 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009253 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009254 */
9255static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009256acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009257 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009258{
9259 struct http_txn *txn = l7;
9260 struct hdr_idx *idx = &txn->hdr_idx;
9261 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009262 const struct http_msg *msg;
9263 const char *hdr_name;
9264 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009265 int cnt;
9266 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009267 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009268
Willy Tarreau24e32d82012-04-23 23:55:44 +02009269 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009270 return 0;
9271
Willy Tarreaue333ec92012-04-16 16:26:40 +02009272 CHECK_HTTP_MESSAGE_FIRST();
9273
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009274 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009275 msg = &txn->req;
9276 hdr_name = "Cookie";
9277 hdr_name_len = 6;
9278 } else {
9279 msg = &txn->rsp;
9280 hdr_name = "Set-Cookie";
9281 hdr_name_len = 10;
9282 }
9283
Willy Tarreau9b28e032012-10-12 23:49:43 +02009284 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009285 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009286 ctx.idx = 0;
9287 cnt = 0;
9288
9289 while (1) {
9290 /* Note: val_beg == NULL every time we need to fetch a new header */
9291 if (!val_beg) {
9292 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9293 break;
9294
Willy Tarreau24e32d82012-04-23 23:55:44 +02009295 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009296 continue;
9297
9298 val_beg = ctx.line + ctx.val;
9299 val_end = val_beg + ctx.vlen;
9300 }
9301
Willy Tarreauf853c462012-04-23 18:53:56 +02009302 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009303 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009304 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009305 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009306 &smp->data.str.str,
9307 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009308 cnt++;
9309 }
9310 }
9311
Willy Tarreauf853c462012-04-23 18:53:56 +02009312 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009313 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009314 return 1;
9315}
9316
Willy Tarreau51539362012-05-08 12:46:28 +02009317/* Fetch an cookie's integer value. The integer value is returned. It
9318 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9319 */
9320static int
9321smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9322 const struct arg *args, struct sample *smp)
9323{
9324 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9325
9326 if (ret > 0) {
9327 smp->type = SMP_T_UINT;
9328 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9329 }
9330
9331 return ret;
9332}
9333
Willy Tarreau8797c062007-05-07 00:55:35 +02009334/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009335/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009336/************************************************************************/
9337
David Cournapeau16023ee2010-12-23 20:55:41 +09009338/*
9339 * Given a path string and its length, find the position of beginning of the
9340 * query string. Returns NULL if no query string is found in the path.
9341 *
9342 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9343 *
9344 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9345 */
bedis4c75cca2012-10-05 08:38:24 +02009346static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009347{
9348 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009349
bedis4c75cca2012-10-05 08:38:24 +02009350 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009351 return p ? p + 1 : NULL;
9352}
9353
bedis4c75cca2012-10-05 08:38:24 +02009354static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009355{
bedis4c75cca2012-10-05 08:38:24 +02009356 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009357}
9358
9359/*
9360 * Given a url parameter, find the starting position of the first occurence,
9361 * or NULL if the parameter is not found.
9362 *
9363 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9364 * the function will return query_string+8.
9365 */
9366static char*
9367find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009368 char* url_param_name, size_t url_param_name_l,
9369 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009370{
9371 char *pos, *last;
9372
9373 pos = query_string;
9374 last = query_string + query_string_l - url_param_name_l - 1;
9375
9376 while (pos <= last) {
9377 if (pos[url_param_name_l] == '=') {
9378 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9379 return pos;
9380 pos += url_param_name_l + 1;
9381 }
bedis4c75cca2012-10-05 08:38:24 +02009382 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009383 pos++;
9384 pos++;
9385 }
9386 return NULL;
9387}
9388
9389/*
9390 * Given a url parameter name, returns its value and size into *value and
9391 * *value_l respectively, and returns non-zero. If the parameter is not found,
9392 * zero is returned and value/value_l are not touched.
9393 */
9394static int
9395find_url_param_value(char* path, size_t path_l,
9396 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009397 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009398{
9399 char *query_string, *qs_end;
9400 char *arg_start;
9401 char *value_start, *value_end;
9402
bedis4c75cca2012-10-05 08:38:24 +02009403 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009404 if (!query_string)
9405 return 0;
9406
9407 qs_end = path + path_l;
9408 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009409 url_param_name, url_param_name_l,
9410 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009411 if (!arg_start)
9412 return 0;
9413
9414 value_start = arg_start + url_param_name_l + 1;
9415 value_end = value_start;
9416
bedis4c75cca2012-10-05 08:38:24 +02009417 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009418 value_end++;
9419
9420 *value = value_start;
9421 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009422 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009423}
9424
9425static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009426smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9427 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09009428{
bedis4c75cca2012-10-05 08:38:24 +02009429 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009430 struct http_txn *txn = l7;
9431 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009432
bedis4c75cca2012-10-05 08:38:24 +02009433 if (!args || args[0].type != ARGT_STR ||
9434 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009435 return 0;
9436
9437 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009438
bedis4c75cca2012-10-05 08:38:24 +02009439 if (args[1].type)
9440 delim = *args[1].data.str.str;
9441
Willy Tarreau9b28e032012-10-12 23:49:43 +02009442 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009443 args->data.str.str, args->data.str.len,
9444 &smp->data.str.str, &smp->data.str.len,
9445 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009446 return 0;
9447
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009448 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009449 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009450 return 1;
9451}
9452
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009453/* Return the signed integer value for the specified url parameter (see url_param
9454 * above).
9455 */
9456static int
9457smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9458 const struct arg *args, struct sample *smp)
9459{
9460 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
9461
9462 if (ret > 0) {
9463 smp->type = SMP_T_UINT;
9464 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9465 }
9466
9467 return ret;
9468}
9469
Willy Tarreau185b5c42012-04-26 15:11:51 +02009470/* This function is used to validate the arguments passed to any "hdr" fetch
9471 * keyword. These keywords support an optional positive or negative occurrence
9472 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9473 * is assumed that the types are already the correct ones. Returns 0 on error,
9474 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9475 * error message in case of error, that the caller is responsible for freeing.
9476 * The initial location must either be freeable or NULL.
9477 */
9478static int val_hdr(struct arg *arg, char **err_msg)
9479{
9480 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009481 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009482 return 0;
9483 }
9484 return 1;
9485}
9486
Willy Tarreau4a568972010-05-12 08:08:50 +02009487/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009488/* All supported ACL keywords must be declared here. */
9489/************************************************************************/
9490
9491/* Note: must not be declared <const> as its list will be overwritten.
9492 * Please take care of keeping this list alphabetically sorted.
9493 */
9494static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009495 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009496 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9497 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9498 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9499 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9500 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9501 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9502 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
9503
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009504 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02009505 { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009506 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02009507 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9508 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9509 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9510 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9511 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9512 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9513 { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009514
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009515 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau185b5c42012-04-26 15:11:51 +02009516 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9517 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9518 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9519 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9520 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009521 { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau185b5c42012-04-26 15:11:51 +02009522 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9523 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9524 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9525 { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009526
9527 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009528 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009529 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9530
9531 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
9532
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009533 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009534 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9535 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9536 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9537 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9538 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9539 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9540 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009541
9542 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009543 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 },
9544 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009545
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009546 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02009547 { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009548 { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02009549 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9550 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9551 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9552 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9553 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9554 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9555 { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009556
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009557 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau185b5c42012-04-26 15:11:51 +02009558 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9559 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9560 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9561 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9562 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009563 { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau185b5c42012-04-26 15:11:51 +02009564 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9565 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9566 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9567 { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009568
9569 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
9570
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009571 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009572 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9573 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9574 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9575 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009576 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009577 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9578 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
9579 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9580 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009581
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009582 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009583 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9584 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9585 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9586 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreaue0db1e82013-01-04 16:31:47 +01009587 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009588 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9589 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9590 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009591 { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009592
9593 { NULL, NULL, NULL, NULL },
9594}};
9595
9596/************************************************************************/
9597/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009598/************************************************************************/
9599/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009600static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau80aca902013-01-07 15:42:20 +01009601 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
9602 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9603 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
9604 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
9605 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9606 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9607 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
9608 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
9609 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9610 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
9611 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02009612 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009613}};
9614
Willy Tarreau8797c062007-05-07 00:55:35 +02009615
9616__attribute__((constructor))
9617static void __http_protocol_init(void)
9618{
9619 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009620 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009621}
9622
9623
Willy Tarreau58f10d72006-12-04 02:26:12 +01009624/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009625 * Local variables:
9626 * c-indent-level: 8
9627 * c-basic-offset: 8
9628 * End:
9629 */