blob: 8d9844011edfdd897433d72ebe275b6c1fec080e [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 Tarreau04ff9f12013-06-10 18:39:42 +0200491/* Find the first or next occurrence of header <name> in message buffer <sol>
492 * using headers index <idx>, and return it in the <ctx> structure. This
493 * structure holds everything necessary to use the header and find next
494 * occurrence. If its <idx> member is 0, the header is searched from the
495 * beginning. Otherwise, the next occurrence is returned. The function returns
496 * 1 when it finds a value, and 0 when there is no more. It is very similar to
497 * http_find_header2() except that it is designed to work with full-line headers
498 * whose comma is not a delimiter but is part of the syntax. As a special case,
499 * if ctx->val is NULL when searching for a new values of a header, the current
500 * header is rescanned. This allows rescanning after a header deletion.
501 */
502int http_find_full_header2(const char *name, int len,
503 char *sol, struct hdr_idx *idx,
504 struct hdr_ctx *ctx)
505{
506 char *eol, *sov;
507 int cur_idx, old_idx;
508
509 cur_idx = ctx->idx;
510 if (cur_idx) {
511 /* We have previously returned a header, let's search another one */
512 sol = ctx->line;
513 eol = sol + idx->v[cur_idx].len;
514 goto next_hdr;
515 }
516
517 /* first request for this header */
518 sol += hdr_idx_first_pos(idx);
519 old_idx = 0;
520 cur_idx = hdr_idx_first_idx(idx);
521 while (cur_idx) {
522 eol = sol + idx->v[cur_idx].len;
523
524 if (len == 0) {
525 /* No argument was passed, we want any header.
526 * To achieve this, we simply build a fake request. */
527 while (sol + len < eol && sol[len] != ':')
528 len++;
529 name = sol;
530 }
531
532 if ((len < eol - sol) &&
533 (sol[len] == ':') &&
534 (strncasecmp(sol, name, len) == 0)) {
535 ctx->del = len;
536 sov = sol + len + 1;
537 while (sov < eol && http_is_lws[(unsigned char)*sov])
538 sov++;
539
540 ctx->line = sol;
541 ctx->prev = old_idx;
542 ctx->idx = cur_idx;
543 ctx->val = sov - sol;
544 ctx->tws = 0;
545 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
546 eol--;
547 ctx->tws++;
548 }
549 ctx->vlen = eol - sov;
550 return 1;
551 }
552 next_hdr:
553 sol = eol + idx->v[cur_idx].cr + 1;
554 old_idx = cur_idx;
555 cur_idx = idx->v[cur_idx].next;
556 }
557 return 0;
558}
559
Willy Tarreau68085d82010-01-18 14:54:04 +0100560/* Find the end of the header value contained between <s> and <e>. See RFC2616,
561 * par 2.2 for more information. Note that it requires a valid header to return
562 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200563 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100564char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565{
566 int quoted, qdpair;
567
568 quoted = qdpair = 0;
569 for (; s < e; s++) {
570 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200571 else if (quoted) {
572 if (*s == '\\') qdpair = 1;
573 else if (*s == '"') quoted = 0;
574 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200575 else if (*s == '"') quoted = 1;
576 else if (*s == ',') return s;
577 }
578 return s;
579}
580
581/* Find the first or next occurrence of header <name> in message buffer <sol>
582 * using headers index <idx>, and return it in the <ctx> structure. This
583 * structure holds everything necessary to use the header and find next
584 * occurrence. If its <idx> member is 0, the header is searched from the
585 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100586 * 1 when it finds a value, and 0 when there is no more. It is designed to work
587 * with headers defined as comma-separated lists. As a special case, if ctx->val
588 * is NULL when searching for a new values of a header, the current header is
589 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200590 */
591int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100592 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200593 struct hdr_ctx *ctx)
594{
Willy Tarreau68085d82010-01-18 14:54:04 +0100595 char *eol, *sov;
596 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200597
Willy Tarreau68085d82010-01-18 14:54:04 +0100598 cur_idx = ctx->idx;
599 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200600 /* We have previously returned a value, let's search
601 * another one on the same line.
602 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200603 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200604 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100605 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200606 eol = sol + idx->v[cur_idx].len;
607
608 if (sov >= eol)
609 /* no more values in this header */
610 goto next_hdr;
611
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 /* values remaining for this header, skip the comma but save it
613 * for later use (eg: for header deletion).
614 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200615 sov++;
616 while (sov < eol && http_is_lws[(unsigned char)*sov])
617 sov++;
618
619 goto return_hdr;
620 }
621
622 /* first request for this header */
623 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200625 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626 while (cur_idx) {
627 eol = sol + idx->v[cur_idx].len;
628
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200629 if (len == 0) {
630 /* No argument was passed, we want any header.
631 * To achieve this, we simply build a fake request. */
632 while (sol + len < eol && sol[len] != ':')
633 len++;
634 name = sol;
635 }
636
Willy Tarreau33a7e692007-06-10 19:45:56 +0200637 if ((len < eol - sol) &&
638 (sol[len] == ':') &&
639 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100640 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200641 sov = sol + len + 1;
642 while (sov < eol && http_is_lws[(unsigned char)*sov])
643 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100644
Willy Tarreau33a7e692007-06-10 19:45:56 +0200645 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100646 ctx->prev = old_idx;
647 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200648 ctx->idx = cur_idx;
649 ctx->val = sov - sol;
650
651 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200652 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200653 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200654 eol--;
655 ctx->tws++;
656 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 ctx->vlen = eol - sov;
658 return 1;
659 }
660 next_hdr:
661 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100662 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200663 cur_idx = idx->v[cur_idx].next;
664 }
665 return 0;
666}
667
668int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100669 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200670 struct hdr_ctx *ctx)
671{
672 return http_find_header2(name, strlen(name), sol, idx, ctx);
673}
674
Willy Tarreau68085d82010-01-18 14:54:04 +0100675/* Remove one value of a header. This only works on a <ctx> returned by one of
676 * the http_find_header functions. The value is removed, as well as surrounding
677 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100678 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100679 * message <msg>. The new index is returned. If it is zero, it means there is
680 * no more header, so any processing may stop. The ctx is always left in a form
681 * that can be handled by http_find_header2() to find next occurrence.
682 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100683int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100684{
685 int cur_idx = ctx->idx;
686 char *sol = ctx->line;
687 struct hdr_idx_elem *hdr;
688 int delta, skip_comma;
689
690 if (!cur_idx)
691 return 0;
692
693 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200694 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100695 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200696 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100697 http_msg_move_end(msg, delta);
698 idx->used--;
699 hdr->len = 0; /* unused entry */
700 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100701 if (idx->tail == ctx->idx)
702 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100703 ctx->idx = ctx->prev; /* walk back to the end of previous header */
704 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
705 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200706 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100707 return ctx->idx;
708 }
709
710 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200711 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
712 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100713 */
714
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200715 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200716 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200717 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 NULL, 0);
719 hdr->len += delta;
720 http_msg_move_end(msg, delta);
721 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200722 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100723 return ctx->idx;
724}
725
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100726/* This function handles a server error at the stream interface level. The
727 * stream interface is assumed to be already in a closed state. An optional
728 * message is copied into the input buffer, and an HTTP status code stored.
729 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100730 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100732static void http_server_error(struct session *t, struct stream_interface *si,
733 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200735 channel_auto_read(si->ob);
736 channel_abort(si->ob);
737 channel_auto_close(si->ob);
738 channel_erase(si->ob);
739 channel_auto_close(si->ib);
740 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100741 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100742 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200743 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744 }
745 if (!(t->flags & SN_ERR_MASK))
746 t->flags |= err;
747 if (!(t->flags & SN_FINST_MASK))
748 t->flags |= finst;
749}
750
Willy Tarreau80587432006-12-24 17:47:20 +0100751/* This function returns the appropriate error location for the given session
752 * and message.
753 */
754
Willy Tarreau783f2582012-09-04 12:19:04 +0200755struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100756{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200757 if (s->be->errmsg[msgnum].str)
758 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100759 else if (s->fe->errmsg[msgnum].str)
760 return &s->fe->errmsg[msgnum];
761 else
762 return &http_err_chunks[msgnum];
763}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764
Willy Tarreau53b6c742006-12-17 13:37:46 +0100765/*
766 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
767 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
768 */
769static http_meth_t find_http_meth(const char *str, const int len)
770{
771 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100772 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100773
774 m = ((unsigned)*str - 'A');
775
776 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100777 for (h = http_methods[m]; h->len > 0; h++) {
778 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100779 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100780 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100781 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100782 };
783 return HTTP_METH_OTHER;
784 }
785 return HTTP_METH_NONE;
786
787}
788
Willy Tarreau21d2af32008-02-14 20:25:24 +0100789/* Parse the URI from the given transaction (which is assumed to be in request
790 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
791 * It is returned otherwise.
792 */
793static char *
794http_get_path(struct http_txn *txn)
795{
796 char *ptr, *end;
797
Willy Tarreau9b28e032012-10-12 23:49:43 +0200798 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100799 end = ptr + txn->req.sl.rq.u_l;
800
801 if (ptr >= end)
802 return NULL;
803
804 /* RFC2616, par. 5.1.2 :
805 * Request-URI = "*" | absuri | abspath | authority
806 */
807
808 if (*ptr == '*')
809 return NULL;
810
811 if (isalpha((unsigned char)*ptr)) {
812 /* this is a scheme as described by RFC3986, par. 3.1 */
813 ptr++;
814 while (ptr < end &&
815 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
816 ptr++;
817 /* skip '://' */
818 if (ptr == end || *ptr++ != ':')
819 return NULL;
820 if (ptr == end || *ptr++ != '/')
821 return NULL;
822 if (ptr == end || *ptr++ != '/')
823 return NULL;
824 }
825 /* skip [user[:passwd]@]host[:[port]] */
826
827 while (ptr < end && *ptr != '/')
828 ptr++;
829
830 if (ptr == end)
831 return NULL;
832
833 /* OK, we got the '/' ! */
834 return ptr;
835}
836
Willy Tarreau71241ab2012-12-27 11:30:54 +0100837/* Returns a 302 for a redirectable request that reaches a server working in
838 * in redirect mode. This may only be called just after the stream interface
839 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
840 * follow normal proxy processing. NOTE: this function is designed to support
841 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100843void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100844{
845 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100846 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100847 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200848 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849
850 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100851 trash.len = strlen(HTTP_302);
852 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100853
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100854 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100855
Willy Tarreauefb453c2008-10-26 20:49:47 +0100856 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100857 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858 return;
859
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100860 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100861 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100862 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
863 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100864 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100865
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200866 /* 3: add the request URI. Since it was already forwarded, we need
867 * to temporarily rewind the buffer.
868 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100869 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200870 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200871
Willy Tarreauefb453c2008-10-26 20:49:47 +0100872 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200873 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 +0200874
Willy Tarreau9b28e032012-10-12 23:49:43 +0200875 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200876
Willy Tarreauefb453c2008-10-26 20:49:47 +0100877 if (!path)
878 return;
879
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100880 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100881 return;
882
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100883 memcpy(trash.str + trash.len, path, len);
884 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100885
886 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100887 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
888 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100889 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100890 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
891 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100892 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100893
894 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200895 si_shutr(si);
896 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100897 si->err_type = SI_ET_NONE;
898 si->err_loc = NULL;
899 si->state = SI_ST_CLO;
900
901 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200902 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100903
904 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100905 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906}
907
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100908/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100909 * that the server side is closed. Note that err_type is actually a
910 * bitmask, where almost only aborts may be cumulated with other
911 * values. We consider that aborted operations are more important
912 * than timeouts or errors due to the fact that nobody else in the
913 * logs might explain incomplete retries. All others should avoid
914 * being cumulated. It should normally not be possible to have multiple
915 * aborts at once, but just in case, the first one in sequence is reported.
916 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100917void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100919 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100920
921 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100922 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200923 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100924 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100925 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200926 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100927 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100928 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200929 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200932 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100933 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200935 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100936 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100937 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200938 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100940 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200941 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942}
943
Willy Tarreau42250582007-04-01 01:30:43 +0200944extern const char sess_term_cond[8];
945extern const char sess_fin_state[8];
946extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200947struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100948struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100949struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100950
Willy Tarreau117f59e2007-03-04 18:17:17 +0100951/*
952 * Capture headers from message starting at <som> according to header list
953 * <cap_hdr>, and fill the <idx> structure appropriately.
954 */
955void capture_headers(char *som, struct hdr_idx *idx,
956 char **cap, struct cap_hdr *cap_hdr)
957{
958 char *eol, *sol, *col, *sov;
959 int cur_idx;
960 struct cap_hdr *h;
961 int len;
962
963 sol = som + hdr_idx_first_pos(idx);
964 cur_idx = hdr_idx_first_idx(idx);
965
966 while (cur_idx) {
967 eol = sol + idx->v[cur_idx].len;
968
969 col = sol;
970 while (col < eol && *col != ':')
971 col++;
972
973 sov = col + 1;
974 while (sov < eol && http_is_lws[(unsigned char)*sov])
975 sov++;
976
977 for (h = cap_hdr; h; h = h->next) {
978 if ((h->namelen == col - sol) &&
979 (strncasecmp(sol, h->name, h->namelen) == 0)) {
980 if (cap[h->index] == NULL)
981 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200982 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100983
984 if (cap[h->index] == NULL) {
985 Alert("HTTP capture : out of memory.\n");
986 continue;
987 }
988
989 len = eol - sov;
990 if (len > h->len)
991 len = h->len;
992
993 memcpy(cap[h->index], sov, len);
994 cap[h->index][len]=0;
995 }
996 }
997 sol = eol + idx->v[cur_idx].cr + 1;
998 cur_idx = idx->v[cur_idx].next;
999 }
1000}
1001
1002
Willy Tarreau42250582007-04-01 01:30:43 +02001003/* either we find an LF at <ptr> or we jump to <bad>.
1004 */
1005#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1006
1007/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1008 * otherwise to <http_msg_ood> with <state> set to <st>.
1009 */
1010#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1011 ptr++; \
1012 if (likely(ptr < end)) \
1013 goto good; \
1014 else { \
1015 state = (st); \
1016 goto http_msg_ood; \
1017 } \
1018 } while (0)
1019
1020
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001022 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1024 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1025 * will give undefined results.
1026 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1027 * and that msg->sol points to the beginning of the response.
1028 * If a complete line is found (which implies that at least one CR or LF is
1029 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1030 * returned indicating an incomplete line (which does not mean that parts have
1031 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1032 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1033 * upon next call.
1034 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001035 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1037 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001040const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001041 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001042 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001043{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001044 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001045
Willy Tarreau8973c702007-01-21 23:58:29 +01001046 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001047 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001048 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001049 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001050 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1051
1052 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001053 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001054 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1055 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001056 state = HTTP_MSG_ERROR;
1057 break;
1058
Willy Tarreau8973c702007-01-21 23:58:29 +01001059 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001060 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001061 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001062 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001063 goto http_msg_rpcode;
1064 }
1065 if (likely(HTTP_IS_SPHT(*ptr)))
1066 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1067 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001068 state = HTTP_MSG_ERROR;
1069 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001070
Willy Tarreau8973c702007-01-21 23:58:29 +01001071 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001072 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001073 if (likely(!HTTP_IS_LWS(*ptr)))
1074 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1075
1076 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001077 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001078 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1079 }
1080
1081 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001082 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001083 http_msg_rsp_reason:
1084 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001086 msg->sl.st.r_l = 0;
1087 goto http_msg_rpline_eol;
1088
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001090 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001091 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001093 goto http_msg_rpreason;
1094 }
1095 if (likely(HTTP_IS_SPHT(*ptr)))
1096 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1097 /* so it's a CR/LF, so there is no reason phrase */
1098 goto http_msg_rsp_reason;
1099
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001101 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 if (likely(!HTTP_IS_CRLF(*ptr)))
1103 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001104 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 http_msg_rpline_eol:
1106 /* We have seen the end of line. Note that we do not
1107 * necessarily have the \n yet, but at least we know that we
1108 * have EITHER \r OR \n, otherwise the response would not be
1109 * complete. We can then record the response length and return
1110 * to the caller which will be able to register it.
1111 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001112 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 return ptr;
1114
1115#ifdef DEBUG_FULL
1116 default:
1117 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1118 exit(1);
1119#endif
1120 }
1121
1122 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001123 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 if (ret_state)
1125 *ret_state = state;
1126 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001127 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129}
1130
Willy Tarreau8973c702007-01-21 23:58:29 +01001131/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 * This function parses a request line between <ptr> and <end>, starting with
1133 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1134 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1135 * will give undefined results.
1136 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1137 * and that msg->sol points to the beginning of the request.
1138 * If a complete line is found (which implies that at least one CR or LF is
1139 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1140 * returned indicating an incomplete line (which does not mean that parts have
1141 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1142 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1143 * upon next call.
1144 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001145 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1147 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001148 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001150const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001151 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001152 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001154 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001155
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001158 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 if (likely(HTTP_IS_TOKEN(*ptr)))
1160 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001163 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001164 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001167 if (likely(HTTP_IS_CRLF(*ptr))) {
1168 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001169 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001173 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 msg->sl.rq.v_l = 0;
1177 goto http_msg_rqline_eol;
1178 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001179 state = HTTP_MSG_ERROR;
1180 break;
1181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001183 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001185 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 goto http_msg_rquri;
1187 }
1188 if (likely(HTTP_IS_SPHT(*ptr)))
1189 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1190 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1191 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001192
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001194 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001195 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001197
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001198 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001199 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1201 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001202
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001203 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001204 /* non-ASCII chars are forbidden unless option
1205 * accept-invalid-http-request is enabled in the frontend.
1206 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001207 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001208 if (msg->err_pos < -1)
1209 goto invalid_char;
1210 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001211 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001212 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1213 }
1214
1215 if (likely(HTTP_IS_CRLF(*ptr))) {
1216 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1217 goto http_msg_req09_uri_e;
1218 }
1219
1220 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001221 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001222 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001223 state = HTTP_MSG_ERROR;
1224 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001227 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001229 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 goto http_msg_rqver;
1231 }
1232 if (likely(HTTP_IS_SPHT(*ptr)))
1233 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1234 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1235 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001239 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001241
1242 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001244 http_msg_rqline_eol:
1245 /* We have seen the end of line. Note that we do not
1246 * necessarily have the \n yet, but at least we know that we
1247 * have EITHER \r OR \n, otherwise the request would not be
1248 * complete. We can then record the request length and return
1249 * to the caller which will be able to register it.
1250 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001251 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001252 return ptr;
1253 }
1254
1255 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001256 state = HTTP_MSG_ERROR;
1257 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259#ifdef DEBUG_FULL
1260 default:
1261 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1262 exit(1);
1263#endif
1264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001274
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001275/*
1276 * Returns the data from Authorization header. Function may be called more
1277 * than once so data is stored in txn->auth_data. When no header is found
1278 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1279 * searching again for something we are unable to find anyway.
1280 */
1281
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001282char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001283
1284int
1285get_http_auth(struct session *s)
1286{
1287
1288 struct http_txn *txn = &s->txn;
1289 struct chunk auth_method;
1290 struct hdr_ctx ctx;
1291 char *h, *p;
1292 int len;
1293
1294#ifdef DEBUG_AUTH
1295 printf("Auth for session %p: %d\n", s, txn->auth.method);
1296#endif
1297
1298 if (txn->auth.method == HTTP_AUTH_WRONG)
1299 return 0;
1300
1301 if (txn->auth.method)
1302 return 1;
1303
1304 txn->auth.method = HTTP_AUTH_WRONG;
1305
1306 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001307
1308 if (txn->flags & TX_USE_PX_CONN) {
1309 h = "Proxy-Authorization";
1310 len = strlen(h);
1311 } else {
1312 h = "Authorization";
1313 len = strlen(h);
1314 }
1315
Willy Tarreau9b28e032012-10-12 23:49:43 +02001316 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317 return 0;
1318
1319 h = ctx.line + ctx.val;
1320
1321 p = memchr(h, ' ', ctx.vlen);
1322 if (!p || p == h)
1323 return 0;
1324
1325 chunk_initlen(&auth_method, h, 0, p-h);
1326 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1327
1328 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1329
1330 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001331 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001332
1333 if (len < 0)
1334 return 0;
1335
1336
1337 get_http_auth_buff[len] = '\0';
1338
1339 p = strchr(get_http_auth_buff, ':');
1340
1341 if (!p)
1342 return 0;
1343
1344 txn->auth.user = get_http_auth_buff;
1345 *p = '\0';
1346 txn->auth.pass = p+1;
1347
1348 txn->auth.method = HTTP_AUTH_BASIC;
1349 return 1;
1350 }
1351
1352 return 0;
1353}
1354
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Willy Tarreau8973c702007-01-21 23:58:29 +01001356/*
1357 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001358 * depending on the initial msg->msg_state. The caller is responsible for
1359 * ensuring that the message does not wrap. The function can be preempted
1360 * everywhere when data are missing and recalled at the exact same location
1361 * with no information loss. The message may even be realigned between two
1362 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001363 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001364 * fields. Note that msg->sol will be initialized after completing the first
1365 * state, so that none of the msg pointers has to be initialized prior to the
1366 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001368void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001370 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001372 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001373
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001374 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001375 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001376 ptr = buf->p + msg->next;
1377 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 if (unlikely(ptr >= end))
1380 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 /*
1384 * First, states that are specific to the response only.
1385 * We check them first so that request and headers are
1386 * closer to each other (accessed more often).
1387 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001388 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001389 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001391 /* we have a start of message, but we have to check
1392 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001393 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001394 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001395 if (unlikely(ptr != buf->p)) {
1396 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001397 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001398 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001399 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 }
Willy Tarreau26927362012-05-18 23:22:52 +02001401 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001402 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001403 hdr_idx_init(idx);
1404 state = HTTP_MSG_RPVER;
1405 goto http_msg_rpver;
1406 }
1407
1408 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1409 goto http_msg_invalid;
1410
1411 if (unlikely(*ptr == '\n'))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1413 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1414 /* stop here */
1415
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001417 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001418 EXPECT_LF_HERE(ptr, http_msg_invalid);
1419 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1420 /* stop here */
1421
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001424 case HTTP_MSG_RPVER_SP:
1425 case HTTP_MSG_RPCODE:
1426 case HTTP_MSG_RPCODE_SP:
1427 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001428 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001429 state, ptr, end,
1430 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 if (unlikely(!ptr))
1432 return;
1433
1434 /* we have a full response and we know that we have either a CR
1435 * or an LF at <ptr>.
1436 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1438
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001439 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 if (likely(*ptr == '\r'))
1441 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1442 goto http_msg_rpline_end;
1443
Willy Tarreau8973c702007-01-21 23:58:29 +01001444 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001445 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 /* msg->sol must point to the first of CR or LF. */
1447 EXPECT_LF_HERE(ptr, http_msg_invalid);
1448 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1449 /* stop here */
1450
1451 /*
1452 * Second, states that are specific to the request only
1453 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001455 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001457 /* we have a start of message, but we have to check
1458 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001459 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001460 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001461 if (likely(ptr != buf->p)) {
1462 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001463 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001464 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001465 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 }
Willy Tarreau26927362012-05-18 23:22:52 +02001467 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001468 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 state = HTTP_MSG_RQMETH;
1470 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1474 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 if (unlikely(*ptr == '\n'))
1477 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1478 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001482 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 EXPECT_LF_HERE(ptr, http_msg_invalid);
1484 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001485 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 case HTTP_MSG_RQMETH_SP:
1490 case HTTP_MSG_RQURI:
1491 case HTTP_MSG_RQURI_SP:
1492 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001493 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001494 state, ptr, end,
1495 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 if (unlikely(!ptr))
1497 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 /* we have a full request and we know that we have either a CR
1500 * or an LF at <ptr>.
1501 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001504 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 if (likely(*ptr == '\r'))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001510 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 /* check for HTTP/0.9 request : no version information available.
1512 * msg->sol must point to the first of CR or LF.
1513 */
1514 if (unlikely(msg->sl.rq.v_l == 0))
1515 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 EXPECT_LF_HERE(ptr, http_msg_invalid);
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001519 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 /*
1522 * Common states below
1523 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001526 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (likely(!HTTP_IS_CRLF(*ptr))) {
1528 goto http_msg_hdr_name;
1529 }
1530
1531 if (likely(*ptr == '\r'))
1532 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1533 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 /* assumes msg->sol points to the first char */
1538 if (likely(HTTP_IS_TOKEN(*ptr)))
1539 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001541 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001543
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001544 if (likely(msg->err_pos < -1) || *ptr == '\n')
1545 goto http_msg_invalid;
1546
1547 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001549
1550 /* and we still accept this non-token character */
1551 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001554 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001555 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (likely(HTTP_IS_SPHT(*ptr)))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 if (likely(!HTTP_IS_CRLF(*ptr))) {
1563 goto http_msg_hdr_val;
1564 }
1565
1566 if (likely(*ptr == '\r'))
1567 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1568 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 EXPECT_LF_HERE(ptr, http_msg_invalid);
1573 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (likely(HTTP_IS_SPHT(*ptr))) {
1578 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001579 for (; buf->p + msg->sov < ptr; msg->sov++)
1580 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 goto http_msg_hdr_l1_sp;
1582 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001583 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001584 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 goto http_msg_complete_header;
1586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001588 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001589 /* assumes msg->sol points to the first char, and msg->sov
1590 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 */
1592 if (likely(!HTTP_IS_CRLF(*ptr)))
1593 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001594
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001595 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 /* Note: we could also copy eol into ->eoh so that we have the
1597 * real header end in case it ends with lots of LWS, but is this
1598 * really needed ?
1599 */
1600 if (likely(*ptr == '\r'))
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1602 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001605 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 EXPECT_LF_HERE(ptr, http_msg_invalid);
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001610 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1612 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001613 for (; buf->p + msg->eol < ptr; msg->eol++)
1614 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 goto http_msg_hdr_val;
1616 }
1617 http_msg_complete_header:
1618 /*
1619 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001620 * Assumes msg->sol points to the first char, msg->sov points
1621 * to the first character of the value and msg->eol to the
1622 * first CR or LF so we know how the line ends. We insert last
1623 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001625 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 idx, idx->tail) < 0))
1627 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001628
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 if (likely(!HTTP_IS_CRLF(*ptr))) {
1631 goto http_msg_hdr_name;
1632 }
1633
1634 if (likely(*ptr == '\r'))
1635 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1636 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001639 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 /* Assumes msg->sol points to the first of either CR or LF */
1641 EXPECT_LF_HERE(ptr, http_msg_invalid);
1642 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001644 msg->eoh = msg->sol;
1645 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001646 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001648
1649 case HTTP_MSG_ERROR:
1650 /* this may only happen if we call http_msg_analyser() twice with an error */
1651 break;
1652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653#ifdef DEBUG_FULL
1654 default:
1655 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1656 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001657#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 }
1659 http_msg_ood:
1660 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001661 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001664
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 http_msg_invalid:
1666 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001668 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 return;
1670}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001671
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001672/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1673 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1674 * nothing is done and 1 is returned.
1675 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001676static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001677{
1678 int delta;
1679 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001680 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001681
1682 if (msg->sl.rq.v_l != 0)
1683 return 1;
1684
Willy Tarreau9b28e032012-10-12 23:49:43 +02001685 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001686 delta = 0;
1687
1688 if (msg->sl.rq.u_l == 0) {
1689 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001690 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001691 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001692 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001693 }
1694 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001695 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001696 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001697 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001698 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001699 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001700 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001701 NULL, NULL);
1702 if (unlikely(!cur_end))
1703 return 0;
1704
1705 /* we have a full HTTP/1.0 request now and we know that
1706 * we have either a CR or an LF at <ptr>.
1707 */
1708 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1709 return 1;
1710}
1711
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001712/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001713 * and "keep-alive" values. If we already know that some headers may safely
1714 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001715 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1716 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001717 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1719 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1720 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001721 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001722 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001723void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001724{
Willy Tarreau5b154472009-12-21 20:11:07 +01001725 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001726 const char *hdr_val = "Connection";
1727 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001728
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001729 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001730 return;
1731
Willy Tarreau88d349d2010-01-25 12:15:43 +01001732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection";
1734 hdr_len = 16;
1735 }
1736
Willy Tarreau5b154472009-12-21 20:11:07 +01001737 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001739 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001740 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1741 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001742 if (to_del & 2)
1743 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001744 else
1745 txn->flags |= TX_CON_KAL_SET;
1746 }
1747 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1748 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001749 if (to_del & 1)
1750 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001751 else
1752 txn->flags |= TX_CON_CLO_SET;
1753 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001754 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1755 txn->flags |= TX_HDR_CONN_UPG;
1756 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001757 }
1758
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001759 txn->flags |= TX_HDR_CONN_PRS;
1760 return;
1761}
Willy Tarreau5b154472009-12-21 20:11:07 +01001762
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001763/* Apply desired changes on the Connection: header. Values may be removed and/or
1764 * added depending on the <wanted> flags, which are exclusively composed of
1765 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1766 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1767 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001768void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001769{
1770 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001771 const char *hdr_val = "Connection";
1772 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001773
1774 ctx.idx = 0;
1775
Willy Tarreau88d349d2010-01-25 12:15:43 +01001776
1777 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1778 hdr_val = "Proxy-Connection";
1779 hdr_len = 16;
1780 }
1781
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001782 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001784 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1785 if (wanted & TX_CON_KAL_SET)
1786 txn->flags |= TX_CON_KAL_SET;
1787 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001788 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001789 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001790 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1791 if (wanted & TX_CON_CLO_SET)
1792 txn->flags |= TX_CON_CLO_SET;
1793 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001794 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001795 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001796 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001797
1798 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1799 return;
1800
1801 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1802 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001803 hdr_val = "Connection: close";
1804 hdr_len = 17;
1805 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1806 hdr_val = "Proxy-Connection: close";
1807 hdr_len = 23;
1808 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001809 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001810 }
1811
1812 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1813 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001814 hdr_val = "Connection: keep-alive";
1815 hdr_len = 22;
1816 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1817 hdr_val = "Proxy-Connection: keep-alive";
1818 hdr_len = 28;
1819 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001820 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821 }
1822 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001823}
1824
Willy Tarreaua458b672012-03-05 11:17:50 +01001825/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001827 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001829 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001830 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001831static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001832{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001833 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001835 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001836 const char *end = buf->data + buf->size;
1837 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 unsigned int chunk = 0;
1839
1840 /* The chunk size is in the following form, though we are only
1841 * interested in the size and CRLF :
1842 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1843 */
1844 while (1) {
1845 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001846 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001849 if (c < 0) /* not a hex digit anymore */
1850 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001851 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001852 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001853 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001854 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 chunk = (chunk << 4) + c;
1856 }
1857
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001859 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001860 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861
1862 while (http_is_spht[(unsigned char)*ptr]) {
1863 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001865 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001866 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 }
1868
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 /* Up to there, we know that at least one byte is present at *ptr. Check
1870 * for the end of chunk size.
1871 */
1872 while (1) {
1873 if (likely(HTTP_IS_CRLF(*ptr))) {
1874 /* we now have a CR or an LF at ptr */
1875 if (likely(*ptr == '\r')) {
1876 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001877 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001878 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 return 0;
1880 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001881
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001883 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001885 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 /* done */
1887 break;
1888 }
1889 else if (*ptr == ';') {
1890 /* chunk extension, ends at next CRLF */
1891 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001892 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001893 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001894 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
1896 while (!HTTP_IS_CRLF(*ptr)) {
1897 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001899 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 return 0;
1901 }
1902 /* we have a CRLF now, loop above */
1903 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001904 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001906 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 }
1908
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001910 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001913 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001914 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001915 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001917 msg->chunk_len = chunk;
1918 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001921 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001923 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001924}
1925
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001926/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001927 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 * the trailers is found, it is automatically scheduled to be forwarded,
1929 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1930 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001931 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001932 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001933 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001934 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1935 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001936 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001937 * matches the length of trailers already parsed and not forwarded. It is also
1938 * important to note that this function is designed to be able to parse wrapped
1939 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001941static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001943 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001944
Willy Tarreaua458b672012-03-05 11:17:50 +01001945 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001947 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001948 const char *ptr = b_ptr(buf, msg->next);
1949 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001950 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951
1952 /* scan current line and stop at LF or CRLF */
1953 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001954 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 return 0;
1956
1957 if (*ptr == '\n') {
1958 if (!p1)
1959 p1 = ptr;
1960 p2 = ptr;
1961 break;
1962 }
1963
1964 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001965 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001966 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001967 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001968 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001969 p1 = ptr;
1970 }
1971
1972 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001973 if (ptr >= buf->data + buf->size)
1974 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 }
1976
1977 /* after LF; point to beginning of next line */
1978 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001979 if (p2 >= buf->data + buf->size)
1980 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001982 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001983 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001985
1986 /* schedule this line for forwarding */
1987 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 if (msg->sov >= buf->size)
1989 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001991 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001992 /* LF/CRLF at beginning of line => end of trailers at p2.
1993 * Everything was scheduled for forwarding, there's nothing
1994 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001995 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001996 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 msg->msg_state = HTTP_MSG_DONE;
1998 return 1;
1999 }
2000 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002001 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002 }
2003}
2004
Willy Tarreau54d23df2012-10-25 19:04:45 +02002005/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002007 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002008 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2010 * not enough data are available, the function does not change anything and
2011 * returns zero. If a parse error is encountered, the function returns < 0 and
2012 * does not change anything. Note: this function is designed to parse wrapped
2013 * CRLF at the end of the buffer.
2014 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002015static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002016{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002017 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002019 int bytes;
2020
2021 /* NB: we'll check data availabilty at the end. It's not a
2022 * problem because whatever we match first will be checked
2023 * against the correct length.
2024 */
2025 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002026 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002027 if (*ptr == '\r') {
2028 bytes++;
2029 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002030 if (ptr >= buf->data + buf->size)
2031 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002032 }
2033
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002034 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002037 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002038 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002039 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002040 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041
2042 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002043 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002045 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2046 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002047 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2049 return 1;
2050}
Willy Tarreau5b154472009-12-21 20:11:07 +01002051
William Lallemand82fe75c2012-10-23 10:25:10 +02002052
2053/*
2054 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002055 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002056int select_compression_request_header(struct session *s, struct buffer *req)
2057{
2058 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002059 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002060 struct hdr_ctx ctx;
2061 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002062 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002063
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002064 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2065 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002066 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2067 */
2068 ctx.idx = 0;
2069 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2070 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002071 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2072 (ctx.vlen < 31 ||
2073 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2074 ctx.line[ctx.val + 30] < '6' ||
2075 (ctx.line[ctx.val + 30] == '6' &&
2076 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2077 s->comp_algo = NULL;
2078 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002079 }
2080
William Lallemand82fe75c2012-10-23 10:25:10 +02002081 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002082 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 +02002083 ctx.idx = 0;
2084 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002085 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002086 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2087 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002088
2089 /* remove all occurrences of the header when "compression offload" is set */
2090
2091 if ((s->be->comp && s->be->comp->offload) ||
2092 (s->fe->comp && s->fe->comp->offload)) {
2093 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2094 ctx.idx = 0;
2095 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2096 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2097 }
2098 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002099 return 1;
2100 }
2101 }
2102 }
2103 }
2104
2105 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002106 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2107 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002108 if (comp_algo->add_data == identity_add_data) {
2109 s->comp_algo = comp_algo;
2110 return 1;
2111 }
2112 }
2113 }
2114
2115 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002116 return 0;
2117}
2118
2119/*
2120 * Selects a comression algorithm depending of the server response.
2121 */
2122int select_compression_response_header(struct session *s, struct buffer *res)
2123{
2124 struct http_txn *txn = &s->txn;
2125 struct http_msg *msg = &txn->rsp;
2126 struct hdr_ctx ctx;
2127 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002128
2129 /* no common compression algorithm was found in request header */
2130 if (s->comp_algo == NULL)
2131 goto fail;
2132
2133 /* HTTP < 1.1 should not be compressed */
2134 if (!(msg->flags & HTTP_MSGF_VER_11))
2135 goto fail;
2136
William Lallemandd3002612012-11-26 14:34:47 +01002137 /* 200 only */
2138 if (txn->status != 200)
2139 goto fail;
2140
William Lallemand82fe75c2012-10-23 10:25:10 +02002141 /* Content-Length is null */
2142 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2143 goto fail;
2144
Willy Tarreau667c2a32013-04-09 08:13:58 +02002145 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2146 if (msg->flags & HTTP_MSGF_TE_CHNK)
2147 goto fail;
2148
William Lallemand82fe75c2012-10-23 10:25:10 +02002149 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002150 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002151 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2152 goto fail;
2153
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002154 /* no compression when Cache-Control: no-transform is present in the message */
2155 ctx.idx = 0;
2156 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2157 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2158 goto fail;
2159 }
2160
William Lallemand82fe75c2012-10-23 10:25:10 +02002161 comp_type = NULL;
2162
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002163 /* we don't want to compress multipart content-types, nor content-types that are
2164 * not listed in the "compression type" directive if any. If no content-type was
2165 * found but configuration requires one, we don't compress either. Backend has
2166 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002167 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002168 ctx.idx = 0;
2169 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2170 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2171 goto fail;
2172
2173 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2174 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002175 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002176 if (ctx.vlen >= comp_type->name_len &&
2177 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002178 /* this Content-Type should be compressed */
2179 break;
2180 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002181 /* this Content-Type should not be compressed */
2182 if (comp_type == NULL)
2183 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002184 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002185 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002186 else { /* no content-type header */
2187 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2188 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002189 }
2190
William Lallemandd85f9172012-11-09 17:05:39 +01002191 /* limit compression rate */
2192 if (global.comp_rate_lim > 0)
2193 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2194 goto fail;
2195
William Lallemand072a2bf2012-11-20 17:01:01 +01002196 /* limit cpu usage */
2197 if (idle_pct < compress_min_idle)
2198 goto fail;
2199
William Lallemand4c49fae2012-11-07 15:00:23 +01002200 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002201 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002202 goto fail;
2203
William Lallemandec3e3892012-11-12 17:02:18 +01002204 s->flags |= SN_COMP_READY;
2205
William Lallemand82fe75c2012-10-23 10:25:10 +02002206 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002207 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002208 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2209 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2210
2211 /* add Transfer-Encoding header */
2212 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2213 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2214
2215 /*
2216 * Add Content-Encoding header when it's not identity encoding.
2217 * RFC 2616 : Identity encoding: This content-coding is used only in the
2218 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2219 * header.
2220 */
2221 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002222 trash.len = 18;
2223 memcpy(trash.str, "Content-Encoding: ", trash.len);
2224 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2225 trash.len += s->comp_algo->name_len;
2226 trash.str[trash.len] = '\0';
2227 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002228 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 return 1;
2230
2231fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002232 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002233 return 0;
2234}
2235
2236
Willy Tarreaud787e662009-07-07 10:14:51 +02002237/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2238 * processing can continue on next analysers, or zero if it either needs more
2239 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2240 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2241 * when it has nothing left to do, and may remove any analyser when it wants to
2242 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002243 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002244int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002245{
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 /*
2247 * We will parse the partial (or complete) lines.
2248 * We will check the request syntax, and also join multi-line
2249 * headers. An index of all the lines will be elaborated while
2250 * parsing.
2251 *
2252 * For the parsing, we use a 28 states FSM.
2253 *
2254 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002255 * req->buf->p = beginning of request
2256 * req->buf->p + msg->eoh = end of processed headers / start of current one
2257 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002258 * msg->eol = end of current header or line (LF or CRLF)
2259 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002260 *
2261 * At end of parsing, we may perform a capture of the error (if any), and
2262 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2263 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2264 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002266
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002268 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002269 struct http_txn *txn = &s->txn;
2270 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002271 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002272
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002273 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 +01002274 now_ms, __FUNCTION__,
2275 s,
2276 req,
2277 req->rex, req->wex,
2278 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002279 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002280 req->analysers);
2281
Willy Tarreau52a0c602009-08-16 22:45:38 +02002282 /* we're speaking HTTP here, so let's speak HTTP to the client */
2283 s->srv_error = http_return_srv_error;
2284
Willy Tarreau83e3af02009-12-28 17:39:57 +01002285 /* There's a protected area at the end of the buffer for rewriting
2286 * purposes. We don't want to start to parse the request if the
2287 * protected area is affected, because we may have to move processed
2288 * data later, which is much more complicated.
2289 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002290 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002291 if (txn->flags & TX_NOT_FIRST) {
2292 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002293 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002294 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002295 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002296 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002297 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002298 return 0;
2299 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002300 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2301 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2302 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002303 }
2304
Willy Tarreau065e8332010-01-08 00:30:20 +01002305 /* Note that we have the same problem with the response ; we
2306 * may want to send a redirect, error or anything which requires
2307 * some spare space. So we'll ensure that we have at least
2308 * maxrewrite bytes available in the response buffer before
2309 * processing that one. This will only affect pipelined
2310 * keep-alive requests.
2311 */
2312 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002313 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002314 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2315 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2316 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002317 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002318 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002319 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002320 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002321 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002322 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002323 return 0;
2324 }
2325 }
2326
Willy Tarreau9b28e032012-10-12 23:49:43 +02002327 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002328 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002329 }
2330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /* 1: we might have to print this header in debug mode */
2332 if (unlikely((global.mode & MODE_DEBUG) &&
2333 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002334 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002336
Willy Tarreau9b28e032012-10-12 23:49:43 +02002337 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002338 /* this is a bit complex : in case of error on the request line,
2339 * we know that rq.l is still zero, so we display only the part
2340 * up to the end of the line (truncated by debug_hdr).
2341 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002342 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 sol += hdr_idx_first_pos(&txn->hdr_idx);
2346 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 while (cur_idx) {
2349 eol = sol + txn->hdr_idx.v[cur_idx].len;
2350 debug_hdr("clihdr", s, sol, eol);
2351 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2352 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002353 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 }
2355
Willy Tarreau58f10d72006-12-04 02:26:12 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 /*
2358 * Now we quickly check if we have found a full valid request.
2359 * If not so, we check the FD and buffer states before leaving.
2360 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002361 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002362 * requests are checked first. When waiting for a second request
2363 * on a keep-alive session, if we encounter and error, close, t/o,
2364 * we note the error in the session flags but don't set any state.
2365 * Since the error will be noted there, it will not be counted by
2366 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002367 * Last, we may increase some tracked counters' http request errors on
2368 * the cases that are deliberately the client's fault. For instance,
2369 * a timeout or connection reset is not counted as an error. However
2370 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002372
Willy Tarreau655dce92009-11-08 13:10:58 +01002373 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002374 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002376 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002377 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002378 session_inc_http_req_ctr(s);
2379 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002380 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002382 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002383
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 /* 1: Since we are in header mode, if there's no space
2385 * left for headers, we won't be able to free more
2386 * later, so the session will never terminate. We
2387 * must terminate it now.
2388 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002389 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* FIXME: check if URI is set and return Status
2391 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002392 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002393 session_inc_http_req_ctr(s);
2394 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002395 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002396 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002397 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002398 goto return_bad_req;
2399 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002400
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002402 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002403 if (!(s->flags & SN_ERR_MASK))
2404 s->flags |= SN_ERR_CLICL;
2405
Willy Tarreaufcffa692010-01-10 14:21:19 +01002406 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002407 goto failed_keep_alive;
2408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002410 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002411 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002412 session_inc_http_err_ctr(s);
2413 }
2414
Willy Tarreaudc979f22012-12-04 10:39:01 +01002415 txn->status = 400;
2416 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 msg->msg_state = HTTP_MSG_ERROR;
2418 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002419
Willy Tarreauda7ff642010-06-23 11:44:09 +02002420 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002421 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002422 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002423 if (s->listener->counters)
2424 s->listener->counters->failed_req++;
2425
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 if (!(s->flags & SN_FINST_MASK))
2427 s->flags |= SN_FINST_R;
2428 return 0;
2429 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002432 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002433 if (!(s->flags & SN_ERR_MASK))
2434 s->flags |= SN_ERR_CLITO;
2435
Willy Tarreaufcffa692010-01-10 14:21:19 +01002436 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002437 goto failed_keep_alive;
2438
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002440 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002441 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002442 session_inc_http_err_ctr(s);
2443 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002445 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002446 msg->msg_state = HTTP_MSG_ERROR;
2447 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002448
Willy Tarreauda7ff642010-06-23 11:44:09 +02002449 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002450 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002451 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002452 if (s->listener->counters)
2453 s->listener->counters->failed_req++;
2454
Willy Tarreau59234e92008-11-30 23:51:27 +01002455 if (!(s->flags & SN_FINST_MASK))
2456 s->flags |= SN_FINST_R;
2457 return 0;
2458 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002459
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002461 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002462 if (!(s->flags & SN_ERR_MASK))
2463 s->flags |= SN_ERR_CLICL;
2464
Willy Tarreaufcffa692010-01-10 14:21:19 +01002465 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002466 goto failed_keep_alive;
2467
Willy Tarreau4076a152009-04-02 15:18:36 +02002468 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002469 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002471 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 msg->msg_state = HTTP_MSG_ERROR;
2473 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002474
Willy Tarreauda7ff642010-06-23 11:44:09 +02002475 session_inc_http_err_ctr(s);
2476 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002477 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002478 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002479 if (s->listener->counters)
2480 s->listener->counters->failed_req++;
2481
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 if (!(s->flags & SN_FINST_MASK))
2483 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002484 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002485 }
2486
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002487 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002488 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2489 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002490#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002491 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002492 /* We need more data, we have to re-enable quick-ack in case we
2493 * previously disabled it, otherwise we might cause the client
2494 * to delay next data.
2495 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002496 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002497 }
2498#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002499
Willy Tarreaufcffa692010-01-10 14:21:19 +01002500 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2501 /* If the client starts to talk, let's fall back to
2502 * request timeout processing.
2503 */
2504 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002505 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002506 }
2507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002509 if (!tick_isset(req->analyse_exp)) {
2510 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2511 (txn->flags & TX_WAIT_NEXT_RQ) &&
2512 tick_isset(s->be->timeout.httpka))
2513 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2514 else
2515 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2516 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002517
Willy Tarreau59234e92008-11-30 23:51:27 +01002518 /* we're not ready yet */
2519 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002520
2521 failed_keep_alive:
2522 /* Here we process low-level errors for keep-alive requests. In
2523 * short, if the request is not the first one and it experiences
2524 * a timeout, read error or shutdown, we just silently close so
2525 * that the client can try again.
2526 */
2527 txn->status = 0;
2528 msg->msg_state = HTTP_MSG_RQBEFORE;
2529 req->analysers = 0;
2530 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002531 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002532 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002533 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002534 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 /* OK now we have a complete HTTP request with indexed headers. Let's
2538 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002539 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002540 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002541 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002542 * byte after the last LF. msg->sov points to the first byte of data.
2543 * msg->eol cannot be trusted because it may have been left uninitialized
2544 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002545 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002546
Willy Tarreauda7ff642010-06-23 11:44:09 +02002547 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002548 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2549
Willy Tarreaub16a5742010-01-10 14:46:16 +01002550 if (txn->flags & TX_WAIT_NEXT_RQ) {
2551 /* kill the pending keep-alive timeout */
2552 txn->flags &= ~TX_WAIT_NEXT_RQ;
2553 req->analyse_exp = TICK_ETERNITY;
2554 }
2555
2556
Willy Tarreaud787e662009-07-07 10:14:51 +02002557 /* Maybe we found in invalid header name while we were configured not
2558 * to block on that, so we have to capture it now.
2559 */
2560 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002561 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /*
2564 * 1: identify the method
2565 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002566 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002567
2568 /* we can make use of server redirect on GET and HEAD */
2569 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2570 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /*
2573 * 2: check if the URI matches the monitor_uri.
2574 * We have to do this for every request which gets in, because
2575 * the monitor-uri is defined by the frontend.
2576 */
2577 if (unlikely((s->fe->monitor_uri_len != 0) &&
2578 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002579 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 s->fe->monitor_uri,
2581 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002582 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002584 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002586
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002588 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002589
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002591 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002592 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002593
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 ret = acl_pass(ret);
2595 if (cond->pol == ACL_COND_UNLESS)
2596 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002597
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 if (ret) {
2599 /* we fail this request, let's return 503 service unavail */
2600 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002601 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002602 if (!(s->flags & SN_ERR_MASK))
2603 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002605 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002606 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 /* nothing to fail, let's reply normaly */
2609 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002610 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002611 if (!(s->flags & SN_ERR_MASK))
2612 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 goto return_prx_cond;
2614 }
2615
2616 /*
2617 * 3: Maybe we have to copy the original REQURI for the logs ?
2618 * Note: we cannot log anymore if the request has been
2619 * classified as invalid.
2620 */
2621 if (unlikely(s->logs.logwait & LW_REQ)) {
2622 /* we have a complete HTTP request that we must log */
2623 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2624 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 if (urilen >= REQURI_LEN)
2627 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002628 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002630
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002631 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 s->do_log(s);
2633 } else {
2634 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 }
Willy Tarreau06619262006-12-17 08:37:22 +01002637
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002638 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
2639 s->unique_id = pool_alloc2(pool2_uniqueid);
William Lallemanda73203e2012-03-12 12:48:57 +01002640
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002642 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002643 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002644
Willy Tarreau5b154472009-12-21 20:11:07 +01002645 /* ... and check if the request is HTTP/1.1 or above */
2646 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002647 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2648 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2649 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002650 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002651
2652 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002653 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 +01002654
Willy Tarreau88d349d2010-01-25 12:15:43 +01002655 /* if the frontend has "option http-use-proxy-header", we'll check if
2656 * we have what looks like a proxied connection instead of a connection,
2657 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2658 * Note that this is *not* RFC-compliant, however browsers and proxies
2659 * happen to do that despite being non-standard :-(
2660 * We consider that a request not beginning with either '/' or '*' is
2661 * a proxied connection, which covers both "scheme://location" and
2662 * CONNECT ip:port.
2663 */
2664 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002665 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002666 txn->flags |= TX_USE_PX_CONN;
2667
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002668 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002669 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002670
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002672 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002673 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002675
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002676 /* 6: determine the transfer-length.
2677 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2678 * the presence of a message-body in a REQUEST and its transfer length
2679 * must be determined that way (in order of precedence) :
2680 * 1. The presence of a message-body in a request is signaled by the
2681 * inclusion of a Content-Length or Transfer-Encoding header field
2682 * in the request's header fields. When a request message contains
2683 * both a message-body of non-zero length and a method that does
2684 * not define any semantics for that request message-body, then an
2685 * origin server SHOULD either ignore the message-body or respond
2686 * with an appropriate error message (e.g., 413). A proxy or
2687 * gateway, when presented the same request, SHOULD either forward
2688 * the request inbound with the message- body or ignore the
2689 * message-body when determining a response.
2690 *
2691 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2692 * and the "chunked" transfer-coding (Section 6.2) is used, the
2693 * transfer-length is defined by the use of this transfer-coding.
2694 * If a Transfer-Encoding header field is present and the "chunked"
2695 * transfer-coding is not present, the transfer-length is defined
2696 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002697 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002698 * 3. If a Content-Length header field is present, its decimal value in
2699 * OCTETs represents both the entity-length and the transfer-length.
2700 * If a message is received with both a Transfer-Encoding header
2701 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002702 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002703 * 4. By the server closing the connection. (Closing the connection
2704 * cannot be used to indicate the end of a request body, since that
2705 * would leave no possibility for the server to send back a response.)
2706 *
2707 * Whenever a transfer-coding is applied to a message-body, the set of
2708 * transfer-codings MUST include "chunked", unless the message indicates
2709 * it is terminated by closing the connection. When the "chunked"
2710 * transfer-coding is used, it MUST be the last transfer-coding applied
2711 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002712 */
2713
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002714 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002715 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002716 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002717 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002718 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002719 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002720 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2721 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002722 /* bad transfer-encoding (chunked followed by something else) */
2723 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002724 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002725 break;
2726 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002727 }
2728
Willy Tarreau32b47f42009-10-18 20:55:02 +02002729 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002730 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002731 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002732 signed long long cl;
2733
Willy Tarreauad14f752011-09-02 20:33:27 +02002734 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002735 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002736 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002737 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002738
Willy Tarreauad14f752011-09-02 20:33:27 +02002739 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002740 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002741 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002742 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002743
Willy Tarreauad14f752011-09-02 20:33:27 +02002744 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002745 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002747 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002748
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002749 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002750 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002752 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002753
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002754 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002755 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002756 }
2757
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002758 /* bodyless requests have a known length */
2759 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002760 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002761
Willy Tarreaud787e662009-07-07 10:14:51 +02002762 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002763 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002764 req->analyse_exp = TICK_ETERNITY;
2765 return 1;
2766
2767 return_bad_req:
2768 /* We centralize bad requests processing here */
2769 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2770 /* we detected a parsing error. We want to archive this request
2771 * in the dedicated proxy area for later troubleshooting.
2772 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002773 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002774 }
2775
2776 txn->req.msg_state = HTTP_MSG_ERROR;
2777 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002778 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002779
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002780 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002781 if (s->listener->counters)
2782 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002783
2784 return_prx_cond:
2785 if (!(s->flags & SN_ERR_MASK))
2786 s->flags |= SN_ERR_PRXCOND;
2787 if (!(s->flags & SN_FINST_MASK))
2788 s->flags |= SN_FINST_R;
2789
2790 req->analysers = 0;
2791 req->analyse_exp = TICK_ETERNITY;
2792 return 0;
2793}
2794
Cyril Bonté70be45d2010-10-12 00:14:35 +02002795/* We reached the stats page through a POST request.
2796 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002797 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002798 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002799int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002800{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002801 struct proxy *px = NULL;
2802 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002803
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002804 char key[LINESIZE];
2805 int action = ST_ADM_ACTION_NONE;
2806 int reprocess = 0;
2807
2808 int total_servers = 0;
2809 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002810
2811 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002812 char *st_cur_param = NULL;
2813 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002814
Willy Tarreau9b28e032012-10-12 23:49:43 +02002815 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002816 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002817
2818 cur_param = next_param = end_params;
2819
Willy Tarreau9b28e032012-10-12 23:49:43 +02002820 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002821 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002822 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002823 return 1;
2824 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002825 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002826 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002827 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002828 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002829 }
2830
2831 *end_params = '\0';
2832
Willy Tarreau295a8372011-03-10 11:25:07 +01002833 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002834
2835 /*
2836 * Parse the parameters in reverse order to only store the last value.
2837 * From the html form, the backend and the action are at the end.
2838 */
2839 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002840 char *value;
2841 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002842
2843 cur_param--;
2844 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002845 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002846 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002847 poffset = (cur_param != first_param ? 1 : 0);
2848 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2849 if ((plen > 0) && (plen <= sizeof(key))) {
2850 strncpy(key, cur_param + poffset, plen);
2851 key[plen - 1] = '\0';
2852 } else {
2853 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2854 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002855 }
2856
2857 /* Parse the value */
2858 value = key;
2859 while (*value != '\0' && *value != '=') {
2860 value++;
2861 }
2862 if (*value == '=') {
2863 /* Ok, a value is found, we can mark the end of the key */
2864 *value++ = '\0';
2865 }
2866
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002867 if (!url_decode(key) || !url_decode(value))
2868 break;
2869
Cyril Bonté70be45d2010-10-12 00:14:35 +02002870 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002871 if (!px && (strcmp(key, "b") == 0)) {
2872 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2873 /* the backend name is unknown or ambiguous (duplicate names) */
2874 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2875 goto out;
2876 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002878 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002879 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002880 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002881 }
2882 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002883 action = ST_ADM_ACTION_ENABLE;
2884 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002885 else if (strcmp(value, "stop") == 0) {
2886 action = ST_ADM_ACTION_STOP;
2887 }
2888 else if (strcmp(value, "start") == 0) {
2889 action = ST_ADM_ACTION_START;
2890 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002891 else if (strcmp(value, "shutdown") == 0) {
2892 action = ST_ADM_ACTION_SHUTDOWN;
2893 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002894 else {
2895 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2896 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002897 }
2898 }
2899 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002900 if (!(px && action)) {
2901 /*
2902 * Indicates that we'll need to reprocess the parameters
2903 * as soon as backend and action are known
2904 */
2905 if (!reprocess) {
2906 st_cur_param = cur_param;
2907 st_next_param = next_param;
2908 }
2909 reprocess = 1;
2910 }
2911 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002912 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002913 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002914 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002915 /* Not already in maintenance, we can change the server state */
2916 sv->state |= SRV_MAINTAIN;
2917 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002918 altered_servers++;
2919 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002920 }
2921 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002922 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002923 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002924 /* Already in maintenance, we can change the server state */
2925 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002926 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002927 altered_servers++;
2928 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002929 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002930 break;
2931 case ST_ADM_ACTION_STOP:
2932 case ST_ADM_ACTION_START:
2933 if (action == ST_ADM_ACTION_START)
2934 sv->uweight = sv->iweight;
2935 else
2936 sv->uweight = 0;
2937
2938 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2939 /* we must take care of not pushing the server to full throttle during slow starts */
2940 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2941 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2942 else
2943 sv->eweight = BE_WEIGHT_SCALE;
2944 sv->eweight *= sv->uweight;
2945 } else {
2946 sv->eweight = sv->uweight;
2947 }
2948
2949 /* static LB algorithms are a bit harder to update */
2950 if (px->lbprm.update_server_eweight)
2951 px->lbprm.update_server_eweight(sv);
2952 else if (sv->eweight) {
2953 if (px->lbprm.set_server_status_up)
2954 px->lbprm.set_server_status_up(sv);
2955 }
2956 else {
2957 if (px->lbprm.set_server_status_down)
2958 px->lbprm.set_server_status_down(sv);
2959 }
2960 altered_servers++;
2961 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002962 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002963 case ST_ADM_ACTION_SHUTDOWN:
2964 if (px->state != PR_STSTOPPED) {
2965 struct session *sess, *sess_bck;
2966
2967 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2968 if (sess->srv_conn == sv)
2969 session_shutdown(sess, SN_ERR_KILLED);
2970
2971 altered_servers++;
2972 total_servers++;
2973 }
2974 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002975 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002976 } else {
2977 /* the server name is unknown or ambiguous (duplicate names) */
2978 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002979 }
2980 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002981 if (reprocess && px && action) {
2982 /* Now, we know the backend and the action chosen by the user.
2983 * We can safely restart from the first server parameter
2984 * to reprocess them
2985 */
2986 cur_param = st_cur_param;
2987 next_param = st_next_param;
2988 reprocess = 0;
2989 goto reprocess_servers;
2990 }
2991
Cyril Bonté70be45d2010-10-12 00:14:35 +02002992 next_param = cur_param;
2993 }
2994 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002995
2996 if (total_servers == 0) {
2997 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2998 }
2999 else if (altered_servers == 0) {
3000 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
3001 }
3002 else if (altered_servers == total_servers) {
3003 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
3004 }
3005 else {
3006 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
3007 }
3008 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01003009 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003010}
3011
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003012/* This function checks whether we need to enable a POST analyser to parse a
3013 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003014 * if it needs to come back again, otherwise non-zero if it finishes. In the
3015 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003016 */
3017int http_handle_stats(struct session *s, struct channel *req)
3018{
3019 struct stats_admin_rule *stats_admin_rule;
3020 struct stream_interface *si = s->rep->prod;
3021 struct http_txn *txn = &s->txn;
3022 struct http_msg *msg = &txn->req;
3023 struct uri_auth *uri = s->be->uri_auth;
3024
3025 /* now check whether we have some admin rules for this request */
3026 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3027 int ret = 1;
3028
3029 if (stats_admin_rule->cond) {
3030 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3031 ret = acl_pass(ret);
3032 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3033 ret = !ret;
3034 }
3035
3036 if (ret) {
3037 /* no rule, or the rule matches */
3038 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
3039 break;
3040 }
3041 }
3042
3043 /* Was the status page requested with a POST ? */
3044 if (unlikely(txn->meth == HTTP_METH_POST)) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003045 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3046
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003047 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
3048 if (msg->msg_state < HTTP_MSG_100_SENT) {
3049 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3050 * send an HTTP/1.1 100 Continue intermediate response.
3051 */
3052 if (msg->flags & HTTP_MSGF_VER_11) {
3053 struct hdr_ctx ctx;
3054 ctx.idx = 0;
3055 /* Expect is allowed in 1.1, look for it */
3056 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3057 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3058 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3059 }
3060 }
3061 msg->msg_state = HTTP_MSG_100_SENT;
3062 s->logs.tv_request = now; /* update the request timer to reflect full request */
3063 }
3064 if (!http_process_req_stat_post(si, txn, req))
3065 return 0; /* we need more data */
3066 }
3067 else
3068 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003069 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3070 scope_txt[0] = 0;
3071 if (si->applet.ctx.stats.scope_len) {
3072 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3073 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(req->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3074 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3075 }
3076
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003077
3078 /* We don't want to land on the posted stats page because a refresh will
3079 * repost the data. We don't want this to happen on accident so we redirect
3080 * the browse to the stats page with a GET.
3081 */
3082 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04003083 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003084 "Cache-Control: no-cache\r\n"
3085 "Content-Type: text/plain\r\n"
3086 "Connection: close\r\n"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003087 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003088 "\r\n",
3089 uri->uri_prefix,
3090 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3091 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3092 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3093 stat_status_codes[si->applet.ctx.stats.st_code] :
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003094 stat_status_codes[STAT_STATUS_UNKN],
3095 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3096 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3097 scope_txt);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003098
3099 s->txn.status = 303;
3100 s->logs.tv_request = now;
3101 stream_int_retnclose(req->prod, &trash);
3102 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3103
3104 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3105 s->fe->fe_counters.intercepted_req++;
3106
3107 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003108 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003109 if (!(s->flags & SN_FINST_MASK))
3110 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003111 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003112 return 1;
3113 }
3114
3115 /* OK, let's go on now */
3116
3117 chunk_printf(&trash,
3118 "HTTP/1.0 200 OK\r\n"
3119 "Cache-Control: no-cache\r\n"
3120 "Connection: close\r\n"
3121 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003122 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003123
3124 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3125 chunk_appendf(&trash, "Refresh: %d\r\n",
3126 uri->refresh);
3127
3128 chunk_appendf(&trash, "\r\n");
3129
3130 s->txn.status = 200;
3131 s->logs.tv_request = now;
3132
3133 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3134 s->fe->fe_counters.intercepted_req++;
3135
3136 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003137 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003138 if (!(s->flags & SN_FINST_MASK))
3139 s->flags |= SN_FINST_R;
3140
3141 if (s->txn.meth == HTTP_METH_HEAD) {
3142 /* that's all we return in case of HEAD request, so let's immediately close. */
3143 stream_int_retnclose(req->prod, &trash);
3144 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003145 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003146 return 1;
3147 }
3148
3149 /* OK, push the response and hand over to the stats I/O handler */
3150 bi_putchk(s->rep, &trash);
3151
3152 s->task->nice = -32; /* small boost for HTTP statistics */
3153 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3154 s->target = s->rep->prod->conn->target; // for logging only
3155 s->rep->prod->conn->xprt_ctx = s;
3156 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3157 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003158 return 1;
3159}
3160
Willy Tarreau20b0de52012-12-24 15:45:22 +01003161/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003162 * transaction <txn>. Returns the first rule that prevents further processing
3163 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3164 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3165 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003166 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003167static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003168http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003169{
Willy Tarreauff011f22011-01-06 17:51:27 +01003170 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003171 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003172
Willy Tarreauff011f22011-01-06 17:51:27 +01003173 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003174 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003175 continue;
3176
Willy Tarreau96257ec2012-12-27 10:46:37 +01003177 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003178 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003179 int ret;
3180
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003181 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003182 ret = acl_pass(ret);
3183
Willy Tarreauff011f22011-01-06 17:51:27 +01003184 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003185 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003186
3187 if (!ret) /* condition not matched */
3188 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003189 }
3190
Willy Tarreau20b0de52012-12-24 15:45:22 +01003191
Willy Tarreau96257ec2012-12-27 10:46:37 +01003192 switch (rule->action) {
3193 case HTTP_REQ_ACT_ALLOW:
3194 return NULL; /* "allow" rules are OK */
3195
3196 case HTTP_REQ_ACT_DENY:
3197 txn->flags |= TX_CLDENY;
3198 return rule;
3199
Willy Tarreauccbcc372012-12-27 12:37:57 +01003200 case HTTP_REQ_ACT_TARPIT:
3201 txn->flags |= TX_CLTARPIT;
3202 return rule;
3203
Willy Tarreau96257ec2012-12-27 10:46:37 +01003204 case HTTP_REQ_ACT_AUTH:
3205 return rule;
3206
Willy Tarreau81499eb2012-12-27 12:19:02 +01003207 case HTTP_REQ_ACT_REDIR:
3208 return rule;
3209
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003210 case HTTP_REQ_ACT_SET_NICE:
3211 s->task->nice = rule->arg.nice;
3212 break;
3213
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003214 case HTTP_REQ_ACT_SET_TOS:
3215#ifdef IP_TOS
3216 if (s->req->prod->conn->addr.to.ss_family == AF_INET)
3217 setsockopt(s->req->prod->conn->t.sock.fd, IPPROTO_IP, IP_TOS, &rule->arg.tos, sizeof(rule->arg.tos));
3218#endif
3219 break;
3220
Willy Tarreau51347ed2013-06-11 19:34:13 +02003221 case HTTP_REQ_ACT_SET_MARK:
3222#ifdef SO_MARK
3223 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3224#endif
3225 break;
3226
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003227 case HTTP_REQ_ACT_SET_LOGL:
3228 s->logs.level = rule->arg.loglevel;
3229 break;
3230
Willy Tarreau96257ec2012-12-27 10:46:37 +01003231 case HTTP_REQ_ACT_SET_HDR:
3232 ctx.idx = 0;
3233 /* remove all occurrences of the header */
3234 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3235 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3236 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003237 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003238 /* now fall through to header addition */
3239
3240 case HTTP_REQ_ACT_ADD_HDR:
3241 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3242 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3243 trash.len = rule->arg.hdr_add.name_len;
3244 trash.str[trash.len++] = ':';
3245 trash.str[trash.len++] = ' ';
3246 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3247 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3248 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003249 }
3250 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003251
3252 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003253 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003254}
3255
Willy Tarreau71241ab2012-12-27 11:30:54 +01003256
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003257/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3258 * transaction <txn>. Returns the first rule that prevents further processing
3259 * of the response (deny, ...) or NULL if it executed all rules or stopped
3260 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3261 * rule.
3262 */
3263static struct http_res_rule *
3264http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3265{
3266 struct http_res_rule *rule;
3267 struct hdr_ctx ctx;
3268
3269 list_for_each_entry(rule, rules, list) {
3270 if (rule->action >= HTTP_RES_ACT_MAX)
3271 continue;
3272
3273 /* check optional condition */
3274 if (rule->cond) {
3275 int ret;
3276
3277 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3278 ret = acl_pass(ret);
3279
3280 if (rule->cond->pol == ACL_COND_UNLESS)
3281 ret = !ret;
3282
3283 if (!ret) /* condition not matched */
3284 continue;
3285 }
3286
3287
3288 switch (rule->action) {
3289 case HTTP_RES_ACT_ALLOW:
3290 return NULL; /* "allow" rules are OK */
3291
3292 case HTTP_RES_ACT_DENY:
3293 txn->flags |= TX_SVDENY;
3294 return rule;
3295
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003296 case HTTP_RES_ACT_SET_NICE:
3297 s->task->nice = rule->arg.nice;
3298 break;
3299
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003300 case HTTP_RES_ACT_SET_TOS:
3301#ifdef IP_TOS
3302 if (s->req->prod->conn->addr.to.ss_family == AF_INET)
3303 setsockopt(s->req->prod->conn->t.sock.fd, IPPROTO_IP, IP_TOS, &rule->arg.tos, sizeof(rule->arg.tos));
3304#endif
3305 break;
3306
Willy Tarreau51347ed2013-06-11 19:34:13 +02003307 case HTTP_RES_ACT_SET_MARK:
3308#ifdef SO_MARK
3309 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3310#endif
3311 break;
3312
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003313 case HTTP_RES_ACT_SET_LOGL:
3314 s->logs.level = rule->arg.loglevel;
3315 break;
3316
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003317 case HTTP_RES_ACT_SET_HDR:
3318 ctx.idx = 0;
3319 /* remove all occurrences of the header */
3320 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3321 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3322 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3323 }
3324 /* now fall through to header addition */
3325
3326 case HTTP_RES_ACT_ADD_HDR:
3327 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3328 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3329 trash.len = rule->arg.hdr_add.name_len;
3330 trash.str[trash.len++] = ':';
3331 trash.str[trash.len++] = ' ';
3332 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3333 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3334 break;
3335 }
3336 }
3337
3338 /* we reached the end of the rules, nothing to report */
3339 return NULL;
3340}
3341
3342
Willy Tarreau71241ab2012-12-27 11:30:54 +01003343/* Perform an HTTP redirect based on the information in <rule>. The function
3344 * returns non-zero on success, or zero in case of a, irrecoverable error such
3345 * as too large a request to build a valid response.
3346 */
3347static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3348{
3349 struct http_msg *msg = &txn->req;
3350 const char *msg_fmt;
3351
3352 /* build redirect message */
3353 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003354 case 308:
3355 msg_fmt = HTTP_308;
3356 break;
3357 case 307:
3358 msg_fmt = HTTP_307;
3359 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003360 case 303:
3361 msg_fmt = HTTP_303;
3362 break;
3363 case 301:
3364 msg_fmt = HTTP_301;
3365 break;
3366 case 302:
3367 default:
3368 msg_fmt = HTTP_302;
3369 break;
3370 }
3371
3372 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3373 return 0;
3374
3375 switch(rule->type) {
3376 case REDIRECT_TYPE_SCHEME: {
3377 const char *path;
3378 const char *host;
3379 struct hdr_ctx ctx;
3380 int pathlen;
3381 int hostlen;
3382
3383 host = "";
3384 hostlen = 0;
3385 ctx.idx = 0;
3386 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3387 host = ctx.line + ctx.val;
3388 hostlen = ctx.vlen;
3389 }
3390
3391 path = http_get_path(txn);
3392 /* build message using path */
3393 if (path) {
3394 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3395 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3396 int qs = 0;
3397 while (qs < pathlen) {
3398 if (path[qs] == '?') {
3399 pathlen = qs;
3400 break;
3401 }
3402 qs++;
3403 }
3404 }
3405 } else {
3406 path = "/";
3407 pathlen = 1;
3408 }
3409
3410 /* check if we can add scheme + "://" + host + path */
3411 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3412 return 0;
3413
3414 /* add scheme */
3415 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3416 trash.len += rule->rdr_len;
3417
3418 /* add "://" */
3419 memcpy(trash.str + trash.len, "://", 3);
3420 trash.len += 3;
3421
3422 /* add host */
3423 memcpy(trash.str + trash.len, host, hostlen);
3424 trash.len += hostlen;
3425
3426 /* add path */
3427 memcpy(trash.str + trash.len, path, pathlen);
3428 trash.len += pathlen;
3429
3430 /* append a slash at the end of the location is needed and missing */
3431 if (trash.len && trash.str[trash.len - 1] != '/' &&
3432 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3433 if (trash.len > trash.size - 5)
3434 return 0;
3435 trash.str[trash.len] = '/';
3436 trash.len++;
3437 }
3438
3439 break;
3440 }
3441 case REDIRECT_TYPE_PREFIX: {
3442 const char *path;
3443 int pathlen;
3444
3445 path = http_get_path(txn);
3446 /* build message using path */
3447 if (path) {
3448 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3449 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3450 int qs = 0;
3451 while (qs < pathlen) {
3452 if (path[qs] == '?') {
3453 pathlen = qs;
3454 break;
3455 }
3456 qs++;
3457 }
3458 }
3459 } else {
3460 path = "/";
3461 pathlen = 1;
3462 }
3463
3464 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3465 return 0;
3466
3467 /* add prefix. Note that if prefix == "/", we don't want to
3468 * add anything, otherwise it makes it hard for the user to
3469 * configure a self-redirection.
3470 */
3471 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3472 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3473 trash.len += rule->rdr_len;
3474 }
3475
3476 /* add path */
3477 memcpy(trash.str + trash.len, path, pathlen);
3478 trash.len += pathlen;
3479
3480 /* append a slash at the end of the location is needed and missing */
3481 if (trash.len && trash.str[trash.len - 1] != '/' &&
3482 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3483 if (trash.len > trash.size - 5)
3484 return 0;
3485 trash.str[trash.len] = '/';
3486 trash.len++;
3487 }
3488
3489 break;
3490 }
3491 case REDIRECT_TYPE_LOCATION:
3492 default:
3493 if (trash.len + rule->rdr_len > trash.size - 4)
3494 return 0;
3495
3496 /* add location */
3497 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3498 trash.len += rule->rdr_len;
3499 break;
3500 }
3501
3502 if (rule->cookie_len) {
3503 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3504 trash.len += 14;
3505 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3506 trash.len += rule->cookie_len;
3507 memcpy(trash.str + trash.len, "\r\n", 2);
3508 trash.len += 2;
3509 }
3510
3511 /* add end of headers and the keep-alive/close status.
3512 * We may choose to set keep-alive if the Location begins
3513 * with a slash, because the client will come back to the
3514 * same server.
3515 */
3516 txn->status = rule->code;
3517 /* let's log the request time */
3518 s->logs.tv_request = now;
3519
3520 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3521 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3522 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3523 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3524 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3525 /* keep-alive possible */
3526 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3527 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3528 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3529 trash.len += 30;
3530 } else {
3531 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3532 trash.len += 24;
3533 }
3534 }
3535 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3536 trash.len += 4;
3537 bo_inject(txn->rsp.chn, trash.str, trash.len);
3538 /* "eat" the request */
3539 bi_fast_delete(txn->req.chn->buf, msg->sov);
3540 msg->sov = 0;
3541 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3542 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3543 txn->req.msg_state = HTTP_MSG_CLOSED;
3544 txn->rsp.msg_state = HTTP_MSG_DONE;
3545 } else {
3546 /* keep-alive not possible */
3547 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3548 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3549 trash.len += 29;
3550 } else {
3551 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3552 trash.len += 23;
3553 }
3554 stream_int_retnclose(txn->req.chn->prod, &trash);
3555 txn->req.chn->analysers = 0;
3556 }
3557
3558 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003559 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003560 if (!(s->flags & SN_FINST_MASK))
3561 s->flags |= SN_FINST_R;
3562
3563 return 1;
3564}
3565
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003566/* This stream analyser runs all HTTP request processing which is common to
3567 * frontends and backends, which means blocking ACLs, filters, connection-close,
3568 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003569 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003570 * either needs more data or wants to immediately abort the request (eg: deny,
3571 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003572 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003573int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003574{
Willy Tarreaud787e662009-07-07 10:14:51 +02003575 struct http_txn *txn = &s->txn;
3576 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003577 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003578 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003579 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003580 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003581 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003582
Willy Tarreau655dce92009-11-08 13:10:58 +01003583 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003584 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003585 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003586 return 0;
3587 }
3588
Willy Tarreau3a816292009-07-07 10:55:49 +02003589 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003590 req->analyse_exp = TICK_ETERNITY;
3591
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003592 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 +02003593 now_ms, __FUNCTION__,
3594 s,
3595 req,
3596 req->rex, req->wex,
3597 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003598 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003599 req->analysers);
3600
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 /* first check whether we have some ACLs set to block this request */
3602 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003603 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003604
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003605 ret = acl_pass(ret);
3606 if (cond->pol == ACL_COND_UNLESS)
3607 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003608
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003609 if (ret) {
3610 txn->status = 403;
3611 /* let's log the request time */
3612 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003613 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003614 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003615 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003616 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003617 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003618
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003619 /* just in case we have some per-backend tracking */
3620 session_inc_be_http_req_ctr(s);
3621
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003622 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003623 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003624
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003625 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003626 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003627 do_stats = stats_check_uri(s->rep->prod, txn, px);
3628 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003629 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 +01003630 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003631 else
3632 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003633
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003634 /* return a 403 if either rule has blocked */
Willy Tarreauccbcc372012-12-27 12:37:57 +01003635 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3636 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003637 txn->status = 403;
3638 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003639 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003640 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003641 s->fe->fe_counters.denied_req++;
3642 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3643 s->be->be_counters.denied_req++;
3644 if (s->listener->counters)
3645 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003646 goto return_prx_cond;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003647 }
3648 /* When a connection is tarpitted, we use the tarpit timeout,
3649 * which may be the same as the connect timeout if unspecified.
3650 * If unset, then set it to zero because we really want it to
3651 * eventually expire. We build the tarpit as an analyser.
3652 */
3653 if (txn->flags & TX_CLTARPIT) {
3654 channel_erase(s->req);
3655 /* wipe the request out so that we can drop the connection early
3656 * if the client closes first.
3657 */
3658 channel_dont_connect(req);
3659 req->analysers = 0; /* remove switching rules etc... */
3660 req->analysers |= AN_REQ_HTTP_TARPIT;
3661 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3662 if (!req->analyse_exp)
3663 req->analyse_exp = tick_add(now_ms, 0);
3664 session_inc_http_err_ctr(s);
3665 s->fe->fe_counters.denied_req++;
3666 if (s->fe != s->be)
3667 s->be->be_counters.denied_req++;
3668 if (s->listener->counters)
3669 s->listener->counters->denied_req++;
3670 return 1;
3671 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003672 }
3673
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003674 /* try headers filters */
3675 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003676 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003677 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003678
Willy Tarreau59234e92008-11-30 23:51:27 +01003679 /* has the request been denied ? */
3680 if (txn->flags & TX_CLDENY) {
3681 /* no need to go further */
3682 txn->status = 403;
3683 /* let's log the request time */
3684 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003685 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003686 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003687 goto return_prx_cond;
3688 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003689
3690 /* When a connection is tarpitted, we use the tarpit timeout,
3691 * which may be the same as the connect timeout if unspecified.
3692 * If unset, then set it to zero because we really want it to
3693 * eventually expire. We build the tarpit as an analyser.
3694 */
3695 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003696 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003697 /* wipe the request out so that we can drop the connection early
3698 * if the client closes first.
3699 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003700 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003701 req->analysers = 0; /* remove switching rules etc... */
3702 req->analysers |= AN_REQ_HTTP_TARPIT;
3703 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3704 if (!req->analyse_exp)
3705 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003706 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003707 return 1;
3708 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003709 }
Willy Tarreau06619262006-12-17 08:37:22 +01003710
Willy Tarreau5b154472009-12-21 20:11:07 +01003711 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3712 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003713 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3714 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003715 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3716 * avoid to redo the same work if FE and BE have the same settings (common).
3717 * The method consists in checking if options changed between the two calls
3718 * (implying that either one is non-null, or one of them is non-null and we
3719 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003720 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003721
Willy Tarreaudc008c52010-02-01 16:20:08 +01003722 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3723 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3724 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3725 (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 +01003726 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003727
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003728 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3729 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003730 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003731 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3732 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003733 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003734 tmp = TX_CON_WANT_CLO;
3735
Willy Tarreau5b154472009-12-21 20:11:07 +01003736 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3737 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003738
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003739 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3740 /* parse the Connection header and possibly clean it */
3741 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003742 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003743 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3744 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003745 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003746 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003747 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003748 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003749 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003750
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003751 /* check if client or config asks for explicit close in KAL/SCL */
3752 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3753 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3754 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003755 (!(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 +01003756 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003757 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003758 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003759 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3760 }
Willy Tarreau78599912009-10-17 20:12:21 +02003761
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003762 /* we can be blocked here because the request needs to be authenticated,
3763 * either to pass or to access stats.
3764 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003765 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003766 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003767
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003768 if (!realm)
3769 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3770
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003771 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003772 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003773 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003774 /* on 401 we still count one error, because normal browsing
3775 * won't significantly increase the counter but brute force
3776 * attempts will.
3777 */
3778 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003779 goto return_prx_cond;
3780 }
3781
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003782 /* add request headers from the rule sets in the same order */
3783 list_for_each_entry(wl, &px->req_add, list) {
3784 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003785 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003786 ret = acl_pass(ret);
3787 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3788 ret = !ret;
3789 if (!ret)
3790 continue;
3791 }
3792
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003793 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003794 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003795 }
3796
3797 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3798 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3799 goto return_bad_req;
3800 req->analyse_exp = TICK_ETERNITY;
3801 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003802 }
3803
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003804 if (unlikely(do_stats)) {
3805 /* process the stats request now */
3806 if (!http_handle_stats(s, req)) {
3807 /* we need more data, let's come back here later */
3808 req->analysers |= an_bit;
3809 channel_dont_connect(req);
Willy Tarreau7fe33002013-04-21 08:04:22 +02003810 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003811 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003812 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003813 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003814
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003815 /* check whether we have some ACLs set to redirect this request */
3816 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003817 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003818 int ret;
3819
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003820 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003821 ret = acl_pass(ret);
3822 if (rule->cond->pol == ACL_COND_UNLESS)
3823 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003824 if (!ret)
3825 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003826 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003827 if (!http_apply_redirect_rule(rule, s, txn))
3828 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003829
Willy Tarreau71241ab2012-12-27 11:30:54 +01003830 req->analyse_exp = TICK_ETERNITY;
3831 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003832 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003833
Willy Tarreau2be39392010-01-03 17:24:51 +01003834 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3835 * If this happens, then the data will not come immediately, so we must
3836 * send all what we have without waiting. Note that due to the small gain
3837 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003838 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003839 * itself once used.
3840 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003841 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003842
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003843 /* that's OK for us now, let's move on to next analysers */
3844 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003845
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003846 return_bad_req:
3847 /* We centralize bad requests processing here */
3848 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3849 /* we detected a parsing error. We want to archive this request
3850 * in the dedicated proxy area for later troubleshooting.
3851 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003852 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003853 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003854
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003855 txn->req.msg_state = HTTP_MSG_ERROR;
3856 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003857 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003858
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003859 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003860 if (s->listener->counters)
3861 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003862
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003863 return_prx_cond:
3864 if (!(s->flags & SN_ERR_MASK))
3865 s->flags |= SN_ERR_PRXCOND;
3866 if (!(s->flags & SN_FINST_MASK))
3867 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003868
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003869 req->analysers = 0;
3870 req->analyse_exp = TICK_ETERNITY;
3871 return 0;
3872}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003873
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003874/* This function performs all the processing enabled for the current request.
3875 * It returns 1 if the processing can continue on next analysers, or zero if it
3876 * needs more data, encounters an error, or wants to immediately abort the
3877 * request. It relies on buffers flags, and updates s->req->analysers.
3878 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003879int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003880{
3881 struct http_txn *txn = &s->txn;
3882 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003883
Willy Tarreau655dce92009-11-08 13:10:58 +01003884 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003885 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003886 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003887 return 0;
3888 }
3889
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003890 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 +02003891 now_ms, __FUNCTION__,
3892 s,
3893 req,
3894 req->rex, req->wex,
3895 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003896 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003897 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003898
William Lallemand82fe75c2012-10-23 10:25:10 +02003899 if (s->fe->comp || s->be->comp)
3900 select_compression_request_header(s, req->buf);
3901
Willy Tarreau59234e92008-11-30 23:51:27 +01003902 /*
3903 * Right now, we know that we have processed the entire headers
3904 * and that unwanted requests have been filtered out. We can do
3905 * whatever we want with the remaining request. Also, now we
3906 * may have separate values for ->fe, ->be.
3907 */
Willy Tarreau06619262006-12-17 08:37:22 +01003908
Willy Tarreau59234e92008-11-30 23:51:27 +01003909 /*
3910 * If HTTP PROXY is set we simply get remote server address
3911 * parsing incoming request.
3912 */
3913 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003914 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 +01003915 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003916
Willy Tarreau59234e92008-11-30 23:51:27 +01003917 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003918 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003919 * Note that doing so might move headers in the request, but
3920 * the fields will stay coherent and the URI will not move.
3921 * This should only be performed in the backend.
3922 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003923 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003924 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3925 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003926
Willy Tarreau59234e92008-11-30 23:51:27 +01003927 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003928 * 8: the appsession cookie was looked up very early in 1.2,
3929 * so let's do the same now.
3930 */
3931
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003932 /* It needs to look into the URI unless persistence must be ignored */
3933 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003934 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 +01003935 }
3936
William Lallemanda73203e2012-03-12 12:48:57 +01003937 /* add unique-id if "header-unique-id" is specified */
3938
3939 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3940 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3941
3942 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003943 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3944 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003945 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003946 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003947 goto return_bad_req;
3948 }
3949
Cyril Bontéb21570a2009-11-29 20:04:48 +01003950 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003951 * 9: add X-Forwarded-For if either the frontend or the backend
3952 * asks for it.
3953 */
3954 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003955 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003956 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003957 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3958 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003959 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003960 /* The header is set to be added only if none is present
3961 * and we found it, so don't do anything.
3962 */
3963 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003964 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 /* Add an X-Forwarded-For header unless the source IP is
3966 * in the 'except' network range.
3967 */
3968 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003969 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003970 != s->fe->except_net.s_addr) &&
3971 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003972 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003973 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003974 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003975 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003976 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003977
3978 /* Note: we rely on the backend to get the header name to be used for
3979 * x-forwarded-for, because the header is really meant for the backends.
3980 * However, if the backend did not specify any option, we have to rely
3981 * on the frontend's header name.
3982 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003983 if (s->be->fwdfor_hdr_len) {
3984 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003985 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003986 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003987 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003989 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003990 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003992 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003993 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003994 }
3995 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003996 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003997 /* FIXME: for the sake of completeness, we should also support
3998 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003999 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004000 int len;
4001 char pn[INET6_ADDRSTRLEN];
4002 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004003 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004004 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004005
Willy Tarreau59234e92008-11-30 23:51:27 +01004006 /* Note: we rely on the backend to get the header name to be used for
4007 * x-forwarded-for, because the header is really meant for the backends.
4008 * However, if the backend did not specify any option, we have to rely
4009 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004010 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004011 if (s->be->fwdfor_hdr_len) {
4012 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004013 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004014 } else {
4015 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004016 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004017 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004018 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004019
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004020 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004021 goto return_bad_req;
4022 }
4023 }
4024
4025 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004026 * 10: add X-Original-To if either the frontend or the backend
4027 * asks for it.
4028 */
4029 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4030
4031 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004032 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004033 /* Add an X-Original-To header unless the destination IP is
4034 * in the 'except' network range.
4035 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004036 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004037
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004038 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004039 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004040 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004041 != s->fe->except_to.s_addr) &&
4042 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004043 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004044 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004045 int len;
4046 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004047 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004048
4049 /* Note: we rely on the backend to get the header name to be used for
4050 * x-original-to, because the header is really meant for the backends.
4051 * However, if the backend did not specify any option, we have to rely
4052 * on the frontend's header name.
4053 */
4054 if (s->be->orgto_hdr_len) {
4055 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004056 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004057 } else {
4058 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004059 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004060 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004061 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004062
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004063 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004064 goto return_bad_req;
4065 }
4066 }
4067 }
4068
Willy Tarreau50fc7772012-11-11 22:19:57 +01004069 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4070 * If an "Upgrade" token is found, the header is left untouched in order not to have
4071 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4072 * "Upgrade" is present in the Connection header.
4073 */
4074 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4075 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4076 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004077 unsigned int want_flags = 0;
4078
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004079 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004080 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
4081 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
4082 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004083 want_flags |= TX_CON_CLO_SET;
4084 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004085 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
4086 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
4087 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004088 want_flags |= TX_CON_KAL_SET;
4089 }
4090
4091 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004092 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004093 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004094
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004095
Willy Tarreau522d6c02009-12-06 18:49:18 +01004096 /* If we have no server assigned yet and we're balancing on url_param
4097 * with a POST request, we may be interested in checking the body for
4098 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004099 */
4100 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4101 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004102 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004103 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004104 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004105 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004106 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004107
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004108 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004109 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004110#ifdef TCP_QUICKACK
4111 /* We expect some data from the client. Unless we know for sure
4112 * we already have a full request, we have to re-enable quick-ack
4113 * in case we previously disabled it, otherwise we might cause
4114 * the client to delay further data.
4115 */
4116 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004117 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004118 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004119 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004120#endif
4121 }
Willy Tarreau03945942009-12-22 16:50:27 +01004122
Willy Tarreau59234e92008-11-30 23:51:27 +01004123 /*************************************************************
4124 * OK, that's finished for the headers. We have done what we *
4125 * could. Let's switch to the DATA state. *
4126 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004127 req->analyse_exp = TICK_ETERNITY;
4128 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004129
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004130 /* if the server closes the connection, we want to immediately react
4131 * and close the socket to save packets and syscalls.
4132 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004133 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4134 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004135
Willy Tarreau59234e92008-11-30 23:51:27 +01004136 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004137 /* OK let's go on with the BODY now */
4138 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004139
Willy Tarreau59234e92008-11-30 23:51:27 +01004140 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004141 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004142 /* we detected a parsing error. We want to archive this request
4143 * in the dedicated proxy area for later troubleshooting.
4144 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004145 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004146 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004147
Willy Tarreau59234e92008-11-30 23:51:27 +01004148 txn->req.msg_state = HTTP_MSG_ERROR;
4149 txn->status = 400;
4150 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004151 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004152
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004153 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004154 if (s->listener->counters)
4155 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004156
Willy Tarreau59234e92008-11-30 23:51:27 +01004157 if (!(s->flags & SN_ERR_MASK))
4158 s->flags |= SN_ERR_PRXCOND;
4159 if (!(s->flags & SN_FINST_MASK))
4160 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004161 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004162}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004163
Willy Tarreau60b85b02008-11-30 23:28:40 +01004164/* This function is an analyser which processes the HTTP tarpit. It always
4165 * returns zero, at the beginning because it prevents any other processing
4166 * from occurring, and at the end because it terminates the request.
4167 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004168int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004169{
4170 struct http_txn *txn = &s->txn;
4171
4172 /* This connection is being tarpitted. The CLIENT side has
4173 * already set the connect expiration date to the right
4174 * timeout. We just have to check that the client is still
4175 * there and that the timeout has not expired.
4176 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004177 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004178 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004179 !tick_is_expired(req->analyse_exp, now_ms))
4180 return 0;
4181
4182 /* We will set the queue timer to the time spent, just for
4183 * logging purposes. We fake a 500 server error, so that the
4184 * attacker will not suspect his connection has been tarpitted.
4185 * It will not cause trouble to the logs because we can exclude
4186 * the tarpitted connections by filtering on the 'PT' status flags.
4187 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004188 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4189
4190 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004191 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004192 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004193
4194 req->analysers = 0;
4195 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004196
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004197 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004198 if (s->listener->counters)
4199 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004200
Willy Tarreau60b85b02008-11-30 23:28:40 +01004201 if (!(s->flags & SN_ERR_MASK))
4202 s->flags |= SN_ERR_PRXCOND;
4203 if (!(s->flags & SN_FINST_MASK))
4204 s->flags |= SN_FINST_T;
4205 return 0;
4206}
4207
Willy Tarreaud34af782008-11-30 23:36:37 +01004208/* This function is an analyser which processes the HTTP request body. It looks
4209 * for parameters to be used for the load balancing algorithm (url_param). It
4210 * must only be called after the standard HTTP request processing has occurred,
4211 * because it expects the request to be parsed. It returns zero if it needs to
4212 * read more data, or 1 once it has completed its analysis.
4213 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004214int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004215{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004216 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004217 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004218 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004219
4220 /* We have to parse the HTTP request body to find any required data.
4221 * "balance url_param check_post" should have been the only way to get
4222 * into this. We were brought here after HTTP header analysis, so all
4223 * related structures are ready.
4224 */
4225
Willy Tarreau522d6c02009-12-06 18:49:18 +01004226 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4227 goto missing_data;
4228
4229 if (msg->msg_state < HTTP_MSG_100_SENT) {
4230 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4231 * send an HTTP/1.1 100 Continue intermediate response.
4232 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004233 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004234 struct hdr_ctx ctx;
4235 ctx.idx = 0;
4236 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004237 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004238 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004239 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004240 }
4241 }
4242 msg->msg_state = HTTP_MSG_100_SENT;
4243 }
4244
4245 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004246 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004247 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004248 * is still null. We must save the body in msg->next because it
4249 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004250 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004251 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004252
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004253 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004254 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4255 else
4256 msg->msg_state = HTTP_MSG_DATA;
4257 }
4258
4259 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004260 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004261 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004262 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004263 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004264 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004265
Willy Tarreau115acb92009-12-26 13:56:06 +01004266 if (!ret)
4267 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004268 else if (ret < 0) {
4269 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004270 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004271 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004272 }
4273
Willy Tarreaud98cf932009-12-27 22:54:55 +01004274 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004275 * We have the first data byte is in msg->sov. We're waiting for at
4276 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004277 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004278
Willy Tarreau124d9912011-03-01 20:30:48 +01004279 if (msg->body_len < limit)
4280 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004281
Willy Tarreau9b28e032012-10-12 23:49:43 +02004282 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004283 goto http_end;
4284
4285 missing_data:
4286 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004287 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004288 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004289 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004290 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004291
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004292 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004293 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004294 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004295
4296 if (!(s->flags & SN_ERR_MASK))
4297 s->flags |= SN_ERR_CLITO;
4298 if (!(s->flags & SN_FINST_MASK))
4299 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004300 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004301 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004302
4303 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004304 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004305 /* Not enough data. We'll re-use the http-request
4306 * timeout here. Ideally, we should set the timeout
4307 * relative to the accept() date. We just set the
4308 * request timeout once at the beginning of the
4309 * request.
4310 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004311 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004312 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004313 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004314 return 0;
4315 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004316
4317 http_end:
4318 /* The situation will not evolve, so let's give up on the analysis. */
4319 s->logs.tv_request = now; /* update the request timer to reflect full request */
4320 req->analysers &= ~an_bit;
4321 req->analyse_exp = TICK_ETERNITY;
4322 return 1;
4323
4324 return_bad_req: /* let's centralize all bad requests */
4325 txn->req.msg_state = HTTP_MSG_ERROR;
4326 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004327 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004328
Willy Tarreau79ebac62010-06-07 13:47:49 +02004329 if (!(s->flags & SN_ERR_MASK))
4330 s->flags |= SN_ERR_PRXCOND;
4331 if (!(s->flags & SN_FINST_MASK))
4332 s->flags |= SN_FINST_R;
4333
Willy Tarreau522d6c02009-12-06 18:49:18 +01004334 return_err_msg:
4335 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004336 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004337 if (s->listener->counters)
4338 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004339 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004340}
4341
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004342/* send a server's name with an outgoing request over an established connection.
4343 * Note: this function is designed to be called once the request has been scheduled
4344 * for being forwarded. This is the reason why it rewinds the buffer before
4345 * proceeding.
4346 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004347int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004348
4349 struct hdr_ctx ctx;
4350
Mark Lamourinec2247f02012-01-04 13:02:01 -05004351 char *hdr_name = be->server_id_hdr_name;
4352 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004353 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004354 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004355 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004356
William Lallemandd9e90662012-01-30 17:27:17 +01004357 ctx.idx = 0;
4358
Willy Tarreau9b28e032012-10-12 23:49:43 +02004359 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004360 if (old_o) {
4361 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004362 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004363 }
4364
Willy Tarreau9b28e032012-10-12 23:49:43 +02004365 old_i = chn->buf->i;
4366 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 -05004367 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004368 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004369 }
4370
4371 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004372 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004373 memcpy(hdr_val, hdr_name, hdr_name_len);
4374 hdr_val += hdr_name_len;
4375 *hdr_val++ = ':';
4376 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004377 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4378 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004379
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004380 if (old_o) {
4381 /* If this was a forwarded request, we must readjust the amount of
4382 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004383 * variations. Note that if the request was already scheduled for
4384 * forwarding, it had its req->sol pointing to the body, which
4385 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004386 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004387 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004388 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004389 }
4390
Mark Lamourinec2247f02012-01-04 13:02:01 -05004391 return 0;
4392}
4393
Willy Tarreau610ecce2010-01-04 21:15:02 +01004394/* Terminate current transaction and prepare a new one. This is very tricky
4395 * right now but it works.
4396 */
4397void http_end_txn_clean_session(struct session *s)
4398{
4399 /* FIXME: We need a more portable way of releasing a backend's and a
4400 * server's connections. We need a safer way to reinitialize buffer
4401 * flags. We also need a more accurate method for computing per-request
4402 * data.
4403 */
4404 http_silent_debug(__LINE__, s);
4405
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004406 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004407 si_shutr(s->req->cons);
4408 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409
4410 http_silent_debug(__LINE__, s);
4411
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004412 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004414 if (unlikely(s->srv_conn))
4415 sess_change_server(s, NULL);
4416 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417
4418 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4419 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004420 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004421
4422 if (s->txn.status) {
4423 int n;
4424
4425 n = s->txn.status / 100;
4426 if (n < 1 || n > 5)
4427 n = 0;
4428
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004429 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004430 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004431 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004432 s->fe->fe_counters.p.http.comp_rsp++;
4433 }
Willy Tarreau24657792010-02-26 10:30:28 +01004434 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004435 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004436 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004437 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004438 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004439 s->be->be_counters.p.http.comp_rsp++;
4440 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441 }
4442
4443 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004444 s->logs.bytes_in -= s->req->buf->i;
4445 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446
4447 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004448 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004449 !(s->flags & SN_MONITOR) &&
4450 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4451 s->do_log(s);
4452 }
4453
4454 s->logs.accept_date = date; /* user-visible date for logging */
4455 s->logs.tv_accept = now; /* corrected date for internal use */
4456 tv_zero(&s->logs.tv_request);
4457 s->logs.t_queue = -1;
4458 s->logs.t_connect = -1;
4459 s->logs.t_data = -1;
4460 s->logs.t_close = 0;
4461 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4462 s->logs.srv_queue_size = 0; /* we will get this number soon */
4463
Willy Tarreau9b28e032012-10-12 23:49:43 +02004464 s->logs.bytes_in = s->req->total = s->req->buf->i;
4465 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004466
4467 if (s->pend_pos)
4468 pendconn_free(s->pend_pos);
4469
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004470 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004471 if (s->flags & SN_CURR_SESS) {
4472 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004473 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004475 if (may_dequeue_tasks(objt_server(s->target), s->be))
4476 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004477 }
4478
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004479 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480
4481 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004482 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4483 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004484 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004486 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004487 s->req->cons->err_loc = NULL;
4488 s->req->cons->exp = TICK_ETERNITY;
4489 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004490 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4491 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 +02004492 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 +01004493 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004494
4495 if (s->flags & SN_COMP_READY)
4496 s->comp_algo->end(&s->comp_ctx);
4497 s->comp_algo = NULL;
4498 s->flags &= ~SN_COMP_READY;
4499
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 s->txn.meth = 0;
4501 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004502 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004503 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004504 s->req->cons->flags |= SI_FL_INDEP_STR;
4505
Willy Tarreau96e31212011-05-30 18:10:30 +02004506 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004507 s->req->flags |= CF_NEVER_WAIT;
4508 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004509 }
4510
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 /* if the request buffer is not empty, it means we're
4512 * about to process another request, so send pending
4513 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004514 * Just don't do this if the buffer is close to be full,
4515 * because the request will wait for it to flush a little
4516 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004518 if (s->req->buf->i) {
4519 if (s->rep->buf->o &&
4520 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4521 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004522 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004523 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004524
4525 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004526 channel_auto_read(s->req);
4527 channel_auto_close(s->req);
4528 channel_auto_read(s->rep);
4529 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004530
Willy Tarreau342b11c2010-11-24 16:22:09 +01004531 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004532 s->rep->analysers = 0;
4533
4534 http_silent_debug(__LINE__, s);
4535}
4536
4537
4538/* This function updates the request state machine according to the response
4539 * state machine and buffer flags. It returns 1 if it changes anything (flag
4540 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4541 * it is only used to find when a request/response couple is complete. Both
4542 * this function and its equivalent should loop until both return zero. It
4543 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4544 */
4545int http_sync_req_state(struct session *s)
4546{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004547 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004549 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 unsigned int old_state = txn->req.msg_state;
4551
4552 http_silent_debug(__LINE__, s);
4553 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4554 return 0;
4555
4556 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004557 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004558 * We can shut the read side unless we want to abort_on_close,
4559 * or we have a POST request. The issue with POST requests is
4560 * that some browsers still send a CRLF after the request, and
4561 * this CRLF must be read so that it does not remain in the kernel
4562 * buffers, otherwise a close could cause an RST on some systems
4563 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004564 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004565 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004566 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567
Willy Tarreau40f151a2012-12-20 12:10:09 +01004568 /* if the server closes the connection, we want to immediately react
4569 * and close the socket to save packets and syscalls.
4570 */
4571 chn->cons->flags |= SI_FL_NOHALF;
4572
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4574 goto wait_other_side;
4575
4576 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4577 /* The server has not finished to respond, so we
4578 * don't want to move in order not to upset it.
4579 */
4580 goto wait_other_side;
4581 }
4582
4583 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4584 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004585 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004586 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004587 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 goto wait_other_side;
4589 }
4590
4591 /* When we get here, it means that both the request and the
4592 * response have finished receiving. Depending on the connection
4593 * mode, we'll have to wait for the last bytes to leave in either
4594 * direction, and sometimes for a close to be effective.
4595 */
4596
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004597 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4598 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004599 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4600 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004601 }
4602 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4603 /* Option forceclose is set, or either side wants to close,
4604 * let's enforce it now that we're not expecting any new
4605 * data to come. The caller knows the session is complete
4606 * once both states are CLOSED.
4607 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004608 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4609 channel_shutr_now(chn);
4610 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004612 }
4613 else {
4614 /* The last possible modes are keep-alive and tunnel. Since tunnel
4615 * mode does not set the body analyser, we can't reach this place
4616 * in tunnel mode, so we're left with keep-alive only.
4617 * This mode is currently not implemented, we switch to tunnel mode.
4618 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004619 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004620 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004621 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622 }
4623
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004624 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004625 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004626 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004627
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004628 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004629 txn->req.msg_state = HTTP_MSG_CLOSING;
4630 goto http_msg_closing;
4631 }
4632 else {
4633 txn->req.msg_state = HTTP_MSG_CLOSED;
4634 goto http_msg_closed;
4635 }
4636 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004637 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004638 }
4639
4640 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4641 http_msg_closing:
4642 /* nothing else to forward, just waiting for the output buffer
4643 * to be empty and for the shutw_now to take effect.
4644 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004645 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004646 txn->req.msg_state = HTTP_MSG_CLOSED;
4647 goto http_msg_closed;
4648 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004649 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004650 txn->req.msg_state = HTTP_MSG_ERROR;
4651 goto wait_other_side;
4652 }
4653 }
4654
4655 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4656 http_msg_closed:
4657 goto wait_other_side;
4658 }
4659
4660 wait_other_side:
4661 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004662 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004663}
4664
4665
4666/* This function updates the response state machine according to the request
4667 * state machine and buffer flags. It returns 1 if it changes anything (flag
4668 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4669 * it is only used to find when a request/response couple is complete. Both
4670 * this function and its equivalent should loop until both return zero. It
4671 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4672 */
4673int http_sync_res_state(struct session *s)
4674{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004675 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004677 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 unsigned int old_state = txn->rsp.msg_state;
4679
4680 http_silent_debug(__LINE__, s);
4681 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4682 return 0;
4683
4684 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4685 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004686 * still monitor the server connection for a possible close
4687 * while the request is being uploaded, so we don't disable
4688 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004689 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004690 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004691
4692 if (txn->req.msg_state == HTTP_MSG_ERROR)
4693 goto wait_other_side;
4694
4695 if (txn->req.msg_state < HTTP_MSG_DONE) {
4696 /* The client seems to still be sending data, probably
4697 * because we got an error response during an upload.
4698 * We have the choice of either breaking the connection
4699 * or letting it pass through. Let's do the later.
4700 */
4701 goto wait_other_side;
4702 }
4703
4704 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4705 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004706 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004707 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004708 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004709 goto wait_other_side;
4710 }
4711
4712 /* When we get here, it means that both the request and the
4713 * response have finished receiving. Depending on the connection
4714 * mode, we'll have to wait for the last bytes to leave in either
4715 * direction, and sometimes for a close to be effective.
4716 */
4717
4718 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4719 /* Server-close mode : shut read and wait for the request
4720 * side to close its output buffer. The caller will detect
4721 * when we're in DONE and the other is in CLOSED and will
4722 * catch that for the final cleanup.
4723 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004724 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4725 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004726 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004727 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4728 /* Option forceclose is set, or either side wants to close,
4729 * let's enforce it now that we're not expecting any new
4730 * data to come. The caller knows the session is complete
4731 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004733 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4734 channel_shutr_now(chn);
4735 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004736 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004737 }
4738 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004739 /* The last possible modes are keep-alive and tunnel. Since tunnel
4740 * mode does not set the body analyser, we can't reach this place
4741 * in tunnel mode, so we're left with keep-alive only.
4742 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004744 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004746 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004747 }
4748
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004749 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004750 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004751 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004752 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4753 goto http_msg_closing;
4754 }
4755 else {
4756 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4757 goto http_msg_closed;
4758 }
4759 }
4760 goto wait_other_side;
4761 }
4762
4763 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4764 http_msg_closing:
4765 /* nothing else to forward, just waiting for the output buffer
4766 * to be empty and for the shutw_now to take effect.
4767 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004768 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4770 goto http_msg_closed;
4771 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004772 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004774 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004775 if (objt_server(s->target))
4776 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004777 goto wait_other_side;
4778 }
4779 }
4780
4781 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4782 http_msg_closed:
4783 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004784 bi_erase(chn);
4785 channel_auto_close(chn);
4786 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787 goto wait_other_side;
4788 }
4789
4790 wait_other_side:
4791 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004792 /* We force the response to leave immediately if we're waiting for the
4793 * other side, since there is no pending shutdown to push it out.
4794 */
4795 if (!channel_is_empty(chn))
4796 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004797 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798}
4799
4800
4801/* Resync the request and response state machines. Return 1 if either state
4802 * changes.
4803 */
4804int http_resync_states(struct session *s)
4805{
4806 struct http_txn *txn = &s->txn;
4807 int old_req_state = txn->req.msg_state;
4808 int old_res_state = txn->rsp.msg_state;
4809
4810 http_silent_debug(__LINE__, s);
4811 http_sync_req_state(s);
4812 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004813 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004814 if (!http_sync_res_state(s))
4815 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004816 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004817 if (!http_sync_req_state(s))
4818 break;
4819 }
4820 http_silent_debug(__LINE__, s);
4821 /* OK, both state machines agree on a compatible state.
4822 * There are a few cases we're interested in :
4823 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4824 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4825 * directions, so let's simply disable both analysers.
4826 * - HTTP_MSG_CLOSED on the response only means we must abort the
4827 * request.
4828 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4829 * with server-close mode means we've completed one request and we
4830 * must re-initialize the server connection.
4831 */
4832
4833 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4834 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4835 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4836 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4837 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004838 channel_auto_close(s->req);
4839 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004840 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004841 channel_auto_close(s->rep);
4842 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004843 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004844 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4845 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004846 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004847 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004848 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004849 channel_auto_close(s->rep);
4850 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004851 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004852 channel_abort(s->req);
4853 channel_auto_close(s->req);
4854 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004855 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004856 }
4857 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4858 txn->rsp.msg_state == HTTP_MSG_DONE &&
4859 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4860 /* server-close: terminate this server connection and
4861 * reinitialize a fresh-new transaction.
4862 */
4863 http_end_txn_clean_session(s);
4864 }
4865
4866 http_silent_debug(__LINE__, s);
4867 return txn->req.msg_state != old_req_state ||
4868 txn->rsp.msg_state != old_res_state;
4869}
4870
Willy Tarreaud98cf932009-12-27 22:54:55 +01004871/* This function is an analyser which forwards request body (including chunk
4872 * sizes if any). It is called as soon as we must forward, even if we forward
4873 * zero byte. The only situation where it must not be called is when we're in
4874 * tunnel mode and we want to forward till the close. It's used both to forward
4875 * remaining data and to resync after end of body. It expects the msg_state to
4876 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4877 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004878 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004879 * bytes of pending data + the headers if not already done (between sol and sov).
4880 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004881 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004882int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004883{
4884 struct http_txn *txn = &s->txn;
4885 struct http_msg *msg = &s->txn.req;
4886
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004887 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4888 return 0;
4889
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004890 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004891 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004892 /* Output closed while we were sending data. We must abort and
4893 * wake the other side up.
4894 */
4895 msg->msg_state = HTTP_MSG_ERROR;
4896 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004897 return 1;
4898 }
4899
Willy Tarreau4fe41902010-06-07 22:27:41 +02004900 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004901 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004902
4903 /* Note that we don't have to send 100-continue back because we don't
4904 * need the data to complete our job, and it's up to the server to
4905 * decide whether to return 100, 417 or anything else in return of
4906 * an "Expect: 100-continue" header.
4907 */
4908
4909 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004910 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004911 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004912 * is still null. We must save the body in msg->next because it
4913 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004914 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004915 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004916
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004917 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004918 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004919 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004920 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004921 }
4922
Willy Tarreaud98cf932009-12-27 22:54:55 +01004923 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004924 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004925
Willy Tarreau610ecce2010-01-04 21:15:02 +01004926 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004927 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004928 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004929 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004930 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004931 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004932 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004933 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004934 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004935
Willy Tarreaucaabe412010-01-03 23:08:28 +01004936 if (msg->msg_state == HTTP_MSG_DATA) {
4937 /* must still forward */
4938 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004939 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004940
4941 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004942 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004943 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004944 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004945 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004946 }
4947 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004948 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004949 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004950 * TRAILERS state.
4951 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004952 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004953
Willy Tarreau54d23df2012-10-25 19:04:45 +02004954 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004955 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004956 else if (ret < 0) {
4957 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004958 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004959 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004960 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004961 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004962 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004963 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004964 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004965 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004966 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004967
4968 if (ret == 0)
4969 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004970 else if (ret < 0) {
4971 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004972 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004973 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004974 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004975 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004976 /* we're in MSG_CHUNK_SIZE now */
4977 }
4978 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004979 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004980
4981 if (ret == 0)
4982 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004983 else if (ret < 0) {
4984 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004985 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004986 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004987 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004988 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004989 /* we're in HTTP_MSG_DONE now */
4990 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004992 int old_state = msg->msg_state;
4993
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004995 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004996 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4997 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004998 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999 if (http_resync_states(s)) {
5000 /* some state changes occurred, maybe the analyser
5001 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005002 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005003 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005004 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005005 /* request errors are most likely due to
5006 * the server aborting the transfer.
5007 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005008 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005009 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005010 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005011 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005012 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005013 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005015 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005016
5017 /* If "option abortonclose" is set on the backend, we
5018 * want to monitor the client's connection and forward
5019 * any shutdown notification to the server, which will
5020 * decide whether to close or to go on processing the
5021 * request.
5022 */
5023 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005024 channel_auto_read(req);
5025 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005026 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005027 else if (s->txn.meth == HTTP_METH_POST) {
5028 /* POST requests may require to read extra CRLF
5029 * sent by broken browsers and which could cause
5030 * an RST to be sent upon close on some systems
5031 * (eg: Linux).
5032 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005033 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005034 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005035
Willy Tarreau610ecce2010-01-04 21:15:02 +01005036 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005037 }
5038 }
5039
Willy Tarreaud98cf932009-12-27 22:54:55 +01005040 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005041 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005042 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005043 if (!(s->flags & SN_ERR_MASK))
5044 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005045 if (!(s->flags & SN_FINST_MASK)) {
5046 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5047 s->flags |= SN_FINST_H;
5048 else
5049 s->flags |= SN_FINST_D;
5050 }
5051
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005052 s->fe->fe_counters.cli_aborts++;
5053 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005054 if (objt_server(s->target))
5055 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005056
5057 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005058 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005059
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005060 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005061 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005062 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005063
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005064 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005065 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005066 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005067 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005068 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005069
Willy Tarreau5c620922011-05-11 19:56:11 +02005070 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005071 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005072 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005073 * modes are already handled by the stream sock layer. We must not do
5074 * this in content-length mode because it could present the MSG_MORE
5075 * flag with the last block of forwarded data, which would cause an
5076 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005077 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005078 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005079 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005080
Willy Tarreau610ecce2010-01-04 21:15:02 +01005081 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005082 return 0;
5083
Willy Tarreaud98cf932009-12-27 22:54:55 +01005084 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005085 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005086 if (s->listener->counters)
5087 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005088 return_bad_req_stats_ok:
5089 txn->req.msg_state = HTTP_MSG_ERROR;
5090 if (txn->status) {
5091 /* Note: we don't send any error if some data were already sent */
5092 stream_int_retnclose(req->prod, NULL);
5093 } else {
5094 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005095 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005096 }
5097 req->analysers = 0;
5098 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005099
5100 if (!(s->flags & SN_ERR_MASK))
5101 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005102 if (!(s->flags & SN_FINST_MASK)) {
5103 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5104 s->flags |= SN_FINST_H;
5105 else
5106 s->flags |= SN_FINST_D;
5107 }
5108 return 0;
5109
5110 aborted_xfer:
5111 txn->req.msg_state = HTTP_MSG_ERROR;
5112 if (txn->status) {
5113 /* Note: we don't send any error if some data were already sent */
5114 stream_int_retnclose(req->prod, NULL);
5115 } else {
5116 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005117 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005118 }
5119 req->analysers = 0;
5120 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5121
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005122 s->fe->fe_counters.srv_aborts++;
5123 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005124 if (objt_server(s->target))
5125 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005126
5127 if (!(s->flags & SN_ERR_MASK))
5128 s->flags |= SN_ERR_SRVCL;
5129 if (!(s->flags & SN_FINST_MASK)) {
5130 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5131 s->flags |= SN_FINST_H;
5132 else
5133 s->flags |= SN_FINST_D;
5134 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005135 return 0;
5136}
5137
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5139 * processing can continue on next analysers, or zero if it either needs more
5140 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5141 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5142 * when it has nothing left to do, and may remove any analyser when it wants to
5143 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005144 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005145int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005146{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147 struct http_txn *txn = &s->txn;
5148 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005149 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005150 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005152 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005153
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005154 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 +02005155 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005156 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005157 rep,
5158 rep->rex, rep->wex,
5159 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005160 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005161 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005162
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005163 /*
5164 * Now parse the partial (or complete) lines.
5165 * We will check the response syntax, and also join multi-line
5166 * headers. An index of all the lines will be elaborated while
5167 * parsing.
5168 *
5169 * For the parsing, we use a 28 states FSM.
5170 *
5171 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005172 * rep->buf->p = beginning of response
5173 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5174 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005175 * msg->eol = end of current header or line (LF or CRLF)
5176 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005177 */
5178
Willy Tarreau83e3af02009-12-28 17:39:57 +01005179 /* There's a protected area at the end of the buffer for rewriting
5180 * purposes. We don't want to start to parse the request if the
5181 * protected area is affected, because we may have to move processed
5182 * data later, which is much more complicated.
5183 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005184 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005185 if (unlikely(!channel_reserved(rep))) {
5186 /* some data has still not left the buffer, wake us once that's done */
5187 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5188 goto abort_response;
5189 channel_dont_close(rep);
5190 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5191 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005192 }
5193
Willy Tarreau379357a2013-06-08 12:55:46 +02005194 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5195 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5196 buffer_slow_realign(rep->buf);
5197
Willy Tarreau9b28e032012-10-12 23:49:43 +02005198 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005199 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005200 }
5201
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 /* 1: we might have to print this header in debug mode */
5203 if (unlikely((global.mode & MODE_DEBUG) &&
5204 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005205 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005206 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207
Willy Tarreau9b28e032012-10-12 23:49:43 +02005208 sol = rep->buf->p;
5209 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005210 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005211
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005212 sol += hdr_idx_first_pos(&txn->hdr_idx);
5213 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005214
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005215 while (cur_idx) {
5216 eol = sol + txn->hdr_idx.v[cur_idx].len;
5217 debug_hdr("srvhdr", s, sol, eol);
5218 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5219 cur_idx = txn->hdr_idx.v[cur_idx].next;
5220 }
5221 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005222
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005223 /*
5224 * Now we quickly check if we have found a full valid response.
5225 * If not so, we check the FD and buffer states before leaving.
5226 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005227 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005228 * responses are checked first.
5229 *
5230 * Depending on whether the client is still there or not, we
5231 * may send an error response back or not. Note that normally
5232 * we should only check for HTTP status there, and check I/O
5233 * errors somewhere else.
5234 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005235
Willy Tarreau655dce92009-11-08 13:10:58 +01005236 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005237 /* Invalid response */
5238 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5239 /* we detected a parsing error. We want to archive this response
5240 * in the dedicated proxy area for later troubleshooting.
5241 */
5242 hdr_response_bad:
5243 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005244 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005245
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005246 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005247 if (objt_server(s->target)) {
5248 objt_server(s->target)->counters.failed_resp++;
5249 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005250 }
Willy Tarreau64648412010-03-05 10:41:54 +01005251 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005252 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253 rep->analysers = 0;
5254 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005255 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005256 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005257 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005258
5259 if (!(s->flags & SN_ERR_MASK))
5260 s->flags |= SN_ERR_PRXCOND;
5261 if (!(s->flags & SN_FINST_MASK))
5262 s->flags |= SN_FINST_H;
5263
5264 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005265 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005266
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005267 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005268 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005269 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005270 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005271 goto hdr_response_bad;
5272 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005273
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005274 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005275 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005276 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005277 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005278
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005279 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005280 if (objt_server(s->target)) {
5281 objt_server(s->target)->counters.failed_resp++;
5282 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005283 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005284
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005285 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005286 rep->analysers = 0;
5287 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005288 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005289 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005290 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005291
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292 if (!(s->flags & SN_ERR_MASK))
5293 s->flags |= SN_ERR_SRVCL;
5294 if (!(s->flags & SN_FINST_MASK))
5295 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005296 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005297 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005298
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005299 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005300 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005301 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005302 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005303
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005304 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005305 if (objt_server(s->target)) {
5306 objt_server(s->target)->counters.failed_resp++;
5307 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005308 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005309
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005310 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005311 rep->analysers = 0;
5312 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005313 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005314 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005315 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005316
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005317 if (!(s->flags & SN_ERR_MASK))
5318 s->flags |= SN_ERR_SRVTO;
5319 if (!(s->flags & SN_FINST_MASK))
5320 s->flags |= SN_FINST_H;
5321 return 0;
5322 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005323
Willy Tarreauf003d372012-11-26 13:35:37 +01005324 /* client abort with an abortonclose */
5325 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5326 s->fe->fe_counters.cli_aborts++;
5327 s->be->be_counters.cli_aborts++;
5328 if (objt_server(s->target))
5329 objt_server(s->target)->counters.cli_aborts++;
5330
5331 rep->analysers = 0;
5332 channel_auto_close(rep);
5333
5334 txn->status = 400;
5335 bi_erase(rep);
5336 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5337
5338 if (!(s->flags & SN_ERR_MASK))
5339 s->flags |= SN_ERR_CLICL;
5340 if (!(s->flags & SN_FINST_MASK))
5341 s->flags |= SN_FINST_H;
5342
5343 /* process_session() will take care of the error */
5344 return 0;
5345 }
5346
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005347 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005348 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005349 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005350 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005351
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005352 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005353 if (objt_server(s->target)) {
5354 objt_server(s->target)->counters.failed_resp++;
5355 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005356 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005357
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005358 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005359 rep->analysers = 0;
5360 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005361 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005362 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005363 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005364
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005365 if (!(s->flags & SN_ERR_MASK))
5366 s->flags |= SN_ERR_SRVCL;
5367 if (!(s->flags & SN_FINST_MASK))
5368 s->flags |= SN_FINST_H;
5369 return 0;
5370 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005371
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005372 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005373 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005375 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005376
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005377 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005378 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005379 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005380
5381 if (!(s->flags & SN_ERR_MASK))
5382 s->flags |= SN_ERR_CLICL;
5383 if (!(s->flags & SN_FINST_MASK))
5384 s->flags |= SN_FINST_H;
5385
5386 /* process_session() will take care of the error */
5387 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005388 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005389
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005390 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005391 return 0;
5392 }
5393
5394 /* More interesting part now : we know that we have a complete
5395 * response which at least looks like HTTP. We have an indicator
5396 * of each header's length, so we can parse them quickly.
5397 */
5398
5399 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005400 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005401
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005402 /*
5403 * 1: get the status code
5404 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005405 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005406 if (n < 1 || n > 5)
5407 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005408 /* when the client triggers a 4xx from the server, it's most often due
5409 * to a missing object or permission. These events should be tracked
5410 * because if they happen often, it may indicate a brute force or a
5411 * vulnerability scan.
5412 */
5413 if (n == 4)
5414 session_inc_http_err_ctr(s);
5415
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005416 if (objt_server(s->target))
5417 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005418
Willy Tarreau5b154472009-12-21 20:11:07 +01005419 /* check if the response is HTTP/1.1 or above */
5420 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005421 ((rep->buf->p[5] > '1') ||
5422 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005423 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005424
5425 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005426 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 +01005427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005428 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005429 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005430
Willy Tarreau9b28e032012-10-12 23:49:43 +02005431 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005432
Willy Tarreau39650402010-03-15 19:44:39 +01005433 /* Adjust server's health based on status code. Note: status codes 501
5434 * and 505 are triggered on demand by client request, so we must not
5435 * count them as server failures.
5436 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005437 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005438 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005439 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005440 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005441 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005442 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005443
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005444 /*
5445 * 2: check for cacheability.
5446 */
5447
5448 switch (txn->status) {
5449 case 200:
5450 case 203:
5451 case 206:
5452 case 300:
5453 case 301:
5454 case 410:
5455 /* RFC2616 @13.4:
5456 * "A response received with a status code of
5457 * 200, 203, 206, 300, 301 or 410 MAY be stored
5458 * by a cache (...) unless a cache-control
5459 * directive prohibits caching."
5460 *
5461 * RFC2616 @9.5: POST method :
5462 * "Responses to this method are not cacheable,
5463 * unless the response includes appropriate
5464 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005465 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005466 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005467 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005468 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5469 break;
5470 default:
5471 break;
5472 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005473
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005474 /*
5475 * 3: we may need to capture headers
5476 */
5477 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005478 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005479 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005480 txn->rsp.cap, s->fe->rsp_cap);
5481
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005482 /* 4: determine the transfer-length.
5483 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5484 * the presence of a message-body in a RESPONSE and its transfer length
5485 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005486 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005487 * All responses to the HEAD request method MUST NOT include a
5488 * message-body, even though the presence of entity-header fields
5489 * might lead one to believe they do. All 1xx (informational), 204
5490 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5491 * message-body. All other responses do include a message-body,
5492 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005493 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005494 * 1. Any response which "MUST NOT" include a message-body (such as the
5495 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5496 * always terminated by the first empty line after the header fields,
5497 * regardless of the entity-header fields present in the message.
5498 *
5499 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5500 * the "chunked" transfer-coding (Section 6.2) is used, the
5501 * transfer-length is defined by the use of this transfer-coding.
5502 * If a Transfer-Encoding header field is present and the "chunked"
5503 * transfer-coding is not present, the transfer-length is defined by
5504 * the sender closing the connection.
5505 *
5506 * 3. If a Content-Length header field is present, its decimal value in
5507 * OCTETs represents both the entity-length and the transfer-length.
5508 * If a message is received with both a Transfer-Encoding header
5509 * field and a Content-Length header field, the latter MUST be ignored.
5510 *
5511 * 4. If the message uses the media type "multipart/byteranges", and
5512 * the transfer-length is not otherwise specified, then this self-
5513 * delimiting media type defines the transfer-length. This media
5514 * type MUST NOT be used unless the sender knows that the recipient
5515 * can parse it; the presence in a request of a Range header with
5516 * multiple byte-range specifiers from a 1.1 client implies that the
5517 * client can parse multipart/byteranges responses.
5518 *
5519 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005520 */
5521
5522 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005523 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005524 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005525 * FIXME: should we parse anyway and return an error on chunked encoding ?
5526 */
5527 if (txn->meth == HTTP_METH_HEAD ||
5528 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005529 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005530 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005531 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005532 goto skip_content_length;
5533 }
5534
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005535 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005536 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005537 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005538 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005539 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005540 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5541 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005542 /* bad transfer-encoding (chunked followed by something else) */
5543 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005544 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005545 break;
5546 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005547 }
5548
5549 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5550 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005551 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005552 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005553 signed long long cl;
5554
Willy Tarreauad14f752011-09-02 20:33:27 +02005555 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005556 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005557 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005558 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005559
Willy Tarreauad14f752011-09-02 20:33:27 +02005560 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005561 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005562 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005563 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005564
Willy Tarreauad14f752011-09-02 20:33:27 +02005565 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005566 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005567 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005568 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005569
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005570 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005571 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005573 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005575 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005576 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005577 }
5578
William Lallemand82fe75c2012-10-23 10:25:10 +02005579 if (s->fe->comp || s->be->comp)
5580 select_compression_response_header(s, rep->buf);
5581
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005582 /* FIXME: we should also implement the multipart/byterange method.
5583 * For now on, we resort to close mode in this case (unknown length).
5584 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005585skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005586
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005587 /* end of job, return OK */
5588 rep->analysers &= ~an_bit;
5589 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005590 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005591 return 1;
5592}
5593
5594/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005595 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5596 * and updates t->rep->analysers. It might make sense to explode it into several
5597 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005598 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005599int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005600{
5601 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005602 struct http_msg *msg = &txn->rsp;
5603 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005604 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005605 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005606
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005607 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 +02005608 now_ms, __FUNCTION__,
5609 t,
5610 rep,
5611 rep->rex, rep->wex,
5612 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005613 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005614 rep->analysers);
5615
Willy Tarreau655dce92009-11-08 13:10:58 +01005616 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005617 return 0;
5618
5619 rep->analysers &= ~an_bit;
5620 rep->analyse_exp = TICK_ETERNITY;
5621
Willy Tarreau5b154472009-12-21 20:11:07 +01005622 /* Now we have to check if we need to modify the Connection header.
5623 * This is more difficult on the response than it is on the request,
5624 * because we can have two different HTTP versions and we don't know
5625 * how the client will interprete a response. For instance, let's say
5626 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5627 * HTTP/1.1 response without any header. Maybe it will bound itself to
5628 * HTTP/1.0 because it only knows about it, and will consider the lack
5629 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5630 * the lack of header as a keep-alive. Thus we will use two flags
5631 * indicating how a request MAY be understood by the client. In case
5632 * of multiple possibilities, we'll fix the header to be explicit. If
5633 * ambiguous cases such as both close and keepalive are seen, then we
5634 * will fall back to explicit close. Note that we won't take risks with
5635 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005636 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005637 */
5638
Willy Tarreaudc008c52010-02-01 16:20:08 +01005639 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5640 txn->status == 101)) {
5641 /* Either we've established an explicit tunnel, or we're
5642 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005643 * to understand the next protocols. We have to switch to tunnel
5644 * mode, so that we transfer the request and responses then let
5645 * this protocol pass unmodified. When we later implement specific
5646 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005647 * header which contains information about that protocol for
5648 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005649 */
5650 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5651 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005652 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5653 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5654 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005655 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005656
Willy Tarreau60466522010-01-18 19:08:45 +01005657 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005658 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005659 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5660 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005661
Willy Tarreau60466522010-01-18 19:08:45 +01005662 /* now adjust header transformations depending on current state */
5663 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5664 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5665 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005666 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005667 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005668 }
Willy Tarreau60466522010-01-18 19:08:45 +01005669 else { /* SCL / KAL */
5670 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005671 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005672 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005673 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005674
Willy Tarreau60466522010-01-18 19:08:45 +01005675 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005676 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005677
Willy Tarreau60466522010-01-18 19:08:45 +01005678 /* Some keep-alive responses are converted to Server-close if
5679 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005680 */
Willy Tarreau60466522010-01-18 19:08:45 +01005681 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5682 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005683 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005684 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005685 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005686 }
5687
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005688 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005689 /*
5690 * 3: we will have to evaluate the filters.
5691 * As opposed to version 1.2, now they will be evaluated in the
5692 * filters order and not in the header order. This means that
5693 * each filter has to be validated among all headers.
5694 *
5695 * Filters are tried with ->be first, then with ->fe if it is
5696 * different from ->be.
5697 */
5698
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005699 cur_proxy = t->be;
5700 while (1) {
5701 struct proxy *rule_set = cur_proxy;
5702
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005703 /* evaluate http-response rules */
5704 if (!http_res_last_rule)
5705 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5706
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005707 /* try headers filters */
5708 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005709 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005710 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005711 if (objt_server(t->target)) {
5712 objt_server(t->target)->counters.failed_resp++;
5713 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005714 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005715 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005716 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005717 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005718 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005719 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005720 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005721 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005722 if (!(t->flags & SN_ERR_MASK))
5723 t->flags |= SN_ERR_PRXCOND;
5724 if (!(t->flags & SN_FINST_MASK))
5725 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005726 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005727 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005728 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005729
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005730 /* has the response been denied ? */
5731 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005732 if (objt_server(t->target))
5733 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005734
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005735 t->be->be_counters.denied_resp++;
5736 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005737 if (t->listener->counters)
5738 t->listener->counters->denied_resp++;
5739
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005740 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005741 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005742
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005743 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005744 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005745 if (txn->status < 200)
5746 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005747 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005748 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005749 ret = acl_pass(ret);
5750 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5751 ret = !ret;
5752 if (!ret)
5753 continue;
5754 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005755 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005756 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005757 }
5758
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005759 /* check whether we're already working on the frontend */
5760 if (cur_proxy == t->fe)
5761 break;
5762 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005763 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005764
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005765 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005766 * We may be facing a 100-continue response, in which case this
5767 * is not the right response, and we're waiting for the next one.
5768 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005769 * next one.
5770 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005771 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005772 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005773 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005774 msg->msg_state = HTTP_MSG_RPBEFORE;
5775 txn->status = 0;
5776 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5777 return 1;
5778 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005779 else if (unlikely(txn->status < 200))
5780 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005781
5782 /* we don't have any 1xx status code now */
5783
5784 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005785 * 4: check for server cookie.
5786 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005787 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5788 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005789 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005790
Willy Tarreaubaaee002006-06-26 02:48:02 +02005791
Willy Tarreaua15645d2007-03-18 16:22:39 +01005792 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005793 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 */
Willy Tarreau67402132012-05-31 20:40:20 +02005795 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005796 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005797
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005798 /*
5799 * 6: add server cookie in the response if needed
5800 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005801 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005802 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005803 (!(t->flags & SN_DIRECT) ||
5804 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5805 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5806 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5807 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005808 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005809 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005810 /* the server is known, it's not the one the client requested, or the
5811 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005812 * insert a set-cookie here, except if we want to insert only on POST
5813 * requests and this one isn't. Note that servers which don't have cookies
5814 * (eg: some backup servers) will return a full cookie removal request.
5815 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005816 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005817 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005818 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5819 t->be->cookie_name);
5820 }
5821 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005822 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005823
5824 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5825 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005826 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5827 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5828 trash.len += 5;
5829
Willy Tarreauef4f3912010-10-07 21:00:29 +02005830 if (t->be->cookie_maxlife) {
5831 /* emit first_date, which is either the original one or
5832 * the current date.
5833 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005834 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005835 s30tob64(txn->cookie_first_date ?
5836 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005837 (date.tv_sec+3) >> 2, trash.str + trash.len);
5838 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005839 }
5840 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005841 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005842 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005843
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005844 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005845 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005846
Willy Tarreau4992dd22012-05-31 21:02:17 +02005847 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005848 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005849
5850 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005851 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005852
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005853 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005854 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005855
Willy Tarreauf1348312010-10-07 15:54:11 +02005856 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005857 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005858 /* the server did not change, only the date was updated */
5859 txn->flags |= TX_SCK_UPDATED;
5860 else
5861 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005862
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005863 /* Here, we will tell an eventual cache on the client side that we don't
5864 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5865 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5866 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5867 */
Willy Tarreau67402132012-05-31 20:40:20 +02005868 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005869
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005870 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5871
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005872 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005873 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005874 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005875 }
5876 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005877
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005878 /*
5879 * 7: check if result will be cacheable with a cookie.
5880 * We'll block the response if security checks have caught
5881 * nasty things such as a cacheable cookie.
5882 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005883 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5884 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005885 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005886
5887 /* we're in presence of a cacheable response containing
5888 * a set-cookie header. We'll block it as requested by
5889 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005890 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005891 if (objt_server(t->target))
5892 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005893
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005894 t->be->be_counters.denied_resp++;
5895 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005896 if (t->listener->counters)
5897 t->listener->counters->denied_resp++;
5898
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005899 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005900 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005901 send_log(t->be, LOG_ALERT,
5902 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005903 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005904 goto return_srv_prx_502;
5905 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906
5907 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005908 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005909 * If an "Upgrade" token is found, the header is left untouched in order
5910 * not to have to deal with some client bugs : some of them fail an upgrade
5911 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005912 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005913 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5914 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5915 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005916 unsigned int want_flags = 0;
5917
5918 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5919 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5920 /* we want a keep-alive response here. Keep-alive header
5921 * required if either side is not 1.1.
5922 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005923 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005924 want_flags |= TX_CON_KAL_SET;
5925 }
5926 else {
5927 /* we want a close response here. Close header required if
5928 * the server is 1.1, regardless of the client.
5929 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005930 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005931 want_flags |= TX_CON_CLO_SET;
5932 }
5933
5934 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005935 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005936 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005937
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005938 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005939 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005940 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005941 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005942
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005943 /*************************************************************
5944 * OK, that's finished for the headers. We have done what we *
5945 * could. Let's switch to the DATA state. *
5946 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005948 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005949
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005950 /* if the user wants to log as soon as possible, without counting
5951 * bytes from the server, then this is the right moment. We have
5952 * to temporarily assign bytes_out to log what we currently have.
5953 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005954 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005955 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5956 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005957 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005958 t->logs.bytes_out = 0;
5959 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005960
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005961 /* Note: we must not try to cheat by jumping directly to DATA,
5962 * otherwise we would not let the client side wake up.
5963 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005964
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005965 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005966 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005967 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005968}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005969
Willy Tarreaud98cf932009-12-27 22:54:55 +01005970/* This function is an analyser which forwards response body (including chunk
5971 * sizes if any). It is called as soon as we must forward, even if we forward
5972 * zero byte. The only situation where it must not be called is when we're in
5973 * tunnel mode and we want to forward till the close. It's used both to forward
5974 * remaining data and to resync after end of body. It expects the msg_state to
5975 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5976 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005977 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005978 * bytes of pending data + the headers if not already done (between sol and sov).
5979 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005980 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005981int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005982{
5983 struct http_txn *txn = &s->txn;
5984 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005985 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005986 static struct buffer *tmpbuf = NULL;
5987 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005988 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005989 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005990
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005991 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5992 return 0;
5993
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005994 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005995 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005996 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005997 /* Output closed while we were sending data. We must abort and
5998 * wake the other side up.
5999 */
6000 msg->msg_state = HTTP_MSG_ERROR;
6001 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006002 return 1;
6003 }
6004
Willy Tarreau4fe41902010-06-07 22:27:41 +02006005 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006006 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006007
William Lallemand82fe75c2012-10-23 10:25:10 +02006008 /* this is the first time we need the compression buffer */
6009 if (s->comp_algo != NULL && tmpbuf == NULL) {
6010 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6011 goto aborted_xfer; /* no memory */
6012 }
6013
Willy Tarreaud98cf932009-12-27 22:54:55 +01006014 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006015 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006016 * rep->buf.p still points to the beginning of the message and msg->sol
6017 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006018 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006019 channel_forward(res, msg->sov);
6020 msg->next = 0;
6021 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006022
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006023 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006024 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006025 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006026 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006027 }
6028
William Lallemand82fe75c2012-10-23 10:25:10 +02006029 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006030 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02006031 if (ret < 0)
6032 goto missing_data; /* not enough spaces in buffers */
6033 compressing = 1;
6034 }
6035
Willy Tarreaud98cf932009-12-27 22:54:55 +01006036 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006037 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006038 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006039 if (s->comp_algo == NULL) {
6040 bytes = msg->sov - msg->sol;
6041 if (msg->chunk_len || bytes) {
6042 msg->sol = msg->sov;
6043 msg->next -= bytes; /* will be forwarded */
6044 msg->chunk_len += bytes;
6045 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6046 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006047 }
6048
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006049 switch (msg->msg_state - HTTP_MSG_DATA) {
6050 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006051 if (compressing) {
6052 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6053 if (ret < 0)
6054 goto aborted_xfer;
6055 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006056
6057 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006058 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01006059
6060 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006061 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006062 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006063 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006064 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006065 if (compressing && consumed_data) {
6066 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6067 compressing = 0;
6068 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006069 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006070 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006071 /* fall through for HTTP_MSG_CHUNK_CRLF */
6072
6073 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6074 /* we want the CRLF after the data */
6075
6076 ret = http_skip_chunk_crlf(msg);
6077 if (ret == 0)
6078 goto missing_data;
6079 else if (ret < 0) {
6080 if (msg->err_pos >= 0)
6081 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6082 goto return_bad_res;
6083 }
6084 /* skipping data in buffer for compression */
6085 if (compressing) {
6086 b_adv(res->buf, msg->next);
6087 msg->next = 0;
6088 msg->sov = 0;
6089 msg->sol = 0;
6090 }
6091 /* we're in MSG_CHUNK_SIZE now, fall through */
6092
6093 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006094 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006095 * set ->sov and ->next to point to the body and switch to DATA or
6096 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006097 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006098
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006099 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006100 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006101 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006102 else if (ret < 0) {
6103 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006104 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006105 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006106 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006107 if (compressing) {
6108 if (likely(msg->chunk_len > 0)) {
6109 /* skipping data if we are in compression mode */
6110 b_adv(res->buf, msg->next);
6111 msg->next = 0;
6112 msg->sov = 0;
6113 msg->sol = 0;
6114 } else {
6115 if (consumed_data) {
6116 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6117 compressing = 0;
6118 }
6119 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006120 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006121 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006122 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006123
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006124 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6125 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006126 if (ret == 0)
6127 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006128 else if (ret < 0) {
6129 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006130 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006131 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006132 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006133 if (s->comp_algo != NULL) {
6134 /* forwarding trailers */
6135 channel_forward(res, msg->next);
6136 msg->next = 0;
6137 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006138 /* we're in HTTP_MSG_DONE now, but we might still have
6139 * some data pending, so let's loop over once.
6140 */
6141 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006142
6143 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006144 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006145
6146 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006147 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006148 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6149 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006150 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006151 if (http_resync_states(s)) {
6152 http_silent_debug(__LINE__, s);
6153 /* some state changes occurred, maybe the analyser
6154 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006155 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006156 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006157 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006158 /* response errors are most likely due to
6159 * the client aborting the transfer.
6160 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006161 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006162 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006163 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006164 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006165 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006166 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006167 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006168 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006169 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006170 }
6171 }
6172
Willy Tarreaud98cf932009-12-27 22:54:55 +01006173 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006174 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006175 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6176 compressing = 0;
6177 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006178
6179 if (res->flags & CF_SHUTW)
6180 goto aborted_xfer;
6181
6182 /* stop waiting for data if the input is closed before the end. If the
6183 * client side was already closed, it means that the client has aborted,
6184 * so we don't want to count this as a server abort. Otherwise it's a
6185 * server abort.
6186 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006187 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006188 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6189 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006190 if (!(s->flags & SN_ERR_MASK))
6191 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006192 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006193 if (objt_server(s->target))
6194 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006195 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006196 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006197
Willy Tarreau40dba092010-03-04 18:14:51 +01006198 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006199 if (!s->req->analysers)
6200 goto return_bad_res;
6201
Willy Tarreauea953162012-05-18 23:41:28 +02006202 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006203 if (s->comp_algo == NULL) {
6204 bytes = msg->sov - msg->sol;
6205 if (msg->chunk_len || bytes) {
6206 msg->sol = msg->sov;
6207 msg->next -= bytes; /* will be forwarded */
6208 msg->chunk_len += bytes;
6209 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6210 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006211 }
6212
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006213 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006214 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006215 * Similarly, with keep-alive on the client side, we don't want to forward a
6216 * close.
6217 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006218 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006219 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6220 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006221 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006222
Willy Tarreau5c620922011-05-11 19:56:11 +02006223 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006224 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006225 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006226 * modes are already handled by the stream sock layer. We must not do
6227 * this in content-length mode because it could present the MSG_MORE
6228 * flag with the last block of forwarded data, which would cause an
6229 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006230 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006231 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006232 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006233
Willy Tarreaud98cf932009-12-27 22:54:55 +01006234 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006235 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006236 return 0;
6237
Willy Tarreau40dba092010-03-04 18:14:51 +01006238 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006239 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006240 if (objt_server(s->target))
6241 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006242
6243 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006244 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006245 /* don't send any error message as we're in the body */
6246 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006247 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006248 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006249 if (objt_server(s->target))
6250 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006251
6252 if (!(s->flags & SN_ERR_MASK))
6253 s->flags |= SN_ERR_PRXCOND;
6254 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006255 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006256 return 0;
6257
6258 aborted_xfer:
6259 txn->rsp.msg_state = HTTP_MSG_ERROR;
6260 /* don't send any error message as we're in the body */
6261 stream_int_retnclose(res->cons, NULL);
6262 res->analysers = 0;
6263 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6264
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006265 s->fe->fe_counters.cli_aborts++;
6266 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006267 if (objt_server(s->target))
6268 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006269
6270 if (!(s->flags & SN_ERR_MASK))
6271 s->flags |= SN_ERR_CLICL;
6272 if (!(s->flags & SN_FINST_MASK))
6273 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006274 return 0;
6275}
6276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006277/* Iterate the same filter through all request headers.
6278 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006279 * Since it can manage the switch to another backend, it updates the per-proxy
6280 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006282int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006284 char term;
6285 char *cur_ptr, *cur_end, *cur_next;
6286 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006287 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006288 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006289 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006291 last_hdr = 0;
6292
Willy Tarreau9b28e032012-10-12 23:49:43 +02006293 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006294 old_idx = 0;
6295
6296 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006297 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006298 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006299 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 (exp->action == ACT_ALLOW ||
6301 exp->action == ACT_DENY ||
6302 exp->action == ACT_TARPIT))
6303 return 0;
6304
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006305 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006306 if (!cur_idx)
6307 break;
6308
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006309 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006310 cur_ptr = cur_next;
6311 cur_end = cur_ptr + cur_hdr->len;
6312 cur_next = cur_end + cur_hdr->cr + 1;
6313
6314 /* Now we have one header between cur_ptr and cur_end,
6315 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006316 */
6317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006318 /* The annoying part is that pattern matching needs
6319 * that we modify the contents to null-terminate all
6320 * strings before testing them.
6321 */
6322
6323 term = *cur_end;
6324 *cur_end = '\0';
6325
6326 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6327 switch (exp->action) {
6328 case ACT_SETBE:
6329 /* It is not possible to jump a second time.
6330 * FIXME: should we return an HTTP/500 here so that
6331 * the admin knows there's a problem ?
6332 */
6333 if (t->be != t->fe)
6334 break;
6335
6336 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006337 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006338 last_hdr = 1;
6339 break;
6340
6341 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006342 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006343 last_hdr = 1;
6344 break;
6345
6346 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006347 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006349
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006350 t->fe->fe_counters.denied_req++;
6351 if (t->fe != t->be)
6352 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006353 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006354 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006355
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006356 break;
6357
6358 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006359 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006360 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006361
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006362 t->fe->fe_counters.denied_req++;
6363 if (t->fe != t->be)
6364 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006365 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006366 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006368 break;
6369
6370 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006371 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6372 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006373 /* FIXME: if the user adds a newline in the replacement, the
6374 * index will not be recalculated for now, and the new line
6375 * will not be counted as a new header.
6376 */
6377
6378 cur_end += delta;
6379 cur_next += delta;
6380 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006381 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382 break;
6383
6384 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006385 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006386 cur_next += delta;
6387
Willy Tarreaufa355d42009-11-29 18:12:29 +01006388 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006389 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6390 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006391 cur_hdr->len = 0;
6392 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006393 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006394 break;
6395
6396 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006398 if (cur_end)
6399 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006401 /* keep the link from this header to next one in case of later
6402 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006403 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006404 old_idx = cur_idx;
6405 }
6406 return 0;
6407}
6408
6409
6410/* Apply the filter to the request line.
6411 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6412 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006413 * Since it can manage the switch to another backend, it updates the per-proxy
6414 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006415 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006416int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006417{
6418 char term;
6419 char *cur_ptr, *cur_end;
6420 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006421 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006422 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423
Willy Tarreau3d300592007-03-18 18:34:41 +01006424 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006425 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006426 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006427 (exp->action == ACT_ALLOW ||
6428 exp->action == ACT_DENY ||
6429 exp->action == ACT_TARPIT))
6430 return 0;
6431 else if (exp->action == ACT_REMOVE)
6432 return 0;
6433
6434 done = 0;
6435
Willy Tarreau9b28e032012-10-12 23:49:43 +02006436 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006437 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006438
6439 /* Now we have the request line between cur_ptr and cur_end */
6440
6441 /* The annoying part is that pattern matching needs
6442 * that we modify the contents to null-terminate all
6443 * strings before testing them.
6444 */
6445
6446 term = *cur_end;
6447 *cur_end = '\0';
6448
6449 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6450 switch (exp->action) {
6451 case ACT_SETBE:
6452 /* It is not possible to jump a second time.
6453 * FIXME: should we return an HTTP/500 here so that
6454 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006455 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 if (t->be != t->fe)
6457 break;
6458
6459 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006460 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006461 done = 1;
6462 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006465 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 done = 1;
6467 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006470 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006471
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006472 t->fe->fe_counters.denied_req++;
6473 if (t->fe != t->be)
6474 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006475 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006476 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 done = 1;
6479 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006481 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006482 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006483
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006484 t->fe->fe_counters.denied_req++;
6485 if (t->fe != t->be)
6486 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006487 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006488 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 done = 1;
6491 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493 case ACT_REPLACE:
6494 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006495 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6496 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 /* FIXME: if the user adds a newline in the replacement, the
6498 * index will not be recalculated for now, and the new line
6499 * will not be counted as a new header.
6500 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006501
Willy Tarreaufa355d42009-11-29 18:12:29 +01006502 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006504 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 HTTP_MSG_RQMETH,
6506 cur_ptr, cur_end + 1,
6507 NULL, NULL);
6508 if (unlikely(!cur_end))
6509 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 /* we have a full request and we know that we have either a CR
6512 * or an LF at <ptr>.
6513 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006514 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6515 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006516 /* there is no point trying this regex on headers */
6517 return 1;
6518 }
6519 }
6520 *cur_end = term; /* restore the string terminator */
6521 return done;
6522}
Willy Tarreau97de6242006-12-27 17:18:38 +01006523
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006527 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529 * unparsable request. Since it can manage the switch to another backend, it
6530 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006532int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006534 struct http_txn *txn = &s->txn;
6535 struct hdr_exp *exp;
6536
6537 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006538 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 /*
6541 * The interleaving of transformations and verdicts
6542 * makes it difficult to decide to continue or stop
6543 * the evaluation.
6544 */
6545
Willy Tarreau6c123b12010-01-28 20:22:06 +01006546 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6547 break;
6548
Willy Tarreau3d300592007-03-18 18:34:41 +01006549 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006551 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006553
6554 /* if this filter had a condition, evaluate it now and skip to
6555 * next filter if the condition does not match.
6556 */
6557 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006558 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006559 ret = acl_pass(ret);
6560 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6561 ret = !ret;
6562
6563 if (!ret)
6564 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006565 }
6566
6567 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006568 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 if (unlikely(ret < 0))
6570 return -1;
6571
6572 if (likely(ret == 0)) {
6573 /* The filter did not match the request, it can be
6574 * iterated through all headers.
6575 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006576 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006577 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006578 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580}
6581
6582
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006585 * Try to retrieve the server associated to the appsession.
6586 * If the server is found, it's assigned to the session.
6587 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006588void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006589 struct http_txn *txn = &t->txn;
6590 appsess *asession = NULL;
6591 char *sessid_temp = NULL;
6592
Cyril Bontéb21570a2009-11-29 20:04:48 +01006593 if (len > t->be->appsession_len) {
6594 len = t->be->appsession_len;
6595 }
6596
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006597 if (t->be->options2 & PR_O2_AS_REQL) {
6598 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006599 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006600 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006601 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006602 }
6603
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006604 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006605 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6606 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6607 return;
6608 }
6609
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006610 memcpy(txn->sessid, buf, len);
6611 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006612 }
6613
6614 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6615 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6616 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6617 return;
6618 }
6619
Cyril Bontéb21570a2009-11-29 20:04:48 +01006620 memcpy(sessid_temp, buf, len);
6621 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006622
6623 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6624 /* free previously allocated memory */
6625 pool_free2(apools.sessid, sessid_temp);
6626
6627 if (asession != NULL) {
6628 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6629 if (!(t->be->options2 & PR_O2_AS_REQL))
6630 asession->request_count++;
6631
6632 if (asession->serverid != NULL) {
6633 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006634
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006635 while (srv) {
6636 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006637 if ((srv->state & SRV_RUNNING) ||
6638 (t->be->options & PR_O_PERSIST) ||
6639 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006640 /* we found the server and it's usable */
6641 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006642 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006643 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006644 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006645
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006646 break;
6647 } else {
6648 txn->flags &= ~TX_CK_MASK;
6649 txn->flags |= TX_CK_DOWN;
6650 }
6651 }
6652 srv = srv->next;
6653 }
6654 }
6655 }
6656}
6657
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006658/* Find the end of a cookie value contained between <s> and <e>. It works the
6659 * same way as with headers above except that the semi-colon also ends a token.
6660 * See RFC2965 for more information. Note that it requires a valid header to
6661 * return a valid result.
6662 */
6663char *find_cookie_value_end(char *s, const char *e)
6664{
6665 int quoted, qdpair;
6666
6667 quoted = qdpair = 0;
6668 for (; s < e; s++) {
6669 if (qdpair) qdpair = 0;
6670 else if (quoted) {
6671 if (*s == '\\') qdpair = 1;
6672 else if (*s == '"') quoted = 0;
6673 }
6674 else if (*s == '"') quoted = 1;
6675 else if (*s == ',' || *s == ';') return s;
6676 }
6677 return s;
6678}
6679
6680/* Delete a value in a header between delimiters <from> and <next> in buffer
6681 * <buf>. The number of characters displaced is returned, and the pointer to
6682 * the first delimiter is updated if required. The function tries as much as
6683 * possible to respect the following principles :
6684 * - replace <from> delimiter by the <next> one unless <from> points to a
6685 * colon, in which case <next> is simply removed
6686 * - set exactly one space character after the new first delimiter, unless
6687 * there are not enough characters in the block being moved to do so.
6688 * - remove unneeded spaces before the previous delimiter and after the new
6689 * one.
6690 *
6691 * It is the caller's responsibility to ensure that :
6692 * - <from> points to a valid delimiter or the colon ;
6693 * - <next> points to a valid delimiter or the final CR/LF ;
6694 * - there are non-space chars before <from> ;
6695 * - there is a CR/LF at or after <next>.
6696 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006697int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006698{
6699 char *prev = *from;
6700
6701 if (*prev == ':') {
6702 /* We're removing the first value, preserve the colon and add a
6703 * space if possible.
6704 */
6705 if (!http_is_crlf[(unsigned char)*next])
6706 next++;
6707 prev++;
6708 if (prev < next)
6709 *prev++ = ' ';
6710
6711 while (http_is_spht[(unsigned char)*next])
6712 next++;
6713 } else {
6714 /* Remove useless spaces before the old delimiter. */
6715 while (http_is_spht[(unsigned char)*(prev-1)])
6716 prev--;
6717 *from = prev;
6718
6719 /* copy the delimiter and if possible a space if we're
6720 * not at the end of the line.
6721 */
6722 if (!http_is_crlf[(unsigned char)*next]) {
6723 *prev++ = *next++;
6724 if (prev + 1 < next)
6725 *prev++ = ' ';
6726 while (http_is_spht[(unsigned char)*next])
6727 next++;
6728 }
6729 }
6730 return buffer_replace2(buf, prev, next, NULL, 0);
6731}
6732
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006733/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006734 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006735 * desirable to call it only when needed. This code is quite complex because
6736 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6737 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006739void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006740{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006741 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006742 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006743 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006744 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6745 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006747 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006748 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006749 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006750
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006751 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006753 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006755 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006756 hdr_beg = hdr_next;
6757 hdr_end = hdr_beg + cur_hdr->len;
6758 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006759
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006760 /* We have one full header between hdr_beg and hdr_end, and the
6761 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006762 * "Cookie:" headers.
6763 */
6764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006765 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006766 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767 old_idx = cur_idx;
6768 continue;
6769 }
6770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 del_from = NULL; /* nothing to be deleted */
6772 preserve_hdr = 0; /* assume we may kill the whole header */
6773
Willy Tarreau58f10d72006-12-04 02:26:12 +01006774 /* Now look for cookies. Conforming to RFC2109, we have to support
6775 * attributes whose name begin with a '$', and associate them with
6776 * the right cookie, if we want to delete this cookie.
6777 * So there are 3 cases for each cookie read :
6778 * 1) it's a special attribute, beginning with a '$' : ignore it.
6779 * 2) it's a server id cookie that we *MAY* want to delete : save
6780 * some pointers on it (last semi-colon, beginning of cookie...)
6781 * 3) it's an application cookie : we *MAY* have to delete a previous
6782 * "special" cookie.
6783 * At the end of loop, if a "special" cookie remains, we may have to
6784 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006785 * *MUST* delete it.
6786 *
6787 * Note: RFC2965 is unclear about the processing of spaces around
6788 * the equal sign in the ATTR=VALUE form. A careful inspection of
6789 * the RFC explicitly allows spaces before it, and not within the
6790 * tokens (attrs or values). An inspection of RFC2109 allows that
6791 * too but section 10.1.3 lets one think that spaces may be allowed
6792 * after the equal sign too, resulting in some (rare) buggy
6793 * implementations trying to do that. So let's do what servers do.
6794 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6795 * allowed quoted strings in values, with any possible character
6796 * after a backslash, including control chars and delimitors, which
6797 * causes parsing to become ambiguous. Browsers also allow spaces
6798 * within values even without quotes.
6799 *
6800 * We have to keep multiple pointers in order to support cookie
6801 * removal at the beginning, middle or end of header without
6802 * corrupting the header. All of these headers are valid :
6803 *
6804 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6805 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6806 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6807 * | | | | | | | | |
6808 * | | | | | | | | hdr_end <--+
6809 * | | | | | | | +--> next
6810 * | | | | | | +----> val_end
6811 * | | | | | +-----------> val_beg
6812 * | | | | +--------------> equal
6813 * | | | +----------------> att_end
6814 * | | +---------------------> att_beg
6815 * | +--------------------------> prev
6816 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006817 */
6818
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006819 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6820 /* Iterate through all cookies on this line */
6821
6822 /* find att_beg */
6823 att_beg = prev + 1;
6824 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6825 att_beg++;
6826
6827 /* find att_end : this is the first character after the last non
6828 * space before the equal. It may be equal to hdr_end.
6829 */
6830 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006831
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006832 while (equal < hdr_end) {
6833 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006834 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006835 if (http_is_spht[(unsigned char)*equal++])
6836 continue;
6837 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838 }
6839
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6841 * is between <att_beg> and <equal>, both may be identical.
6842 */
6843
6844 /* look for end of cookie if there is an equal sign */
6845 if (equal < hdr_end && *equal == '=') {
6846 /* look for the beginning of the value */
6847 val_beg = equal + 1;
6848 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6849 val_beg++;
6850
6851 /* find the end of the value, respecting quotes */
6852 next = find_cookie_value_end(val_beg, hdr_end);
6853
6854 /* make val_end point to the first white space or delimitor after the value */
6855 val_end = next;
6856 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6857 val_end--;
6858 } else {
6859 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006861
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 /* We have nothing to do with attributes beginning with '$'. However,
6863 * they will automatically be removed if a header before them is removed,
6864 * since they're supposed to be linked together.
6865 */
6866 if (*att_beg == '$')
6867 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006869 /* Ignore cookies with no equal sign */
6870 if (equal == next) {
6871 /* This is not our cookie, so we must preserve it. But if we already
6872 * scheduled another cookie for removal, we cannot remove the
6873 * complete header, but we can remove the previous block itself.
6874 */
6875 preserve_hdr = 1;
6876 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006877 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 val_end += delta;
6879 next += delta;
6880 hdr_end += delta;
6881 hdr_next += delta;
6882 cur_hdr->len += delta;
6883 http_msg_move_end(&txn->req, delta);
6884 prev = del_from;
6885 del_from = NULL;
6886 }
6887 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006888 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 /* if there are spaces around the equal sign, we need to
6891 * strip them otherwise we'll get trouble for cookie captures,
6892 * or even for rewrites. Since this happens extremely rarely,
6893 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6896 int stripped_before = 0;
6897 int stripped_after = 0;
6898
6899 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006900 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 equal += stripped_before;
6902 val_beg += stripped_before;
6903 }
6904
6905 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006906 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 val_beg += stripped_after;
6908 stripped_before += stripped_after;
6909 }
6910
6911 val_end += stripped_before;
6912 next += stripped_before;
6913 hdr_end += stripped_before;
6914 hdr_next += stripped_before;
6915 cur_hdr->len += stripped_before;
6916 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006917 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006918 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 /* First, let's see if we want to capture this cookie. We check
6921 * that we don't already have a client side cookie, because we
6922 * can only capture one. Also as an optimisation, we ignore
6923 * cookies shorter than the declared name.
6924 */
6925 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6926 (val_end - att_beg >= t->fe->capture_namelen) &&
6927 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6928 int log_len = val_end - att_beg;
6929
6930 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6931 Alert("HTTP logging : out of memory.\n");
6932 } else {
6933 if (log_len > t->fe->capture_len)
6934 log_len = t->fe->capture_len;
6935 memcpy(txn->cli_cookie, att_beg, log_len);
6936 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006937 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006938 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006939
Willy Tarreaubca99692010-10-06 19:25:55 +02006940 /* Persistence cookies in passive, rewrite or insert mode have the
6941 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006942 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006943 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006945 * For cookies in prefix mode, the form is :
6946 *
6947 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 */
6949 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6950 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6951 struct server *srv = t->be->srv;
6952 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006954 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6955 * have the server ID between val_beg and delim, and the original cookie between
6956 * delim+1 and val_end. Otherwise, delim==val_end :
6957 *
6958 * Cookie: NAME=SRV; # in all but prefix modes
6959 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6960 * | || || | |+-> next
6961 * | || || | +--> val_end
6962 * | || || +---------> delim
6963 * | || |+------------> val_beg
6964 * | || +-------------> att_end = equal
6965 * | |+-----------------> att_beg
6966 * | +------------------> prev
6967 * +-------------------------> hdr_beg
6968 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006969
Willy Tarreau67402132012-05-31 20:40:20 +02006970 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006971 for (delim = val_beg; delim < val_end; delim++)
6972 if (*delim == COOKIE_DELIM)
6973 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006974 } else {
6975 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006977 /* Now check if the cookie contains a date field, which would
6978 * appear after a vertical bar ('|') just after the server name
6979 * and before the delimiter.
6980 */
6981 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6982 if (vbar1) {
6983 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006984 * right is the last seen date. It is a base64 encoded
6985 * 30-bit value representing the UNIX date since the
6986 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006987 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006988 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006989 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006990 if (val_end - vbar1 >= 5) {
6991 val = b64tos30(vbar1);
6992 if (val > 0)
6993 txn->cookie_last_date = val << 2;
6994 }
6995 /* look for a second vertical bar */
6996 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6997 if (vbar1 && (val_end - vbar1 > 5)) {
6998 val = b64tos30(vbar1 + 1);
6999 if (val > 0)
7000 txn->cookie_first_date = val << 2;
7001 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007002 }
7003 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004
Willy Tarreauf64d1412010-10-07 20:06:11 +02007005 /* if the cookie has an expiration date and the proxy wants to check
7006 * it, then we do that now. We first check if the cookie is too old,
7007 * then only if it has expired. We detect strict overflow because the
7008 * time resolution here is not great (4 seconds). Cookies with dates
7009 * in the future are ignored if their offset is beyond one day. This
7010 * allows an admin to fix timezone issues without expiring everyone
7011 * and at the same time avoids keeping unwanted side effects for too
7012 * long.
7013 */
7014 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007015 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7016 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007017 txn->flags &= ~TX_CK_MASK;
7018 txn->flags |= TX_CK_OLD;
7019 delim = val_beg; // let's pretend we have not found the cookie
7020 txn->cookie_first_date = 0;
7021 txn->cookie_last_date = 0;
7022 }
7023 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007024 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7025 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007026 txn->flags &= ~TX_CK_MASK;
7027 txn->flags |= TX_CK_EXPIRED;
7028 delim = val_beg; // let's pretend we have not found the cookie
7029 txn->cookie_first_date = 0;
7030 txn->cookie_last_date = 0;
7031 }
7032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007033 /* Here, we'll look for the first running server which supports the cookie.
7034 * This allows to share a same cookie between several servers, for example
7035 * to dedicate backup servers to specific servers only.
7036 * However, to prevent clients from sticking to cookie-less backup server
7037 * when they have incidentely learned an empty cookie, we simply ignore
7038 * empty cookies and mark them as invalid.
7039 * The same behaviour is applied when persistence must be ignored.
7040 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007041 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007044 while (srv) {
7045 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7046 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7047 if ((srv->state & SRV_RUNNING) ||
7048 (t->be->options & PR_O_PERSIST) ||
7049 (t->flags & SN_FORCE_PRST)) {
7050 /* we found the server and we can use it */
7051 txn->flags &= ~TX_CK_MASK;
7052 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7053 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007054 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 break;
7056 } else {
7057 /* we found a server, but it's down,
7058 * mark it as such and go on in case
7059 * another one is available.
7060 */
7061 txn->flags &= ~TX_CK_MASK;
7062 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007063 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007065 srv = srv->next;
7066 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067
Willy Tarreauf64d1412010-10-07 20:06:11 +02007068 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007069 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007070 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007071 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7072 txn->flags |= TX_CK_UNUSED;
7073 else
7074 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007075 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007077 /* depending on the cookie mode, we may have to either :
7078 * - delete the complete cookie if we're in insert+indirect mode, so that
7079 * the server never sees it ;
7080 * - remove the server id from the cookie value, and tag the cookie as an
7081 * application cookie so that it does not get accidentely removed later,
7082 * if we're in cookie prefix mode
7083 */
Willy Tarreau67402132012-05-31 20:40:20 +02007084 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086
Willy Tarreau9b28e032012-10-12 23:49:43 +02007087 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 val_end += delta;
7089 next += delta;
7090 hdr_end += delta;
7091 hdr_next += delta;
7092 cur_hdr->len += delta;
7093 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007095 del_from = NULL;
7096 preserve_hdr = 1; /* we want to keep this cookie */
7097 }
7098 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007099 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007100 del_from = prev;
7101 }
7102 } else {
7103 /* This is not our cookie, so we must preserve it. But if we already
7104 * scheduled another cookie for removal, we cannot remove the
7105 * complete header, but we can remove the previous block itself.
7106 */
7107 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007109 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007110 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007111 if (att_beg >= del_from)
7112 att_beg += delta;
7113 if (att_end >= del_from)
7114 att_end += delta;
7115 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 val_end += delta;
7117 next += delta;
7118 hdr_end += delta;
7119 hdr_next += delta;
7120 cur_hdr->len += delta;
7121 http_msg_move_end(&txn->req, delta);
7122 prev = del_from;
7123 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007125 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007126
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007127 /* Look for the appsession cookie unless persistence must be ignored */
7128 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7129 int cmp_len, value_len;
7130 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007131
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007132 if (t->be->options2 & PR_O2_AS_PFX) {
7133 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7134 value_begin = att_beg + t->be->appsession_name_len;
7135 value_len = val_end - att_beg - t->be->appsession_name_len;
7136 } else {
7137 cmp_len = att_end - att_beg;
7138 value_begin = val_beg;
7139 value_len = val_end - val_beg;
7140 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007141
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 /* let's see if the cookie is our appcookie */
7143 if (cmp_len == t->be->appsession_name_len &&
7144 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7145 manage_client_side_appsession(t, value_begin, value_len);
7146 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147 }
7148
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007149 /* continue with next cookie on this header line */
7150 att_beg = next;
7151 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007153 /* There are no more cookies on this line.
7154 * We may still have one (or several) marked for deletion at the
7155 * end of the line. We must do this now in two ways :
7156 * - if some cookies must be preserved, we only delete from the
7157 * mark to the end of line ;
7158 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007160 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007162 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007163 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007164 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007165 cur_hdr->len += delta;
7166 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007167 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007168
7169 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007170 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7171 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007173 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007174 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007175 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007176 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177 }
7178
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007179 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007180 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007181 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007182}
7183
7184
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185/* Iterate the same filter through all response headers contained in <rtr>.
7186 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7187 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007188int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189{
7190 char term;
7191 char *cur_ptr, *cur_end, *cur_next;
7192 int cur_idx, old_idx, last_hdr;
7193 struct http_txn *txn = &t->txn;
7194 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007195 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196
7197 last_hdr = 0;
7198
Willy Tarreau9b28e032012-10-12 23:49:43 +02007199 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 old_idx = 0;
7201
7202 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007203 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007205 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 (exp->action == ACT_ALLOW ||
7207 exp->action == ACT_DENY))
7208 return 0;
7209
7210 cur_idx = txn->hdr_idx.v[old_idx].next;
7211 if (!cur_idx)
7212 break;
7213
7214 cur_hdr = &txn->hdr_idx.v[cur_idx];
7215 cur_ptr = cur_next;
7216 cur_end = cur_ptr + cur_hdr->len;
7217 cur_next = cur_end + cur_hdr->cr + 1;
7218
7219 /* Now we have one header between cur_ptr and cur_end,
7220 * and the next header starts at cur_next.
7221 */
7222
7223 /* The annoying part is that pattern matching needs
7224 * that we modify the contents to null-terminate all
7225 * strings before testing them.
7226 */
7227
7228 term = *cur_end;
7229 *cur_end = '\0';
7230
7231 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7232 switch (exp->action) {
7233 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007234 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 last_hdr = 1;
7236 break;
7237
7238 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007239 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 last_hdr = 1;
7241 break;
7242
7243 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007244 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7245 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 /* FIXME: if the user adds a newline in the replacement, the
7247 * index will not be recalculated for now, and the new line
7248 * will not be counted as a new header.
7249 */
7250
7251 cur_end += delta;
7252 cur_next += delta;
7253 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007254 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 break;
7256
7257 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007258 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007259 cur_next += delta;
7260
Willy Tarreaufa355d42009-11-29 18:12:29 +01007261 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7263 txn->hdr_idx.used--;
7264 cur_hdr->len = 0;
7265 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007266 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267 break;
7268
7269 }
7270 }
7271 if (cur_end)
7272 *cur_end = term; /* restore the string terminator */
7273
7274 /* keep the link from this header to next one in case of later
7275 * removal of next header.
7276 */
7277 old_idx = cur_idx;
7278 }
7279 return 0;
7280}
7281
7282
7283/* Apply the filter to the status line in the response buffer <rtr>.
7284 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7285 * or -1 if a replacement resulted in an invalid status line.
7286 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007287int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288{
7289 char term;
7290 char *cur_ptr, *cur_end;
7291 int done;
7292 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007293 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294
7295
Willy Tarreau3d300592007-03-18 18:34:41 +01007296 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007298 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 (exp->action == ACT_ALLOW ||
7300 exp->action == ACT_DENY))
7301 return 0;
7302 else if (exp->action == ACT_REMOVE)
7303 return 0;
7304
7305 done = 0;
7306
Willy Tarreau9b28e032012-10-12 23:49:43 +02007307 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007308 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309
7310 /* Now we have the status line between cur_ptr and cur_end */
7311
7312 /* The annoying part is that pattern matching needs
7313 * that we modify the contents to null-terminate all
7314 * strings before testing them.
7315 */
7316
7317 term = *cur_end;
7318 *cur_end = '\0';
7319
7320 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7321 switch (exp->action) {
7322 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007323 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 done = 1;
7325 break;
7326
7327 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007328 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007329 done = 1;
7330 break;
7331
7332 case ACT_REPLACE:
7333 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007334 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7335 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 /* FIXME: if the user adds a newline in the replacement, the
7337 * index will not be recalculated for now, and the new line
7338 * will not be counted as a new header.
7339 */
7340
Willy Tarreaufa355d42009-11-29 18:12:29 +01007341 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007343 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007344 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345 cur_ptr, cur_end + 1,
7346 NULL, NULL);
7347 if (unlikely(!cur_end))
7348 return -1;
7349
7350 /* we have a full respnse and we know that we have either a CR
7351 * or an LF at <ptr>.
7352 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007353 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007354 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 /* there is no point trying this regex on headers */
7356 return 1;
7357 }
7358 }
7359 *cur_end = term; /* restore the string terminator */
7360 return done;
7361}
7362
7363
7364
7365/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007366 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7368 * unparsable response.
7369 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007370int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007372 struct http_txn *txn = &s->txn;
7373 struct hdr_exp *exp;
7374
7375 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 int ret;
7377
7378 /*
7379 * The interleaving of transformations and verdicts
7380 * makes it difficult to decide to continue or stop
7381 * the evaluation.
7382 */
7383
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007384 if (txn->flags & TX_SVDENY)
7385 break;
7386
Willy Tarreau3d300592007-03-18 18:34:41 +01007387 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7389 exp->action == ACT_PASS)) {
7390 exp = exp->next;
7391 continue;
7392 }
7393
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007394 /* if this filter had a condition, evaluate it now and skip to
7395 * next filter if the condition does not match.
7396 */
7397 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007398 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007399 ret = acl_pass(ret);
7400 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7401 ret = !ret;
7402 if (!ret)
7403 continue;
7404 }
7405
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007407 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 if (unlikely(ret < 0))
7409 return -1;
7410
7411 if (likely(ret == 0)) {
7412 /* The filter did not match the response, it can be
7413 * iterated through all headers.
7414 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007415 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417 }
7418 return 0;
7419}
7420
7421
Willy Tarreaua15645d2007-03-18 16:22:39 +01007422/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007423 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007424 * desirable to call it only when needed. This function is also used when we
7425 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007427void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007428{
7429 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007430 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007432 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007433 char *hdr_beg, *hdr_end, *hdr_next;
7434 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007435
Willy Tarreaua15645d2007-03-18 16:22:39 +01007436 /* Iterate through the headers.
7437 * we start with the start line.
7438 */
7439 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007440 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441
7442 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7443 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007444 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007445
7446 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007447 hdr_beg = hdr_next;
7448 hdr_end = hdr_beg + cur_hdr->len;
7449 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007450
Willy Tarreau24581ba2010-08-31 22:39:35 +02007451 /* We have one full header between hdr_beg and hdr_end, and the
7452 * next header starts at hdr_next. We're only interested in
7453 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007454 */
7455
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 is_cookie2 = 0;
7457 prev = hdr_beg + 10;
7458 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007459 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007460 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7461 if (!val) {
7462 old_idx = cur_idx;
7463 continue;
7464 }
7465 is_cookie2 = 1;
7466 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467 }
7468
Willy Tarreau24581ba2010-08-31 22:39:35 +02007469 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7470 * <prev> points to the colon.
7471 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007472 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7475 * check-cache is enabled) and we are not interested in checking
7476 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007477 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007478 if (t->be->cookie_name == NULL &&
7479 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007480 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007481 return;
7482
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 /* OK so now we know we have to process this response cookie.
7484 * The format of the Set-Cookie header is slightly different
7485 * from the format of the Cookie header in that it does not
7486 * support the comma as a cookie delimiter (thus the header
7487 * cannot be folded) because the Expires attribute described in
7488 * the original Netscape's spec may contain an unquoted date
7489 * with a comma inside. We have to live with this because
7490 * many browsers don't support Max-Age and some browsers don't
7491 * support quoted strings. However the Set-Cookie2 header is
7492 * clean.
7493 *
7494 * We have to keep multiple pointers in order to support cookie
7495 * removal at the beginning, middle or end of header without
7496 * corrupting the header (in case of set-cookie2). A special
7497 * pointer, <scav> points to the beginning of the set-cookie-av
7498 * fields after the first semi-colon. The <next> pointer points
7499 * either to the end of line (set-cookie) or next unquoted comma
7500 * (set-cookie2). All of these headers are valid :
7501 *
7502 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7503 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7504 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7505 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7506 * | | | | | | | | | |
7507 * | | | | | | | | +-> next hdr_end <--+
7508 * | | | | | | | +------------> scav
7509 * | | | | | | +--------------> val_end
7510 * | | | | | +--------------------> val_beg
7511 * | | | | +----------------------> equal
7512 * | | | +------------------------> att_end
7513 * | | +----------------------------> att_beg
7514 * | +------------------------------> prev
7515 * +-----------------------------------------> hdr_beg
7516 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007517
Willy Tarreau24581ba2010-08-31 22:39:35 +02007518 for (; prev < hdr_end; prev = next) {
7519 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 /* find att_beg */
7522 att_beg = prev + 1;
7523 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7524 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525
Willy Tarreau24581ba2010-08-31 22:39:35 +02007526 /* find att_end : this is the first character after the last non
7527 * space before the equal. It may be equal to hdr_end.
7528 */
7529 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530
Willy Tarreau24581ba2010-08-31 22:39:35 +02007531 while (equal < hdr_end) {
7532 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7533 break;
7534 if (http_is_spht[(unsigned char)*equal++])
7535 continue;
7536 att_end = equal;
7537 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007538
Willy Tarreau24581ba2010-08-31 22:39:35 +02007539 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7540 * is between <att_beg> and <equal>, both may be identical.
7541 */
7542
7543 /* look for end of cookie if there is an equal sign */
7544 if (equal < hdr_end && *equal == '=') {
7545 /* look for the beginning of the value */
7546 val_beg = equal + 1;
7547 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7548 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549
Willy Tarreau24581ba2010-08-31 22:39:35 +02007550 /* find the end of the value, respecting quotes */
7551 next = find_cookie_value_end(val_beg, hdr_end);
7552
7553 /* make val_end point to the first white space or delimitor after the value */
7554 val_end = next;
7555 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7556 val_end--;
7557 } else {
7558 /* <equal> points to next comma, semi-colon or EOL */
7559 val_beg = val_end = next = equal;
7560 }
7561
7562 if (next < hdr_end) {
7563 /* Set-Cookie2 supports multiple cookies, and <next> points to
7564 * a colon or semi-colon before the end. So skip all attr-value
7565 * pairs and look for the next comma. For Set-Cookie, since
7566 * commas are permitted in values, skip to the end.
7567 */
7568 if (is_cookie2)
7569 next = find_hdr_value_end(next, hdr_end);
7570 else
7571 next = hdr_end;
7572 }
7573
7574 /* Now everything is as on the diagram above */
7575
7576 /* Ignore cookies with no equal sign */
7577 if (equal == val_end)
7578 continue;
7579
7580 /* If there are spaces around the equal sign, we need to
7581 * strip them otherwise we'll get trouble for cookie captures,
7582 * or even for rewrites. Since this happens extremely rarely,
7583 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7586 int stripped_before = 0;
7587 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588
Willy Tarreau24581ba2010-08-31 22:39:35 +02007589 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007590 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007591 equal += stripped_before;
7592 val_beg += stripped_before;
7593 }
7594
7595 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007596 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007597 val_beg += stripped_after;
7598 stripped_before += stripped_after;
7599 }
7600
7601 val_end += stripped_before;
7602 next += stripped_before;
7603 hdr_end += stripped_before;
7604 hdr_next += stripped_before;
7605 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007606 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007607 }
7608
7609 /* First, let's see if we want to capture this cookie. We check
7610 * that we don't already have a server side cookie, because we
7611 * can only capture one. Also as an optimisation, we ignore
7612 * cookies shorter than the declared name.
7613 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007614 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007615 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007616 (val_end - att_beg >= t->fe->capture_namelen) &&
7617 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7618 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007619 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620 Alert("HTTP logging : out of memory.\n");
7621 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007622 else {
7623 if (log_len > t->fe->capture_len)
7624 log_len = t->fe->capture_len;
7625 memcpy(txn->srv_cookie, att_beg, log_len);
7626 txn->srv_cookie[log_len] = 0;
7627 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628 }
7629
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007630 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007632 if (!(t->flags & SN_IGNORE_PRST) &&
7633 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7634 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007635 /* assume passive cookie by default */
7636 txn->flags &= ~TX_SCK_MASK;
7637 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638
7639 /* If the cookie is in insert mode on a known server, we'll delete
7640 * this occurrence because we'll insert another one later.
7641 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 * a direct access.
7643 */
Willy Tarreau67402132012-05-31 20:40:20 +02007644 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007645 /* The "preserve" flag was set, we don't want to touch the
7646 * server's cookie.
7647 */
7648 }
Willy Tarreau67402132012-05-31 20:40:20 +02007649 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7650 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007651 /* this cookie must be deleted */
7652 if (*prev == ':' && next == hdr_end) {
7653 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007654 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007655 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7656 txn->hdr_idx.used--;
7657 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007658 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007659 hdr_next += delta;
7660 http_msg_move_end(&txn->rsp, delta);
7661 /* note: while both invalid now, <next> and <hdr_end>
7662 * are still equal, so the for() will stop as expected.
7663 */
7664 } else {
7665 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007666 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 next = prev;
7668 hdr_end += delta;
7669 hdr_next += delta;
7670 cur_hdr->len += delta;
7671 http_msg_move_end(&txn->rsp, delta);
7672 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007673 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007674 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007675 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 }
Willy Tarreau67402132012-05-31 20:40:20 +02007677 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007678 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 * with this server since we know it.
7680 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007681 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007682 next += delta;
7683 hdr_end += delta;
7684 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007686 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687
Willy Tarreauf1348312010-10-07 15:54:11 +02007688 txn->flags &= ~TX_SCK_MASK;
7689 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007691 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007693 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007695 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007696 next += delta;
7697 hdr_end += delta;
7698 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007699 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007700 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701
Willy Tarreau827aee92011-03-10 16:55:02 +01007702 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007703 txn->flags &= ~TX_SCK_MASK;
7704 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705 }
7706 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007707 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7708 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007709 int cmp_len, value_len;
7710 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007711
Cyril Bontéb21570a2009-11-29 20:04:48 +01007712 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007713 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7714 value_begin = att_beg + t->be->appsession_name_len;
7715 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007716 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007717 cmp_len = att_end - att_beg;
7718 value_begin = val_beg;
7719 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007720 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007721
Cyril Bonté17530c32010-04-06 21:11:10 +02007722 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007723 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7724 /* free a possibly previously allocated memory */
7725 pool_free2(apools.sessid, txn->sessid);
7726
Cyril Bontéb21570a2009-11-29 20:04:48 +01007727 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007728 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007729 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7730 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7731 return;
7732 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007733 memcpy(txn->sessid, value_begin, value_len);
7734 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007735 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007736 }
7737 /* that's done for this cookie, check the next one on the same
7738 * line when next != hdr_end (only if is_cookie2).
7739 */
7740 }
7741 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007742 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007743 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007744
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007745 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007746 appsess *asession = NULL;
7747 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007748 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007749 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007750 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007751 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7752 Alert("Not enough Memory process_srv():asession:calloc().\n");
7753 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7754 return;
7755 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007756 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7757
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007758 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7759 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7760 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007761 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007762 return;
7763 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007764 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007765 asession->sessid[t->be->appsession_len] = 0;
7766
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007767 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007768 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007769 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007770 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007771 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007772 return;
7773 }
7774 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007775 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007776
7777 asession->request_count = 0;
7778 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7779 }
7780
7781 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7782 asession->request_count++;
7783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784}
7785
7786
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787/*
7788 * Check if response is cacheable or not. Updates t->flags.
7789 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007790void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791{
7792 struct http_txn *txn = &t->txn;
7793 char *p1, *p2;
7794
7795 char *cur_ptr, *cur_end, *cur_next;
7796 int cur_idx;
7797
Willy Tarreau5df51872007-11-25 16:20:08 +01007798 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 return;
7800
7801 /* Iterate through the headers.
7802 * we start with the start line.
7803 */
7804 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007805 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007806
7807 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7808 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007809 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810
7811 cur_hdr = &txn->hdr_idx.v[cur_idx];
7812 cur_ptr = cur_next;
7813 cur_end = cur_ptr + cur_hdr->len;
7814 cur_next = cur_end + cur_hdr->cr + 1;
7815
7816 /* We have one full header between cur_ptr and cur_end, and the
7817 * next header starts at cur_next. We're only interested in
7818 * "Cookie:" headers.
7819 */
7820
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007821 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7822 if (val) {
7823 if ((cur_end - (cur_ptr + val) >= 8) &&
7824 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7825 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7826 return;
7827 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828 }
7829
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007830 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7831 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 continue;
7833
7834 /* OK, right now we know we have a cache-control header at cur_ptr */
7835
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007836 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837
7838 if (p1 >= cur_end) /* no more info */
7839 continue;
7840
7841 /* p1 is at the beginning of the value */
7842 p2 = p1;
7843
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007844 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 p2++;
7846
7847 /* we have a complete value between p1 and p2 */
7848 if (p2 < cur_end && *p2 == '=') {
7849 /* we have something of the form no-cache="set-cookie" */
7850 if ((cur_end - p1 >= 21) &&
7851 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7852 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007853 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007854 continue;
7855 }
7856
7857 /* OK, so we know that either p2 points to the end of string or to a comma */
7858 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7859 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7860 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7861 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007862 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007863 return;
7864 }
7865
7866 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007867 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868 continue;
7869 }
7870 }
7871}
7872
7873
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874/*
7875 * Try to retrieve a known appsession in the URI, then the associated server.
7876 * If the server is found, it's assigned to the session.
7877 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007878void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007879{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007880 char *end_params, *first_param, *cur_param, *next_param;
7881 char separator;
7882 int value_len;
7883
7884 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007885
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007886 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007887 (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 +01007888 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007889 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007890
Cyril Bontéb21570a2009-11-29 20:04:48 +01007891 first_param = NULL;
7892 switch (mode) {
7893 case PR_O2_AS_M_PP:
7894 first_param = memchr(begin, ';', len);
7895 break;
7896 case PR_O2_AS_M_QS:
7897 first_param = memchr(begin, '?', len);
7898 break;
7899 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007900
Cyril Bontéb21570a2009-11-29 20:04:48 +01007901 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007902 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007903 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007904
Cyril Bontéb21570a2009-11-29 20:04:48 +01007905 switch (mode) {
7906 case PR_O2_AS_M_PP:
7907 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7908 end_params = (char *) begin + len;
7909 }
7910 separator = ';';
7911 break;
7912 case PR_O2_AS_M_QS:
7913 end_params = (char *) begin + len;
7914 separator = '&';
7915 break;
7916 default:
7917 /* unknown mode, shouldn't happen */
7918 return;
7919 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007920
Cyril Bontéb21570a2009-11-29 20:04:48 +01007921 cur_param = next_param = end_params;
7922 while (cur_param > first_param) {
7923 cur_param--;
7924 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7925 /* let's see if this is the appsession parameter */
7926 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7927 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7928 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7929 /* Cool... it's the right one */
7930 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7931 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7932 if (value_len > 0) {
7933 manage_client_side_appsession(t, cur_param, value_len);
7934 }
7935 break;
7936 }
7937 next_param = cur_param;
7938 }
7939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007940#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007941 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007942 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007943#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007944}
7945
Willy Tarreaub2513902006-12-17 14:52:38 +01007946/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007947 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007948 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007949 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007950 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007951 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007952 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007953 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007954 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007955int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007956{
7957 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007958 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007959 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007960 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007961
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007962 if (!uri_auth)
7963 return 0;
7964
Cyril Bonté70be45d2010-10-12 00:14:35 +02007965 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007966 return 0;
7967
Willy Tarreau295a8372011-03-10 11:25:07 +01007968 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007969 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007970 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007972 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007973 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007974 return 0;
7975
Willy Tarreau3a215be2012-03-09 21:39:51 +01007976 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007977 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007978 return 0;
7979
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007980 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007981 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007982 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007983 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007984 break;
7985 }
7986 h++;
7987 }
7988
7989 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007990 h = uri + uri_auth->uri_len;
7991 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007992 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007993 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007994 break;
7995 }
7996 h++;
7997 }
7998 }
7999
Willy Tarreau3a215be2012-03-09 21:39:51 +01008000 h = uri + uri_auth->uri_len;
8001 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02008002 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01008003 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02008004 break;
8005 }
8006 h++;
8007 }
8008
Willy Tarreau3a215be2012-03-09 21:39:51 +01008009 h = uri + uri_auth->uri_len;
8010 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02008011 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02008012 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02008013 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02008014 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02008015 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
8016 if (strncmp(stat_status_codes[i], h, 4) == 0) {
8017 si->applet.ctx.stats.st_code = i;
8018 break;
8019 }
8020 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02008021 break;
8022 }
8023 h++;
8024 }
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02008025
8026 si->applet.ctx.stats.scope_str = 0;
8027 si->applet.ctx.stats.scope_len = 0;
8028 h = uri + uri_auth->uri_len;
8029 while (h <= uri + msg->sl.rq.u_l - 8) {
8030 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
8031 int itx = 0;
8032 const char *h2;
8033 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
8034 const char *err;
8035
8036 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
8037 h2 = h;
8038 si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p;
8039 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
8040 itx++;
8041 h++;
8042 }
8043
8044 if (itx > STAT_SCOPE_TXT_MAXLEN)
8045 itx = STAT_SCOPE_TXT_MAXLEN;
8046 si->applet.ctx.stats.scope_len = itx;
8047
8048 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
8049 memcpy(scope_txt, h2, itx);
8050 scope_txt[itx] = '\0';
8051 err = invalid_char(scope_txt);
8052 if (err) {
8053 /* bad char in search text => clear scope */
8054 si->applet.ctx.stats.scope_str = 0;
8055 si->applet.ctx.stats.scope_len = 0;
8056 }
8057 break;
8058 }
8059 h++;
8060 }
8061
8062
Willy Tarreaub2513902006-12-17 14:52:38 +01008063 return 1;
8064}
8065
Willy Tarreau4076a152009-04-02 15:18:36 +02008066/*
8067 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008068 * By default it tries to report the error position as msg->err_pos. However if
8069 * this one is not set, it will then report msg->next, which is the last known
8070 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008071 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008072 */
8073void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008074 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01008075 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008076{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008077 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008078 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008079
Willy Tarreau9b28e032012-10-12 23:49:43 +02008080 es->len = MIN(chn->buf->i, sizeof(es->buf));
8081 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008082 len1 = MIN(len1, es->len);
8083 len2 = es->len - len1; /* remaining data if buffer wraps */
8084
Willy Tarreau9b28e032012-10-12 23:49:43 +02008085 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008086 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008087 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008088
Willy Tarreau4076a152009-04-02 15:18:36 +02008089 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008090 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008091 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008092 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008093
Willy Tarreau4076a152009-04-02 15:18:36 +02008094 es->when = date; // user-visible date
8095 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008096 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008097 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008098 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01008099 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008100 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008101 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008102 es->s_flags = s->flags;
8103 es->t_flags = s->txn.flags;
8104 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008105 es->b_out = chn->buf->o;
8106 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008107 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008108 es->m_clen = msg->chunk_len;
8109 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008110}
Willy Tarreaub2513902006-12-17 14:52:38 +01008111
Willy Tarreau294c4732011-12-16 21:35:50 +01008112/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8113 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8114 * performed over the whole headers. Otherwise it must contain a valid header
8115 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8116 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8117 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8118 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008119 * -1. The value fetch stops at commas, so this function is suited for use with
8120 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008121 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008122 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008123unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008124 struct hdr_idx *idx, int occ,
8125 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008126{
Willy Tarreau294c4732011-12-16 21:35:50 +01008127 struct hdr_ctx local_ctx;
8128 char *ptr_hist[MAX_HDR_HISTORY];
8129 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008130 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008131 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008132
Willy Tarreau294c4732011-12-16 21:35:50 +01008133 if (!ctx) {
8134 local_ctx.idx = 0;
8135 ctx = &local_ctx;
8136 }
8137
Willy Tarreaubce70882009-09-07 11:51:47 +02008138 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008139 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008140 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008141 occ--;
8142 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008143 *vptr = ctx->line + ctx->val;
8144 *vlen = ctx->vlen;
8145 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008146 }
8147 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008148 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008149 }
8150
8151 /* negative occurrence, we scan all the list then walk back */
8152 if (-occ > MAX_HDR_HISTORY)
8153 return 0;
8154
Willy Tarreau294c4732011-12-16 21:35:50 +01008155 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008156 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008157 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8158 len_hist[hist_ptr] = ctx->vlen;
8159 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008160 hist_ptr = 0;
8161 found++;
8162 }
8163 if (-occ > found)
8164 return 0;
8165 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008166 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8167 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8168 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008169 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008170 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008171 if (hist_ptr >= MAX_HDR_HISTORY)
8172 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008173 *vptr = ptr_hist[hist_ptr];
8174 *vlen = len_hist[hist_ptr];
8175 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008176}
8177
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008178/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8179 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8180 * performed over the whole headers. Otherwise it must contain a valid header
8181 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8182 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8183 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8184 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8185 * -1. This function differs from http_get_hdr() in that it only returns full
8186 * line header values and does not stop at commas.
8187 * The return value is 0 if nothing was found, or non-zero otherwise.
8188 */
8189unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8190 struct hdr_idx *idx, int occ,
8191 struct hdr_ctx *ctx, char **vptr, int *vlen)
8192{
8193 struct hdr_ctx local_ctx;
8194 char *ptr_hist[MAX_HDR_HISTORY];
8195 int len_hist[MAX_HDR_HISTORY];
8196 unsigned int hist_ptr;
8197 int found;
8198
8199 if (!ctx) {
8200 local_ctx.idx = 0;
8201 ctx = &local_ctx;
8202 }
8203
8204 if (occ >= 0) {
8205 /* search from the beginning */
8206 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8207 occ--;
8208 if (occ <= 0) {
8209 *vptr = ctx->line + ctx->val;
8210 *vlen = ctx->vlen;
8211 return 1;
8212 }
8213 }
8214 return 0;
8215 }
8216
8217 /* negative occurrence, we scan all the list then walk back */
8218 if (-occ > MAX_HDR_HISTORY)
8219 return 0;
8220
8221 found = hist_ptr = 0;
8222 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8223 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8224 len_hist[hist_ptr] = ctx->vlen;
8225 if (++hist_ptr >= MAX_HDR_HISTORY)
8226 hist_ptr = 0;
8227 found++;
8228 }
8229 if (-occ > found)
8230 return 0;
8231 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8232 * find occurrence -occ, so we have to check [hist_ptr+occ].
8233 */
8234 hist_ptr += occ;
8235 if (hist_ptr >= MAX_HDR_HISTORY)
8236 hist_ptr -= MAX_HDR_HISTORY;
8237 *vptr = ptr_hist[hist_ptr];
8238 *vlen = len_hist[hist_ptr];
8239 return 1;
8240}
8241
Willy Tarreaubaaee002006-06-26 02:48:02 +02008242/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008243 * Print a debug line with a header. Always stop at the first CR or LF char,
8244 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8245 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008246 */
8247void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8248{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008249 int max;
8250 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01008251 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
8252 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008253
8254 for (max = 0; start + max < end; max++)
8255 if (start[max] == '\r' || start[max] == '\n')
8256 break;
8257
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008258 UBOUND(max, trash.size - trash.len - 3);
8259 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8260 trash.str[trash.len++] = '\n';
8261 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008262}
8263
Willy Tarreau0937bc42009-12-22 15:03:09 +01008264/*
8265 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8266 * the required fields are properly allocated and that we only need to (re)init
8267 * them. This should be used before processing any new request.
8268 */
8269void http_init_txn(struct session *s)
8270{
8271 struct http_txn *txn = &s->txn;
8272 struct proxy *fe = s->fe;
8273
8274 txn->flags = 0;
8275 txn->status = -1;
8276
William Lallemand5f232402012-04-05 18:02:55 +02008277 global.req_count++;
8278
Willy Tarreauf64d1412010-10-07 20:06:11 +02008279 txn->cookie_first_date = 0;
8280 txn->cookie_last_date = 0;
8281
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008282 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008283 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008284 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008285 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008286 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008287 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008288 txn->req.chunk_len = 0LL;
8289 txn->req.body_len = 0LL;
8290 txn->rsp.chunk_len = 0LL;
8291 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008292 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8293 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008294 txn->req.chn = s->req;
8295 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008296
8297 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008298
8299 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8300 if (fe->options2 & PR_O2_REQBUG_OK)
8301 txn->req.err_pos = -1; /* let buggy requests pass */
8302
Willy Tarreau46023632010-01-07 22:51:47 +01008303 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008304 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8305
Willy Tarreau46023632010-01-07 22:51:47 +01008306 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008307 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8308
8309 if (txn->hdr_idx.v)
8310 hdr_idx_init(&txn->hdr_idx);
8311}
8312
8313/* to be used at the end of a transaction */
8314void http_end_txn(struct session *s)
8315{
8316 struct http_txn *txn = &s->txn;
8317
8318 /* these ones will have been dynamically allocated */
8319 pool_free2(pool2_requri, txn->uri);
8320 pool_free2(pool2_capture, txn->cli_cookie);
8321 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008322 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008323 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008324
William Lallemanda73203e2012-03-12 12:48:57 +01008325 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008326 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008327 txn->uri = NULL;
8328 txn->srv_cookie = NULL;
8329 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008330
8331 if (txn->req.cap) {
8332 struct cap_hdr *h;
8333 for (h = s->fe->req_cap; h; h = h->next)
8334 pool_free2(h->pool, txn->req.cap[h->index]);
8335 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8336 }
8337
8338 if (txn->rsp.cap) {
8339 struct cap_hdr *h;
8340 for (h = s->fe->rsp_cap; h; h = h->next)
8341 pool_free2(h->pool, txn->rsp.cap[h->index]);
8342 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8343 }
8344
Willy Tarreau0937bc42009-12-22 15:03:09 +01008345}
8346
8347/* to be used at the end of a transaction to prepare a new one */
8348void http_reset_txn(struct session *s)
8349{
8350 http_end_txn(s);
8351 http_init_txn(s);
8352
8353 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008354 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008355 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008356 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008357 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008358 /* re-init store persistence */
8359 s->store_count = 0;
8360
Willy Tarreau0937bc42009-12-22 15:03:09 +01008361 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008362
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008363 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008364
Willy Tarreau739cfba2010-01-25 23:11:14 +01008365 /* We must trim any excess data from the response buffer, because we
8366 * may have blocked an invalid response from a server that we don't
8367 * want to accidentely forward once we disable the analysers, nor do
8368 * we want those data to come along with next response. A typical
8369 * example of such data would be from a buggy server responding to
8370 * a HEAD with some data, or sending more than the advertised
8371 * content-length.
8372 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008373 if (unlikely(s->rep->buf->i))
8374 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008375
Willy Tarreau0937bc42009-12-22 15:03:09 +01008376 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008377 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008378
Willy Tarreaud04e8582010-05-31 12:31:35 +02008379 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008380 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008381
8382 s->req->rex = TICK_ETERNITY;
8383 s->req->wex = TICK_ETERNITY;
8384 s->req->analyse_exp = TICK_ETERNITY;
8385 s->rep->rex = TICK_ETERNITY;
8386 s->rep->wex = TICK_ETERNITY;
8387 s->rep->analyse_exp = TICK_ETERNITY;
8388}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008389
Willy Tarreauff011f22011-01-06 17:51:27 +01008390void free_http_req_rules(struct list *r) {
8391 struct http_req_rule *tr, *pr;
8392
8393 list_for_each_entry_safe(pr, tr, r, list) {
8394 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008395 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008396 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008397
8398 free(pr);
8399 }
8400}
8401
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008402/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008403struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8404{
8405 struct http_req_rule *rule;
8406 int cur_arg;
8407
8408 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8409 if (!rule) {
8410 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008411 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008412 }
8413
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008414 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008415 rule->action = HTTP_REQ_ACT_ALLOW;
8416 cur_arg = 1;
8417 } else if (!strcmp(args[0], "deny")) {
8418 rule->action = HTTP_REQ_ACT_DENY;
8419 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008420 } else if (!strcmp(args[0], "tarpit")) {
8421 rule->action = HTTP_REQ_ACT_TARPIT;
8422 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008423 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008424 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008425 cur_arg = 1;
8426
8427 while(*args[cur_arg]) {
8428 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008429 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008430 cur_arg+=2;
8431 continue;
8432 } else
8433 break;
8434 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008435 } else if (!strcmp(args[0], "set-nice")) {
8436 rule->action = HTTP_REQ_ACT_SET_NICE;
8437 cur_arg = 1;
8438
8439 if (!*args[cur_arg] ||
8440 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8441 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8442 file, linenum, args[0]);
8443 goto out_err;
8444 }
8445 rule->arg.nice = atoi(args[cur_arg]);
8446 if (rule->arg.nice < -1024)
8447 rule->arg.nice = -1024;
8448 else if (rule->arg.nice > 1024)
8449 rule->arg.nice = 1024;
8450 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008451 } else if (!strcmp(args[0], "set-tos")) {
8452#ifdef IP_TOS
8453 char *err;
8454 rule->action = HTTP_REQ_ACT_SET_TOS;
8455 cur_arg = 1;
8456
8457 if (!*args[cur_arg] ||
8458 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8459 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8460 file, linenum, args[0]);
8461 goto out_err;
8462 }
8463
8464 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8465 if (err && *err != '\0') {
8466 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8467 file, linenum, err, args[0]);
8468 goto out_err;
8469 }
8470 cur_arg++;
8471#else
8472 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8473 goto out_err;
8474#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008475 } else if (!strcmp(args[0], "set-mark")) {
8476#ifdef SO_MARK
8477 char *err;
8478 rule->action = HTTP_REQ_ACT_SET_MARK;
8479 cur_arg = 1;
8480
8481 if (!*args[cur_arg] ||
8482 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8483 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8484 file, linenum, args[0]);
8485 goto out_err;
8486 }
8487
8488 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8489 if (err && *err != '\0') {
8490 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8491 file, linenum, err, args[0]);
8492 goto out_err;
8493 }
8494 cur_arg++;
8495 global.last_checks |= LSTCHK_NETADM;
8496#else
8497 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8498 goto out_err;
8499#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008500 } else if (!strcmp(args[0], "set-log-level")) {
8501 rule->action = HTTP_REQ_ACT_SET_LOGL;
8502 cur_arg = 1;
8503
8504 if (!*args[cur_arg] ||
8505 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8506 bad_log_level:
8507 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8508 file, linenum, args[0]);
8509 goto out_err;
8510 }
8511 if (strcmp(args[cur_arg], "silent") == 0)
8512 rule->arg.loglevel = -1;
8513 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8514 goto bad_log_level;
8515 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008516 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8517 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8518 cur_arg = 1;
8519
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008520 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8521 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008522 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8523 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008524 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008525 }
8526
8527 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8528 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8529 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008530
8531 proxy->conf.args.ctx = ARGC_HDR;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008532 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8533 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008534 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008535 } else if (strcmp(args[0], "redirect") == 0) {
8536 struct redirect_rule *redir;
8537 char *errmsg;
8538
8539 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8540 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8541 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8542 goto out_err;
8543 }
8544
8545 /* this redirect rule might already contain a parsed condition which
8546 * we'll pass to the http-request rule.
8547 */
8548 rule->action = HTTP_REQ_ACT_REDIR;
8549 rule->arg.redir = redir;
8550 rule->cond = redir->cond;
8551 redir->cond = NULL;
8552 cur_arg = 2;
8553 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008554 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008555 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008556 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008557 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008558 }
8559
8560 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8561 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008562 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008563
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008564 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8565 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8566 file, linenum, args[0], errmsg);
8567 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008568 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008569 }
8570 rule->cond = cond;
8571 }
8572 else if (*args[cur_arg]) {
8573 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8574 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8575 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008576 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008577 }
8578
8579 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008580 out_err:
8581 free(rule);
8582 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008583}
8584
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008585/* parse an "http-respose" rule */
8586struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8587{
8588 struct http_res_rule *rule;
8589 int cur_arg;
8590
8591 rule = calloc(1, sizeof(*rule));
8592 if (!rule) {
8593 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8594 goto out_err;
8595 }
8596
8597 if (!strcmp(args[0], "allow")) {
8598 rule->action = HTTP_RES_ACT_ALLOW;
8599 cur_arg = 1;
8600 } else if (!strcmp(args[0], "deny")) {
8601 rule->action = HTTP_RES_ACT_DENY;
8602 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008603 } else if (!strcmp(args[0], "set-nice")) {
8604 rule->action = HTTP_RES_ACT_SET_NICE;
8605 cur_arg = 1;
8606
8607 if (!*args[cur_arg] ||
8608 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8609 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8610 file, linenum, args[0]);
8611 goto out_err;
8612 }
8613 rule->arg.nice = atoi(args[cur_arg]);
8614 if (rule->arg.nice < -1024)
8615 rule->arg.nice = -1024;
8616 else if (rule->arg.nice > 1024)
8617 rule->arg.nice = 1024;
8618 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008619 } else if (!strcmp(args[0], "set-tos")) {
8620#ifdef IP_TOS
8621 char *err;
8622 rule->action = HTTP_RES_ACT_SET_TOS;
8623 cur_arg = 1;
8624
8625 if (!*args[cur_arg] ||
8626 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8627 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8628 file, linenum, args[0]);
8629 goto out_err;
8630 }
8631
8632 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8633 if (err && *err != '\0') {
8634 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8635 file, linenum, err, args[0]);
8636 goto out_err;
8637 }
8638 cur_arg++;
8639#else
8640 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8641 goto out_err;
8642#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008643 } else if (!strcmp(args[0], "set-mark")) {
8644#ifdef SO_MARK
8645 char *err;
8646 rule->action = HTTP_RES_ACT_SET_MARK;
8647 cur_arg = 1;
8648
8649 if (!*args[cur_arg] ||
8650 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8651 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8652 file, linenum, args[0]);
8653 goto out_err;
8654 }
8655
8656 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8657 if (err && *err != '\0') {
8658 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8659 file, linenum, err, args[0]);
8660 goto out_err;
8661 }
8662 cur_arg++;
8663 global.last_checks |= LSTCHK_NETADM;
8664#else
8665 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8666 goto out_err;
8667#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008668 } else if (!strcmp(args[0], "set-log-level")) {
8669 rule->action = HTTP_RES_ACT_SET_LOGL;
8670 cur_arg = 1;
8671
8672 if (!*args[cur_arg] ||
8673 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8674 bad_log_level:
8675 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8676 file, linenum, args[0]);
8677 goto out_err;
8678 }
8679 if (strcmp(args[cur_arg], "silent") == 0)
8680 rule->arg.loglevel = -1;
8681 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8682 goto bad_log_level;
8683 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008684 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8685 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8686 cur_arg = 1;
8687
8688 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8689 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8690 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8691 file, linenum, args[0]);
8692 goto out_err;
8693 }
8694
8695 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8696 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8697 LIST_INIT(&rule->arg.hdr_add.fmt);
8698
8699 proxy->conf.args.ctx = ARGC_HDR;
8700 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8701 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8702 cur_arg += 2;
8703 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008704 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008705 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8706 goto out_err;
8707 }
8708
8709 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8710 struct acl_cond *cond;
8711 char *errmsg = NULL;
8712
8713 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8714 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8715 file, linenum, args[0], errmsg);
8716 free(errmsg);
8717 goto out_err;
8718 }
8719 rule->cond = cond;
8720 }
8721 else if (*args[cur_arg]) {
8722 Alert("parsing [%s:%d]: 'http-response %s' expects"
8723 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8724 file, linenum, args[0], args[cur_arg]);
8725 goto out_err;
8726 }
8727
8728 return rule;
8729 out_err:
8730 free(rule);
8731 return NULL;
8732}
8733
Willy Tarreau4baae242012-12-27 12:00:31 +01008734/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8735 * with <err> filled with the error message.
8736 */
8737struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8738 const char **args, char **errmsg)
8739{
8740 struct redirect_rule *rule;
8741 int cur_arg;
8742 int type = REDIRECT_TYPE_NONE;
8743 int code = 302;
8744 const char *destination = NULL;
8745 const char *cookie = NULL;
8746 int cookie_set = 0;
8747 unsigned int flags = REDIRECT_FLAG_NONE;
8748 struct acl_cond *cond = NULL;
8749
8750 cur_arg = 0;
8751 while (*(args[cur_arg])) {
8752 if (strcmp(args[cur_arg], "location") == 0) {
8753 if (!*args[cur_arg + 1])
8754 goto missing_arg;
8755
8756 type = REDIRECT_TYPE_LOCATION;
8757 cur_arg++;
8758 destination = args[cur_arg];
8759 }
8760 else if (strcmp(args[cur_arg], "prefix") == 0) {
8761 if (!*args[cur_arg + 1])
8762 goto missing_arg;
8763
8764 type = REDIRECT_TYPE_PREFIX;
8765 cur_arg++;
8766 destination = args[cur_arg];
8767 }
8768 else if (strcmp(args[cur_arg], "scheme") == 0) {
8769 if (!*args[cur_arg + 1])
8770 goto missing_arg;
8771
8772 type = REDIRECT_TYPE_SCHEME;
8773 cur_arg++;
8774 destination = args[cur_arg];
8775 }
8776 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8777 if (!*args[cur_arg + 1])
8778 goto missing_arg;
8779
8780 cur_arg++;
8781 cookie = args[cur_arg];
8782 cookie_set = 1;
8783 }
8784 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8785 if (!*args[cur_arg + 1])
8786 goto missing_arg;
8787
8788 cur_arg++;
8789 cookie = args[cur_arg];
8790 cookie_set = 0;
8791 }
8792 else if (strcmp(args[cur_arg], "code") == 0) {
8793 if (!*args[cur_arg + 1])
8794 goto missing_arg;
8795
8796 cur_arg++;
8797 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008798 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008799 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008800 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008801 args[cur_arg - 1], args[cur_arg]);
8802 return NULL;
8803 }
8804 }
8805 else if (!strcmp(args[cur_arg],"drop-query")) {
8806 flags |= REDIRECT_FLAG_DROP_QS;
8807 }
8808 else if (!strcmp(args[cur_arg],"append-slash")) {
8809 flags |= REDIRECT_FLAG_APPEND_SLASH;
8810 }
8811 else if (strcmp(args[cur_arg], "if") == 0 ||
8812 strcmp(args[cur_arg], "unless") == 0) {
8813 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8814 if (!cond) {
8815 memprintf(errmsg, "error in condition: %s", *errmsg);
8816 return NULL;
8817 }
8818 break;
8819 }
8820 else {
8821 memprintf(errmsg,
8822 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8823 args[cur_arg]);
8824 return NULL;
8825 }
8826 cur_arg++;
8827 }
8828
8829 if (type == REDIRECT_TYPE_NONE) {
8830 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8831 return NULL;
8832 }
8833
8834 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8835 rule->cond = cond;
8836 rule->rdr_str = strdup(destination);
8837 rule->rdr_len = strlen(destination);
8838 if (cookie) {
8839 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8840 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8841 */
8842 rule->cookie_len = strlen(cookie);
8843 if (cookie_set) {
8844 rule->cookie_str = malloc(rule->cookie_len + 10);
8845 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8846 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8847 rule->cookie_len += 9;
8848 } else {
8849 rule->cookie_str = malloc(rule->cookie_len + 21);
8850 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8851 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8852 rule->cookie_len += 20;
8853 }
8854 }
8855 rule->type = type;
8856 rule->code = code;
8857 rule->flags = flags;
8858 LIST_INIT(&rule->list);
8859 return rule;
8860
8861 missing_arg:
8862 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8863 return NULL;
8864}
8865
Willy Tarreau8797c062007-05-07 00:55:35 +02008866/************************************************************************/
8867/* The code below is dedicated to ACL parsing and matching */
8868/************************************************************************/
8869
8870
Willy Tarreau14174bc2012-04-16 14:34:04 +02008871/* This function ensures that the prerequisites for an L7 fetch are ready,
8872 * which means that a request or response is ready. If some data is missing,
8873 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008874 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8875 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008876 *
8877 * The function returns :
8878 * 0 if some data is missing or if the requested data cannot be fetched
8879 * -1 if it is certain that we'll never have any HTTP message there
8880 * 1 if an HTTP message is ready
8881 */
8882static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008883acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008884 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008885{
8886 struct http_txn *txn = l7;
8887 struct http_msg *msg = &txn->req;
8888
8889 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8890 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8891 */
8892
8893 if (unlikely(!s || !txn))
8894 return 0;
8895
8896 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008897 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008898
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008899 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008900 if (unlikely(!s->req))
8901 return 0;
8902
Willy Tarreauaae75e32013-03-29 12:31:49 +01008903 /* If the buffer does not leave enough free space at the end,
8904 * we must first realign it.
8905 */
8906 if (s->req->buf->p > s->req->buf->data &&
8907 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8908 buffer_slow_realign(s->req->buf);
8909
Willy Tarreau14174bc2012-04-16 14:34:04 +02008910 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008911 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008912 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008913 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008914 return -1;
8915 }
8916
8917 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008918 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008919 http_msg_analyzer(msg, &txn->hdr_idx);
8920
8921 /* Still no valid request ? */
8922 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008923 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008924 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008925 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008926 return -1;
8927 }
8928 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008929 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008930 return 0;
8931 }
8932
8933 /* OK we just got a valid HTTP request. We have some minor
8934 * preparation to perform so that further checks can rely
8935 * on HTTP tests.
8936 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008937
8938 /* If the request was parsed but was too large, we must absolutely
8939 * return an error so that it is not processed. At the moment this
8940 * cannot happen, but if the parsers are to change in the future,
8941 * we want this check to be maintained.
8942 */
8943 if (unlikely(s->req->buf->i + s->req->buf->p >
8944 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8945 msg->msg_state = HTTP_MSG_ERROR;
8946 return 1;
8947 }
8948
Willy Tarreau9b28e032012-10-12 23:49:43 +02008949 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008950 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8951 s->flags |= SN_REDIRECTABLE;
8952
8953 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008954 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008955 return -1;
8956 }
8957 }
8958
Willy Tarreau24e32d82012-04-23 23:55:44 +02008959 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008960 return 0; /* data might have moved and indexes changed */
8961
8962 /* otherwise everything's ready for the request */
8963 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008964 else {
8965 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008966 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8967 return 0;
8968 }
8969
8970 /* everything's OK */
8971 return 1;
8972}
Willy Tarreau8797c062007-05-07 00:55:35 +02008973
Willy Tarreauc0239e02012-04-16 14:42:55 +02008974#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008975 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 +02008976
Willy Tarreau24e32d82012-04-23 23:55:44 +02008977#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008978 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 +02008979
Willy Tarreau8797c062007-05-07 00:55:35 +02008980
8981/* 1. Check on METHOD
8982 * We use the pre-parsed method if it is known, and store its number as an
8983 * integer. If it is unknown, we use the pointer and the length.
8984 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008985static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008986{
8987 int len, meth;
8988
Willy Tarreauae8b7962007-06-09 23:10:04 +02008989 len = strlen(*text);
8990 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008991
8992 pattern->val.i = meth;
8993 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008994 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008995 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008996 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008997 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008998 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008999 pattern->len = len;
9000 }
9001 return 1;
9002}
9003
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009004/* This function fetches the method of current HTTP request and stores
9005 * it in the global pattern struct as a chunk. There are two possibilities :
9006 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9007 * in <len> and <ptr> is NULL ;
9008 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9009 * <len> to its length.
9010 * This is intended to be used with acl_match_meth() only.
9011 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009012static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009013smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009014 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02009015{
9016 int meth;
9017 struct http_txn *txn = l7;
9018
Willy Tarreau24e32d82012-04-23 23:55:44 +02009019 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009020
Willy Tarreau8797c062007-05-07 00:55:35 +02009021 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02009022 smp->type = SMP_T_UINT;
9023 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009024 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009025 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9026 /* ensure the indexes are not affected */
9027 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009028 smp->type = SMP_T_CSTR;
9029 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009030 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009031 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009032 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009033 return 1;
9034}
9035
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009036/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02009037static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02009038{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009039 int icase;
9040
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009041
Willy Tarreauf853c462012-04-23 18:53:56 +02009042 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009043 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009044 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009045 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02009046 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009047 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009048
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009049 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9050 if (pattern->val.i != HTTP_METH_OTHER)
9051 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009052
9053 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009054 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02009055 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009056
9057 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009058 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9059 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02009060 return ACL_PAT_FAIL;
9061 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02009062}
9063
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009064static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009065smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009066 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02009067{
9068 struct http_txn *txn = l7;
9069 char *ptr;
9070 int len;
9071
Willy Tarreauc0239e02012-04-16 14:42:55 +02009072 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009073
Willy Tarreau8797c062007-05-07 00:55:35 +02009074 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009075 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009076
9077 while ((len-- > 0) && (*ptr++ != '/'));
9078 if (len <= 0)
9079 return 0;
9080
Willy Tarreauf853c462012-04-23 18:53:56 +02009081 smp->type = SMP_T_CSTR;
9082 smp->data.str.str = ptr;
9083 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009084
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009085 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009086 return 1;
9087}
9088
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009089static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009090smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009091 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02009092{
9093 struct http_txn *txn = l7;
9094 char *ptr;
9095 int len;
9096
Willy Tarreauc0239e02012-04-16 14:42:55 +02009097 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009098
Willy Tarreauf26b2522012-12-14 08:33:14 +01009099 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9100 return 0;
9101
Willy Tarreau8797c062007-05-07 00:55:35 +02009102 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009103 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009104
9105 while ((len-- > 0) && (*ptr++ != '/'));
9106 if (len <= 0)
9107 return 0;
9108
Willy Tarreauf853c462012-04-23 18:53:56 +02009109 smp->type = SMP_T_CSTR;
9110 smp->data.str.str = ptr;
9111 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009112
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009113 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009114 return 1;
9115}
9116
9117/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009118static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009119smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009120 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02009121{
9122 struct http_txn *txn = l7;
9123 char *ptr;
9124 int len;
9125
Willy Tarreauc0239e02012-04-16 14:42:55 +02009126 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009127
Willy Tarreauf26b2522012-12-14 08:33:14 +01009128 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9129 return 0;
9130
Willy Tarreau8797c062007-05-07 00:55:35 +02009131 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009132 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009133
Willy Tarreauf853c462012-04-23 18:53:56 +02009134 smp->type = SMP_T_UINT;
9135 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009136 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009137 return 1;
9138}
9139
9140/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009141static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009142smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009143 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02009144{
9145 struct http_txn *txn = l7;
9146
Willy Tarreauc0239e02012-04-16 14:42:55 +02009147 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009148
Willy Tarreauf853c462012-04-23 18:53:56 +02009149 smp->type = SMP_T_CSTR;
9150 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009151 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009152 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009153 return 1;
9154}
9155
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009156static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009157smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009158 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009159{
9160 struct http_txn *txn = l7;
9161
Willy Tarreauc0239e02012-04-16 14:42:55 +02009162 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009163
9164 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009165 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
9166 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009167 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009168 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009169 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009170
9171 /*
9172 * If we are parsing url in frontend space, we prepare backend stage
9173 * to not parse again the same url ! optimization lazyness...
9174 */
9175 if (px->options & PR_O_HTTP_PROXY)
9176 l4->flags |= SN_ADDR_SET;
9177
Willy Tarreau37406352012-04-23 16:16:37 +02009178 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009179 return 1;
9180}
9181
9182static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009183smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009184 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009185{
9186 struct http_txn *txn = l7;
9187
Willy Tarreauc0239e02012-04-16 14:42:55 +02009188 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009189
9190 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009191 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 +02009192 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009193 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009194
9195 if (px->options & PR_O_HTTP_PROXY)
9196 l4->flags |= SN_ADDR_SET;
9197
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009198 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009199 return 1;
9200}
9201
Willy Tarreau185b5c42012-04-26 15:11:51 +02009202/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9203 * Accepts an optional argument of type string containing the header field name,
9204 * and an optional argument of type signed or unsigned integer to request an
9205 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009206 * headers are considered from the first one. It does not stop on commas and
9207 * returns full lines instead (useful for User-Agent or Date for example).
9208 */
9209static int
9210smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9211 const struct arg *args, struct sample *smp)
9212{
9213 struct http_txn *txn = l7;
9214 struct hdr_idx *idx = &txn->hdr_idx;
9215 struct hdr_ctx *ctx = smp->ctx.a[0];
9216 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9217 int occ = 0;
9218 const char *name_str = NULL;
9219 int name_len = 0;
9220
9221 if (!ctx) {
9222 /* first call */
9223 ctx = &static_hdr_ctx;
9224 ctx->idx = 0;
9225 smp->ctx.a[0] = ctx;
9226 }
9227
9228 if (args) {
9229 if (args[0].type != ARGT_STR)
9230 return 0;
9231 name_str = args[0].data.str.str;
9232 name_len = args[0].data.str.len;
9233
9234 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9235 occ = args[1].data.uint;
9236 }
9237
9238 CHECK_HTTP_MESSAGE_FIRST();
9239
9240 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9241 /* search for header from the beginning */
9242 ctx->idx = 0;
9243
9244 if (!occ && !(opt & SMP_OPT_ITERATE))
9245 /* no explicit occurrence and single fetch => last header by default */
9246 occ = -1;
9247
9248 if (!occ)
9249 /* prepare to report multiple occurrences for ACL fetches */
9250 smp->flags |= SMP_F_NOT_LAST;
9251
9252 smp->type = SMP_T_CSTR;
9253 smp->flags |= SMP_F_VOL_HDR;
9254 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9255 return 1;
9256
9257 smp->flags &= ~SMP_F_NOT_LAST;
9258 return 0;
9259}
9260
9261/* 6. Check on HTTP header count. The number of occurrences is returned.
9262 * Accepts exactly 1 argument of type string. It does not stop on commas and
9263 * returns full lines instead (useful for User-Agent or Date for example).
9264 */
9265static int
9266smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9267 const struct arg *args, struct sample *smp)
9268{
9269 struct http_txn *txn = l7;
9270 struct hdr_idx *idx = &txn->hdr_idx;
9271 struct hdr_ctx ctx;
9272 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9273 int cnt;
9274
9275 if (!args || args->type != ARGT_STR)
9276 return 0;
9277
9278 CHECK_HTTP_MESSAGE_FIRST();
9279
9280 ctx.idx = 0;
9281 cnt = 0;
9282 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9283 cnt++;
9284
9285 smp->type = SMP_T_UINT;
9286 smp->data.uint = cnt;
9287 smp->flags = SMP_F_VOL_HDR;
9288 return 1;
9289}
9290
9291/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9292 * Accepts an optional argument of type string containing the header field name,
9293 * and an optional argument of type signed or unsigned integer to request an
9294 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009295 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009296 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009297static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009298smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009299 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009300{
9301 struct http_txn *txn = l7;
9302 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009303 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009304 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009305 int occ = 0;
9306 const char *name_str = NULL;
9307 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009308
Willy Tarreaua890d072013-04-02 12:01:06 +02009309 if (!ctx) {
9310 /* first call */
9311 ctx = &static_hdr_ctx;
9312 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009313 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009314 }
9315
Willy Tarreau185b5c42012-04-26 15:11:51 +02009316 if (args) {
9317 if (args[0].type != ARGT_STR)
9318 return 0;
9319 name_str = args[0].data.str.str;
9320 name_len = args[0].data.str.len;
9321
9322 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9323 occ = args[1].data.uint;
9324 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009325
Willy Tarreaue333ec92012-04-16 16:26:40 +02009326 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009327
Willy Tarreau185b5c42012-04-26 15:11:51 +02009328 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009329 /* search for header from the beginning */
9330 ctx->idx = 0;
9331
Willy Tarreau185b5c42012-04-26 15:11:51 +02009332 if (!occ && !(opt & SMP_OPT_ITERATE))
9333 /* no explicit occurrence and single fetch => last header by default */
9334 occ = -1;
9335
9336 if (!occ)
9337 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009338 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009339
Willy Tarreau185b5c42012-04-26 15:11:51 +02009340 smp->type = SMP_T_CSTR;
9341 smp->flags |= SMP_F_VOL_HDR;
9342 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 +02009343 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009344
Willy Tarreau37406352012-04-23 16:16:37 +02009345 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009346 return 0;
9347}
9348
Willy Tarreauc11416f2007-06-17 16:58:38 +02009349/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009350 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009351 */
9352static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009353smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009354 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009355{
9356 struct http_txn *txn = l7;
9357 struct hdr_idx *idx = &txn->hdr_idx;
9358 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009359 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009360 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009361
Willy Tarreau24e32d82012-04-23 23:55:44 +02009362 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009363 return 0;
9364
Willy Tarreaue333ec92012-04-16 16:26:40 +02009365 CHECK_HTTP_MESSAGE_FIRST();
9366
Willy Tarreau33a7e692007-06-10 19:45:56 +02009367 ctx.idx = 0;
9368 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009369 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 +02009370 cnt++;
9371
Willy Tarreauf853c462012-04-23 18:53:56 +02009372 smp->type = SMP_T_UINT;
9373 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009374 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009375 return 1;
9376}
9377
Willy Tarreau185b5c42012-04-26 15:11:51 +02009378/* Fetch an HTTP header's integer value. The integer value is returned. It
9379 * takes a mandatory argument of type string and an optional one of type int
9380 * to designate a specific occurrence. It returns an unsigned integer, which
9381 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009382 */
9383static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009384smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009385 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009386{
Willy Tarreau185b5c42012-04-26 15:11:51 +02009387 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009388
Willy Tarreauf853c462012-04-23 18:53:56 +02009389 if (ret > 0) {
9390 smp->type = SMP_T_UINT;
9391 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9392 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009393
Willy Tarreaud53e2422012-04-16 17:21:11 +02009394 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009395}
9396
Cyril Bonté69fa9922012-10-25 00:01:06 +02009397/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9398 * and an optional one of type int to designate a specific occurrence.
9399 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009400 */
9401static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009402smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009403 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02009404{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009405 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009406
Willy Tarreau185b5c42012-04-26 15:11:51 +02009407 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009408 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9409 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009410 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009411 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009412 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009413 if (smp->data.str.len < temp->size - 1) {
9414 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9415 temp->str[smp->data.str.len] = '\0';
9416 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9417 smp->type = SMP_T_IPV6;
9418 break;
9419 }
9420 }
9421 }
9422
Willy Tarreaud53e2422012-04-16 17:21:11 +02009423 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009424 if (!(smp->flags & SMP_F_NOT_LAST))
9425 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009426 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009427 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009428}
9429
Willy Tarreau737b0c12007-06-10 21:28:46 +02009430/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9431 * the first '/' after the possible hostname, and ends before the possible '?'.
9432 */
9433static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009434smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009435 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009436{
9437 struct http_txn *txn = l7;
9438 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009439
Willy Tarreauc0239e02012-04-16 14:42:55 +02009440 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009441
Willy Tarreau9b28e032012-10-12 23:49:43 +02009442 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009443 ptr = http_get_path(txn);
9444 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009445 return 0;
9446
9447 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009448 smp->type = SMP_T_CSTR;
9449 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009450
9451 while (ptr < end && *ptr != '?')
9452 ptr++;
9453
Willy Tarreauf853c462012-04-23 18:53:56 +02009454 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009455 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009456 return 1;
9457}
9458
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009459/* This produces a concatenation of the first occurrence of the Host header
9460 * followed by the path component if it begins with a slash ('/'). This means
9461 * that '*' will not be added, resulting in exactly the first Host entry.
9462 * If no Host header is found, then the path is returned as-is. The returned
9463 * value is stored in the trash so it does not need to be marked constant.
9464 */
9465static int
9466smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9467 const struct arg *args, struct sample *smp)
9468{
9469 struct http_txn *txn = l7;
9470 char *ptr, *end, *beg;
9471 struct hdr_ctx ctx;
9472
9473 CHECK_HTTP_MESSAGE_FIRST();
9474
9475 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009476 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 +02009477 !ctx.vlen)
9478 return smp_fetch_path(px, l4, l7, opt, args, smp);
9479
9480 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009481 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009482 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009483 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009484 smp->data.str.len = ctx.vlen;
9485
9486 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009487 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 +02009488 beg = http_get_path(txn);
9489 if (!beg)
9490 beg = end;
9491
9492 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9493
9494 if (beg < ptr && *beg == '/') {
9495 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9496 smp->data.str.len += ptr - beg;
9497 }
9498
9499 smp->flags = SMP_F_VOL_1ST;
9500 return 1;
9501}
9502
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009503/* This produces a 32-bit hash of the concatenation of the first occurrence of
9504 * the Host header followed by the path component if it begins with a slash ('/').
9505 * This means that '*' will not be added, resulting in exactly the first Host
9506 * entry. If no Host header is found, then the path is used. The resulting value
9507 * is hashed using the url hash followed by a full avalanche hash and provides a
9508 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
9509 * high-traffic sites without having to store whole paths.
9510 */
9511static int
9512smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9513 const struct arg *args, struct sample *smp)
9514{
9515 struct http_txn *txn = l7;
9516 struct hdr_ctx ctx;
9517 unsigned int hash = 0;
9518 char *ptr, *beg, *end;
9519 int len;
9520
9521 CHECK_HTTP_MESSAGE_FIRST();
9522
9523 ctx.idx = 0;
9524 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9525 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9526 ptr = ctx.line + ctx.val;
9527 len = ctx.vlen;
9528 while (len--)
9529 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9530 }
9531
9532 /* now retrieve the path */
9533 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9534 beg = http_get_path(txn);
9535 if (!beg)
9536 beg = end;
9537
9538 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9539
9540 if (beg < ptr && *beg == '/') {
9541 while (beg < ptr)
9542 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9543 }
9544 hash = full_hash(hash);
9545
9546 smp->type = SMP_T_UINT;
9547 smp->data.uint = hash;
9548 smp->flags = SMP_F_VOL_1ST;
9549 return 1;
9550}
9551
Willy Tarreau4a550602012-12-09 14:53:32 +01009552/* This concatenates the source address with the 32-bit hash of the Host and
9553 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
9554 * per-url counters. The result is a binary block from 8 to 20 bytes depending
9555 * on the source address length. The URL hash is stored before the address so
9556 * that in environments where IPv6 is insignificant, truncating the output to
9557 * 8 bytes would still work.
9558 */
9559static int
9560smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9561 const struct arg *args, struct sample *smp)
9562{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009563 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01009564
9565 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
9566 return 0;
9567
Willy Tarreau47ca5452012-12-23 20:22:19 +01009568 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009569 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9570 temp->len += sizeof(smp->data.uint);
9571
9572 switch (l4->si[0].conn->addr.from.ss_family) {
9573 case AF_INET:
9574 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
9575 temp->len += 4;
9576 break;
9577 case AF_INET6:
9578 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
9579 temp->len += 16;
9580 break;
9581 default:
9582 return 0;
9583 }
9584
9585 smp->data.str = *temp;
9586 smp->type = SMP_T_BIN;
9587 return 1;
9588}
9589
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009590static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009591smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009592 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009593{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009594 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9595 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9596 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009597
Willy Tarreau24e32d82012-04-23 23:55:44 +02009598 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009599
Willy Tarreauf853c462012-04-23 18:53:56 +02009600 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009601 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009602 return 1;
9603}
9604
Willy Tarreau7f18e522010-10-22 20:04:13 +02009605/* return a valid test if the current request is the first one on the connection */
9606static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009607smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009608 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009609{
9610 if (!s)
9611 return 0;
9612
Willy Tarreauf853c462012-04-23 18:53:56 +02009613 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009614 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009615 return 1;
9616}
9617
Willy Tarreau34db1082012-04-19 17:16:54 +02009618/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009619static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009620smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009621 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009622{
9623
Willy Tarreau24e32d82012-04-23 23:55:44 +02009624 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009625 return 0;
9626
Willy Tarreauc0239e02012-04-16 14:42:55 +02009627 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009628
Willy Tarreauc0239e02012-04-16 14:42:55 +02009629 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009630 return 0;
9631
Willy Tarreauf853c462012-04-23 18:53:56 +02009632 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009633 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 +01009634 return 1;
9635}
Willy Tarreau8797c062007-05-07 00:55:35 +02009636
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009637/* Accepts exactly 1 argument of type userlist */
9638static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009639smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009640 const struct arg *args, struct sample *smp)
9641{
9642
9643 if (!args || args->type != ARGT_USR)
9644 return 0;
9645
9646 CHECK_HTTP_MESSAGE_FIRST();
9647
9648 if (!get_http_auth(l4))
9649 return 0;
9650
9651 /* acl_match_auth() will need several information at once */
9652 smp->ctx.a[0] = args->data.usr; /* user list */
9653 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9654 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9655
9656 /* if the user does not belong to the userlist or has a wrong password,
9657 * report that it unconditionally does not match. Otherwise we return
9658 * a non-zero integer which will be ignored anyway since all the params
9659 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
9660 */
9661 smp->type = SMP_T_BOOL;
9662 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9663 if (smp->data.uint)
9664 smp->type = SMP_T_UINT;
9665
9666 return 1;
9667}
9668
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009669/* Try to find the next occurrence of a cookie name in a cookie header value.
9670 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9671 * the cookie value is returned into *value and *value_l, and the function
9672 * returns a pointer to the next pointer to search from if the value was found.
9673 * Otherwise if the cookie was not found, NULL is returned and neither value
9674 * nor value_l are touched. The input <hdr> string should first point to the
9675 * header's value, and the <hdr_end> pointer must point to the first character
9676 * not part of the value. <list> must be non-zero if value may represent a list
9677 * of values (cookie headers). This makes it faster to abort parsing when no
9678 * list is expected.
9679 */
9680static char *
9681extract_cookie_value(char *hdr, const char *hdr_end,
9682 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009683 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009684{
9685 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9686 char *next;
9687
9688 /* we search at least a cookie name followed by an equal, and more
9689 * generally something like this :
9690 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9691 */
9692 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9693 /* Iterate through all cookies on this line */
9694
9695 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9696 att_beg++;
9697
9698 /* find att_end : this is the first character after the last non
9699 * space before the equal. It may be equal to hdr_end.
9700 */
9701 equal = att_end = att_beg;
9702
9703 while (equal < hdr_end) {
9704 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9705 break;
9706 if (http_is_spht[(unsigned char)*equal++])
9707 continue;
9708 att_end = equal;
9709 }
9710
9711 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9712 * is between <att_beg> and <equal>, both may be identical.
9713 */
9714
9715 /* look for end of cookie if there is an equal sign */
9716 if (equal < hdr_end && *equal == '=') {
9717 /* look for the beginning of the value */
9718 val_beg = equal + 1;
9719 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9720 val_beg++;
9721
9722 /* find the end of the value, respecting quotes */
9723 next = find_cookie_value_end(val_beg, hdr_end);
9724
9725 /* make val_end point to the first white space or delimitor after the value */
9726 val_end = next;
9727 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9728 val_end--;
9729 } else {
9730 val_beg = val_end = next = equal;
9731 }
9732
9733 /* We have nothing to do with attributes beginning with '$'. However,
9734 * they will automatically be removed if a header before them is removed,
9735 * since they're supposed to be linked together.
9736 */
9737 if (*att_beg == '$')
9738 continue;
9739
9740 /* Ignore cookies with no equal sign */
9741 if (equal == next)
9742 continue;
9743
9744 /* Now we have the cookie name between att_beg and att_end, and
9745 * its value between val_beg and val_end.
9746 */
9747
9748 if (att_end - att_beg == cookie_name_l &&
9749 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9750 /* let's return this value and indicate where to go on from */
9751 *value = val_beg;
9752 *value_l = val_end - val_beg;
9753 return next + 1;
9754 }
9755
9756 /* Set-Cookie headers only have the name in the first attr=value part */
9757 if (!list)
9758 break;
9759 }
9760
9761 return NULL;
9762}
9763
Willy Tarreaue333ec92012-04-16 16:26:40 +02009764/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009765 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009766 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009767 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009768 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009769 * Accepts exactly 1 argument of type string. If the input options indicate
9770 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009771 */
9772static int
Willy Tarreau51539362012-05-08 12:46:28 +02009773smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9774 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009775{
9776 struct http_txn *txn = l7;
9777 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009778 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009779 const struct http_msg *msg;
9780 const char *hdr_name;
9781 int hdr_name_len;
9782 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009783 int occ = 0;
9784 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009785
Willy Tarreau24e32d82012-04-23 23:55:44 +02009786 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009787 return 0;
9788
Willy Tarreaua890d072013-04-02 12:01:06 +02009789 if (!ctx) {
9790 /* first call */
9791 ctx = &static_hdr_ctx;
9792 ctx->idx = 0;
9793 smp->ctx.a[2] = ctx;
9794 }
9795
Willy Tarreaue333ec92012-04-16 16:26:40 +02009796 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009797
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009798 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009799 msg = &txn->req;
9800 hdr_name = "Cookie";
9801 hdr_name_len = 6;
9802 } else {
9803 msg = &txn->rsp;
9804 hdr_name = "Set-Cookie";
9805 hdr_name_len = 10;
9806 }
9807
Willy Tarreau28376d62012-04-26 21:26:10 +02009808 if (!occ && !(opt & SMP_OPT_ITERATE))
9809 /* no explicit occurrence and single fetch => last cookie by default */
9810 occ = -1;
9811
9812 /* OK so basically here, either we want only one value and it's the
9813 * last one, or we want to iterate over all of them and we fetch the
9814 * next one.
9815 */
9816
Willy Tarreau9b28e032012-10-12 23:49:43 +02009817 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009818 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009819 /* search for the header from the beginning, we must first initialize
9820 * the search parameters.
9821 */
Willy Tarreau37406352012-04-23 16:16:37 +02009822 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009823 ctx->idx = 0;
9824 }
9825
Willy Tarreau28376d62012-04-26 21:26:10 +02009826 smp->flags |= SMP_F_VOL_HDR;
9827
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009828 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009829 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9830 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009831 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9832 goto out;
9833
Willy Tarreau24e32d82012-04-23 23:55:44 +02009834 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009835 continue;
9836
Willy Tarreau37406352012-04-23 16:16:37 +02009837 smp->ctx.a[0] = ctx->line + ctx->val;
9838 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009839 }
9840
Willy Tarreauf853c462012-04-23 18:53:56 +02009841 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009842 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009843 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009844 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009845 &smp->data.str.str,
9846 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009847 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009848 found = 1;
9849 if (occ >= 0) {
9850 /* one value was returned into smp->data.str.{str,len} */
9851 smp->flags |= SMP_F_NOT_LAST;
9852 return 1;
9853 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009854 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009855 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009856 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009857 /* all cookie headers and values were scanned. If we're looking for the
9858 * last occurrence, we may return it now.
9859 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009860 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009861 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009862 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009863}
9864
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009865/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009866 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009867 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009868 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009869 */
9870static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009871smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009872 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009873{
9874 struct http_txn *txn = l7;
9875 struct hdr_idx *idx = &txn->hdr_idx;
9876 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009877 const struct http_msg *msg;
9878 const char *hdr_name;
9879 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009880 int cnt;
9881 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009882 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009883
Willy Tarreau24e32d82012-04-23 23:55:44 +02009884 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009885 return 0;
9886
Willy Tarreaue333ec92012-04-16 16:26:40 +02009887 CHECK_HTTP_MESSAGE_FIRST();
9888
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009889 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009890 msg = &txn->req;
9891 hdr_name = "Cookie";
9892 hdr_name_len = 6;
9893 } else {
9894 msg = &txn->rsp;
9895 hdr_name = "Set-Cookie";
9896 hdr_name_len = 10;
9897 }
9898
Willy Tarreau9b28e032012-10-12 23:49:43 +02009899 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009900 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009901 ctx.idx = 0;
9902 cnt = 0;
9903
9904 while (1) {
9905 /* Note: val_beg == NULL every time we need to fetch a new header */
9906 if (!val_beg) {
9907 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9908 break;
9909
Willy Tarreau24e32d82012-04-23 23:55:44 +02009910 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009911 continue;
9912
9913 val_beg = ctx.line + ctx.val;
9914 val_end = val_beg + ctx.vlen;
9915 }
9916
Willy Tarreauf853c462012-04-23 18:53:56 +02009917 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009918 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009919 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009920 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009921 &smp->data.str.str,
9922 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009923 cnt++;
9924 }
9925 }
9926
Willy Tarreauf853c462012-04-23 18:53:56 +02009927 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009928 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009929 return 1;
9930}
9931
Willy Tarreau51539362012-05-08 12:46:28 +02009932/* Fetch an cookie's integer value. The integer value is returned. It
9933 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9934 */
9935static int
9936smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9937 const struct arg *args, struct sample *smp)
9938{
9939 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9940
9941 if (ret > 0) {
9942 smp->type = SMP_T_UINT;
9943 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9944 }
9945
9946 return ret;
9947}
9948
Willy Tarreau8797c062007-05-07 00:55:35 +02009949/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009950/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009951/************************************************************************/
9952
David Cournapeau16023ee2010-12-23 20:55:41 +09009953/*
9954 * Given a path string and its length, find the position of beginning of the
9955 * query string. Returns NULL if no query string is found in the path.
9956 *
9957 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9958 *
9959 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9960 */
bedis4c75cca2012-10-05 08:38:24 +02009961static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009962{
9963 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009964
bedis4c75cca2012-10-05 08:38:24 +02009965 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009966 return p ? p + 1 : NULL;
9967}
9968
bedis4c75cca2012-10-05 08:38:24 +02009969static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009970{
bedis4c75cca2012-10-05 08:38:24 +02009971 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009972}
9973
9974/*
9975 * Given a url parameter, find the starting position of the first occurence,
9976 * or NULL if the parameter is not found.
9977 *
9978 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9979 * the function will return query_string+8.
9980 */
9981static char*
9982find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009983 char* url_param_name, size_t url_param_name_l,
9984 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009985{
9986 char *pos, *last;
9987
9988 pos = query_string;
9989 last = query_string + query_string_l - url_param_name_l - 1;
9990
9991 while (pos <= last) {
9992 if (pos[url_param_name_l] == '=') {
9993 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9994 return pos;
9995 pos += url_param_name_l + 1;
9996 }
bedis4c75cca2012-10-05 08:38:24 +02009997 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009998 pos++;
9999 pos++;
10000 }
10001 return NULL;
10002}
10003
10004/*
10005 * Given a url parameter name, returns its value and size into *value and
10006 * *value_l respectively, and returns non-zero. If the parameter is not found,
10007 * zero is returned and value/value_l are not touched.
10008 */
10009static int
10010find_url_param_value(char* path, size_t path_l,
10011 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010012 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010013{
10014 char *query_string, *qs_end;
10015 char *arg_start;
10016 char *value_start, *value_end;
10017
bedis4c75cca2012-10-05 08:38:24 +020010018 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010019 if (!query_string)
10020 return 0;
10021
10022 qs_end = path + path_l;
10023 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010024 url_param_name, url_param_name_l,
10025 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010026 if (!arg_start)
10027 return 0;
10028
10029 value_start = arg_start + url_param_name_l + 1;
10030 value_end = value_start;
10031
bedis4c75cca2012-10-05 08:38:24 +020010032 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010033 value_end++;
10034
10035 *value = value_start;
10036 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010037 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010038}
10039
10040static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010041smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10042 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +090010043{
bedis4c75cca2012-10-05 08:38:24 +020010044 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010045 struct http_txn *txn = l7;
10046 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010047
bedis4c75cca2012-10-05 08:38:24 +020010048 if (!args || args[0].type != ARGT_STR ||
10049 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010050 return 0;
10051
10052 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010053
bedis4c75cca2012-10-05 08:38:24 +020010054 if (args[1].type)
10055 delim = *args[1].data.str.str;
10056
Willy Tarreau9b28e032012-10-12 23:49:43 +020010057 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010058 args->data.str.str, args->data.str.len,
10059 &smp->data.str.str, &smp->data.str.len,
10060 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010061 return 0;
10062
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010063 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010064 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010065 return 1;
10066}
10067
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010068/* Return the signed integer value for the specified url parameter (see url_param
10069 * above).
10070 */
10071static int
10072smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10073 const struct arg *args, struct sample *smp)
10074{
10075 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
10076
10077 if (ret > 0) {
10078 smp->type = SMP_T_UINT;
10079 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10080 }
10081
10082 return ret;
10083}
10084
Willy Tarreau185b5c42012-04-26 15:11:51 +020010085/* This function is used to validate the arguments passed to any "hdr" fetch
10086 * keyword. These keywords support an optional positive or negative occurrence
10087 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10088 * is assumed that the types are already the correct ones. Returns 0 on error,
10089 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10090 * error message in case of error, that the caller is responsible for freeing.
10091 * The initial location must either be freeable or NULL.
10092 */
10093static int val_hdr(struct arg *arg, char **err_msg)
10094{
10095 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010096 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010097 return 0;
10098 }
10099 return 1;
10100}
10101
Willy Tarreau4a568972010-05-12 08:08:50 +020010102/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010103/* All supported ACL keywords must be declared here. */
10104/************************************************************************/
10105
10106/* Note: must not be declared <const> as its list will be overwritten.
10107 * Please take care of keeping this list alphabetically sorted.
10108 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010109static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010110 { "base", "base", acl_parse_str, acl_match_str },
10111 { "base_beg", "base", acl_parse_str, acl_match_beg },
10112 { "base_dir", "base", acl_parse_str, acl_match_dir },
10113 { "base_dom", "base", acl_parse_str, acl_match_dom },
10114 { "base_end", "base", acl_parse_str, acl_match_end },
10115 { "base_len", "base", acl_parse_int, acl_match_len },
10116 { "base_reg", "base", acl_parse_reg, acl_match_reg },
10117 { "base_sub", "base", acl_parse_str, acl_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010118
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010119 { "cook", "req.cook", acl_parse_str, acl_match_str },
10120 { "cook_beg", "req.cook", acl_parse_str, acl_match_beg },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010121 { "cook_dir", "req.cook", acl_parse_str, acl_match_dir },
10122 { "cook_dom", "req.cook", acl_parse_str, acl_match_dom },
10123 { "cook_end", "req.cook", acl_parse_str, acl_match_end },
10124 { "cook_len", "req.cook", acl_parse_int, acl_match_len },
10125 { "cook_reg", "req.cook", acl_parse_reg, acl_match_reg },
10126 { "cook_sub", "req.cook", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010127
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010128 { "hdr", "req.hdr", acl_parse_str, acl_match_str },
10129 { "hdr_beg", "req.hdr", acl_parse_str, acl_match_beg },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010130 { "hdr_dir", "req.hdr", acl_parse_str, acl_match_dir },
10131 { "hdr_dom", "req.hdr", acl_parse_str, acl_match_dom },
10132 { "hdr_end", "req.hdr", acl_parse_str, acl_match_end },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010133 { "hdr_len", "req.hdr", acl_parse_int, acl_match_len },
10134 { "hdr_reg", "req.hdr", acl_parse_reg, acl_match_reg },
10135 { "hdr_sub", "req.hdr", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010136
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010137 { "http_auth_group", NULL, acl_parse_strcat, acl_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010138
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010139 { "method", NULL, acl_parse_meth, acl_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010140
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010141 { "path", "path", acl_parse_str, acl_match_str },
10142 { "path_beg", "path", acl_parse_str, acl_match_beg },
10143 { "path_dir", "path", acl_parse_str, acl_match_dir },
10144 { "path_dom", "path", acl_parse_str, acl_match_dom },
10145 { "path_end", "path", acl_parse_str, acl_match_end },
10146 { "path_len", "path", acl_parse_int, acl_match_len },
10147 { "path_reg", "path", acl_parse_reg, acl_match_reg },
10148 { "path_sub", "path", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010149
Willy Tarreauff5afcc2013-03-31 18:49:18 +020010150 { "req_ver", "req.ver", acl_parse_str, acl_match_str },
10151 { "resp_ver", "res.ver", acl_parse_str, acl_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010152
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010153 { "scook", "res.cook", acl_parse_str, acl_match_str },
10154 { "scook_beg", "res.cook", acl_parse_str, acl_match_beg },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010155 { "scook_dir", "res.cook", acl_parse_str, acl_match_dir },
10156 { "scook_dom", "res.cook", acl_parse_str, acl_match_dom },
10157 { "scook_end", "res.cook", acl_parse_str, acl_match_end },
10158 { "scook_len", "res.cook", acl_parse_int, acl_match_len },
10159 { "scook_reg", "res.cook", acl_parse_reg, acl_match_reg },
10160 { "scook_sub", "res.cook", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010161
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010162 { "shdr", "res.hdr", acl_parse_str, acl_match_str },
10163 { "shdr_beg", "res.hdr", acl_parse_str, acl_match_beg },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010164 { "shdr_dir", "res.hdr", acl_parse_str, acl_match_dir },
10165 { "shdr_dom", "res.hdr", acl_parse_str, acl_match_dom },
10166 { "shdr_end", "res.hdr", acl_parse_str, acl_match_end },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010167 { "shdr_len", "res.hdr", acl_parse_int, acl_match_len },
10168 { "shdr_reg", "res.hdr", acl_parse_reg, acl_match_reg },
10169 { "shdr_sub", "res.hdr", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010170
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010171 { "url", "url", acl_parse_str, acl_match_str },
10172 { "url_beg", "url", acl_parse_str, acl_match_beg },
10173 { "url_dir", "url", acl_parse_str, acl_match_dir },
10174 { "url_dom", "url", acl_parse_str, acl_match_dom },
10175 { "url_end", "url", acl_parse_str, acl_match_end },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010176 { "url_len", "url", acl_parse_int, acl_match_len },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010177 { "url_reg", "url", acl_parse_reg, acl_match_reg },
10178 { "url_sub", "url", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010179
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010180 { "urlp", "urlp", acl_parse_str, acl_match_str },
10181 { "urlp_beg", "urlp", acl_parse_str, acl_match_beg },
10182 { "urlp_dir", "urlp", acl_parse_str, acl_match_dir },
10183 { "urlp_dom", "urlp", acl_parse_str, acl_match_dom },
10184 { "urlp_end", "urlp", acl_parse_str, acl_match_end },
Willy Tarreaud86e29d2013-03-25 08:21:05 +010010185 { "urlp_len", "urlp", acl_parse_int, acl_match_len },
10186 { "urlp_reg", "urlp", acl_parse_reg, acl_match_reg },
10187 { "urlp_sub", "urlp", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010188
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010189 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010190}};
10191
10192/************************************************************************/
10193/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010194/************************************************************************/
10195/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010196static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010197 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10198 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10199 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10200
10201 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10202 * are only here to match the ACL's name, are request-only and are used
10203 * for ACL compatibility only.
10204 */
10205 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10206 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10207 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10208 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10209
10210 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10211 * only here to match the ACL's name, are request-only and are used for
10212 * ACL compatibility only.
10213 */
10214 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10215 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10216 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10217 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10218
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010219 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10220 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010221 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10222 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10223 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010224
10225 /* HTTP protocol on the request path */
10226 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010227 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010228
10229 /* HTTP version on the request path */
10230 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010231 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010232
10233 /* HTTP version on the response path */
10234 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010235 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10236
Willy Tarreau18ed2562013-01-14 15:56:36 +010010237 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10238 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10239 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10240 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10241
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010242 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10243 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010244 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10245 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10246 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10247 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10248
10249 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10250 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10251 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10252 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10253
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010254 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10255 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010256 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10257 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10258 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10259 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10260
Willy Tarreau409bcde2013-01-08 00:31:00 +010010261 /* scook is valid only on the response and is used for ACL compatibility */
10262 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10263 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10264 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10265 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10266
10267 /* shdr is valid only on the response and is used for ACL compatibility */
10268 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10269 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10270 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10271 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10272
10273 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10274 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10275 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10276 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10277 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10278 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10279 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10280 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010281}};
10282
Willy Tarreau8797c062007-05-07 00:55:35 +020010283
10284__attribute__((constructor))
10285static void __http_protocol_init(void)
10286{
10287 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010288 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +020010289}
10290
10291
Willy Tarreau58f10d72006-12-04 02:26:12 +010010292/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010293 * Local variables:
10294 * c-indent-level: 8
10295 * c-basic-offset: 8
10296 * End:
10297 */