blob: 51a43a4493b3bb3b4365eb23bccaa362e2706a96 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
238
239#else
240#error "Check if your OS uses bitfields for fd_sets"
241#endif
242
Willy Tarreau80587432006-12-24 17:47:20 +0100243void init_proto_http()
244{
Willy Tarreau42250582007-04-01 01:30:43 +0200245 int i;
246 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100247 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200248
Willy Tarreau80587432006-12-24 17:47:20 +0100249 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
250 if (!http_err_msgs[msg]) {
251 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
252 abort();
253 }
254
255 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
256 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
257 }
Willy Tarreau42250582007-04-01 01:30:43 +0200258
259 /* initialize the log header encoding map : '{|}"#' should be encoded with
260 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
261 * URL encoding only requires '"', '#' to be encoded as well as non-
262 * printable characters above.
263 */
264 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
265 memset(url_encode_map, 0, sizeof(url_encode_map));
266 for (i = 0; i < 32; i++) {
267 FD_SET(i, hdr_encode_map);
268 FD_SET(i, url_encode_map);
269 }
270 for (i = 127; i < 256; i++) {
271 FD_SET(i, hdr_encode_map);
272 FD_SET(i, url_encode_map);
273 }
274
275 tmp = "\"#{|}";
276 while (*tmp) {
277 FD_SET(*tmp, hdr_encode_map);
278 tmp++;
279 }
280
281 tmp = "\"#";
282 while (*tmp) {
283 FD_SET(*tmp, url_encode_map);
284 tmp++;
285 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200286
287 /* memory allocations */
288 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100289 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100290}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreau53b6c742006-12-17 13:37:46 +0100292/*
293 * We have 26 list of methods (1 per first letter), each of which can have
294 * up to 3 entries (2 valid, 1 null).
295 */
296struct http_method_desc {
297 http_meth_t meth;
298 int len;
299 const char text[8];
300};
301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100302const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100303 ['C' - 'A'] = {
304 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
305 },
306 ['D' - 'A'] = {
307 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
308 },
309 ['G' - 'A'] = {
310 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
311 },
312 ['H' - 'A'] = {
313 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
314 },
315 ['P' - 'A'] = {
316 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
317 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
318 },
319 ['T' - 'A'] = {
320 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
321 },
322 /* rest is empty like this :
323 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
324 */
325};
326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100327/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100329 * RFC2616 for those chars.
330 */
331
332const char http_is_spht[256] = {
333 [' '] = 1, ['\t'] = 1,
334};
335
336const char http_is_crlf[256] = {
337 ['\r'] = 1, ['\n'] = 1,
338};
339
340const char http_is_lws[256] = {
341 [' '] = 1, ['\t'] = 1,
342 ['\r'] = 1, ['\n'] = 1,
343};
344
345const char http_is_sep[256] = {
346 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
347 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
348 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
349 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
350 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
351};
352
353const char http_is_ctl[256] = {
354 [0 ... 31] = 1,
355 [127] = 1,
356};
357
358/*
359 * A token is any ASCII char that is neither a separator nor a CTL char.
360 * Do not overwrite values in assignment since gcc-2.95 will not handle
361 * them correctly. Instead, define every non-CTL char's status.
362 */
363const char http_is_token[256] = {
364 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
365 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
366 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
367 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
368 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
369 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
370 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
371 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
372 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
373 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
374 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
375 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
376 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
377 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
378 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
379 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
380 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
381 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
382 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
383 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
384 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
385 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
386 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
387 ['|'] = 1, ['}'] = 0, ['~'] = 1,
388};
389
390
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100391/*
392 * An http ver_token is any ASCII which can be found in an HTTP version,
393 * which includes 'H', 'T', 'P', '/', '.' and any digit.
394 */
395const char http_is_ver_token[256] = {
396 ['.'] = 1, ['/'] = 1,
397 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
398 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
399 ['H'] = 1, ['P'] = 1, ['T'] = 1,
400};
401
402
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100403/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100404 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
405 */
406#if defined(DEBUG_FSM)
407static void http_silent_debug(int line, struct session *s)
408{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100409 chunk_printf(&trash,
410 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
411 line,
412 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
413 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
414 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100415
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100416 chunk_printf(&trash,
417 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
418 line,
419 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
420 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
421 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100422}
423#else
424#define http_silent_debug(l,s) do { } while (0)
425#endif
426
427/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100428 * Adds a header and its CRLF at the tail of the message's buffer, just before
429 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100430 * The header is also automatically added to the index <hdr_idx>, and the end
431 * of headers is automatically adjusted. The number of bytes added is returned
432 * on success, otherwise <0 is returned indicating an error.
433 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100434int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100435{
436 int bytes, len;
437
438 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200439 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440 if (!bytes)
441 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100442 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
444}
445
446/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100447 * Adds a header and its CRLF at the tail of the message's buffer, just before
448 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100449 * the buffer is only opened and the space reserved, but nothing is copied.
450 * The header is also automatically added to the index <hdr_idx>, and the end
451 * of headers is automatically adjusted. The number of bytes added is returned
452 * on success, otherwise <0 is returned indicating an error.
453 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100454int http_header_add_tail2(struct http_msg *msg,
455 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456{
457 int bytes;
458
Willy Tarreau9b28e032012-10-12 23:49:43 +0200459 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100460 if (!bytes)
461 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100462 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100463 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
466/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100467 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
468 * If so, returns the position of the first non-space character relative to
469 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
470 * to return a pointer to the place after the first space. Returns 0 if the
471 * header name does not match. Checks are case-insensitive.
472 */
473int http_header_match2(const char *hdr, const char *end,
474 const char *name, int len)
475{
476 const char *val;
477
478 if (hdr + len >= end)
479 return 0;
480 if (hdr[len] != ':')
481 return 0;
482 if (strncasecmp(hdr, name, len) != 0)
483 return 0;
484 val = hdr + len + 1;
485 while (val < end && HTTP_IS_SPHT(*val))
486 val++;
487 if ((val >= end) && (len + 2 <= end - hdr))
488 return len + 2; /* we may replace starting from second space */
489 return val - hdr;
490}
491
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200492/* Find the first or next occurrence of header <name> in message buffer <sol>
493 * using headers index <idx>, and return it in the <ctx> structure. This
494 * structure holds everything necessary to use the header and find next
495 * occurrence. If its <idx> member is 0, the header is searched from the
496 * beginning. Otherwise, the next occurrence is returned. The function returns
497 * 1 when it finds a value, and 0 when there is no more. It is very similar to
498 * http_find_header2() except that it is designed to work with full-line headers
499 * whose comma is not a delimiter but is part of the syntax. As a special case,
500 * if ctx->val is NULL when searching for a new values of a header, the current
501 * header is rescanned. This allows rescanning after a header deletion.
502 */
503int http_find_full_header2(const char *name, int len,
504 char *sol, struct hdr_idx *idx,
505 struct hdr_ctx *ctx)
506{
507 char *eol, *sov;
508 int cur_idx, old_idx;
509
510 cur_idx = ctx->idx;
511 if (cur_idx) {
512 /* We have previously returned a header, let's search another one */
513 sol = ctx->line;
514 eol = sol + idx->v[cur_idx].len;
515 goto next_hdr;
516 }
517
518 /* first request for this header */
519 sol += hdr_idx_first_pos(idx);
520 old_idx = 0;
521 cur_idx = hdr_idx_first_idx(idx);
522 while (cur_idx) {
523 eol = sol + idx->v[cur_idx].len;
524
525 if (len == 0) {
526 /* No argument was passed, we want any header.
527 * To achieve this, we simply build a fake request. */
528 while (sol + len < eol && sol[len] != ':')
529 len++;
530 name = sol;
531 }
532
533 if ((len < eol - sol) &&
534 (sol[len] == ':') &&
535 (strncasecmp(sol, name, len) == 0)) {
536 ctx->del = len;
537 sov = sol + len + 1;
538 while (sov < eol && http_is_lws[(unsigned char)*sov])
539 sov++;
540
541 ctx->line = sol;
542 ctx->prev = old_idx;
543 ctx->idx = cur_idx;
544 ctx->val = sov - sol;
545 ctx->tws = 0;
546 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
547 eol--;
548 ctx->tws++;
549 }
550 ctx->vlen = eol - sov;
551 return 1;
552 }
553 next_hdr:
554 sol = eol + idx->v[cur_idx].cr + 1;
555 old_idx = cur_idx;
556 cur_idx = idx->v[cur_idx].next;
557 }
558 return 0;
559}
560
Willy Tarreau68085d82010-01-18 14:54:04 +0100561/* Find the end of the header value contained between <s> and <e>. See RFC2616,
562 * par 2.2 for more information. Note that it requires a valid header to return
563 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100565char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200566{
567 int quoted, qdpair;
568
569 quoted = qdpair = 0;
570 for (; s < e; s++) {
571 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200572 else if (quoted) {
573 if (*s == '\\') qdpair = 1;
574 else if (*s == '"') quoted = 0;
575 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 else if (*s == '"') quoted = 1;
577 else if (*s == ',') return s;
578 }
579 return s;
580}
581
582/* Find the first or next occurrence of header <name> in message buffer <sol>
583 * using headers index <idx>, and return it in the <ctx> structure. This
584 * structure holds everything necessary to use the header and find next
585 * occurrence. If its <idx> member is 0, the header is searched from the
586 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100587 * 1 when it finds a value, and 0 when there is no more. It is designed to work
588 * with headers defined as comma-separated lists. As a special case, if ctx->val
589 * is NULL when searching for a new values of a header, the current header is
590 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200591 */
592int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100593 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594 struct hdr_ctx *ctx)
595{
Willy Tarreau68085d82010-01-18 14:54:04 +0100596 char *eol, *sov;
597 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200598
Willy Tarreau68085d82010-01-18 14:54:04 +0100599 cur_idx = ctx->idx;
600 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200601 /* We have previously returned a value, let's search
602 * another one on the same line.
603 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200605 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 eol = sol + idx->v[cur_idx].len;
608
609 if (sov >= eol)
610 /* no more values in this header */
611 goto next_hdr;
612
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 /* values remaining for this header, skip the comma but save it
614 * for later use (eg: for header deletion).
615 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 sov++;
617 while (sov < eol && http_is_lws[(unsigned char)*sov])
618 sov++;
619
620 goto return_hdr;
621 }
622
623 /* first request for this header */
624 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627 while (cur_idx) {
628 eol = sol + idx->v[cur_idx].len;
629
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200630 if (len == 0) {
631 /* No argument was passed, we want any header.
632 * To achieve this, we simply build a fake request. */
633 while (sol + len < eol && sol[len] != ':')
634 len++;
635 name = sol;
636 }
637
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638 if ((len < eol - sol) &&
639 (sol[len] == ':') &&
640 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200642 sov = sol + len + 1;
643 while (sov < eol && http_is_lws[(unsigned char)*sov])
644 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100645
Willy Tarreau33a7e692007-06-10 19:45:56 +0200646 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100647 ctx->prev = old_idx;
648 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200649 ctx->idx = cur_idx;
650 ctx->val = sov - sol;
651
652 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200653 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200654 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200655 eol--;
656 ctx->tws++;
657 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 ctx->vlen = eol - sov;
659 return 1;
660 }
661 next_hdr:
662 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100663 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200664 cur_idx = idx->v[cur_idx].next;
665 }
666 return 0;
667}
668
669int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 struct hdr_ctx *ctx)
672{
673 return http_find_header2(name, strlen(name), sol, idx, ctx);
674}
675
Willy Tarreau68085d82010-01-18 14:54:04 +0100676/* Remove one value of a header. This only works on a <ctx> returned by one of
677 * the http_find_header functions. The value is removed, as well as surrounding
678 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100679 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * message <msg>. The new index is returned. If it is zero, it means there is
681 * no more header, so any processing may stop. The ctx is always left in a form
682 * that can be handled by http_find_header2() to find next occurrence.
683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100685{
686 int cur_idx = ctx->idx;
687 char *sol = ctx->line;
688 struct hdr_idx_elem *hdr;
689 int delta, skip_comma;
690
691 if (!cur_idx)
692 return 0;
693
694 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100696 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200697 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100698 http_msg_move_end(msg, delta);
699 idx->used--;
700 hdr->len = 0; /* unused entry */
701 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100702 if (idx->tail == ctx->idx)
703 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100704 ctx->idx = ctx->prev; /* walk back to the end of previous header */
705 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
706 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200707 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 return ctx->idx;
709 }
710
711 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200712 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
713 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100714 */
715
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200716 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200717 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200718 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100719 NULL, 0);
720 hdr->len += delta;
721 http_msg_move_end(msg, delta);
722 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 return ctx->idx;
725}
726
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100727/* This function handles a server error at the stream interface level. The
728 * stream interface is assumed to be already in a closed state. An optional
729 * message is copied into the input buffer, and an HTTP status code stored.
730 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100731 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100733static void http_server_error(struct session *t, struct stream_interface *si,
734 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200736 channel_auto_read(si->ob);
737 channel_abort(si->ob);
738 channel_auto_close(si->ob);
739 channel_erase(si->ob);
740 channel_auto_close(si->ib);
741 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100742 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100743 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200744 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 }
746 if (!(t->flags & SN_ERR_MASK))
747 t->flags |= err;
748 if (!(t->flags & SN_FINST_MASK))
749 t->flags |= finst;
750}
751
Willy Tarreau80587432006-12-24 17:47:20 +0100752/* This function returns the appropriate error location for the given session
753 * and message.
754 */
755
Willy Tarreau783f2582012-09-04 12:19:04 +0200756struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100757{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200758 if (s->be->errmsg[msgnum].str)
759 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100760 else if (s->fe->errmsg[msgnum].str)
761 return &s->fe->errmsg[msgnum];
762 else
763 return &http_err_chunks[msgnum];
764}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765
Willy Tarreau53b6c742006-12-17 13:37:46 +0100766/*
767 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
768 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
769 */
770static http_meth_t find_http_meth(const char *str, const int len)
771{
772 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100773 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100774
775 m = ((unsigned)*str - 'A');
776
777 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100778 for (h = http_methods[m]; h->len > 0; h++) {
779 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100780 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100781 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100782 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100783 };
784 return HTTP_METH_OTHER;
785 }
786 return HTTP_METH_NONE;
787
788}
789
Willy Tarreau21d2af32008-02-14 20:25:24 +0100790/* Parse the URI from the given transaction (which is assumed to be in request
791 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
792 * It is returned otherwise.
793 */
794static char *
795http_get_path(struct http_txn *txn)
796{
797 char *ptr, *end;
798
Willy Tarreau9b28e032012-10-12 23:49:43 +0200799 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100800 end = ptr + txn->req.sl.rq.u_l;
801
802 if (ptr >= end)
803 return NULL;
804
805 /* RFC2616, par. 5.1.2 :
806 * Request-URI = "*" | absuri | abspath | authority
807 */
808
809 if (*ptr == '*')
810 return NULL;
811
812 if (isalpha((unsigned char)*ptr)) {
813 /* this is a scheme as described by RFC3986, par. 3.1 */
814 ptr++;
815 while (ptr < end &&
816 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
817 ptr++;
818 /* skip '://' */
819 if (ptr == end || *ptr++ != ':')
820 return NULL;
821 if (ptr == end || *ptr++ != '/')
822 return NULL;
823 if (ptr == end || *ptr++ != '/')
824 return NULL;
825 }
826 /* skip [user[:passwd]@]host[:[port]] */
827
828 while (ptr < end && *ptr != '/')
829 ptr++;
830
831 if (ptr == end)
832 return NULL;
833
834 /* OK, we got the '/' ! */
835 return ptr;
836}
837
Willy Tarreau71241ab2012-12-27 11:30:54 +0100838/* Returns a 302 for a redirectable request that reaches a server working in
839 * in redirect mode. This may only be called just after the stream interface
840 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
841 * follow normal proxy processing. NOTE: this function is designed to support
842 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100844void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845{
846 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100847 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200849 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850
851 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100852 trash.len = strlen(HTTP_302);
853 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100855 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100856
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100858 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100859 return;
860
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100861 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100862 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100863 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
864 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100865 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100866
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200867 /* 3: add the request URI. Since it was already forwarded, we need
868 * to temporarily rewind the buffer.
869 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200871 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200872
Willy Tarreauefb453c2008-10-26 20:49:47 +0100873 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200874 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200875
Willy Tarreau9b28e032012-10-12 23:49:43 +0200876 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200877
Willy Tarreauefb453c2008-10-26 20:49:47 +0100878 if (!path)
879 return;
880
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100881 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100882 return;
883
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100884 memcpy(trash.str + trash.len, path, len);
885 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100886
887 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100888 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
889 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100890 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100891 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
892 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100893 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100894
895 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200896 si_shutr(si);
897 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100898 si->err_type = SI_ET_NONE;
899 si->err_loc = NULL;
900 si->state = SI_ST_CLO;
901
902 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200903 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100904
905 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100906 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100907}
908
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100909/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100910 * that the server side is closed. Note that err_type is actually a
911 * bitmask, where almost only aborts may be cumulated with other
912 * values. We consider that aborted operations are more important
913 * than timeouts or errors due to the fact that nobody else in the
914 * logs might explain incomplete retries. All others should avoid
915 * being cumulated. It should normally not be possible to have multiple
916 * aborts at once, but just in case, the first one in sequence is reported.
917 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100918void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100920 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100921
922 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100923 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200924 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100926 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200927 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100929 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200930 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100931 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100932 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200933 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100935 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200936 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100938 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200939 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100940 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100941 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200942 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100943}
944
Willy Tarreau42250582007-04-01 01:30:43 +0200945extern const char sess_term_cond[8];
946extern const char sess_fin_state[8];
947extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200948struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100949struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100950struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100951
Willy Tarreau117f59e2007-03-04 18:17:17 +0100952/*
953 * Capture headers from message starting at <som> according to header list
954 * <cap_hdr>, and fill the <idx> structure appropriately.
955 */
956void capture_headers(char *som, struct hdr_idx *idx,
957 char **cap, struct cap_hdr *cap_hdr)
958{
959 char *eol, *sol, *col, *sov;
960 int cur_idx;
961 struct cap_hdr *h;
962 int len;
963
964 sol = som + hdr_idx_first_pos(idx);
965 cur_idx = hdr_idx_first_idx(idx);
966
967 while (cur_idx) {
968 eol = sol + idx->v[cur_idx].len;
969
970 col = sol;
971 while (col < eol && *col != ':')
972 col++;
973
974 sov = col + 1;
975 while (sov < eol && http_is_lws[(unsigned char)*sov])
976 sov++;
977
978 for (h = cap_hdr; h; h = h->next) {
979 if ((h->namelen == col - sol) &&
980 (strncasecmp(sol, h->name, h->namelen) == 0)) {
981 if (cap[h->index] == NULL)
982 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200983 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100984
985 if (cap[h->index] == NULL) {
986 Alert("HTTP capture : out of memory.\n");
987 continue;
988 }
989
990 len = eol - sov;
991 if (len > h->len)
992 len = h->len;
993
994 memcpy(cap[h->index], sov, len);
995 cap[h->index][len]=0;
996 }
997 }
998 sol = eol + idx->v[cur_idx].cr + 1;
999 cur_idx = idx->v[cur_idx].next;
1000 }
1001}
1002
1003
Willy Tarreau42250582007-04-01 01:30:43 +02001004/* either we find an LF at <ptr> or we jump to <bad>.
1005 */
1006#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1007
1008/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1009 * otherwise to <http_msg_ood> with <state> set to <st>.
1010 */
1011#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1012 ptr++; \
1013 if (likely(ptr < end)) \
1014 goto good; \
1015 else { \
1016 state = (st); \
1017 goto http_msg_ood; \
1018 } \
1019 } while (0)
1020
1021
Willy Tarreaubaaee002006-06-26 02:48:02 +02001022/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001023 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1025 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1026 * will give undefined results.
1027 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1028 * and that msg->sol points to the beginning of the response.
1029 * If a complete line is found (which implies that at least one CR or LF is
1030 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1031 * returned indicating an incomplete line (which does not mean that parts have
1032 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1033 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1034 * upon next call.
1035 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001036 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001037 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1038 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001039 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001041const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001042 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001043 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001044{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001045 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001046
Willy Tarreau8973c702007-01-21 23:58:29 +01001047 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001048 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001049 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001050 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001051 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1052
1053 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001054 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001055 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1056 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001057 state = HTTP_MSG_ERROR;
1058 break;
1059
Willy Tarreau8973c702007-01-21 23:58:29 +01001060 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001061 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001062 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001063 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 goto http_msg_rpcode;
1065 }
1066 if (likely(HTTP_IS_SPHT(*ptr)))
1067 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1068 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001069 state = HTTP_MSG_ERROR;
1070 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001071
Willy Tarreau8973c702007-01-21 23:58:29 +01001072 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001073 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001074 if (likely(!HTTP_IS_LWS(*ptr)))
1075 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1076
1077 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001078 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001079 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1080 }
1081
1082 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001083 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001084 http_msg_rsp_reason:
1085 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001087 msg->sl.st.r_l = 0;
1088 goto http_msg_rpline_eol;
1089
Willy Tarreau8973c702007-01-21 23:58:29 +01001090 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001091 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001092 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001093 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001094 goto http_msg_rpreason;
1095 }
1096 if (likely(HTTP_IS_SPHT(*ptr)))
1097 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1098 /* so it's a CR/LF, so there is no reason phrase */
1099 goto http_msg_rsp_reason;
1100
Willy Tarreau8973c702007-01-21 23:58:29 +01001101 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001102 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001103 if (likely(!HTTP_IS_CRLF(*ptr)))
1104 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001105 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001106 http_msg_rpline_eol:
1107 /* We have seen the end of line. Note that we do not
1108 * necessarily have the \n yet, but at least we know that we
1109 * have EITHER \r OR \n, otherwise the response would not be
1110 * complete. We can then record the response length and return
1111 * to the caller which will be able to register it.
1112 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001113 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001114 return ptr;
1115
1116#ifdef DEBUG_FULL
1117 default:
1118 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1119 exit(1);
1120#endif
1121 }
1122
1123 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001124 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 if (ret_state)
1126 *ret_state = state;
1127 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001128 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001130}
1131
Willy Tarreau8973c702007-01-21 23:58:29 +01001132/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001133 * This function parses a request line between <ptr> and <end>, starting with
1134 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1135 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1136 * will give undefined results.
1137 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1138 * and that msg->sol points to the beginning of the request.
1139 * If a complete line is found (which implies that at least one CR or LF is
1140 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1141 * returned indicating an incomplete line (which does not mean that parts have
1142 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1143 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1144 * upon next call.
1145 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001146 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1148 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001149 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001151const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001152 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001153 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001154{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001155 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001159 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001160 if (likely(HTTP_IS_TOKEN(*ptr)))
1161 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001162
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1166 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 if (likely(HTTP_IS_CRLF(*ptr))) {
1169 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001170 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001172 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001173 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001174 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001176 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 msg->sl.rq.v_l = 0;
1178 goto http_msg_rqline_eol;
1179 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001180 state = HTTP_MSG_ERROR;
1181 break;
1182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001184 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001186 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 goto http_msg_rquri;
1188 }
1189 if (likely(HTTP_IS_SPHT(*ptr)))
1190 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1191 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1192 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001193
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001195 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001196 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001198
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001200 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001201 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1202 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001203
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001204 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001205 /* non-ASCII chars are forbidden unless option
1206 * accept-invalid-http-request is enabled in the frontend.
1207 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001208 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001209 if (msg->err_pos < -1)
1210 goto invalid_char;
1211 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001212 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001213 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1214 }
1215
1216 if (likely(HTTP_IS_CRLF(*ptr))) {
1217 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1218 goto http_msg_req09_uri_e;
1219 }
1220
1221 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001222 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001223 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001224 state = HTTP_MSG_ERROR;
1225 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001228 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001230 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 goto http_msg_rqver;
1232 }
1233 if (likely(HTTP_IS_SPHT(*ptr)))
1234 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1235 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1236 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001237
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001239 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001240 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001242
1243 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001244 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001245 http_msg_rqline_eol:
1246 /* We have seen the end of line. Note that we do not
1247 * necessarily have the \n yet, but at least we know that we
1248 * have EITHER \r OR \n, otherwise the request would not be
1249 * complete. We can then record the request length and return
1250 * to the caller which will be able to register it.
1251 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001252 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001253 return ptr;
1254 }
1255
1256 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001257 state = HTTP_MSG_ERROR;
1258 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001260#ifdef DEBUG_FULL
1261 default:
1262 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1263 exit(1);
1264#endif
1265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001268 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 if (ret_state)
1270 *ret_state = state;
1271 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001272 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001276/*
1277 * Returns the data from Authorization header. Function may be called more
1278 * than once so data is stored in txn->auth_data. When no header is found
1279 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1280 * searching again for something we are unable to find anyway.
1281 */
1282
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001283char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001284
1285int
1286get_http_auth(struct session *s)
1287{
1288
1289 struct http_txn *txn = &s->txn;
1290 struct chunk auth_method;
1291 struct hdr_ctx ctx;
1292 char *h, *p;
1293 int len;
1294
1295#ifdef DEBUG_AUTH
1296 printf("Auth for session %p: %d\n", s, txn->auth.method);
1297#endif
1298
1299 if (txn->auth.method == HTTP_AUTH_WRONG)
1300 return 0;
1301
1302 if (txn->auth.method)
1303 return 1;
1304
1305 txn->auth.method = HTTP_AUTH_WRONG;
1306
1307 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001308
1309 if (txn->flags & TX_USE_PX_CONN) {
1310 h = "Proxy-Authorization";
1311 len = strlen(h);
1312 } else {
1313 h = "Authorization";
1314 len = strlen(h);
1315 }
1316
Willy Tarreau9b28e032012-10-12 23:49:43 +02001317 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001318 return 0;
1319
1320 h = ctx.line + ctx.val;
1321
1322 p = memchr(h, ' ', ctx.vlen);
1323 if (!p || p == h)
1324 return 0;
1325
1326 chunk_initlen(&auth_method, h, 0, p-h);
1327 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1328
1329 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1330
1331 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001332 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001333
1334 if (len < 0)
1335 return 0;
1336
1337
1338 get_http_auth_buff[len] = '\0';
1339
1340 p = strchr(get_http_auth_buff, ':');
1341
1342 if (!p)
1343 return 0;
1344
1345 txn->auth.user = get_http_auth_buff;
1346 *p = '\0';
1347 txn->auth.pass = p+1;
1348
1349 txn->auth.method = HTTP_AUTH_BASIC;
1350 return 1;
1351 }
1352
1353 return 0;
1354}
1355
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8973c702007-01-21 23:58:29 +01001357/*
1358 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001359 * depending on the initial msg->msg_state. The caller is responsible for
1360 * ensuring that the message does not wrap. The function can be preempted
1361 * everywhere when data are missing and recalled at the exact same location
1362 * with no information loss. The message may even be realigned between two
1363 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001364 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001365 * fields. Note that msg->sol will be initialized after completing the first
1366 * state, so that none of the msg pointers has to be initialized prior to the
1367 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001369void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001371 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001373 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001374
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001375 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001376 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 ptr = buf->p + msg->next;
1378 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 if (unlikely(ptr >= end))
1381 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001382
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001384 /*
1385 * First, states that are specific to the response only.
1386 * We check them first so that request and headers are
1387 * closer to each other (accessed more often).
1388 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001390 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001392 /* we have a start of message, but we have to check
1393 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001394 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001395 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001396 if (unlikely(ptr != buf->p)) {
1397 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001398 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001399 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001400 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 }
Willy Tarreau26927362012-05-18 23:22:52 +02001402 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001403 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001404 hdr_idx_init(idx);
1405 state = HTTP_MSG_RPVER;
1406 goto http_msg_rpver;
1407 }
1408
1409 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1410 goto http_msg_invalid;
1411
1412 if (unlikely(*ptr == '\n'))
1413 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1414 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1415 /* stop here */
1416
Willy Tarreau8973c702007-01-21 23:58:29 +01001417 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001418 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 EXPECT_LF_HERE(ptr, http_msg_invalid);
1420 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1421 /* stop here */
1422
Willy Tarreau8973c702007-01-21 23:58:29 +01001423 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001424 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001425 case HTTP_MSG_RPVER_SP:
1426 case HTTP_MSG_RPCODE:
1427 case HTTP_MSG_RPCODE_SP:
1428 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001429 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001430 state, ptr, end,
1431 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001432 if (unlikely(!ptr))
1433 return;
1434
1435 /* we have a full response and we know that we have either a CR
1436 * or an LF at <ptr>.
1437 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1439
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001440 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 if (likely(*ptr == '\r'))
1442 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1443 goto http_msg_rpline_end;
1444
Willy Tarreau8973c702007-01-21 23:58:29 +01001445 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001446 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 /* msg->sol must point to the first of CR or LF. */
1448 EXPECT_LF_HERE(ptr, http_msg_invalid);
1449 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1450 /* stop here */
1451
1452 /*
1453 * Second, states that are specific to the request only
1454 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001458 /* we have a start of message, but we have to check
1459 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001460 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001461 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001462 if (likely(ptr != buf->p)) {
1463 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001464 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001465 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 }
Willy Tarreau26927362012-05-18 23:22:52 +02001468 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001469 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 state = HTTP_MSG_RQMETH;
1471 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1475 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (unlikely(*ptr == '\n'))
1478 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1479 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001480 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001489 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_RQMETH_SP:
1491 case HTTP_MSG_RQURI:
1492 case HTTP_MSG_RQURI_SP:
1493 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001494 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001495 state, ptr, end,
1496 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (unlikely(!ptr))
1498 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 /* we have a full request and we know that we have either a CR
1501 * or an LF at <ptr>.
1502 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001504
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001505 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 if (likely(*ptr == '\r'))
1507 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001511 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 /* check for HTTP/0.9 request : no version information available.
1513 * msg->sol must point to the first of CR or LF.
1514 */
1515 if (unlikely(msg->sl.rq.v_l == 0))
1516 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 EXPECT_LF_HERE(ptr, http_msg_invalid);
1519 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8973c702007-01-21 23:58:29 +01001522 /*
1523 * Common states below
1524 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001527 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (likely(!HTTP_IS_CRLF(*ptr))) {
1529 goto http_msg_hdr_name;
1530 }
1531
1532 if (likely(*ptr == '\r'))
1533 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1534 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001537 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 /* assumes msg->sol points to the first char */
1539 if (likely(HTTP_IS_TOKEN(*ptr)))
1540 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001541
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001542 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001545 if (likely(msg->err_pos < -1) || *ptr == '\n')
1546 goto http_msg_invalid;
1547
1548 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001549 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001550
1551 /* and we still accept this non-token character */
1552 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001555 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001556 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 if (likely(HTTP_IS_SPHT(*ptr)))
1558 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001561 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 if (likely(!HTTP_IS_CRLF(*ptr))) {
1564 goto http_msg_hdr_val;
1565 }
1566
1567 if (likely(*ptr == '\r'))
1568 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1569 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001572 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 EXPECT_LF_HERE(ptr, http_msg_invalid);
1574 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001577 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 if (likely(HTTP_IS_SPHT(*ptr))) {
1579 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001580 for (; buf->p + msg->sov < ptr; msg->sov++)
1581 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 goto http_msg_hdr_l1_sp;
1583 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001584 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001585 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 goto http_msg_complete_header;
1587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001589 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001590 /* assumes msg->sol points to the first char, and msg->sov
1591 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 */
1593 if (likely(!HTTP_IS_CRLF(*ptr)))
1594 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001595
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001596 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 /* Note: we could also copy eol into ->eoh so that we have the
1598 * real header end in case it ends with lots of LWS, but is this
1599 * really needed ?
1600 */
1601 if (likely(*ptr == '\r'))
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1603 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001606 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 EXPECT_LF_HERE(ptr, http_msg_invalid);
1608 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001611 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1613 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001614 for (; buf->p + msg->eol < ptr; msg->eol++)
1615 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 goto http_msg_hdr_val;
1617 }
1618 http_msg_complete_header:
1619 /*
1620 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001621 * Assumes msg->sol points to the first char, msg->sov points
1622 * to the first character of the value and msg->eol to the
1623 * first CR or LF so we know how the line ends. We insert last
1624 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001626 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 idx, idx->tail) < 0))
1628 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001629
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 if (likely(!HTTP_IS_CRLF(*ptr))) {
1632 goto http_msg_hdr_name;
1633 }
1634
1635 if (likely(*ptr == '\r'))
1636 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1637 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001640 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 /* Assumes msg->sol points to the first of either CR or LF */
1642 EXPECT_LF_HERE(ptr, http_msg_invalid);
1643 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001644 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001645 msg->eoh = msg->sol;
1646 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001647 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001649
1650 case HTTP_MSG_ERROR:
1651 /* this may only happen if we call http_msg_analyser() twice with an error */
1652 break;
1653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654#ifdef DEBUG_FULL
1655 default:
1656 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1657 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001658#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 }
1660 http_msg_ood:
1661 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001662 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001663 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 http_msg_invalid:
1667 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001668 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001669 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 return;
1671}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001672
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001673/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1674 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1675 * nothing is done and 1 is returned.
1676 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001677static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001678{
1679 int delta;
1680 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001681 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001682
1683 if (msg->sl.rq.v_l != 0)
1684 return 1;
1685
Willy Tarreau9b28e032012-10-12 23:49:43 +02001686 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001687 delta = 0;
1688
1689 if (msg->sl.rq.u_l == 0) {
1690 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001691 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001692 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001693 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001694 }
1695 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001696 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001697 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001698 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001699 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001700 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001701 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001702 NULL, NULL);
1703 if (unlikely(!cur_end))
1704 return 0;
1705
1706 /* we have a full HTTP/1.0 request now and we know that
1707 * we have either a CR or an LF at <ptr>.
1708 */
1709 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1710 return 1;
1711}
1712
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001714 * and "keep-alive" values. If we already know that some headers may safely
1715 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001716 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1717 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001718 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001719 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1720 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1721 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001722 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001723 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001724void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001725{
Willy Tarreau5b154472009-12-21 20:11:07 +01001726 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001727 const char *hdr_val = "Connection";
1728 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001729
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001730 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001731 return;
1732
Willy Tarreau88d349d2010-01-25 12:15:43 +01001733 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1734 hdr_val = "Proxy-Connection";
1735 hdr_len = 16;
1736 }
1737
Willy Tarreau5b154472009-12-21 20:11:07 +01001738 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001739 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001740 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001741 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1742 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001743 if (to_del & 2)
1744 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001745 else
1746 txn->flags |= TX_CON_KAL_SET;
1747 }
1748 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1749 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001750 if (to_del & 1)
1751 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001752 else
1753 txn->flags |= TX_CON_CLO_SET;
1754 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001755 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1756 txn->flags |= TX_HDR_CONN_UPG;
1757 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001758 }
1759
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001760 txn->flags |= TX_HDR_CONN_PRS;
1761 return;
1762}
Willy Tarreau5b154472009-12-21 20:11:07 +01001763
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001764/* Apply desired changes on the Connection: header. Values may be removed and/or
1765 * added depending on the <wanted> flags, which are exclusively composed of
1766 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1767 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1768 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001769void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001770{
1771 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001772 const char *hdr_val = "Connection";
1773 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001774
1775 ctx.idx = 0;
1776
Willy Tarreau88d349d2010-01-25 12:15:43 +01001777
1778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1779 hdr_val = "Proxy-Connection";
1780 hdr_len = 16;
1781 }
1782
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001783 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001784 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001785 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1786 if (wanted & TX_CON_KAL_SET)
1787 txn->flags |= TX_CON_KAL_SET;
1788 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001789 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001790 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001791 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1792 if (wanted & TX_CON_CLO_SET)
1793 txn->flags |= TX_CON_CLO_SET;
1794 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001795 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001796 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001797 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001798
1799 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1800 return;
1801
1802 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1803 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001804 hdr_val = "Connection: close";
1805 hdr_len = 17;
1806 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1807 hdr_val = "Proxy-Connection: close";
1808 hdr_len = 23;
1809 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001810 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001811 }
1812
1813 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1814 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001815 hdr_val = "Connection: keep-alive";
1816 hdr_len = 22;
1817 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1818 hdr_val = "Proxy-Connection: keep-alive";
1819 hdr_len = 28;
1820 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001821 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001822 }
1823 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001824}
1825
Willy Tarreaua458b672012-03-05 11:17:50 +01001826/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001828 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001830 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001831 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001832static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001833{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001834 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001835 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001836 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001837 const char *end = buf->data + buf->size;
1838 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001839 unsigned int chunk = 0;
1840
1841 /* The chunk size is in the following form, though we are only
1842 * interested in the size and CRLF :
1843 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1844 */
1845 while (1) {
1846 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001847 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001848 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001849 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001850 if (c < 0) /* not a hex digit anymore */
1851 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001852 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001853 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001854 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001855 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001856 chunk = (chunk << 4) + c;
1857 }
1858
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001860 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001861 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862
1863 while (http_is_spht[(unsigned char)*ptr]) {
1864 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001865 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001866 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001868 }
1869
Willy Tarreaud98cf932009-12-27 22:54:55 +01001870 /* Up to there, we know that at least one byte is present at *ptr. Check
1871 * for the end of chunk size.
1872 */
1873 while (1) {
1874 if (likely(HTTP_IS_CRLF(*ptr))) {
1875 /* we now have a CR or an LF at ptr */
1876 if (likely(*ptr == '\r')) {
1877 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001878 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001879 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001880 return 0;
1881 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001882
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001884 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001886 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001887 /* done */
1888 break;
1889 }
1890 else if (*ptr == ';') {
1891 /* chunk extension, ends at next CRLF */
1892 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001893 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001894 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001895 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896
1897 while (!HTTP_IS_CRLF(*ptr)) {
1898 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001899 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001900 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901 return 0;
1902 }
1903 /* we have a CRLF now, loop above */
1904 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001905 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001907 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001908 }
1909
Willy Tarreaud98cf932009-12-27 22:54:55 +01001910 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001911 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001914 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001916 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001917 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001918 msg->chunk_len = chunk;
1919 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001922 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001923 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001924 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001925}
1926
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001928 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929 * the trailers is found, it is automatically scheduled to be forwarded,
1930 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1931 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001932 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001933 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001934 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001935 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1936 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001937 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001938 * matches the length of trailers already parsed and not forwarded. It is also
1939 * important to note that this function is designed to be able to parse wrapped
1940 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001942static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001944 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001945
Willy Tarreaua458b672012-03-05 11:17:50 +01001946 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001948 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001949 const char *ptr = b_ptr(buf, msg->next);
1950 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001951 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952
1953 /* scan current line and stop at LF or CRLF */
1954 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001955 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956 return 0;
1957
1958 if (*ptr == '\n') {
1959 if (!p1)
1960 p1 = ptr;
1961 p2 = ptr;
1962 break;
1963 }
1964
1965 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001966 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001967 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001968 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001969 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 p1 = ptr;
1971 }
1972
1973 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001974 if (ptr >= buf->data + buf->size)
1975 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 }
1977
1978 /* after LF; point to beginning of next line */
1979 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001980 if (p2 >= buf->data + buf->size)
1981 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001982
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001983 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001984 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001986
1987 /* schedule this line for forwarding */
1988 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001989 if (msg->sov >= buf->size)
1990 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001991
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001993 /* LF/CRLF at beginning of line => end of trailers at p2.
1994 * Everything was scheduled for forwarding, there's nothing
1995 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001996 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001997 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 msg->msg_state = HTTP_MSG_DONE;
1999 return 1;
2000 }
2001 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003 }
2004}
2005
Willy Tarreau54d23df2012-10-25 19:04:45 +02002006/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002007 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002008 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002009 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002010 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2011 * not enough data are available, the function does not change anything and
2012 * returns zero. If a parse error is encountered, the function returns < 0 and
2013 * does not change anything. Note: this function is designed to parse wrapped
2014 * CRLF at the end of the buffer.
2015 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002016static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002018 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002019 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 int bytes;
2021
2022 /* NB: we'll check data availabilty at the end. It's not a
2023 * problem because whatever we match first will be checked
2024 * against the correct length.
2025 */
2026 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002027 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002028 if (*ptr == '\r') {
2029 bytes++;
2030 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002031 if (ptr >= buf->data + buf->size)
2032 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 }
2034
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002035 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002036 return 0;
2037
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002038 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002041 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002044 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002045 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002046 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2047 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002048 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002049 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2050 return 1;
2051}
Willy Tarreau5b154472009-12-21 20:11:07 +01002052
William Lallemand82fe75c2012-10-23 10:25:10 +02002053
2054/*
2055 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002056 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002057int select_compression_request_header(struct session *s, struct buffer *req)
2058{
2059 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002060 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002061 struct hdr_ctx ctx;
2062 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002063 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002064
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002065 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2066 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002067 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2068 */
2069 ctx.idx = 0;
2070 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2071 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002072 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2073 (ctx.vlen < 31 ||
2074 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2075 ctx.line[ctx.val + 30] < '6' ||
2076 (ctx.line[ctx.val + 30] == '6' &&
2077 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2078 s->comp_algo = NULL;
2079 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002080 }
2081
William Lallemand82fe75c2012-10-23 10:25:10 +02002082 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002083 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002084 ctx.idx = 0;
2085 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002086 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002087 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2088 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002089
2090 /* remove all occurrences of the header when "compression offload" is set */
2091
2092 if ((s->be->comp && s->be->comp->offload) ||
2093 (s->fe->comp && s->fe->comp->offload)) {
2094 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2095 ctx.idx = 0;
2096 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2097 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2098 }
2099 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002100 return 1;
2101 }
2102 }
2103 }
2104 }
2105
2106 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002107 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2108 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002109 if (comp_algo->add_data == identity_add_data) {
2110 s->comp_algo = comp_algo;
2111 return 1;
2112 }
2113 }
2114 }
2115
2116 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002117 return 0;
2118}
2119
2120/*
2121 * Selects a comression algorithm depending of the server response.
2122 */
2123int select_compression_response_header(struct session *s, struct buffer *res)
2124{
2125 struct http_txn *txn = &s->txn;
2126 struct http_msg *msg = &txn->rsp;
2127 struct hdr_ctx ctx;
2128 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002129
2130 /* no common compression algorithm was found in request header */
2131 if (s->comp_algo == NULL)
2132 goto fail;
2133
2134 /* HTTP < 1.1 should not be compressed */
2135 if (!(msg->flags & HTTP_MSGF_VER_11))
2136 goto fail;
2137
William Lallemandd3002612012-11-26 14:34:47 +01002138 /* 200 only */
2139 if (txn->status != 200)
2140 goto fail;
2141
William Lallemand82fe75c2012-10-23 10:25:10 +02002142 /* Content-Length is null */
2143 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2144 goto fail;
2145
Willy Tarreau667c2a32013-04-09 08:13:58 +02002146 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2147 if (msg->flags & HTTP_MSGF_TE_CHNK)
2148 goto fail;
2149
William Lallemand82fe75c2012-10-23 10:25:10 +02002150 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002151 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002152 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2153 goto fail;
2154
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002155 /* no compression when Cache-Control: no-transform is present in the message */
2156 ctx.idx = 0;
2157 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2158 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2159 goto fail;
2160 }
2161
William Lallemand82fe75c2012-10-23 10:25:10 +02002162 comp_type = NULL;
2163
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002164 /* we don't want to compress multipart content-types, nor content-types that are
2165 * not listed in the "compression type" directive if any. If no content-type was
2166 * found but configuration requires one, we don't compress either. Backend has
2167 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002168 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002169 ctx.idx = 0;
2170 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2171 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2172 goto fail;
2173
2174 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2175 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002176 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002177 if (ctx.vlen >= comp_type->name_len &&
2178 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 /* this Content-Type should be compressed */
2180 break;
2181 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002182 /* this Content-Type should not be compressed */
2183 if (comp_type == NULL)
2184 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002185 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002186 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002187 else { /* no content-type header */
2188 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2189 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002190 }
2191
William Lallemandd85f9172012-11-09 17:05:39 +01002192 /* limit compression rate */
2193 if (global.comp_rate_lim > 0)
2194 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2195 goto fail;
2196
William Lallemand072a2bf2012-11-20 17:01:01 +01002197 /* limit cpu usage */
2198 if (idle_pct < compress_min_idle)
2199 goto fail;
2200
William Lallemand4c49fae2012-11-07 15:00:23 +01002201 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002202 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002203 goto fail;
2204
William Lallemandec3e3892012-11-12 17:02:18 +01002205 s->flags |= SN_COMP_READY;
2206
William Lallemand82fe75c2012-10-23 10:25:10 +02002207 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002208 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002209 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2210 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2211
2212 /* add Transfer-Encoding header */
2213 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2214 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2215
2216 /*
2217 * Add Content-Encoding header when it's not identity encoding.
2218 * RFC 2616 : Identity encoding: This content-coding is used only in the
2219 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2220 * header.
2221 */
2222 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002223 trash.len = 18;
2224 memcpy(trash.str, "Content-Encoding: ", trash.len);
2225 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2226 trash.len += s->comp_algo->name_len;
2227 trash.str[trash.len] = '\0';
2228 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002230 return 1;
2231
2232fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002233 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002234 return 0;
2235}
2236
2237
Willy Tarreaud787e662009-07-07 10:14:51 +02002238/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2239 * processing can continue on next analysers, or zero if it either needs more
2240 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2241 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2242 * when it has nothing left to do, and may remove any analyser when it wants to
2243 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002244 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002245int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002246{
Willy Tarreau59234e92008-11-30 23:51:27 +01002247 /*
2248 * We will parse the partial (or complete) lines.
2249 * We will check the request syntax, and also join multi-line
2250 * headers. An index of all the lines will be elaborated while
2251 * parsing.
2252 *
2253 * For the parsing, we use a 28 states FSM.
2254 *
2255 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002256 * req->buf->p = beginning of request
2257 * req->buf->p + msg->eoh = end of processed headers / start of current one
2258 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002259 * msg->eol = end of current header or line (LF or CRLF)
2260 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002261 *
2262 * At end of parsing, we may perform a capture of the error (if any), and
2263 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2264 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2265 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002266 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002267
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002269 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002270 struct http_txn *txn = &s->txn;
2271 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002272 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002273
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002274 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002275 now_ms, __FUNCTION__,
2276 s,
2277 req,
2278 req->rex, req->wex,
2279 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002280 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002281 req->analysers);
2282
Willy Tarreau52a0c602009-08-16 22:45:38 +02002283 /* we're speaking HTTP here, so let's speak HTTP to the client */
2284 s->srv_error = http_return_srv_error;
2285
Willy Tarreau83e3af02009-12-28 17:39:57 +01002286 /* There's a protected area at the end of the buffer for rewriting
2287 * purposes. We don't want to start to parse the request if the
2288 * protected area is affected, because we may have to move processed
2289 * data later, which is much more complicated.
2290 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002291 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002292 if (txn->flags & TX_NOT_FIRST) {
2293 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002294 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002295 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002296 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002297 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002298 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002299 return 0;
2300 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002301 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2302 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2303 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002304 }
2305
Willy Tarreau065e8332010-01-08 00:30:20 +01002306 /* Note that we have the same problem with the response ; we
2307 * may want to send a redirect, error or anything which requires
2308 * some spare space. So we'll ensure that we have at least
2309 * maxrewrite bytes available in the response buffer before
2310 * processing that one. This will only affect pipelined
2311 * keep-alive requests.
2312 */
2313 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002314 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002315 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2316 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2317 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002318 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002319 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002320 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002321 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002322 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002323 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002324 return 0;
2325 }
2326 }
2327
Willy Tarreau9b28e032012-10-12 23:49:43 +02002328 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002329 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002330 }
2331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /* 1: we might have to print this header in debug mode */
2333 if (unlikely((global.mode & MODE_DEBUG) &&
2334 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002335 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002337
Willy Tarreau9b28e032012-10-12 23:49:43 +02002338 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002339 /* this is a bit complex : in case of error on the request line,
2340 * we know that rq.l is still zero, so we display only the part
2341 * up to the end of the line (truncated by debug_hdr).
2342 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002343 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 sol += hdr_idx_first_pos(&txn->hdr_idx);
2347 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002348
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 while (cur_idx) {
2350 eol = sol + txn->hdr_idx.v[cur_idx].len;
2351 debug_hdr("clihdr", s, sol, eol);
2352 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2353 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002354 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 }
2356
Willy Tarreau58f10d72006-12-04 02:26:12 +01002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 /*
2359 * Now we quickly check if we have found a full valid request.
2360 * If not so, we check the FD and buffer states before leaving.
2361 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002362 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002363 * requests are checked first. When waiting for a second request
2364 * on a keep-alive session, if we encounter and error, close, t/o,
2365 * we note the error in the session flags but don't set any state.
2366 * Since the error will be noted there, it will not be counted by
2367 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002368 * Last, we may increase some tracked counters' http request errors on
2369 * the cases that are deliberately the client's fault. For instance,
2370 * a timeout or connection reset is not counted as an error. However
2371 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002372 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002373
Willy Tarreau655dce92009-11-08 13:10:58 +01002374 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002375 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002377 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002378 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002379 session_inc_http_req_ctr(s);
2380 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002381 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002383 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 /* 1: Since we are in header mode, if there's no space
2386 * left for headers, we won't be able to free more
2387 * later, so the session will never terminate. We
2388 * must terminate it now.
2389 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002390 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 /* FIXME: check if URI is set and return Status
2392 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002393 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002394 session_inc_http_req_ctr(s);
2395 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002396 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002397 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002398 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002399 goto return_bad_req;
2400 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002401
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002403 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002404 if (!(s->flags & SN_ERR_MASK))
2405 s->flags |= SN_ERR_CLICL;
2406
Willy Tarreaufcffa692010-01-10 14:21:19 +01002407 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002408 goto failed_keep_alive;
2409
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002411 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002412 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002413 session_inc_http_err_ctr(s);
2414 }
2415
Willy Tarreaudc979f22012-12-04 10:39:01 +01002416 txn->status = 400;
2417 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002418 msg->msg_state = HTTP_MSG_ERROR;
2419 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002420
Willy Tarreauda7ff642010-06-23 11:44:09 +02002421 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002422 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002423 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002424 if (s->listener->counters)
2425 s->listener->counters->failed_req++;
2426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 if (!(s->flags & SN_FINST_MASK))
2428 s->flags |= SN_FINST_R;
2429 return 0;
2430 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002433 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002434 if (!(s->flags & SN_ERR_MASK))
2435 s->flags |= SN_ERR_CLITO;
2436
Willy Tarreaufcffa692010-01-10 14:21:19 +01002437 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002438 goto failed_keep_alive;
2439
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002441 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002442 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002443 session_inc_http_err_ctr(s);
2444 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002446 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002447 msg->msg_state = HTTP_MSG_ERROR;
2448 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002449
Willy Tarreauda7ff642010-06-23 11:44:09 +02002450 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002451 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002452 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002453 if (s->listener->counters)
2454 s->listener->counters->failed_req++;
2455
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 if (!(s->flags & SN_FINST_MASK))
2457 s->flags |= SN_FINST_R;
2458 return 0;
2459 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002460
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002462 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002463 if (!(s->flags & SN_ERR_MASK))
2464 s->flags |= SN_ERR_CLICL;
2465
Willy Tarreaufcffa692010-01-10 14:21:19 +01002466 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002467 goto failed_keep_alive;
2468
Willy Tarreau4076a152009-04-02 15:18:36 +02002469 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002470 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002472 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 msg->msg_state = HTTP_MSG_ERROR;
2474 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002475
Willy Tarreauda7ff642010-06-23 11:44:09 +02002476 session_inc_http_err_ctr(s);
2477 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002478 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002479 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480 if (s->listener->counters)
2481 s->listener->counters->failed_req++;
2482
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 if (!(s->flags & SN_FINST_MASK))
2484 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002485 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002486 }
2487
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002488 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002489 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2490 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002491#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002493 /* We need more data, we have to re-enable quick-ack in case we
2494 * previously disabled it, otherwise we might cause the client
2495 * to delay next data.
2496 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002497 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002498 }
2499#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002500
Willy Tarreaufcffa692010-01-10 14:21:19 +01002501 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2502 /* If the client starts to talk, let's fall back to
2503 * request timeout processing.
2504 */
2505 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002506 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002507 }
2508
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002510 if (!tick_isset(req->analyse_exp)) {
2511 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2512 (txn->flags & TX_WAIT_NEXT_RQ) &&
2513 tick_isset(s->be->timeout.httpka))
2514 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2515 else
2516 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2517 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* we're not ready yet */
2520 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002521
2522 failed_keep_alive:
2523 /* Here we process low-level errors for keep-alive requests. In
2524 * short, if the request is not the first one and it experiences
2525 * a timeout, read error or shutdown, we just silently close so
2526 * that the client can try again.
2527 */
2528 txn->status = 0;
2529 msg->msg_state = HTTP_MSG_RQBEFORE;
2530 req->analysers = 0;
2531 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002532 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002533 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002534 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002535 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002537
Willy Tarreaud787e662009-07-07 10:14:51 +02002538 /* OK now we have a complete HTTP request with indexed headers. Let's
2539 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002540 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002541 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002542 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002543 * byte after the last LF. msg->sov points to the first byte of data.
2544 * msg->eol cannot be trusted because it may have been left uninitialized
2545 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002546 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002547
Willy Tarreauda7ff642010-06-23 11:44:09 +02002548 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002549 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2550
Willy Tarreaub16a5742010-01-10 14:46:16 +01002551 if (txn->flags & TX_WAIT_NEXT_RQ) {
2552 /* kill the pending keep-alive timeout */
2553 txn->flags &= ~TX_WAIT_NEXT_RQ;
2554 req->analyse_exp = TICK_ETERNITY;
2555 }
2556
2557
Willy Tarreaud787e662009-07-07 10:14:51 +02002558 /* Maybe we found in invalid header name while we were configured not
2559 * to block on that, so we have to capture it now.
2560 */
2561 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002562 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002563
Willy Tarreau59234e92008-11-30 23:51:27 +01002564 /*
2565 * 1: identify the method
2566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002567 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002568
2569 /* we can make use of server redirect on GET and HEAD */
2570 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2571 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002572
Willy Tarreau59234e92008-11-30 23:51:27 +01002573 /*
2574 * 2: check if the URI matches the monitor_uri.
2575 * We have to do this for every request which gets in, because
2576 * the monitor-uri is defined by the frontend.
2577 */
2578 if (unlikely((s->fe->monitor_uri_len != 0) &&
2579 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 s->fe->monitor_uri,
2582 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002583 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002585 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002589 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002590
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002592 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002593 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002594
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 ret = acl_pass(ret);
2596 if (cond->pol == ACL_COND_UNLESS)
2597 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 if (ret) {
2600 /* we fail this request, let's return 503 service unavail */
2601 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002602 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002603 if (!(s->flags & SN_ERR_MASK))
2604 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002606 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002607 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 /* nothing to fail, let's reply normaly */
2610 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002611 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002612 if (!(s->flags & SN_ERR_MASK))
2613 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 goto return_prx_cond;
2615 }
2616
2617 /*
2618 * 3: Maybe we have to copy the original REQURI for the logs ?
2619 * Note: we cannot log anymore if the request has been
2620 * classified as invalid.
2621 */
2622 if (unlikely(s->logs.logwait & LW_REQ)) {
2623 /* we have a complete HTTP request that we must log */
2624 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2625 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 if (urilen >= REQURI_LEN)
2628 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002629 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002632 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 s->do_log(s);
2634 } else {
2635 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002636 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002637 }
Willy Tarreau06619262006-12-17 08:37:22 +01002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002640 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002641 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002642
Willy Tarreau5b154472009-12-21 20:11:07 +01002643 /* ... and check if the request is HTTP/1.1 or above */
2644 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002645 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2646 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2647 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002648 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002649
2650 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002651 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002652
Willy Tarreau88d349d2010-01-25 12:15:43 +01002653 /* if the frontend has "option http-use-proxy-header", we'll check if
2654 * we have what looks like a proxied connection instead of a connection,
2655 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2656 * Note that this is *not* RFC-compliant, however browsers and proxies
2657 * happen to do that despite being non-standard :-(
2658 * We consider that a request not beginning with either '/' or '*' is
2659 * a proxied connection, which covers both "scheme://location" and
2660 * CONNECT ip:port.
2661 */
2662 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002663 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002664 txn->flags |= TX_USE_PX_CONN;
2665
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002666 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002667 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002668
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002670 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002671 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002673
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002674 /* 6: determine the transfer-length.
2675 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2676 * the presence of a message-body in a REQUEST and its transfer length
2677 * must be determined that way (in order of precedence) :
2678 * 1. The presence of a message-body in a request is signaled by the
2679 * inclusion of a Content-Length or Transfer-Encoding header field
2680 * in the request's header fields. When a request message contains
2681 * both a message-body of non-zero length and a method that does
2682 * not define any semantics for that request message-body, then an
2683 * origin server SHOULD either ignore the message-body or respond
2684 * with an appropriate error message (e.g., 413). A proxy or
2685 * gateway, when presented the same request, SHOULD either forward
2686 * the request inbound with the message- body or ignore the
2687 * message-body when determining a response.
2688 *
2689 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2690 * and the "chunked" transfer-coding (Section 6.2) is used, the
2691 * transfer-length is defined by the use of this transfer-coding.
2692 * If a Transfer-Encoding header field is present and the "chunked"
2693 * transfer-coding is not present, the transfer-length is defined
2694 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002695 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002696 * 3. If a Content-Length header field is present, its decimal value in
2697 * OCTETs represents both the entity-length and the transfer-length.
2698 * If a message is received with both a Transfer-Encoding header
2699 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002700 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002701 * 4. By the server closing the connection. (Closing the connection
2702 * cannot be used to indicate the end of a request body, since that
2703 * would leave no possibility for the server to send back a response.)
2704 *
2705 * Whenever a transfer-coding is applied to a message-body, the set of
2706 * transfer-codings MUST include "chunked", unless the message indicates
2707 * it is terminated by closing the connection. When the "chunked"
2708 * transfer-coding is used, it MUST be the last transfer-coding applied
2709 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002710 */
2711
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002712 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002713 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002714 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002715 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002716 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002717 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002718 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2719 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002720 /* bad transfer-encoding (chunked followed by something else) */
2721 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002722 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002723 break;
2724 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002725 }
2726
Willy Tarreau32b47f42009-10-18 20:55:02 +02002727 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002728 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002729 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002730 signed long long cl;
2731
Willy Tarreauad14f752011-09-02 20:33:27 +02002732 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002733 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002734 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002735 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002736
Willy Tarreauad14f752011-09-02 20:33:27 +02002737 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002738 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002739 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002740 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002741
Willy Tarreauad14f752011-09-02 20:33:27 +02002742 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002743 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002744 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002745 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002747 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002748 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002749 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002750 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002752 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002753 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002754 }
2755
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002756 /* bodyless requests have a known length */
2757 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002758 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002759
Willy Tarreaud787e662009-07-07 10:14:51 +02002760 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002761 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002762 req->analyse_exp = TICK_ETERNITY;
2763 return 1;
2764
2765 return_bad_req:
2766 /* We centralize bad requests processing here */
2767 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2768 /* we detected a parsing error. We want to archive this request
2769 * in the dedicated proxy area for later troubleshooting.
2770 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002771 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002772 }
2773
2774 txn->req.msg_state = HTTP_MSG_ERROR;
2775 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002776 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002777
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002778 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002779 if (s->listener->counters)
2780 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002781
2782 return_prx_cond:
2783 if (!(s->flags & SN_ERR_MASK))
2784 s->flags |= SN_ERR_PRXCOND;
2785 if (!(s->flags & SN_FINST_MASK))
2786 s->flags |= SN_FINST_R;
2787
2788 req->analysers = 0;
2789 req->analyse_exp = TICK_ETERNITY;
2790 return 0;
2791}
2792
Cyril Bonté70be45d2010-10-12 00:14:35 +02002793/* We reached the stats page through a POST request.
2794 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002795 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002796 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002797int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002798{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002799 struct proxy *px = NULL;
2800 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002801
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002802 char key[LINESIZE];
2803 int action = ST_ADM_ACTION_NONE;
2804 int reprocess = 0;
2805
2806 int total_servers = 0;
2807 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002808
2809 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002810 char *st_cur_param = NULL;
2811 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002812
Willy Tarreau9b28e032012-10-12 23:49:43 +02002813 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002814 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002815
2816 cur_param = next_param = end_params;
2817
Willy Tarreau9b28e032012-10-12 23:49:43 +02002818 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002819 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002820 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002821 return 1;
2822 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002823 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002824 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002825 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002826 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002827 }
2828
2829 *end_params = '\0';
2830
Willy Tarreau295a8372011-03-10 11:25:07 +01002831 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002832
2833 /*
2834 * Parse the parameters in reverse order to only store the last value.
2835 * From the html form, the backend and the action are at the end.
2836 */
2837 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002838 char *value;
2839 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002840
2841 cur_param--;
2842 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002843 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002844 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002845 poffset = (cur_param != first_param ? 1 : 0);
2846 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2847 if ((plen > 0) && (plen <= sizeof(key))) {
2848 strncpy(key, cur_param + poffset, plen);
2849 key[plen - 1] = '\0';
2850 } else {
2851 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2852 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002853 }
2854
2855 /* Parse the value */
2856 value = key;
2857 while (*value != '\0' && *value != '=') {
2858 value++;
2859 }
2860 if (*value == '=') {
2861 /* Ok, a value is found, we can mark the end of the key */
2862 *value++ = '\0';
2863 }
2864
Thierry FOURNIER5068d962013-10-04 16:27:27 +02002865 if (url_decode(key) < 0 || url_decode(value) < 0)
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002866 break;
2867
Cyril Bonté70be45d2010-10-12 00:14:35 +02002868 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002869 if (!px && (strcmp(key, "b") == 0)) {
2870 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2871 /* the backend name is unknown or ambiguous (duplicate names) */
2872 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2873 goto out;
2874 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002875 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002876 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002878 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002879 }
2880 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002881 action = ST_ADM_ACTION_ENABLE;
2882 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002883 else if (strcmp(value, "stop") == 0) {
2884 action = ST_ADM_ACTION_STOP;
2885 }
2886 else if (strcmp(value, "start") == 0) {
2887 action = ST_ADM_ACTION_START;
2888 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002889 else if (strcmp(value, "shutdown") == 0) {
2890 action = ST_ADM_ACTION_SHUTDOWN;
2891 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002892 else {
2893 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2894 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002895 }
2896 }
2897 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002898 if (!(px && action)) {
2899 /*
2900 * Indicates that we'll need to reprocess the parameters
2901 * as soon as backend and action are known
2902 */
2903 if (!reprocess) {
2904 st_cur_param = cur_param;
2905 st_next_param = next_param;
2906 }
2907 reprocess = 1;
2908 }
2909 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002910 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002911 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002912 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002913 /* Not already in maintenance, we can change the server state */
2914 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09002915 set_server_down(&sv->check);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002916 altered_servers++;
2917 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002918 }
2919 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002920 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002921 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002922 /* Already in maintenance, we can change the server state */
Simon Horman4a741432013-02-23 15:35:38 +09002923 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09002924 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002925 altered_servers++;
2926 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002927 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002928 break;
2929 case ST_ADM_ACTION_STOP:
2930 case ST_ADM_ACTION_START:
2931 if (action == ST_ADM_ACTION_START)
2932 sv->uweight = sv->iweight;
2933 else
2934 sv->uweight = 0;
2935
Willy Tarreau004e0452013-11-21 11:22:01 +01002936 server_recalc_eweight(sv);
Willy Tarreau0900bcb2013-12-04 00:05:29 +01002937 set_server_drain_state(sv);
Willy Tarreaud7282242012-06-04 00:22:44 +02002938
Willy Tarreaud7282242012-06-04 00:22:44 +02002939 altered_servers++;
2940 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002941 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002942 case ST_ADM_ACTION_SHUTDOWN:
2943 if (px->state != PR_STSTOPPED) {
2944 struct session *sess, *sess_bck;
2945
2946 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2947 if (sess->srv_conn == sv)
2948 session_shutdown(sess, SN_ERR_KILLED);
2949
2950 altered_servers++;
2951 total_servers++;
2952 }
2953 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002954 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002955 } else {
2956 /* the server name is unknown or ambiguous (duplicate names) */
2957 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002958 }
2959 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002960 if (reprocess && px && action) {
2961 /* Now, we know the backend and the action chosen by the user.
2962 * We can safely restart from the first server parameter
2963 * to reprocess them
2964 */
2965 cur_param = st_cur_param;
2966 next_param = st_next_param;
2967 reprocess = 0;
2968 goto reprocess_servers;
2969 }
2970
Cyril Bonté70be45d2010-10-12 00:14:35 +02002971 next_param = cur_param;
2972 }
2973 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002974
2975 if (total_servers == 0) {
2976 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2977 }
2978 else if (altered_servers == 0) {
2979 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2980 }
2981 else if (altered_servers == total_servers) {
2982 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2983 }
2984 else {
2985 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2986 }
2987 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002988 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002989}
2990
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002991/* This function checks whether we need to enable a POST analyser to parse a
2992 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002993 * if it needs to come back again, otherwise non-zero if it finishes. In the
2994 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002995 */
2996int http_handle_stats(struct session *s, struct channel *req)
2997{
2998 struct stats_admin_rule *stats_admin_rule;
2999 struct stream_interface *si = s->rep->prod;
3000 struct http_txn *txn = &s->txn;
3001 struct http_msg *msg = &txn->req;
3002 struct uri_auth *uri = s->be->uri_auth;
3003
3004 /* now check whether we have some admin rules for this request */
3005 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3006 int ret = 1;
3007
3008 if (stats_admin_rule->cond) {
3009 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3010 ret = acl_pass(ret);
3011 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3012 ret = !ret;
3013 }
3014
3015 if (ret) {
3016 /* no rule, or the rule matches */
3017 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
3018 break;
3019 }
3020 }
3021
3022 /* Was the status page requested with a POST ? */
3023 if (unlikely(txn->meth == HTTP_METH_POST)) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003024 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3025
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003026 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
3027 if (msg->msg_state < HTTP_MSG_100_SENT) {
3028 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3029 * send an HTTP/1.1 100 Continue intermediate response.
3030 */
3031 if (msg->flags & HTTP_MSGF_VER_11) {
3032 struct hdr_ctx ctx;
3033 ctx.idx = 0;
3034 /* Expect is allowed in 1.1, look for it */
3035 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3036 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3037 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3038 }
3039 }
3040 msg->msg_state = HTTP_MSG_100_SENT;
3041 s->logs.tv_request = now; /* update the request timer to reflect full request */
3042 }
3043 if (!http_process_req_stat_post(si, txn, req))
3044 return 0; /* we need more data */
3045 }
3046 else
3047 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003048 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3049 scope_txt[0] = 0;
3050 if (si->applet.ctx.stats.scope_len) {
3051 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3052 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(req->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3053 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3054 }
3055
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003056
3057 /* We don't want to land on the posted stats page because a refresh will
3058 * repost the data. We don't want this to happen on accident so we redirect
3059 * the browse to the stats page with a GET.
3060 */
3061 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04003062 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003063 "Cache-Control: no-cache\r\n"
3064 "Content-Type: text/plain\r\n"
3065 "Connection: close\r\n"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003066 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003067 "\r\n",
3068 uri->uri_prefix,
3069 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3070 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3071 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3072 stat_status_codes[si->applet.ctx.stats.st_code] :
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003073 stat_status_codes[STAT_STATUS_UNKN],
3074 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3075 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3076 scope_txt);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003077
3078 s->txn.status = 303;
3079 s->logs.tv_request = now;
3080 stream_int_retnclose(req->prod, &trash);
3081 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3082
3083 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3084 s->fe->fe_counters.intercepted_req++;
3085
3086 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003087 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003088 if (!(s->flags & SN_FINST_MASK))
3089 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003090 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003091 return 1;
3092 }
3093
3094 /* OK, let's go on now */
3095
3096 chunk_printf(&trash,
3097 "HTTP/1.0 200 OK\r\n"
3098 "Cache-Control: no-cache\r\n"
3099 "Connection: close\r\n"
3100 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003101 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003102
3103 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3104 chunk_appendf(&trash, "Refresh: %d\r\n",
3105 uri->refresh);
3106
3107 chunk_appendf(&trash, "\r\n");
3108
3109 s->txn.status = 200;
3110 s->logs.tv_request = now;
3111
3112 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3113 s->fe->fe_counters.intercepted_req++;
3114
3115 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003116 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003117 if (!(s->flags & SN_FINST_MASK))
3118 s->flags |= SN_FINST_R;
3119
3120 if (s->txn.meth == HTTP_METH_HEAD) {
3121 /* that's all we return in case of HEAD request, so let's immediately close. */
3122 stream_int_retnclose(req->prod, &trash);
3123 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003124 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003125 return 1;
3126 }
3127
3128 /* OK, push the response and hand over to the stats I/O handler */
3129 bi_putchk(s->rep, &trash);
3130
3131 s->task->nice = -32; /* small boost for HTTP statistics */
3132 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3133 s->target = s->rep->prod->conn->target; // for logging only
3134 s->rep->prod->conn->xprt_ctx = s;
3135 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3136 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003137 return 1;
3138}
3139
Lukas Tribus67db8df2013-06-23 17:37:13 +02003140/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3141 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3142 */
3143static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3144{
3145#ifdef IP_TOS
3146 if (from.ss_family == AF_INET)
3147 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3148#endif
3149#ifdef IPV6_TCLASS
3150 if (from.ss_family == AF_INET6) {
3151 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3152 /* v4-mapped addresses need IP_TOS */
3153 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3154 else
3155 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3156 }
3157#endif
3158}
3159
Willy Tarreau20b0de52012-12-24 15:45:22 +01003160/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003161 * transaction <txn>. Returns the first rule that prevents further processing
3162 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3163 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3164 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003165 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003166static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003167http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003168{
Willy Tarreauff011f22011-01-06 17:51:27 +01003169 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003170 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003171
Willy Tarreauff011f22011-01-06 17:51:27 +01003172 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003173 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003174 continue;
3175
Willy Tarreau96257ec2012-12-27 10:46:37 +01003176 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003177 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003178 int ret;
3179
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003180 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003181 ret = acl_pass(ret);
3182
Willy Tarreauff011f22011-01-06 17:51:27 +01003183 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003184 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003185
3186 if (!ret) /* condition not matched */
3187 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003188 }
3189
Willy Tarreau20b0de52012-12-24 15:45:22 +01003190
Willy Tarreau96257ec2012-12-27 10:46:37 +01003191 switch (rule->action) {
3192 case HTTP_REQ_ACT_ALLOW:
3193 return NULL; /* "allow" rules are OK */
3194
3195 case HTTP_REQ_ACT_DENY:
3196 txn->flags |= TX_CLDENY;
3197 return rule;
3198
Willy Tarreauccbcc372012-12-27 12:37:57 +01003199 case HTTP_REQ_ACT_TARPIT:
3200 txn->flags |= TX_CLTARPIT;
3201 return rule;
3202
Willy Tarreau96257ec2012-12-27 10:46:37 +01003203 case HTTP_REQ_ACT_AUTH:
3204 return rule;
3205
Willy Tarreau81499eb2012-12-27 12:19:02 +01003206 case HTTP_REQ_ACT_REDIR:
3207 return rule;
3208
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003209 case HTTP_REQ_ACT_SET_NICE:
3210 s->task->nice = rule->arg.nice;
3211 break;
3212
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003213 case HTTP_REQ_ACT_SET_TOS:
Lukas Tribus67db8df2013-06-23 17:37:13 +02003214 inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003215 break;
3216
Willy Tarreau51347ed2013-06-11 19:34:13 +02003217 case HTTP_REQ_ACT_SET_MARK:
3218#ifdef SO_MARK
3219 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3220#endif
3221 break;
3222
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003223 case HTTP_REQ_ACT_SET_LOGL:
3224 s->logs.level = rule->arg.loglevel;
3225 break;
3226
Willy Tarreau96257ec2012-12-27 10:46:37 +01003227 case HTTP_REQ_ACT_SET_HDR:
3228 ctx.idx = 0;
3229 /* remove all occurrences of the header */
3230 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3231 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3232 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003233 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003234 /* now fall through to header addition */
3235
3236 case HTTP_REQ_ACT_ADD_HDR:
3237 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3238 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3239 trash.len = rule->arg.hdr_add.name_len;
3240 trash.str[trash.len++] = ':';
3241 trash.str[trash.len++] = ' ';
3242 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3243 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3244 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003245 }
3246 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003247
3248 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003249 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003250}
3251
Willy Tarreau71241ab2012-12-27 11:30:54 +01003252
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003253/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3254 * transaction <txn>. Returns the first rule that prevents further processing
3255 * of the response (deny, ...) or NULL if it executed all rules or stopped
3256 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3257 * rule.
3258 */
3259static struct http_res_rule *
3260http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3261{
3262 struct http_res_rule *rule;
3263 struct hdr_ctx ctx;
3264
3265 list_for_each_entry(rule, rules, list) {
3266 if (rule->action >= HTTP_RES_ACT_MAX)
3267 continue;
3268
3269 /* check optional condition */
3270 if (rule->cond) {
3271 int ret;
3272
3273 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3274 ret = acl_pass(ret);
3275
3276 if (rule->cond->pol == ACL_COND_UNLESS)
3277 ret = !ret;
3278
3279 if (!ret) /* condition not matched */
3280 continue;
3281 }
3282
3283
3284 switch (rule->action) {
3285 case HTTP_RES_ACT_ALLOW:
3286 return NULL; /* "allow" rules are OK */
3287
3288 case HTTP_RES_ACT_DENY:
3289 txn->flags |= TX_SVDENY;
3290 return rule;
3291
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003292 case HTTP_RES_ACT_SET_NICE:
3293 s->task->nice = rule->arg.nice;
3294 break;
3295
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003296 case HTTP_RES_ACT_SET_TOS:
Lukas Tribus67db8df2013-06-23 17:37:13 +02003297 inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003298 break;
3299
Willy Tarreau51347ed2013-06-11 19:34:13 +02003300 case HTTP_RES_ACT_SET_MARK:
3301#ifdef SO_MARK
3302 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3303#endif
3304 break;
3305
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003306 case HTTP_RES_ACT_SET_LOGL:
3307 s->logs.level = rule->arg.loglevel;
3308 break;
3309
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003310 case HTTP_RES_ACT_SET_HDR:
3311 ctx.idx = 0;
3312 /* remove all occurrences of the header */
3313 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3314 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3315 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3316 }
3317 /* now fall through to header addition */
3318
3319 case HTTP_RES_ACT_ADD_HDR:
3320 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3321 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3322 trash.len = rule->arg.hdr_add.name_len;
3323 trash.str[trash.len++] = ':';
3324 trash.str[trash.len++] = ' ';
3325 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3326 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3327 break;
3328 }
3329 }
3330
3331 /* we reached the end of the rules, nothing to report */
3332 return NULL;
3333}
3334
3335
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336/* Perform an HTTP redirect based on the information in <rule>. The function
3337 * returns non-zero on success, or zero in case of a, irrecoverable error such
3338 * as too large a request to build a valid response.
3339 */
3340static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3341{
3342 struct http_msg *msg = &txn->req;
3343 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003344 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003345
3346 /* build redirect message */
3347 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003348 case 308:
3349 msg_fmt = HTTP_308;
3350 break;
3351 case 307:
3352 msg_fmt = HTTP_307;
3353 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003354 case 303:
3355 msg_fmt = HTTP_303;
3356 break;
3357 case 301:
3358 msg_fmt = HTTP_301;
3359 break;
3360 case 302:
3361 default:
3362 msg_fmt = HTTP_302;
3363 break;
3364 }
3365
3366 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3367 return 0;
3368
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003369 location = trash.str + trash.len;
3370
Willy Tarreau71241ab2012-12-27 11:30:54 +01003371 switch(rule->type) {
3372 case REDIRECT_TYPE_SCHEME: {
3373 const char *path;
3374 const char *host;
3375 struct hdr_ctx ctx;
3376 int pathlen;
3377 int hostlen;
3378
3379 host = "";
3380 hostlen = 0;
3381 ctx.idx = 0;
3382 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3383 host = ctx.line + ctx.val;
3384 hostlen = ctx.vlen;
3385 }
3386
3387 path = http_get_path(txn);
3388 /* build message using path */
3389 if (path) {
3390 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3391 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3392 int qs = 0;
3393 while (qs < pathlen) {
3394 if (path[qs] == '?') {
3395 pathlen = qs;
3396 break;
3397 }
3398 qs++;
3399 }
3400 }
3401 } else {
3402 path = "/";
3403 pathlen = 1;
3404 }
3405
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003406 if (rule->rdr_str) { /* this is an old "redirect" rule */
3407 /* check if we can add scheme + "://" + host + path */
3408 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3409 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003410
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003411 /* add scheme */
3412 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3413 trash.len += rule->rdr_len;
3414 }
3415 else {
3416 /* add scheme with executing log format */
3417 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003418
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003419 /* check if we can add scheme + "://" + host + path */
3420 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3421 return 0;
3422 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003423 /* add "://" */
3424 memcpy(trash.str + trash.len, "://", 3);
3425 trash.len += 3;
3426
3427 /* add host */
3428 memcpy(trash.str + trash.len, host, hostlen);
3429 trash.len += hostlen;
3430
3431 /* add path */
3432 memcpy(trash.str + trash.len, path, pathlen);
3433 trash.len += pathlen;
3434
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003435 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003436 if (trash.len && trash.str[trash.len - 1] != '/' &&
3437 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3438 if (trash.len > trash.size - 5)
3439 return 0;
3440 trash.str[trash.len] = '/';
3441 trash.len++;
3442 }
3443
3444 break;
3445 }
3446 case REDIRECT_TYPE_PREFIX: {
3447 const char *path;
3448 int pathlen;
3449
3450 path = http_get_path(txn);
3451 /* build message using path */
3452 if (path) {
3453 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3454 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3455 int qs = 0;
3456 while (qs < pathlen) {
3457 if (path[qs] == '?') {
3458 pathlen = qs;
3459 break;
3460 }
3461 qs++;
3462 }
3463 }
3464 } else {
3465 path = "/";
3466 pathlen = 1;
3467 }
3468
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003469 if (rule->rdr_str) { /* this is an old "redirect" rule */
3470 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3471 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003472
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003473 /* add prefix. Note that if prefix == "/", we don't want to
3474 * add anything, otherwise it makes it hard for the user to
3475 * configure a self-redirection.
3476 */
3477 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3478 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3479 trash.len += rule->rdr_len;
3480 }
3481 }
3482 else {
3483 /* add prefix with executing log format */
3484 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3485
3486 /* Check length */
3487 if (trash.len + pathlen > trash.size - 4)
3488 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003489 }
3490
3491 /* add path */
3492 memcpy(trash.str + trash.len, path, pathlen);
3493 trash.len += pathlen;
3494
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003495 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003496 if (trash.len && trash.str[trash.len - 1] != '/' &&
3497 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3498 if (trash.len > trash.size - 5)
3499 return 0;
3500 trash.str[trash.len] = '/';
3501 trash.len++;
3502 }
3503
3504 break;
3505 }
3506 case REDIRECT_TYPE_LOCATION:
3507 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003508 if (rule->rdr_str) { /* this is an old "redirect" rule */
3509 if (trash.len + rule->rdr_len > trash.size - 4)
3510 return 0;
3511
3512 /* add location */
3513 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3514 trash.len += rule->rdr_len;
3515 }
3516 else {
3517 /* add location with executing log format */
3518 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003519
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003520 /* Check left length */
3521 if (trash.len > trash.size - 4)
3522 return 0;
3523 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003524 break;
3525 }
3526
3527 if (rule->cookie_len) {
3528 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3529 trash.len += 14;
3530 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3531 trash.len += rule->cookie_len;
3532 memcpy(trash.str + trash.len, "\r\n", 2);
3533 trash.len += 2;
3534 }
3535
3536 /* add end of headers and the keep-alive/close status.
3537 * We may choose to set keep-alive if the Location begins
3538 * with a slash, because the client will come back to the
3539 * same server.
3540 */
3541 txn->status = rule->code;
3542 /* let's log the request time */
3543 s->logs.tv_request = now;
3544
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003545 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003546 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3547 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3548 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3549 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3550 /* keep-alive possible */
3551 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3552 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3553 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3554 trash.len += 30;
3555 } else {
3556 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3557 trash.len += 24;
3558 }
3559 }
3560 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3561 trash.len += 4;
3562 bo_inject(txn->rsp.chn, trash.str, trash.len);
3563 /* "eat" the request */
3564 bi_fast_delete(txn->req.chn->buf, msg->sov);
3565 msg->sov = 0;
3566 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3567 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3568 txn->req.msg_state = HTTP_MSG_CLOSED;
3569 txn->rsp.msg_state = HTTP_MSG_DONE;
3570 } else {
3571 /* keep-alive not possible */
3572 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3573 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3574 trash.len += 29;
3575 } else {
3576 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3577 trash.len += 23;
3578 }
3579 stream_int_retnclose(txn->req.chn->prod, &trash);
3580 txn->req.chn->analysers = 0;
3581 }
3582
3583 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003584 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003585 if (!(s->flags & SN_FINST_MASK))
3586 s->flags |= SN_FINST_R;
3587
3588 return 1;
3589}
3590
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003591/* This stream analyser runs all HTTP request processing which is common to
3592 * frontends and backends, which means blocking ACLs, filters, connection-close,
3593 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003594 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003595 * either needs more data or wants to immediately abort the request (eg: deny,
3596 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003597 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003598int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003599{
Willy Tarreaud787e662009-07-07 10:14:51 +02003600 struct http_txn *txn = &s->txn;
3601 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003602 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003603 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003604 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003605 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003606 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003607
Willy Tarreau655dce92009-11-08 13:10:58 +01003608 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003609 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003610 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003611 return 0;
3612 }
3613
Willy Tarreau3a816292009-07-07 10:55:49 +02003614 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003615 req->analyse_exp = TICK_ETERNITY;
3616
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003617 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 +02003618 now_ms, __FUNCTION__,
3619 s,
3620 req,
3621 req->rex, req->wex,
3622 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003623 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003624 req->analysers);
3625
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003626 /* first check whether we have some ACLs set to block this request */
3627 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003628 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003629
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003630 ret = acl_pass(ret);
3631 if (cond->pol == ACL_COND_UNLESS)
3632 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003633
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003634 if (ret) {
3635 txn->status = 403;
3636 /* let's log the request time */
3637 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003638 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003639 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003640 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003641 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003642 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003643
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003644 /* just in case we have some per-backend tracking */
3645 session_inc_be_http_req_ctr(s);
3646
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003647 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003648 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003649
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003650 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003651 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003652 do_stats = stats_check_uri(s->rep->prod, txn, px);
3653 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003654 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 +01003655 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003656 else
3657 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003658
Willy Tarreau3b44e722013-11-16 10:28:23 +01003659 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3660 * blocked by an http-request rule.
3661 */
3662 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003663 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003664 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003665 }
Willy Tarreau06619262006-12-17 08:37:22 +01003666
Willy Tarreau3b44e722013-11-16 10:28:23 +01003667 /* return a 403 if either rule has blocked */
3668 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003669 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003670 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003671 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003672 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003673 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003674 s->fe->fe_counters.denied_req++;
3675 if (s->fe != s->be)
3676 s->be->be_counters.denied_req++;
3677 if (s->listener->counters)
3678 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003679 goto return_prx_cond;
3680 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003681
3682 /* When a connection is tarpitted, we use the tarpit timeout,
3683 * which may be the same as the connect timeout if unspecified.
3684 * If unset, then set it to zero because we really want it to
3685 * eventually expire. We build the tarpit as an analyser.
3686 */
3687 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003688 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003689 /* wipe the request out so that we can drop the connection early
3690 * if the client closes first.
3691 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003692 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003693 req->analysers = 0; /* remove switching rules etc... */
3694 req->analysers |= AN_REQ_HTTP_TARPIT;
3695 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3696 if (!req->analyse_exp)
3697 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003698 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003699 s->fe->fe_counters.denied_req++;
3700 if (s->fe != s->be)
3701 s->be->be_counters.denied_req++;
3702 if (s->listener->counters)
3703 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003704 return 1;
3705 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003706 }
Willy Tarreau06619262006-12-17 08:37:22 +01003707
Willy Tarreau5b154472009-12-21 20:11:07 +01003708 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3709 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003710 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3711 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003712 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3713 * avoid to redo the same work if FE and BE have the same settings (common).
3714 * The method consists in checking if options changed between the two calls
3715 * (implying that either one is non-null, or one of them is non-null and we
3716 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003717 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003718
Willy Tarreaudc008c52010-02-01 16:20:08 +01003719 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3720 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3721 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3722 (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 +01003723 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003724
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003725 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3726 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003727 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003728 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3729 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003730 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003731 tmp = TX_CON_WANT_CLO;
3732
Willy Tarreau5b154472009-12-21 20:11:07 +01003733 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3734 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003735
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003736 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3737 /* parse the Connection header and possibly clean it */
3738 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003739 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003740 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3741 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003742 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003743 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003744 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003745 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003746 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003747
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003748 /* check if client or config asks for explicit close in KAL/SCL */
3749 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3750 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3751 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003752 (!(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 +01003753 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003754 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003755 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003756 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3757 }
Willy Tarreau78599912009-10-17 20:12:21 +02003758
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003759 /* we can be blocked here because the request needs to be authenticated,
3760 * either to pass or to access stats.
3761 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003762 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003763 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003764
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003765 if (!realm)
3766 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003768 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003769 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003770 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003771 /* on 401 we still count one error, because normal browsing
3772 * won't significantly increase the counter but brute force
3773 * attempts will.
3774 */
3775 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003776 goto return_prx_cond;
3777 }
3778
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003779 /* add request headers from the rule sets in the same order */
3780 list_for_each_entry(wl, &px->req_add, list) {
3781 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003782 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003783 ret = acl_pass(ret);
3784 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3785 ret = !ret;
3786 if (!ret)
3787 continue;
3788 }
3789
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003790 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003791 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003792 }
3793
3794 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3795 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3796 goto return_bad_req;
3797 req->analyse_exp = TICK_ETERNITY;
3798 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003799 }
3800
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003801 if (unlikely(do_stats)) {
3802 /* process the stats request now */
3803 if (!http_handle_stats(s, req)) {
3804 /* we need more data, let's come back here later */
3805 req->analysers |= an_bit;
3806 channel_dont_connect(req);
Willy Tarreau7fe33002013-04-21 08:04:22 +02003807 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003808 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003809 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003810 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003811
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003812 /* check whether we have some ACLs set to redirect this request */
3813 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003814 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003815 int ret;
3816
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003817 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003818 ret = acl_pass(ret);
3819 if (rule->cond->pol == ACL_COND_UNLESS)
3820 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003821 if (!ret)
3822 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003823 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003824 if (!http_apply_redirect_rule(rule, s, txn))
3825 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003826
Willy Tarreau71241ab2012-12-27 11:30:54 +01003827 req->analyse_exp = TICK_ETERNITY;
3828 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003829 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003830
Willy Tarreau2be39392010-01-03 17:24:51 +01003831 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3832 * If this happens, then the data will not come immediately, so we must
3833 * send all what we have without waiting. Note that due to the small gain
3834 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003835 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003836 * itself once used.
3837 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003838 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003839
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003840 /* that's OK for us now, let's move on to next analysers */
3841 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003842
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003843 return_bad_req:
3844 /* We centralize bad requests processing here */
3845 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3846 /* we detected a parsing error. We want to archive this request
3847 * in the dedicated proxy area for later troubleshooting.
3848 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003849 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003850 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003851
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003852 txn->req.msg_state = HTTP_MSG_ERROR;
3853 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003854 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003855
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003856 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003857 if (s->listener->counters)
3858 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003859
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003860 return_prx_cond:
3861 if (!(s->flags & SN_ERR_MASK))
3862 s->flags |= SN_ERR_PRXCOND;
3863 if (!(s->flags & SN_FINST_MASK))
3864 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003865
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003866 req->analysers = 0;
3867 req->analyse_exp = TICK_ETERNITY;
3868 return 0;
3869}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003870
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003871/* This function performs all the processing enabled for the current request.
3872 * It returns 1 if the processing can continue on next analysers, or zero if it
3873 * needs more data, encounters an error, or wants to immediately abort the
3874 * request. It relies on buffers flags, and updates s->req->analysers.
3875 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003876int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003877{
3878 struct http_txn *txn = &s->txn;
3879 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003880
Willy Tarreau655dce92009-11-08 13:10:58 +01003881 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003882 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003883 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003884 return 0;
3885 }
3886
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003887 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 +02003888 now_ms, __FUNCTION__,
3889 s,
3890 req,
3891 req->rex, req->wex,
3892 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003893 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003894 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003895
William Lallemand82fe75c2012-10-23 10:25:10 +02003896 if (s->fe->comp || s->be->comp)
3897 select_compression_request_header(s, req->buf);
3898
Willy Tarreau59234e92008-11-30 23:51:27 +01003899 /*
3900 * Right now, we know that we have processed the entire headers
3901 * and that unwanted requests have been filtered out. We can do
3902 * whatever we want with the remaining request. Also, now we
3903 * may have separate values for ->fe, ->be.
3904 */
Willy Tarreau06619262006-12-17 08:37:22 +01003905
Willy Tarreau59234e92008-11-30 23:51:27 +01003906 /*
3907 * If HTTP PROXY is set we simply get remote server address
3908 * parsing incoming request.
3909 */
3910 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003911 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 +01003912 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003913
Willy Tarreau59234e92008-11-30 23:51:27 +01003914 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003915 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003916 * Note that doing so might move headers in the request, but
3917 * the fields will stay coherent and the URI will not move.
3918 * This should only be performed in the backend.
3919 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003920 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003921 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3922 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003923
Willy Tarreau59234e92008-11-30 23:51:27 +01003924 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003925 * 8: the appsession cookie was looked up very early in 1.2,
3926 * so let's do the same now.
3927 */
3928
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003929 /* It needs to look into the URI unless persistence must be ignored */
3930 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003931 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 +01003932 }
3933
William Lallemanda73203e2012-03-12 12:48:57 +01003934 /* add unique-id if "header-unique-id" is specified */
3935
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003936 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3937 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3938 goto return_bad_req;
3939 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003940 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003941 }
William Lallemanda73203e2012-03-12 12:48:57 +01003942
3943 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003944 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3945 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003946 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003947 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003948 goto return_bad_req;
3949 }
3950
Cyril Bontéb21570a2009-11-29 20:04:48 +01003951 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003952 * 9: add X-Forwarded-For if either the frontend or the backend
3953 * asks for it.
3954 */
3955 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003956 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003957 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003958 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3959 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003960 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003961 /* The header is set to be added only if none is present
3962 * and we found it, so don't do anything.
3963 */
3964 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003965 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003966 /* Add an X-Forwarded-For header unless the source IP is
3967 * in the 'except' network range.
3968 */
3969 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003970 (((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 +01003971 != s->fe->except_net.s_addr) &&
3972 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003973 (((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 +01003974 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003975 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003976 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003977 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003978
3979 /* Note: we rely on the backend to get the header name to be used for
3980 * x-forwarded-for, because the header is really meant for the backends.
3981 * However, if the backend did not specify any option, we have to rely
3982 * on the frontend's header name.
3983 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003984 if (s->be->fwdfor_hdr_len) {
3985 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003986 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003987 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003988 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003989 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003990 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003991 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003992
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003993 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003994 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003995 }
3996 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003997 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003998 /* FIXME: for the sake of completeness, we should also support
3999 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004000 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004001 int len;
4002 char pn[INET6_ADDRSTRLEN];
4003 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004004 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004005 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004006
Willy Tarreau59234e92008-11-30 23:51:27 +01004007 /* Note: we rely on the backend to get the header name to be used for
4008 * x-forwarded-for, because the header is really meant for the backends.
4009 * However, if the backend did not specify any option, we have to rely
4010 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004011 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004012 if (s->be->fwdfor_hdr_len) {
4013 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004014 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004015 } else {
4016 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004017 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004018 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004019 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004020
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004021 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004022 goto return_bad_req;
4023 }
4024 }
4025
4026 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004027 * 10: add X-Original-To if either the frontend or the backend
4028 * asks for it.
4029 */
4030 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4031
4032 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004033 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004034 /* Add an X-Original-To header unless the destination IP is
4035 * in the 'except' network range.
4036 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004037 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004038
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004039 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004040 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004041 (((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 +02004042 != s->fe->except_to.s_addr) &&
4043 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004044 (((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 +02004045 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004046 int len;
4047 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004048 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004049
4050 /* Note: we rely on the backend to get the header name to be used for
4051 * x-original-to, because the header is really meant for the backends.
4052 * However, if the backend did not specify any option, we have to rely
4053 * on the frontend's header name.
4054 */
4055 if (s->be->orgto_hdr_len) {
4056 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004057 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004058 } else {
4059 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004060 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004061 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004062 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004063
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004064 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004065 goto return_bad_req;
4066 }
4067 }
4068 }
4069
Willy Tarreau50fc7772012-11-11 22:19:57 +01004070 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4071 * If an "Upgrade" token is found, the header is left untouched in order not to have
4072 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4073 * "Upgrade" is present in the Connection header.
4074 */
4075 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4076 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4077 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004078 unsigned int want_flags = 0;
4079
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004080 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004081 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
4082 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
4083 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004084 want_flags |= TX_CON_CLO_SET;
4085 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004086 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
4087 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
4088 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004089 want_flags |= TX_CON_KAL_SET;
4090 }
4091
4092 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004093 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004094 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004095
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004096
Willy Tarreau522d6c02009-12-06 18:49:18 +01004097 /* If we have no server assigned yet and we're balancing on url_param
4098 * with a POST request, we may be interested in checking the body for
4099 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004100 */
4101 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4102 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004103 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004104 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004105 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004106 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004107 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004108
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004109 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004110 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004111#ifdef TCP_QUICKACK
4112 /* We expect some data from the client. Unless we know for sure
4113 * we already have a full request, we have to re-enable quick-ack
4114 * in case we previously disabled it, otherwise we might cause
4115 * the client to delay further data.
4116 */
4117 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004118 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004119 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004120 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004121#endif
4122 }
Willy Tarreau03945942009-12-22 16:50:27 +01004123
Willy Tarreau59234e92008-11-30 23:51:27 +01004124 /*************************************************************
4125 * OK, that's finished for the headers. We have done what we *
4126 * could. Let's switch to the DATA state. *
4127 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004128 req->analyse_exp = TICK_ETERNITY;
4129 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004130
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004131 /* if the server closes the connection, we want to immediately react
4132 * and close the socket to save packets and syscalls.
4133 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004134 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4135 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004136
Willy Tarreau59234e92008-11-30 23:51:27 +01004137 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004138 /* OK let's go on with the BODY now */
4139 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004140
Willy Tarreau59234e92008-11-30 23:51:27 +01004141 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004142 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004143 /* we detected a parsing error. We want to archive this request
4144 * in the dedicated proxy area for later troubleshooting.
4145 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004146 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004147 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004148
Willy Tarreau59234e92008-11-30 23:51:27 +01004149 txn->req.msg_state = HTTP_MSG_ERROR;
4150 txn->status = 400;
4151 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004152 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004153
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004154 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004155 if (s->listener->counters)
4156 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004157
Willy Tarreau59234e92008-11-30 23:51:27 +01004158 if (!(s->flags & SN_ERR_MASK))
4159 s->flags |= SN_ERR_PRXCOND;
4160 if (!(s->flags & SN_FINST_MASK))
4161 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004162 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004163}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004164
Willy Tarreau60b85b02008-11-30 23:28:40 +01004165/* This function is an analyser which processes the HTTP tarpit. It always
4166 * returns zero, at the beginning because it prevents any other processing
4167 * from occurring, and at the end because it terminates the request.
4168 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004169int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004170{
4171 struct http_txn *txn = &s->txn;
4172
4173 /* This connection is being tarpitted. The CLIENT side has
4174 * already set the connect expiration date to the right
4175 * timeout. We just have to check that the client is still
4176 * there and that the timeout has not expired.
4177 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004178 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004179 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004180 !tick_is_expired(req->analyse_exp, now_ms))
4181 return 0;
4182
4183 /* We will set the queue timer to the time spent, just for
4184 * logging purposes. We fake a 500 server error, so that the
4185 * attacker will not suspect his connection has been tarpitted.
4186 * It will not cause trouble to the logs because we can exclude
4187 * the tarpitted connections by filtering on the 'PT' status flags.
4188 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004189 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4190
4191 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004192 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004193 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004194
4195 req->analysers = 0;
4196 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004197
Willy Tarreau60b85b02008-11-30 23:28:40 +01004198 if (!(s->flags & SN_ERR_MASK))
4199 s->flags |= SN_ERR_PRXCOND;
4200 if (!(s->flags & SN_FINST_MASK))
4201 s->flags |= SN_FINST_T;
4202 return 0;
4203}
4204
Willy Tarreaud34af782008-11-30 23:36:37 +01004205/* This function is an analyser which processes the HTTP request body. It looks
4206 * for parameters to be used for the load balancing algorithm (url_param). It
4207 * must only be called after the standard HTTP request processing has occurred,
4208 * because it expects the request to be parsed. It returns zero if it needs to
4209 * read more data, or 1 once it has completed its analysis.
4210 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004211int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004212{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004213 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004214 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004215 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004216
4217 /* We have to parse the HTTP request body to find any required data.
4218 * "balance url_param check_post" should have been the only way to get
4219 * into this. We were brought here after HTTP header analysis, so all
4220 * related structures are ready.
4221 */
4222
Willy Tarreau522d6c02009-12-06 18:49:18 +01004223 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4224 goto missing_data;
4225
4226 if (msg->msg_state < HTTP_MSG_100_SENT) {
4227 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4228 * send an HTTP/1.1 100 Continue intermediate response.
4229 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004230 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004231 struct hdr_ctx ctx;
4232 ctx.idx = 0;
4233 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004234 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004235 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004236 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004237 }
4238 }
4239 msg->msg_state = HTTP_MSG_100_SENT;
4240 }
4241
4242 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004243 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004244 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004245 * is still null. We must save the body in msg->next because it
4246 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004247 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004248 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004249
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004250 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004251 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4252 else
4253 msg->msg_state = HTTP_MSG_DATA;
4254 }
4255
4256 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004257 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004258 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004260 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004261 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004262
Willy Tarreau115acb92009-12-26 13:56:06 +01004263 if (!ret)
4264 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004265 else if (ret < 0) {
4266 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004267 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004268 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004269 }
4270
Willy Tarreaud98cf932009-12-27 22:54:55 +01004271 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004272 * We have the first data byte is in msg->sov. We're waiting for at
4273 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004274 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004275
Willy Tarreau124d9912011-03-01 20:30:48 +01004276 if (msg->body_len < limit)
4277 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004278
Willy Tarreau9b28e032012-10-12 23:49:43 +02004279 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004280 goto http_end;
4281
4282 missing_data:
4283 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004284 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004285 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004286 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004287 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004288
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004289 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004290 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004291 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004292
4293 if (!(s->flags & SN_ERR_MASK))
4294 s->flags |= SN_ERR_CLITO;
4295 if (!(s->flags & SN_FINST_MASK))
4296 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004297 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004298 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004299
4300 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004301 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004302 /* Not enough data. We'll re-use the http-request
4303 * timeout here. Ideally, we should set the timeout
4304 * relative to the accept() date. We just set the
4305 * request timeout once at the beginning of the
4306 * request.
4307 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004308 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004309 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004310 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004311 return 0;
4312 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004313
4314 http_end:
4315 /* The situation will not evolve, so let's give up on the analysis. */
4316 s->logs.tv_request = now; /* update the request timer to reflect full request */
4317 req->analysers &= ~an_bit;
4318 req->analyse_exp = TICK_ETERNITY;
4319 return 1;
4320
4321 return_bad_req: /* let's centralize all bad requests */
4322 txn->req.msg_state = HTTP_MSG_ERROR;
4323 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004324 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004325
Willy Tarreau79ebac62010-06-07 13:47:49 +02004326 if (!(s->flags & SN_ERR_MASK))
4327 s->flags |= SN_ERR_PRXCOND;
4328 if (!(s->flags & SN_FINST_MASK))
4329 s->flags |= SN_FINST_R;
4330
Willy Tarreau522d6c02009-12-06 18:49:18 +01004331 return_err_msg:
4332 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004333 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004334 if (s->listener->counters)
4335 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004336 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004337}
4338
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004339/* send a server's name with an outgoing request over an established connection.
4340 * Note: this function is designed to be called once the request has been scheduled
4341 * for being forwarded. This is the reason why it rewinds the buffer before
4342 * proceeding.
4343 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004344int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004345
4346 struct hdr_ctx ctx;
4347
Mark Lamourinec2247f02012-01-04 13:02:01 -05004348 char *hdr_name = be->server_id_hdr_name;
4349 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004350 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004351 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004352 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004353
William Lallemandd9e90662012-01-30 17:27:17 +01004354 ctx.idx = 0;
4355
Willy Tarreau9b28e032012-10-12 23:49:43 +02004356 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004357 if (old_o) {
4358 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004359 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004360 }
4361
Willy Tarreau9b28e032012-10-12 23:49:43 +02004362 old_i = chn->buf->i;
4363 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 -05004364 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004365 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004366 }
4367
4368 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004369 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004370 memcpy(hdr_val, hdr_name, hdr_name_len);
4371 hdr_val += hdr_name_len;
4372 *hdr_val++ = ':';
4373 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004374 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4375 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004376
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004377 if (old_o) {
4378 /* If this was a forwarded request, we must readjust the amount of
4379 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004380 * variations. Note that if the request was already scheduled for
4381 * forwarding, it had its req->sol pointing to the body, which
4382 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004383 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004384 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004385 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004386 }
4387
Mark Lamourinec2247f02012-01-04 13:02:01 -05004388 return 0;
4389}
4390
Willy Tarreau610ecce2010-01-04 21:15:02 +01004391/* Terminate current transaction and prepare a new one. This is very tricky
4392 * right now but it works.
4393 */
4394void http_end_txn_clean_session(struct session *s)
4395{
4396 /* FIXME: We need a more portable way of releasing a backend's and a
4397 * server's connections. We need a safer way to reinitialize buffer
4398 * flags. We also need a more accurate method for computing per-request
4399 * data.
4400 */
4401 http_silent_debug(__LINE__, s);
4402
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004403 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004404 si_shutr(s->req->cons);
4405 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004406
4407 http_silent_debug(__LINE__, s);
4408
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004409 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004411 if (unlikely(s->srv_conn))
4412 sess_change_server(s, NULL);
4413 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004414
4415 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4416 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004417 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004418
4419 if (s->txn.status) {
4420 int n;
4421
4422 n = s->txn.status / 100;
4423 if (n < 1 || n > 5)
4424 n = 0;
4425
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004426 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004427 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004428 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004429 s->fe->fe_counters.p.http.comp_rsp++;
4430 }
Willy Tarreau24657792010-02-26 10:30:28 +01004431 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004432 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004433 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004434 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004435 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004436 s->be->be_counters.p.http.comp_rsp++;
4437 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004438 }
4439
4440 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004441 s->logs.bytes_in -= s->req->buf->i;
4442 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004443
4444 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004445 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446 !(s->flags & SN_MONITOR) &&
4447 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4448 s->do_log(s);
4449 }
4450
4451 s->logs.accept_date = date; /* user-visible date for logging */
4452 s->logs.tv_accept = now; /* corrected date for internal use */
4453 tv_zero(&s->logs.tv_request);
4454 s->logs.t_queue = -1;
4455 s->logs.t_connect = -1;
4456 s->logs.t_data = -1;
4457 s->logs.t_close = 0;
4458 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4459 s->logs.srv_queue_size = 0; /* we will get this number soon */
4460
Willy Tarreau9b28e032012-10-12 23:49:43 +02004461 s->logs.bytes_in = s->req->total = s->req->buf->i;
4462 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463
4464 if (s->pend_pos)
4465 pendconn_free(s->pend_pos);
4466
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004467 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468 if (s->flags & SN_CURR_SESS) {
4469 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004470 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004471 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004472 if (may_dequeue_tasks(objt_server(s->target), s->be))
4473 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474 }
4475
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004476 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004477
4478 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004479 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4480 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004481 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004483 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004484 s->req->cons->err_loc = NULL;
4485 s->req->cons->exp = TICK_ETERNITY;
4486 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004487 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4488 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 +02004489 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 +01004490 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004491
4492 if (s->flags & SN_COMP_READY)
4493 s->comp_algo->end(&s->comp_ctx);
4494 s->comp_algo = NULL;
4495 s->flags &= ~SN_COMP_READY;
4496
Willy Tarreau610ecce2010-01-04 21:15:02 +01004497 s->txn.meth = 0;
4498 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004499 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004500 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004501 s->req->cons->flags |= SI_FL_INDEP_STR;
4502
Willy Tarreau96e31212011-05-30 18:10:30 +02004503 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004504 s->req->flags |= CF_NEVER_WAIT;
4505 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004506 }
4507
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 /* if the request buffer is not empty, it means we're
4509 * about to process another request, so send pending
4510 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004511 * Just don't do this if the buffer is close to be full,
4512 * because the request will wait for it to flush a little
4513 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004515 if (s->req->buf->i) {
4516 if (s->rep->buf->o &&
4517 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4518 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004519 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004520 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004521
4522 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004523 channel_auto_read(s->req);
4524 channel_auto_close(s->req);
4525 channel_auto_read(s->rep);
4526 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004527
Willy Tarreau342b11c2010-11-24 16:22:09 +01004528 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004529 s->rep->analysers = 0;
4530
4531 http_silent_debug(__LINE__, s);
4532}
4533
4534
4535/* This function updates the request state machine according to the response
4536 * state machine and buffer flags. It returns 1 if it changes anything (flag
4537 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4538 * it is only used to find when a request/response couple is complete. Both
4539 * this function and its equivalent should loop until both return zero. It
4540 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4541 */
4542int http_sync_req_state(struct session *s)
4543{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004544 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004546 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 unsigned int old_state = txn->req.msg_state;
4548
4549 http_silent_debug(__LINE__, s);
4550 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4551 return 0;
4552
4553 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004554 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004555 * We can shut the read side unless we want to abort_on_close,
4556 * or we have a POST request. The issue with POST requests is
4557 * that some browsers still send a CRLF after the request, and
4558 * this CRLF must be read so that it does not remain in the kernel
4559 * buffers, otherwise a close could cause an RST on some systems
4560 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004561 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004562 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004563 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564
Willy Tarreau40f151a2012-12-20 12:10:09 +01004565 /* if the server closes the connection, we want to immediately react
4566 * and close the socket to save packets and syscalls.
4567 */
4568 chn->cons->flags |= SI_FL_NOHALF;
4569
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4571 goto wait_other_side;
4572
4573 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4574 /* The server has not finished to respond, so we
4575 * don't want to move in order not to upset it.
4576 */
4577 goto wait_other_side;
4578 }
4579
4580 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4581 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004582 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004584 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004585 goto wait_other_side;
4586 }
4587
4588 /* When we get here, it means that both the request and the
4589 * response have finished receiving. Depending on the connection
4590 * mode, we'll have to wait for the last bytes to leave in either
4591 * direction, and sometimes for a close to be effective.
4592 */
4593
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004594 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4595 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004596 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4597 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004598 }
4599 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4600 /* Option forceclose is set, or either side wants to close,
4601 * let's enforce it now that we're not expecting any new
4602 * data to come. The caller knows the session is complete
4603 * once both states are CLOSED.
4604 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004605 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4606 channel_shutr_now(chn);
4607 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004608 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004609 }
4610 else {
4611 /* The last possible modes are keep-alive and tunnel. Since tunnel
4612 * mode does not set the body analyser, we can't reach this place
4613 * in tunnel mode, so we're left with keep-alive only.
4614 * This mode is currently not implemented, we switch to tunnel mode.
4615 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004616 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004617 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004618 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 }
4620
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004621 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004623 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004624
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004625 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 txn->req.msg_state = HTTP_MSG_CLOSING;
4627 goto http_msg_closing;
4628 }
4629 else {
4630 txn->req.msg_state = HTTP_MSG_CLOSED;
4631 goto http_msg_closed;
4632 }
4633 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004634 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 }
4636
4637 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4638 http_msg_closing:
4639 /* nothing else to forward, just waiting for the output buffer
4640 * to be empty and for the shutw_now to take effect.
4641 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004642 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004643 txn->req.msg_state = HTTP_MSG_CLOSED;
4644 goto http_msg_closed;
4645 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004646 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004647 txn->req.msg_state = HTTP_MSG_ERROR;
4648 goto wait_other_side;
4649 }
4650 }
4651
4652 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4653 http_msg_closed:
4654 goto wait_other_side;
4655 }
4656
4657 wait_other_side:
4658 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004659 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660}
4661
4662
4663/* This function updates the response state machine according to the request
4664 * state machine and buffer flags. It returns 1 if it changes anything (flag
4665 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4666 * it is only used to find when a request/response couple is complete. Both
4667 * this function and its equivalent should loop until both return zero. It
4668 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4669 */
4670int http_sync_res_state(struct session *s)
4671{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004672 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004673 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004674 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004675 unsigned int old_state = txn->rsp.msg_state;
4676
4677 http_silent_debug(__LINE__, s);
4678 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4679 return 0;
4680
4681 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4682 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004683 * still monitor the server connection for a possible close
4684 * while the request is being uploaded, so we don't disable
4685 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004686 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004687 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004688
4689 if (txn->req.msg_state == HTTP_MSG_ERROR)
4690 goto wait_other_side;
4691
4692 if (txn->req.msg_state < HTTP_MSG_DONE) {
4693 /* The client seems to still be sending data, probably
4694 * because we got an error response during an upload.
4695 * We have the choice of either breaking the connection
4696 * or letting it pass through. Let's do the later.
4697 */
4698 goto wait_other_side;
4699 }
4700
4701 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4702 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004703 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004705 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004706 goto wait_other_side;
4707 }
4708
4709 /* When we get here, it means that both the request and the
4710 * response have finished receiving. Depending on the connection
4711 * mode, we'll have to wait for the last bytes to leave in either
4712 * direction, and sometimes for a close to be effective.
4713 */
4714
4715 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4716 /* Server-close mode : shut read and wait for the request
4717 * side to close its output buffer. The caller will detect
4718 * when we're in DONE and the other is in CLOSED and will
4719 * catch that for the final cleanup.
4720 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004721 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4722 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004723 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004724 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4725 /* Option forceclose is set, or either side wants to close,
4726 * let's enforce it now that we're not expecting any new
4727 * data to come. The caller knows the session is complete
4728 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004730 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4731 channel_shutr_now(chn);
4732 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004733 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004734 }
4735 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004736 /* The last possible modes are keep-alive and tunnel. Since tunnel
4737 * mode does not set the body analyser, we can't reach this place
4738 * in tunnel mode, so we're left with keep-alive only.
4739 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004740 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004741 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004743 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004744 }
4745
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004746 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004747 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004748 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004749 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4750 goto http_msg_closing;
4751 }
4752 else {
4753 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4754 goto http_msg_closed;
4755 }
4756 }
4757 goto wait_other_side;
4758 }
4759
4760 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4761 http_msg_closing:
4762 /* nothing else to forward, just waiting for the output buffer
4763 * to be empty and for the shutw_now to take effect.
4764 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004765 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004766 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4767 goto http_msg_closed;
4768 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004769 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004771 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004772 if (objt_server(s->target))
4773 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004774 goto wait_other_side;
4775 }
4776 }
4777
4778 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4779 http_msg_closed:
4780 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004781 bi_erase(chn);
4782 channel_auto_close(chn);
4783 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004784 goto wait_other_side;
4785 }
4786
4787 wait_other_side:
4788 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004789 /* We force the response to leave immediately if we're waiting for the
4790 * other side, since there is no pending shutdown to push it out.
4791 */
4792 if (!channel_is_empty(chn))
4793 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004794 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004795}
4796
4797
4798/* Resync the request and response state machines. Return 1 if either state
4799 * changes.
4800 */
4801int http_resync_states(struct session *s)
4802{
4803 struct http_txn *txn = &s->txn;
4804 int old_req_state = txn->req.msg_state;
4805 int old_res_state = txn->rsp.msg_state;
4806
4807 http_silent_debug(__LINE__, s);
4808 http_sync_req_state(s);
4809 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004810 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004811 if (!http_sync_res_state(s))
4812 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004813 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004814 if (!http_sync_req_state(s))
4815 break;
4816 }
4817 http_silent_debug(__LINE__, s);
4818 /* OK, both state machines agree on a compatible state.
4819 * There are a few cases we're interested in :
4820 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4821 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4822 * directions, so let's simply disable both analysers.
4823 * - HTTP_MSG_CLOSED on the response only means we must abort the
4824 * request.
4825 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4826 * with server-close mode means we've completed one request and we
4827 * must re-initialize the server connection.
4828 */
4829
4830 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4831 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4832 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4833 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4834 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004835 channel_auto_close(s->req);
4836 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004838 channel_auto_close(s->rep);
4839 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004840 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004841 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4842 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004843 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004844 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004845 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004846 channel_auto_close(s->rep);
4847 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004848 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004849 channel_abort(s->req);
4850 channel_auto_close(s->req);
4851 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004852 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004853 }
4854 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4855 txn->rsp.msg_state == HTTP_MSG_DONE &&
4856 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4857 /* server-close: terminate this server connection and
4858 * reinitialize a fresh-new transaction.
4859 */
4860 http_end_txn_clean_session(s);
4861 }
4862
4863 http_silent_debug(__LINE__, s);
4864 return txn->req.msg_state != old_req_state ||
4865 txn->rsp.msg_state != old_res_state;
4866}
4867
Willy Tarreaud98cf932009-12-27 22:54:55 +01004868/* This function is an analyser which forwards request body (including chunk
4869 * sizes if any). It is called as soon as we must forward, even if we forward
4870 * zero byte. The only situation where it must not be called is when we're in
4871 * tunnel mode and we want to forward till the close. It's used both to forward
4872 * remaining data and to resync after end of body. It expects the msg_state to
4873 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4874 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004875 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004876 * bytes of pending data + the headers if not already done (between sol and sov).
4877 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004878 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004879int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004880{
4881 struct http_txn *txn = &s->txn;
4882 struct http_msg *msg = &s->txn.req;
4883
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004884 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4885 return 0;
4886
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004887 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004888 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004889 /* Output closed while we were sending data. We must abort and
4890 * wake the other side up.
4891 */
4892 msg->msg_state = HTTP_MSG_ERROR;
4893 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004894 return 1;
4895 }
4896
Willy Tarreau4fe41902010-06-07 22:27:41 +02004897 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004898 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004899
4900 /* Note that we don't have to send 100-continue back because we don't
4901 * need the data to complete our job, and it's up to the server to
4902 * decide whether to return 100, 417 or anything else in return of
4903 * an "Expect: 100-continue" header.
4904 */
4905
4906 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004907 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004908 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004909 * is still null. We must save the body in msg->next because it
4910 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004911 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004912 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004913
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004914 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004915 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004916 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004917 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004918 }
4919
Willy Tarreaud98cf932009-12-27 22:54:55 +01004920 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004921 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004922
Willy Tarreau610ecce2010-01-04 21:15:02 +01004923 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004924 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004925 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004926 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004927 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004928 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004929 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004930 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004931 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004932
Willy Tarreaucaabe412010-01-03 23:08:28 +01004933 if (msg->msg_state == HTTP_MSG_DATA) {
4934 /* must still forward */
4935 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004936 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004937
4938 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004939 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004940 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004942 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004943 }
4944 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004945 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004946 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004947 * TRAILERS state.
4948 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004949 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004950
Willy Tarreau54d23df2012-10-25 19:04:45 +02004951 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004952 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004953 else if (ret < 0) {
4954 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004955 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004956 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004957 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004958 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004959 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004960 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004961 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004962 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004963 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004964
4965 if (ret == 0)
4966 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004967 else if (ret < 0) {
4968 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004969 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004970 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004971 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004972 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004973 /* we're in MSG_CHUNK_SIZE now */
4974 }
4975 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004976 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004977
4978 if (ret == 0)
4979 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004980 else if (ret < 0) {
4981 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004982 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004983 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004985 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004986 /* we're in HTTP_MSG_DONE now */
4987 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004988 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004989 int old_state = msg->msg_state;
4990
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004992 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004993 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4994 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004995 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004996 if (http_resync_states(s)) {
4997 /* some state changes occurred, maybe the analyser
4998 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004999 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005000 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005001 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005002 /* request errors are most likely due to
5003 * the server aborting the transfer.
5004 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005005 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005006 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005007 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005008 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005010 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005011 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005012 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005013
5014 /* If "option abortonclose" is set on the backend, we
5015 * want to monitor the client's connection and forward
5016 * any shutdown notification to the server, which will
5017 * decide whether to close or to go on processing the
5018 * request.
5019 */
5020 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005021 channel_auto_read(req);
5022 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005023 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005024 else if (s->txn.meth == HTTP_METH_POST) {
5025 /* POST requests may require to read extra CRLF
5026 * sent by broken browsers and which could cause
5027 * an RST to be sent upon close on some systems
5028 * (eg: Linux).
5029 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005030 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005031 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005032
Willy Tarreau610ecce2010-01-04 21:15:02 +01005033 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005034 }
5035 }
5036
Willy Tarreaud98cf932009-12-27 22:54:55 +01005037 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005038 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005039 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005040 if (!(s->flags & SN_ERR_MASK))
5041 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005042 if (!(s->flags & SN_FINST_MASK)) {
5043 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5044 s->flags |= SN_FINST_H;
5045 else
5046 s->flags |= SN_FINST_D;
5047 }
5048
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005049 s->fe->fe_counters.cli_aborts++;
5050 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005051 if (objt_server(s->target))
5052 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005053
5054 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005055 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005056
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005057 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005058 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005059 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005061 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005062 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005063 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005064 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005065 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005066
Willy Tarreau5c620922011-05-11 19:56:11 +02005067 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005068 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005069 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005070 * modes are already handled by the stream sock layer. We must not do
5071 * this in content-length mode because it could present the MSG_MORE
5072 * flag with the last block of forwarded data, which would cause an
5073 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005074 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005075 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005076 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005077
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005079 return 0;
5080
Willy Tarreaud98cf932009-12-27 22:54:55 +01005081 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005082 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005083 if (s->listener->counters)
5084 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005085 return_bad_req_stats_ok:
5086 txn->req.msg_state = HTTP_MSG_ERROR;
5087 if (txn->status) {
5088 /* Note: we don't send any error if some data were already sent */
5089 stream_int_retnclose(req->prod, NULL);
5090 } else {
5091 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005092 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005093 }
5094 req->analysers = 0;
5095 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005096
5097 if (!(s->flags & SN_ERR_MASK))
5098 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005099 if (!(s->flags & SN_FINST_MASK)) {
5100 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5101 s->flags |= SN_FINST_H;
5102 else
5103 s->flags |= SN_FINST_D;
5104 }
5105 return 0;
5106
5107 aborted_xfer:
5108 txn->req.msg_state = HTTP_MSG_ERROR;
5109 if (txn->status) {
5110 /* Note: we don't send any error if some data were already sent */
5111 stream_int_retnclose(req->prod, NULL);
5112 } else {
5113 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005114 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005115 }
5116 req->analysers = 0;
5117 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5118
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005119 s->fe->fe_counters.srv_aborts++;
5120 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005121 if (objt_server(s->target))
5122 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005123
5124 if (!(s->flags & SN_ERR_MASK))
5125 s->flags |= SN_ERR_SRVCL;
5126 if (!(s->flags & SN_FINST_MASK)) {
5127 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5128 s->flags |= SN_FINST_H;
5129 else
5130 s->flags |= SN_FINST_D;
5131 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005132 return 0;
5133}
5134
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005135/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5136 * processing can continue on next analysers, or zero if it either needs more
5137 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5138 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5139 * when it has nothing left to do, and may remove any analyser when it wants to
5140 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005141 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005142int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005143{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005144 struct http_txn *txn = &s->txn;
5145 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005146 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005147 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005149 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005150
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005151 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 +02005152 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005153 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005154 rep,
5155 rep->rex, rep->wex,
5156 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005157 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005158 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005159
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005160 /*
5161 * Now parse the partial (or complete) lines.
5162 * We will check the response syntax, and also join multi-line
5163 * headers. An index of all the lines will be elaborated while
5164 * parsing.
5165 *
5166 * For the parsing, we use a 28 states FSM.
5167 *
5168 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005169 * rep->buf->p = beginning of response
5170 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5171 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005172 * msg->eol = end of current header or line (LF or CRLF)
5173 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005174 */
5175
Willy Tarreau83e3af02009-12-28 17:39:57 +01005176 /* There's a protected area at the end of the buffer for rewriting
5177 * purposes. We don't want to start to parse the request if the
5178 * protected area is affected, because we may have to move processed
5179 * data later, which is much more complicated.
5180 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005181 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005182 if (unlikely(!channel_reserved(rep))) {
5183 /* some data has still not left the buffer, wake us once that's done */
5184 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5185 goto abort_response;
5186 channel_dont_close(rep);
5187 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5188 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005189 }
5190
Willy Tarreau379357a2013-06-08 12:55:46 +02005191 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5192 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5193 buffer_slow_realign(rep->buf);
5194
Willy Tarreau9b28e032012-10-12 23:49:43 +02005195 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005196 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005197 }
5198
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005199 /* 1: we might have to print this header in debug mode */
5200 if (unlikely((global.mode & MODE_DEBUG) &&
5201 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005202 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005203 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005204
Willy Tarreau9b28e032012-10-12 23:49:43 +02005205 sol = rep->buf->p;
5206 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005207 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209 sol += hdr_idx_first_pos(&txn->hdr_idx);
5210 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005211
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005212 while (cur_idx) {
5213 eol = sol + txn->hdr_idx.v[cur_idx].len;
5214 debug_hdr("srvhdr", s, sol, eol);
5215 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5216 cur_idx = txn->hdr_idx.v[cur_idx].next;
5217 }
5218 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005219
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220 /*
5221 * Now we quickly check if we have found a full valid response.
5222 * If not so, we check the FD and buffer states before leaving.
5223 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005224 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225 * responses are checked first.
5226 *
5227 * Depending on whether the client is still there or not, we
5228 * may send an error response back or not. Note that normally
5229 * we should only check for HTTP status there, and check I/O
5230 * errors somewhere else.
5231 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232
Willy Tarreau655dce92009-11-08 13:10:58 +01005233 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005234 /* Invalid response */
5235 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5236 /* we detected a parsing error. We want to archive this response
5237 * in the dedicated proxy area for later troubleshooting.
5238 */
5239 hdr_response_bad:
5240 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005241 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005242
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005243 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005244 if (objt_server(s->target)) {
5245 objt_server(s->target)->counters.failed_resp++;
5246 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005247 }
Willy Tarreau64648412010-03-05 10:41:54 +01005248 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005249 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005250 rep->analysers = 0;
5251 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005252 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005253 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005254 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255
5256 if (!(s->flags & SN_ERR_MASK))
5257 s->flags |= SN_ERR_PRXCOND;
5258 if (!(s->flags & SN_FINST_MASK))
5259 s->flags |= SN_FINST_H;
5260
5261 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005262 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005263
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005265 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005266 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005267 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268 goto hdr_response_bad;
5269 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005270
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005271 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005272 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005273 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005274 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005275
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005276 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005277 if (objt_server(s->target)) {
5278 objt_server(s->target)->counters.failed_resp++;
5279 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005280 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005281
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005282 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005283 rep->analysers = 0;
5284 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005285 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005286 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005287 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005288
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005289 if (!(s->flags & SN_ERR_MASK))
5290 s->flags |= SN_ERR_SRVCL;
5291 if (!(s->flags & SN_FINST_MASK))
5292 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005293 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005294 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005295
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005296 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005297 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005298 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005299 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005300
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005301 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005302 if (objt_server(s->target)) {
5303 objt_server(s->target)->counters.failed_resp++;
5304 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005305 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005306
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005307 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005308 rep->analysers = 0;
5309 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005310 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005311 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005312 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005313
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005314 if (!(s->flags & SN_ERR_MASK))
5315 s->flags |= SN_ERR_SRVTO;
5316 if (!(s->flags & SN_FINST_MASK))
5317 s->flags |= SN_FINST_H;
5318 return 0;
5319 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005320
Willy Tarreauf003d372012-11-26 13:35:37 +01005321 /* client abort with an abortonclose */
5322 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5323 s->fe->fe_counters.cli_aborts++;
5324 s->be->be_counters.cli_aborts++;
5325 if (objt_server(s->target))
5326 objt_server(s->target)->counters.cli_aborts++;
5327
5328 rep->analysers = 0;
5329 channel_auto_close(rep);
5330
5331 txn->status = 400;
5332 bi_erase(rep);
5333 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5334
5335 if (!(s->flags & SN_ERR_MASK))
5336 s->flags |= SN_ERR_CLICL;
5337 if (!(s->flags & SN_FINST_MASK))
5338 s->flags |= SN_FINST_H;
5339
5340 /* process_session() will take care of the error */
5341 return 0;
5342 }
5343
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005344 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005345 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005346 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005347 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005348
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005349 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005350 if (objt_server(s->target)) {
5351 objt_server(s->target)->counters.failed_resp++;
5352 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005353 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005354
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005355 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005356 rep->analysers = 0;
5357 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005358 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005359 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005360 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005361
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005362 if (!(s->flags & SN_ERR_MASK))
5363 s->flags |= SN_ERR_SRVCL;
5364 if (!(s->flags & SN_FINST_MASK))
5365 s->flags |= SN_FINST_H;
5366 return 0;
5367 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005368
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005369 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005370 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005371 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005372 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005373
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005374 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005375 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005376 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005377
5378 if (!(s->flags & SN_ERR_MASK))
5379 s->flags |= SN_ERR_CLICL;
5380 if (!(s->flags & SN_FINST_MASK))
5381 s->flags |= SN_FINST_H;
5382
5383 /* process_session() will take care of the error */
5384 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005385 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005386
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005387 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005388 return 0;
5389 }
5390
5391 /* More interesting part now : we know that we have a complete
5392 * response which at least looks like HTTP. We have an indicator
5393 * of each header's length, so we can parse them quickly.
5394 */
5395
5396 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005397 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005399 /*
5400 * 1: get the status code
5401 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005402 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005403 if (n < 1 || n > 5)
5404 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005405 /* when the client triggers a 4xx from the server, it's most often due
5406 * to a missing object or permission. These events should be tracked
5407 * because if they happen often, it may indicate a brute force or a
5408 * vulnerability scan.
5409 */
5410 if (n == 4)
5411 session_inc_http_err_ctr(s);
5412
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005413 if (objt_server(s->target))
5414 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415
Willy Tarreau5b154472009-12-21 20:11:07 +01005416 /* check if the response is HTTP/1.1 or above */
5417 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005418 ((rep->buf->p[5] > '1') ||
5419 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005420 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005421
5422 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005423 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 +01005424
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005425 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005426 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005427
Willy Tarreau9b28e032012-10-12 23:49:43 +02005428 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429
Willy Tarreau39650402010-03-15 19:44:39 +01005430 /* Adjust server's health based on status code. Note: status codes 501
5431 * and 505 are triggered on demand by client request, so we must not
5432 * count them as server failures.
5433 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005434 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005435 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005436 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005437 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005438 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005439 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005440
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005441 /*
5442 * 2: check for cacheability.
5443 */
5444
5445 switch (txn->status) {
5446 case 200:
5447 case 203:
5448 case 206:
5449 case 300:
5450 case 301:
5451 case 410:
5452 /* RFC2616 @13.4:
5453 * "A response received with a status code of
5454 * 200, 203, 206, 300, 301 or 410 MAY be stored
5455 * by a cache (...) unless a cache-control
5456 * directive prohibits caching."
5457 *
5458 * RFC2616 @9.5: POST method :
5459 * "Responses to this method are not cacheable,
5460 * unless the response includes appropriate
5461 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005462 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005464 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005465 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5466 break;
5467 default:
5468 break;
5469 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005470
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005471 /*
5472 * 3: we may need to capture headers
5473 */
5474 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005475 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005476 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005477 txn->rsp.cap, s->fe->rsp_cap);
5478
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005479 /* 4: determine the transfer-length.
5480 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5481 * the presence of a message-body in a RESPONSE and its transfer length
5482 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005483 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005484 * All responses to the HEAD request method MUST NOT include a
5485 * message-body, even though the presence of entity-header fields
5486 * might lead one to believe they do. All 1xx (informational), 204
5487 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5488 * message-body. All other responses do include a message-body,
5489 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005490 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005491 * 1. Any response which "MUST NOT" include a message-body (such as the
5492 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5493 * always terminated by the first empty line after the header fields,
5494 * regardless of the entity-header fields present in the message.
5495 *
5496 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5497 * the "chunked" transfer-coding (Section 6.2) is used, the
5498 * transfer-length is defined by the use of this transfer-coding.
5499 * If a Transfer-Encoding header field is present and the "chunked"
5500 * transfer-coding is not present, the transfer-length is defined by
5501 * the sender closing the connection.
5502 *
5503 * 3. If a Content-Length header field is present, its decimal value in
5504 * OCTETs represents both the entity-length and the transfer-length.
5505 * If a message is received with both a Transfer-Encoding header
5506 * field and a Content-Length header field, the latter MUST be ignored.
5507 *
5508 * 4. If the message uses the media type "multipart/byteranges", and
5509 * the transfer-length is not otherwise specified, then this self-
5510 * delimiting media type defines the transfer-length. This media
5511 * type MUST NOT be used unless the sender knows that the recipient
5512 * can parse it; the presence in a request of a Range header with
5513 * multiple byte-range specifiers from a 1.1 client implies that the
5514 * client can parse multipart/byteranges responses.
5515 *
5516 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005517 */
5518
5519 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005520 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005521 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005522 * FIXME: should we parse anyway and return an error on chunked encoding ?
5523 */
5524 if (txn->meth == HTTP_METH_HEAD ||
5525 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005526 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005527 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005528 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005529 goto skip_content_length;
5530 }
5531
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005532 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005533 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005534 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005535 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005536 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005537 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5538 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005539 /* bad transfer-encoding (chunked followed by something else) */
5540 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005541 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005542 break;
5543 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005544 }
5545
5546 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5547 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005548 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005549 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005550 signed long long cl;
5551
Willy Tarreauad14f752011-09-02 20:33:27 +02005552 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005553 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005554 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005555 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005556
Willy Tarreauad14f752011-09-02 20:33:27 +02005557 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005558 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005559 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005560 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005561
Willy Tarreauad14f752011-09-02 20:33:27 +02005562 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005563 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005564 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005565 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005566
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005567 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005568 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005569 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005570 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005571
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005572 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005573 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574 }
5575
William Lallemand82fe75c2012-10-23 10:25:10 +02005576 if (s->fe->comp || s->be->comp)
5577 select_compression_response_header(s, rep->buf);
5578
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005579 /* FIXME: we should also implement the multipart/byterange method.
5580 * For now on, we resort to close mode in this case (unknown length).
5581 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005582skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005583
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005584 /* end of job, return OK */
5585 rep->analysers &= ~an_bit;
5586 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005587 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005588 return 1;
5589}
5590
5591/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005592 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5593 * and updates t->rep->analysers. It might make sense to explode it into several
5594 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005595 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005596int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005597{
5598 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005599 struct http_msg *msg = &txn->rsp;
5600 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005601 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005602 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005603
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005604 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 +02005605 now_ms, __FUNCTION__,
5606 t,
5607 rep,
5608 rep->rex, rep->wex,
5609 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005610 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005611 rep->analysers);
5612
Willy Tarreau655dce92009-11-08 13:10:58 +01005613 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005614 return 0;
5615
5616 rep->analysers &= ~an_bit;
5617 rep->analyse_exp = TICK_ETERNITY;
5618
Willy Tarreau5b154472009-12-21 20:11:07 +01005619 /* Now we have to check if we need to modify the Connection header.
5620 * This is more difficult on the response than it is on the request,
5621 * because we can have two different HTTP versions and we don't know
5622 * how the client will interprete a response. For instance, let's say
5623 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5624 * HTTP/1.1 response without any header. Maybe it will bound itself to
5625 * HTTP/1.0 because it only knows about it, and will consider the lack
5626 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5627 * the lack of header as a keep-alive. Thus we will use two flags
5628 * indicating how a request MAY be understood by the client. In case
5629 * of multiple possibilities, we'll fix the header to be explicit. If
5630 * ambiguous cases such as both close and keepalive are seen, then we
5631 * will fall back to explicit close. Note that we won't take risks with
5632 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005633 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005634 */
5635
Willy Tarreaudc008c52010-02-01 16:20:08 +01005636 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5637 txn->status == 101)) {
5638 /* Either we've established an explicit tunnel, or we're
5639 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005640 * to understand the next protocols. We have to switch to tunnel
5641 * mode, so that we transfer the request and responses then let
5642 * this protocol pass unmodified. When we later implement specific
5643 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005644 * header which contains information about that protocol for
5645 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005646 */
5647 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5648 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005649 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5650 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5651 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005652 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005653
Willy Tarreau60466522010-01-18 19:08:45 +01005654 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005655 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005656 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5657 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005658
Willy Tarreau60466522010-01-18 19:08:45 +01005659 /* now adjust header transformations depending on current state */
5660 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5661 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5662 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005663 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005664 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005665 }
Willy Tarreau60466522010-01-18 19:08:45 +01005666 else { /* SCL / KAL */
5667 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005668 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005669 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005670 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005671
Willy Tarreau60466522010-01-18 19:08:45 +01005672 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005673 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005674
Willy Tarreau60466522010-01-18 19:08:45 +01005675 /* Some keep-alive responses are converted to Server-close if
5676 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005677 */
Willy Tarreau60466522010-01-18 19:08:45 +01005678 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5679 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005680 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005681 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005682 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005683 }
5684
Willy Tarreau7959a552013-09-23 16:44:27 +02005685 /* we want to have the response time before we start processing it */
5686 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
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 Tarreau7959a552013-09-23 16:44:27 +02005719 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005720 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005721 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005722 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005723 if (!(t->flags & SN_ERR_MASK))
5724 t->flags |= SN_ERR_PRXCOND;
5725 if (!(t->flags & SN_FINST_MASK))
5726 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005727 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005728 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005729 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005730
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005731 /* has the response been denied ? */
5732 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005733 if (objt_server(t->target))
5734 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005735
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005736 t->be->be_counters.denied_resp++;
5737 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005738 if (t->listener->counters)
5739 t->listener->counters->denied_resp++;
5740
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005741 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005742 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005743
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005744 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005745 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005746 if (txn->status < 200)
5747 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005748 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005749 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005750 ret = acl_pass(ret);
5751 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5752 ret = !ret;
5753 if (!ret)
5754 continue;
5755 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005756 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005757 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005758 }
5759
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005760 /* check whether we're already working on the frontend */
5761 if (cur_proxy == t->fe)
5762 break;
5763 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005764 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005765
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005766 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005767 * We may be facing a 100-continue response, in which case this
5768 * is not the right response, and we're waiting for the next one.
5769 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005770 * next one.
5771 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005772 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005773 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005774 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005775 msg->msg_state = HTTP_MSG_RPBEFORE;
5776 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005777 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005778 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5779 return 1;
5780 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005781 else if (unlikely(txn->status < 200))
5782 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005783
5784 /* we don't have any 1xx status code now */
5785
5786 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005787 * 4: check for server cookie.
5788 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005789 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5790 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005791 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005792
Willy Tarreaubaaee002006-06-26 02:48:02 +02005793
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005795 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005796 */
Willy Tarreau67402132012-05-31 20:40:20 +02005797 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005798 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005799
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005800 /*
5801 * 6: add server cookie in the response if needed
5802 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005803 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005804 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005805 (!(t->flags & SN_DIRECT) ||
5806 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5807 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5808 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5809 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005810 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005811 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005812 /* the server is known, it's not the one the client requested, or the
5813 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005814 * insert a set-cookie here, except if we want to insert only on POST
5815 * requests and this one isn't. Note that servers which don't have cookies
5816 * (eg: some backup servers) will return a full cookie removal request.
5817 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005818 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005819 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005820 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5821 t->be->cookie_name);
5822 }
5823 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005824 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005825
5826 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5827 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005828 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5829 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5830 trash.len += 5;
5831
Willy Tarreauef4f3912010-10-07 21:00:29 +02005832 if (t->be->cookie_maxlife) {
5833 /* emit first_date, which is either the original one or
5834 * the current date.
5835 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005836 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005837 s30tob64(txn->cookie_first_date ?
5838 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005839 (date.tv_sec+3) >> 2, trash.str + trash.len);
5840 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005841 }
5842 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005843 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005844 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005845
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005846 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005847 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005848
Willy Tarreau4992dd22012-05-31 21:02:17 +02005849 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005850 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005851
5852 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005853 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005854
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005855 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005856 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005857
Willy Tarreauf1348312010-10-07 15:54:11 +02005858 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005859 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005860 /* the server did not change, only the date was updated */
5861 txn->flags |= TX_SCK_UPDATED;
5862 else
5863 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005864
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005865 /* Here, we will tell an eventual cache on the client side that we don't
5866 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5867 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5868 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5869 */
Willy Tarreau67402132012-05-31 20:40:20 +02005870 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005871
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005872 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5873
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005874 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005875 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877 }
5878 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005879
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005880 /*
5881 * 7: check if result will be cacheable with a cookie.
5882 * We'll block the response if security checks have caught
5883 * nasty things such as a cacheable cookie.
5884 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005885 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5886 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005887 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005888
5889 /* we're in presence of a cacheable response containing
5890 * a set-cookie header. We'll block it as requested by
5891 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005892 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005893 if (objt_server(t->target))
5894 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005895
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005896 t->be->be_counters.denied_resp++;
5897 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005898 if (t->listener->counters)
5899 t->listener->counters->denied_resp++;
5900
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005901 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005902 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005903 send_log(t->be, LOG_ALERT,
5904 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005905 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005906 goto return_srv_prx_502;
5907 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005908
5909 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005910 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005911 * If an "Upgrade" token is found, the header is left untouched in order
5912 * not to have to deal with some client bugs : some of them fail an upgrade
5913 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005914 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005915 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5916 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5917 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005918 unsigned int want_flags = 0;
5919
5920 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5921 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5922 /* we want a keep-alive response here. Keep-alive header
5923 * required if either side is not 1.1.
5924 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005925 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005926 want_flags |= TX_CON_KAL_SET;
5927 }
5928 else {
5929 /* we want a close response here. Close header required if
5930 * the server is 1.1, regardless of the client.
5931 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005932 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005933 want_flags |= TX_CON_CLO_SET;
5934 }
5935
5936 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005937 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005938 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005940 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005941 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005942 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005943 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005944
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005945 /*************************************************************
5946 * OK, that's finished for the headers. We have done what we *
5947 * could. Let's switch to the DATA state. *
5948 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005949
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;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006349 break;
6350
6351 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006352 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006354 break;
6355
6356 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006357 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6358 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006359 /* FIXME: if the user adds a newline in the replacement, the
6360 * index will not be recalculated for now, and the new line
6361 * will not be counted as a new header.
6362 */
6363
6364 cur_end += delta;
6365 cur_next += delta;
6366 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006367 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006368 break;
6369
6370 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006371 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006372 cur_next += delta;
6373
Willy Tarreaufa355d42009-11-29 18:12:29 +01006374 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006375 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6376 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006377 cur_hdr->len = 0;
6378 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006379 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380 break;
6381
6382 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006383 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006384 if (cur_end)
6385 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006387 /* keep the link from this header to next one in case of later
6388 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006389 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006390 old_idx = cur_idx;
6391 }
6392 return 0;
6393}
6394
6395
6396/* Apply the filter to the request line.
6397 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6398 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006399 * Since it can manage the switch to another backend, it updates the per-proxy
6400 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006401 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006402int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006403{
6404 char term;
6405 char *cur_ptr, *cur_end;
6406 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006407 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006408 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006409
Willy Tarreau3d300592007-03-18 18:34:41 +01006410 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006411 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006412 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006413 (exp->action == ACT_ALLOW ||
6414 exp->action == ACT_DENY ||
6415 exp->action == ACT_TARPIT))
6416 return 0;
6417 else if (exp->action == ACT_REMOVE)
6418 return 0;
6419
6420 done = 0;
6421
Willy Tarreau9b28e032012-10-12 23:49:43 +02006422 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006423 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424
6425 /* Now we have the request line between cur_ptr and cur_end */
6426
6427 /* The annoying part is that pattern matching needs
6428 * that we modify the contents to null-terminate all
6429 * strings before testing them.
6430 */
6431
6432 term = *cur_end;
6433 *cur_end = '\0';
6434
6435 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6436 switch (exp->action) {
6437 case ACT_SETBE:
6438 /* It is not possible to jump a second time.
6439 * FIXME: should we return an HTTP/500 here so that
6440 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006442 if (t->be != t->fe)
6443 break;
6444
6445 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006446 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 done = 1;
6448 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006450 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006451 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006452 done = 1;
6453 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006455 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006456 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006457 done = 1;
6458 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006460 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006461 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006462 done = 1;
6463 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465 case ACT_REPLACE:
6466 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006467 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6468 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 /* FIXME: if the user adds a newline in the replacement, the
6470 * index will not be recalculated for now, and the new line
6471 * will not be counted as a new header.
6472 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006473
Willy Tarreaufa355d42009-11-29 18:12:29 +01006474 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006476 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 HTTP_MSG_RQMETH,
6478 cur_ptr, cur_end + 1,
6479 NULL, NULL);
6480 if (unlikely(!cur_end))
6481 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 /* we have a full request and we know that we have either a CR
6484 * or an LF at <ptr>.
6485 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006486 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6487 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 /* there is no point trying this regex on headers */
6489 return 1;
6490 }
6491 }
6492 *cur_end = term; /* restore the string terminator */
6493 return done;
6494}
Willy Tarreau97de6242006-12-27 17:18:38 +01006495
Willy Tarreau58f10d72006-12-04 02:26:12 +01006496
Willy Tarreau58f10d72006-12-04 02:26:12 +01006497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006498/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006499 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006500 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006501 * unparsable request. Since it can manage the switch to another backend, it
6502 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006504int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006506 struct http_txn *txn = &s->txn;
6507 struct hdr_exp *exp;
6508
6509 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006510 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 /*
6513 * The interleaving of transformations and verdicts
6514 * makes it difficult to decide to continue or stop
6515 * the evaluation.
6516 */
6517
Willy Tarreau6c123b12010-01-28 20:22:06 +01006518 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6519 break;
6520
Willy Tarreau3d300592007-03-18 18:34:41 +01006521 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006523 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006525
6526 /* if this filter had a condition, evaluate it now and skip to
6527 * next filter if the condition does not match.
6528 */
6529 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006530 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006531 ret = acl_pass(ret);
6532 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6533 ret = !ret;
6534
6535 if (!ret)
6536 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006537 }
6538
6539 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006540 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 if (unlikely(ret < 0))
6542 return -1;
6543
6544 if (likely(ret == 0)) {
6545 /* The filter did not match the request, it can be
6546 * iterated through all headers.
6547 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006548 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006549 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006550 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006551 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552}
6553
6554
Willy Tarreaua15645d2007-03-18 16:22:39 +01006555
Willy Tarreau58f10d72006-12-04 02:26:12 +01006556/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006557 * Try to retrieve the server associated to the appsession.
6558 * If the server is found, it's assigned to the session.
6559 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006560void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006561 struct http_txn *txn = &t->txn;
6562 appsess *asession = NULL;
6563 char *sessid_temp = NULL;
6564
Cyril Bontéb21570a2009-11-29 20:04:48 +01006565 if (len > t->be->appsession_len) {
6566 len = t->be->appsession_len;
6567 }
6568
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006569 if (t->be->options2 & PR_O2_AS_REQL) {
6570 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006571 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006572 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006573 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006574 }
6575
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006576 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006577 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6578 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6579 return;
6580 }
6581
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006582 memcpy(txn->sessid, buf, len);
6583 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006584 }
6585
6586 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6587 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6588 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6589 return;
6590 }
6591
Cyril Bontéb21570a2009-11-29 20:04:48 +01006592 memcpy(sessid_temp, buf, len);
6593 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006594
6595 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6596 /* free previously allocated memory */
6597 pool_free2(apools.sessid, sessid_temp);
6598
6599 if (asession != NULL) {
6600 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6601 if (!(t->be->options2 & PR_O2_AS_REQL))
6602 asession->request_count++;
6603
6604 if (asession->serverid != NULL) {
6605 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006606
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006607 while (srv) {
6608 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006609 if ((srv->state & SRV_RUNNING) ||
6610 (t->be->options & PR_O_PERSIST) ||
6611 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006612 /* we found the server and it's usable */
6613 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006614 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006615 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006616 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006617
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006618 break;
6619 } else {
6620 txn->flags &= ~TX_CK_MASK;
6621 txn->flags |= TX_CK_DOWN;
6622 }
6623 }
6624 srv = srv->next;
6625 }
6626 }
6627 }
6628}
6629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006630/* Find the end of a cookie value contained between <s> and <e>. It works the
6631 * same way as with headers above except that the semi-colon also ends a token.
6632 * See RFC2965 for more information. Note that it requires a valid header to
6633 * return a valid result.
6634 */
6635char *find_cookie_value_end(char *s, const char *e)
6636{
6637 int quoted, qdpair;
6638
6639 quoted = qdpair = 0;
6640 for (; s < e; s++) {
6641 if (qdpair) qdpair = 0;
6642 else if (quoted) {
6643 if (*s == '\\') qdpair = 1;
6644 else if (*s == '"') quoted = 0;
6645 }
6646 else if (*s == '"') quoted = 1;
6647 else if (*s == ',' || *s == ';') return s;
6648 }
6649 return s;
6650}
6651
6652/* Delete a value in a header between delimiters <from> and <next> in buffer
6653 * <buf>. The number of characters displaced is returned, and the pointer to
6654 * the first delimiter is updated if required. The function tries as much as
6655 * possible to respect the following principles :
6656 * - replace <from> delimiter by the <next> one unless <from> points to a
6657 * colon, in which case <next> is simply removed
6658 * - set exactly one space character after the new first delimiter, unless
6659 * there are not enough characters in the block being moved to do so.
6660 * - remove unneeded spaces before the previous delimiter and after the new
6661 * one.
6662 *
6663 * It is the caller's responsibility to ensure that :
6664 * - <from> points to a valid delimiter or the colon ;
6665 * - <next> points to a valid delimiter or the final CR/LF ;
6666 * - there are non-space chars before <from> ;
6667 * - there is a CR/LF at or after <next>.
6668 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006669int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006670{
6671 char *prev = *from;
6672
6673 if (*prev == ':') {
6674 /* We're removing the first value, preserve the colon and add a
6675 * space if possible.
6676 */
6677 if (!http_is_crlf[(unsigned char)*next])
6678 next++;
6679 prev++;
6680 if (prev < next)
6681 *prev++ = ' ';
6682
6683 while (http_is_spht[(unsigned char)*next])
6684 next++;
6685 } else {
6686 /* Remove useless spaces before the old delimiter. */
6687 while (http_is_spht[(unsigned char)*(prev-1)])
6688 prev--;
6689 *from = prev;
6690
6691 /* copy the delimiter and if possible a space if we're
6692 * not at the end of the line.
6693 */
6694 if (!http_is_crlf[(unsigned char)*next]) {
6695 *prev++ = *next++;
6696 if (prev + 1 < next)
6697 *prev++ = ' ';
6698 while (http_is_spht[(unsigned char)*next])
6699 next++;
6700 }
6701 }
6702 return buffer_replace2(buf, prev, next, NULL, 0);
6703}
6704
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006705/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006706 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006707 * desirable to call it only when needed. This code is quite complex because
6708 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6709 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006711void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006712{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006713 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006714 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006715 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006716 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6717 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006718
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006720 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006721 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006722
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006723 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006724 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006725 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006726
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006727 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 hdr_beg = hdr_next;
6729 hdr_end = hdr_beg + cur_hdr->len;
6730 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006731
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 /* We have one full header between hdr_beg and hdr_end, and the
6733 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734 * "Cookie:" headers.
6735 */
6736
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006738 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006739 old_idx = cur_idx;
6740 continue;
6741 }
6742
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 del_from = NULL; /* nothing to be deleted */
6744 preserve_hdr = 0; /* assume we may kill the whole header */
6745
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746 /* Now look for cookies. Conforming to RFC2109, we have to support
6747 * attributes whose name begin with a '$', and associate them with
6748 * the right cookie, if we want to delete this cookie.
6749 * So there are 3 cases for each cookie read :
6750 * 1) it's a special attribute, beginning with a '$' : ignore it.
6751 * 2) it's a server id cookie that we *MAY* want to delete : save
6752 * some pointers on it (last semi-colon, beginning of cookie...)
6753 * 3) it's an application cookie : we *MAY* have to delete a previous
6754 * "special" cookie.
6755 * At the end of loop, if a "special" cookie remains, we may have to
6756 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757 * *MUST* delete it.
6758 *
6759 * Note: RFC2965 is unclear about the processing of spaces around
6760 * the equal sign in the ATTR=VALUE form. A careful inspection of
6761 * the RFC explicitly allows spaces before it, and not within the
6762 * tokens (attrs or values). An inspection of RFC2109 allows that
6763 * too but section 10.1.3 lets one think that spaces may be allowed
6764 * after the equal sign too, resulting in some (rare) buggy
6765 * implementations trying to do that. So let's do what servers do.
6766 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6767 * allowed quoted strings in values, with any possible character
6768 * after a backslash, including control chars and delimitors, which
6769 * causes parsing to become ambiguous. Browsers also allow spaces
6770 * within values even without quotes.
6771 *
6772 * We have to keep multiple pointers in order to support cookie
6773 * removal at the beginning, middle or end of header without
6774 * corrupting the header. All of these headers are valid :
6775 *
6776 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6777 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6778 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6779 * | | | | | | | | |
6780 * | | | | | | | | hdr_end <--+
6781 * | | | | | | | +--> next
6782 * | | | | | | +----> val_end
6783 * | | | | | +-----------> val_beg
6784 * | | | | +--------------> equal
6785 * | | | +----------------> att_end
6786 * | | +---------------------> att_beg
6787 * | +--------------------------> prev
6788 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006789 */
6790
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6792 /* Iterate through all cookies on this line */
6793
6794 /* find att_beg */
6795 att_beg = prev + 1;
6796 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6797 att_beg++;
6798
6799 /* find att_end : this is the first character after the last non
6800 * space before the equal. It may be equal to hdr_end.
6801 */
6802 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006803
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006804 while (equal < hdr_end) {
6805 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006806 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006807 if (http_is_spht[(unsigned char)*equal++])
6808 continue;
6809 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006810 }
6811
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006812 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6813 * is between <att_beg> and <equal>, both may be identical.
6814 */
6815
6816 /* look for end of cookie if there is an equal sign */
6817 if (equal < hdr_end && *equal == '=') {
6818 /* look for the beginning of the value */
6819 val_beg = equal + 1;
6820 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6821 val_beg++;
6822
6823 /* find the end of the value, respecting quotes */
6824 next = find_cookie_value_end(val_beg, hdr_end);
6825
6826 /* make val_end point to the first white space or delimitor after the value */
6827 val_end = next;
6828 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6829 val_end--;
6830 } else {
6831 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006832 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006833
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834 /* We have nothing to do with attributes beginning with '$'. However,
6835 * they will automatically be removed if a header before them is removed,
6836 * since they're supposed to be linked together.
6837 */
6838 if (*att_beg == '$')
6839 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006840
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006841 /* Ignore cookies with no equal sign */
6842 if (equal == next) {
6843 /* This is not our cookie, so we must preserve it. But if we already
6844 * scheduled another cookie for removal, we cannot remove the
6845 * complete header, but we can remove the previous block itself.
6846 */
6847 preserve_hdr = 1;
6848 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006849 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006850 val_end += delta;
6851 next += delta;
6852 hdr_end += delta;
6853 hdr_next += delta;
6854 cur_hdr->len += delta;
6855 http_msg_move_end(&txn->req, delta);
6856 prev = del_from;
6857 del_from = NULL;
6858 }
6859 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006861
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 /* if there are spaces around the equal sign, we need to
6863 * strip them otherwise we'll get trouble for cookie captures,
6864 * or even for rewrites. Since this happens extremely rarely,
6865 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006866 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006867 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6868 int stripped_before = 0;
6869 int stripped_after = 0;
6870
6871 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006872 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 equal += stripped_before;
6874 val_beg += stripped_before;
6875 }
6876
6877 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006878 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006879 val_beg += stripped_after;
6880 stripped_before += stripped_after;
6881 }
6882
6883 val_end += stripped_before;
6884 next += stripped_before;
6885 hdr_end += stripped_before;
6886 hdr_next += stripped_before;
6887 cur_hdr->len += stripped_before;
6888 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006891
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006892 /* First, let's see if we want to capture this cookie. We check
6893 * that we don't already have a client side cookie, because we
6894 * can only capture one. Also as an optimisation, we ignore
6895 * cookies shorter than the declared name.
6896 */
6897 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6898 (val_end - att_beg >= t->fe->capture_namelen) &&
6899 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6900 int log_len = val_end - att_beg;
6901
6902 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6903 Alert("HTTP logging : out of memory.\n");
6904 } else {
6905 if (log_len > t->fe->capture_len)
6906 log_len = t->fe->capture_len;
6907 memcpy(txn->cli_cookie, att_beg, log_len);
6908 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006909 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006910 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006911
Willy Tarreaubca99692010-10-06 19:25:55 +02006912 /* Persistence cookies in passive, rewrite or insert mode have the
6913 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006915 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006916 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006917 * For cookies in prefix mode, the form is :
6918 *
6919 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 */
6921 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6922 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6923 struct server *srv = t->be->srv;
6924 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6927 * have the server ID between val_beg and delim, and the original cookie between
6928 * delim+1 and val_end. Otherwise, delim==val_end :
6929 *
6930 * Cookie: NAME=SRV; # in all but prefix modes
6931 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6932 * | || || | |+-> next
6933 * | || || | +--> val_end
6934 * | || || +---------> delim
6935 * | || |+------------> val_beg
6936 * | || +-------------> att_end = equal
6937 * | |+-----------------> att_beg
6938 * | +------------------> prev
6939 * +-------------------------> hdr_beg
6940 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006941
Willy Tarreau67402132012-05-31 20:40:20 +02006942 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006943 for (delim = val_beg; delim < val_end; delim++)
6944 if (*delim == COOKIE_DELIM)
6945 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006946 } else {
6947 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006949 /* Now check if the cookie contains a date field, which would
6950 * appear after a vertical bar ('|') just after the server name
6951 * and before the delimiter.
6952 */
6953 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6954 if (vbar1) {
6955 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006956 * right is the last seen date. It is a base64 encoded
6957 * 30-bit value representing the UNIX date since the
6958 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006959 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006960 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006961 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006962 if (val_end - vbar1 >= 5) {
6963 val = b64tos30(vbar1);
6964 if (val > 0)
6965 txn->cookie_last_date = val << 2;
6966 }
6967 /* look for a second vertical bar */
6968 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6969 if (vbar1 && (val_end - vbar1 > 5)) {
6970 val = b64tos30(vbar1 + 1);
6971 if (val > 0)
6972 txn->cookie_first_date = val << 2;
6973 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006974 }
6975 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006976
Willy Tarreauf64d1412010-10-07 20:06:11 +02006977 /* if the cookie has an expiration date and the proxy wants to check
6978 * it, then we do that now. We first check if the cookie is too old,
6979 * then only if it has expired. We detect strict overflow because the
6980 * time resolution here is not great (4 seconds). Cookies with dates
6981 * in the future are ignored if their offset is beyond one day. This
6982 * allows an admin to fix timezone issues without expiring everyone
6983 * and at the same time avoids keeping unwanted side effects for too
6984 * long.
6985 */
6986 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006987 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6988 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006989 txn->flags &= ~TX_CK_MASK;
6990 txn->flags |= TX_CK_OLD;
6991 delim = val_beg; // let's pretend we have not found the cookie
6992 txn->cookie_first_date = 0;
6993 txn->cookie_last_date = 0;
6994 }
6995 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006996 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6997 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006998 txn->flags &= ~TX_CK_MASK;
6999 txn->flags |= TX_CK_EXPIRED;
7000 delim = val_beg; // let's pretend we have not found the cookie
7001 txn->cookie_first_date = 0;
7002 txn->cookie_last_date = 0;
7003 }
7004
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005 /* Here, we'll look for the first running server which supports the cookie.
7006 * This allows to share a same cookie between several servers, for example
7007 * to dedicate backup servers to specific servers only.
7008 * However, to prevent clients from sticking to cookie-less backup server
7009 * when they have incidentely learned an empty cookie, we simply ignore
7010 * empty cookies and mark them as invalid.
7011 * The same behaviour is applied when persistence must be ignored.
7012 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007013 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007014 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007016 while (srv) {
7017 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7018 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7019 if ((srv->state & SRV_RUNNING) ||
7020 (t->be->options & PR_O_PERSIST) ||
7021 (t->flags & SN_FORCE_PRST)) {
7022 /* we found the server and we can use it */
7023 txn->flags &= ~TX_CK_MASK;
7024 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7025 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007026 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 break;
7028 } else {
7029 /* we found a server, but it's down,
7030 * mark it as such and go on in case
7031 * another one is available.
7032 */
7033 txn->flags &= ~TX_CK_MASK;
7034 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007036 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007037 srv = srv->next;
7038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039
Willy Tarreauf64d1412010-10-07 20:06:11 +02007040 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007041 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007043 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7044 txn->flags |= TX_CK_UNUSED;
7045 else
7046 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007047 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007049 /* depending on the cookie mode, we may have to either :
7050 * - delete the complete cookie if we're in insert+indirect mode, so that
7051 * the server never sees it ;
7052 * - remove the server id from the cookie value, and tag the cookie as an
7053 * application cookie so that it does not get accidentely removed later,
7054 * if we're in cookie prefix mode
7055 */
Willy Tarreau67402132012-05-31 20:40:20 +02007056 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007057 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007058
Willy Tarreau9b28e032012-10-12 23:49:43 +02007059 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007060 val_end += delta;
7061 next += delta;
7062 hdr_end += delta;
7063 hdr_next += delta;
7064 cur_hdr->len += delta;
7065 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007067 del_from = NULL;
7068 preserve_hdr = 1; /* we want to keep this cookie */
7069 }
7070 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007071 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 del_from = prev;
7073 }
7074 } else {
7075 /* This is not our cookie, so we must preserve it. But if we already
7076 * scheduled another cookie for removal, we cannot remove the
7077 * complete header, but we can remove the previous block itself.
7078 */
7079 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007080
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007081 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007082 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007083 if (att_beg >= del_from)
7084 att_beg += delta;
7085 if (att_end >= del_from)
7086 att_end += delta;
7087 val_beg += delta;
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);
7094 prev = del_from;
7095 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007099 /* Look for the appsession cookie unless persistence must be ignored */
7100 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7101 int cmp_len, value_len;
7102 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007103
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007104 if (t->be->options2 & PR_O2_AS_PFX) {
7105 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7106 value_begin = att_beg + t->be->appsession_name_len;
7107 value_len = val_end - att_beg - t->be->appsession_name_len;
7108 } else {
7109 cmp_len = att_end - att_beg;
7110 value_begin = val_beg;
7111 value_len = val_end - val_beg;
7112 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007113
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007114 /* let's see if the cookie is our appcookie */
7115 if (cmp_len == t->be->appsession_name_len &&
7116 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7117 manage_client_side_appsession(t, value_begin, value_len);
7118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119 }
7120
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007121 /* continue with next cookie on this header line */
7122 att_beg = next;
7123 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007125 /* There are no more cookies on this line.
7126 * We may still have one (or several) marked for deletion at the
7127 * end of the line. We must do this now in two ways :
7128 * - if some cookies must be preserved, we only delete from the
7129 * mark to the end of line ;
7130 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007131 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007132 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007133 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007134 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007135 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007136 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007137 cur_hdr->len += delta;
7138 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007139 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007140
7141 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007142 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7143 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007144 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007145 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007146 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007147 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007148 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007149 }
7150
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007151 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007153 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154}
7155
7156
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157/* Iterate the same filter through all response headers contained in <rtr>.
7158 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7159 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007160int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161{
7162 char term;
7163 char *cur_ptr, *cur_end, *cur_next;
7164 int cur_idx, old_idx, last_hdr;
7165 struct http_txn *txn = &t->txn;
7166 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007167 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007168
7169 last_hdr = 0;
7170
Willy Tarreau9b28e032012-10-12 23:49:43 +02007171 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172 old_idx = 0;
7173
7174 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007175 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007177 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007178 (exp->action == ACT_ALLOW ||
7179 exp->action == ACT_DENY))
7180 return 0;
7181
7182 cur_idx = txn->hdr_idx.v[old_idx].next;
7183 if (!cur_idx)
7184 break;
7185
7186 cur_hdr = &txn->hdr_idx.v[cur_idx];
7187 cur_ptr = cur_next;
7188 cur_end = cur_ptr + cur_hdr->len;
7189 cur_next = cur_end + cur_hdr->cr + 1;
7190
7191 /* Now we have one header between cur_ptr and cur_end,
7192 * and the next header starts at cur_next.
7193 */
7194
7195 /* The annoying part is that pattern matching needs
7196 * that we modify the contents to null-terminate all
7197 * strings before testing them.
7198 */
7199
7200 term = *cur_end;
7201 *cur_end = '\0';
7202
7203 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7204 switch (exp->action) {
7205 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007206 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 last_hdr = 1;
7208 break;
7209
7210 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007211 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212 last_hdr = 1;
7213 break;
7214
7215 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007216 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7217 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218 /* FIXME: if the user adds a newline in the replacement, the
7219 * index will not be recalculated for now, and the new line
7220 * will not be counted as a new header.
7221 */
7222
7223 cur_end += delta;
7224 cur_next += delta;
7225 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007226 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 break;
7228
7229 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007230 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 cur_next += delta;
7232
Willy Tarreaufa355d42009-11-29 18:12:29 +01007233 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007234 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7235 txn->hdr_idx.used--;
7236 cur_hdr->len = 0;
7237 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007238 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239 break;
7240
7241 }
7242 }
7243 if (cur_end)
7244 *cur_end = term; /* restore the string terminator */
7245
7246 /* keep the link from this header to next one in case of later
7247 * removal of next header.
7248 */
7249 old_idx = cur_idx;
7250 }
7251 return 0;
7252}
7253
7254
7255/* Apply the filter to the status line in the response buffer <rtr>.
7256 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7257 * or -1 if a replacement resulted in an invalid status line.
7258 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007259int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260{
7261 char term;
7262 char *cur_ptr, *cur_end;
7263 int done;
7264 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007265 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007266
7267
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007270 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 (exp->action == ACT_ALLOW ||
7272 exp->action == ACT_DENY))
7273 return 0;
7274 else if (exp->action == ACT_REMOVE)
7275 return 0;
7276
7277 done = 0;
7278
Willy Tarreau9b28e032012-10-12 23:49:43 +02007279 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007280 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281
7282 /* Now we have the status line between cur_ptr and cur_end */
7283
7284 /* The annoying part is that pattern matching needs
7285 * that we modify the contents to null-terminate all
7286 * strings before testing them.
7287 */
7288
7289 term = *cur_end;
7290 *cur_end = '\0';
7291
7292 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7293 switch (exp->action) {
7294 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007295 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296 done = 1;
7297 break;
7298
7299 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007300 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 done = 1;
7302 break;
7303
7304 case ACT_REPLACE:
7305 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007306 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7307 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308 /* FIXME: if the user adds a newline in the replacement, the
7309 * index will not be recalculated for now, and the new line
7310 * will not be counted as a new header.
7311 */
7312
Willy Tarreaufa355d42009-11-29 18:12:29 +01007313 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007315 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007316 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007317 cur_ptr, cur_end + 1,
7318 NULL, NULL);
7319 if (unlikely(!cur_end))
7320 return -1;
7321
7322 /* we have a full respnse and we know that we have either a CR
7323 * or an LF at <ptr>.
7324 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007325 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007326 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327 /* there is no point trying this regex on headers */
7328 return 1;
7329 }
7330 }
7331 *cur_end = term; /* restore the string terminator */
7332 return done;
7333}
7334
7335
7336
7337/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007338 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7340 * unparsable response.
7341 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007342int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007344 struct http_txn *txn = &s->txn;
7345 struct hdr_exp *exp;
7346
7347 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 int ret;
7349
7350 /*
7351 * The interleaving of transformations and verdicts
7352 * makes it difficult to decide to continue or stop
7353 * the evaluation.
7354 */
7355
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007356 if (txn->flags & TX_SVDENY)
7357 break;
7358
Willy Tarreau3d300592007-03-18 18:34:41 +01007359 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7361 exp->action == ACT_PASS)) {
7362 exp = exp->next;
7363 continue;
7364 }
7365
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007366 /* if this filter had a condition, evaluate it now and skip to
7367 * next filter if the condition does not match.
7368 */
7369 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007370 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007371 ret = acl_pass(ret);
7372 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7373 ret = !ret;
7374 if (!ret)
7375 continue;
7376 }
7377
Willy Tarreaua15645d2007-03-18 16:22:39 +01007378 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007379 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380 if (unlikely(ret < 0))
7381 return -1;
7382
7383 if (likely(ret == 0)) {
7384 /* The filter did not match the response, it can be
7385 * iterated through all headers.
7386 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007387 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389 }
7390 return 0;
7391}
7392
7393
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007395 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007396 * desirable to call it only when needed. This function is also used when we
7397 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007399void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007400{
7401 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007402 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007403 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007404 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007405 char *hdr_beg, *hdr_end, *hdr_next;
7406 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 /* Iterate through the headers.
7409 * we start with the start line.
7410 */
7411 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007412 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007413
7414 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7415 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007416 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417
7418 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007419 hdr_beg = hdr_next;
7420 hdr_end = hdr_beg + cur_hdr->len;
7421 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007422
Willy Tarreau24581ba2010-08-31 22:39:35 +02007423 /* We have one full header between hdr_beg and hdr_end, and the
7424 * next header starts at hdr_next. We're only interested in
7425 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 */
7427
Willy Tarreau24581ba2010-08-31 22:39:35 +02007428 is_cookie2 = 0;
7429 prev = hdr_beg + 10;
7430 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007431 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007432 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7433 if (!val) {
7434 old_idx = cur_idx;
7435 continue;
7436 }
7437 is_cookie2 = 1;
7438 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007439 }
7440
Willy Tarreau24581ba2010-08-31 22:39:35 +02007441 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7442 * <prev> points to the colon.
7443 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007444 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007445
Willy Tarreau24581ba2010-08-31 22:39:35 +02007446 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7447 * check-cache is enabled) and we are not interested in checking
7448 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007449 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007450 if (t->be->cookie_name == NULL &&
7451 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007452 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453 return;
7454
Willy Tarreau24581ba2010-08-31 22:39:35 +02007455 /* OK so now we know we have to process this response cookie.
7456 * The format of the Set-Cookie header is slightly different
7457 * from the format of the Cookie header in that it does not
7458 * support the comma as a cookie delimiter (thus the header
7459 * cannot be folded) because the Expires attribute described in
7460 * the original Netscape's spec may contain an unquoted date
7461 * with a comma inside. We have to live with this because
7462 * many browsers don't support Max-Age and some browsers don't
7463 * support quoted strings. However the Set-Cookie2 header is
7464 * clean.
7465 *
7466 * We have to keep multiple pointers in order to support cookie
7467 * removal at the beginning, middle or end of header without
7468 * corrupting the header (in case of set-cookie2). A special
7469 * pointer, <scav> points to the beginning of the set-cookie-av
7470 * fields after the first semi-colon. The <next> pointer points
7471 * either to the end of line (set-cookie) or next unquoted comma
7472 * (set-cookie2). All of these headers are valid :
7473 *
7474 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7475 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7476 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7477 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7478 * | | | | | | | | | |
7479 * | | | | | | | | +-> next hdr_end <--+
7480 * | | | | | | | +------------> scav
7481 * | | | | | | +--------------> val_end
7482 * | | | | | +--------------------> val_beg
7483 * | | | | +----------------------> equal
7484 * | | | +------------------------> att_end
7485 * | | +----------------------------> att_beg
7486 * | +------------------------------> prev
7487 * +-----------------------------------------> hdr_beg
7488 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007489
Willy Tarreau24581ba2010-08-31 22:39:35 +02007490 for (; prev < hdr_end; prev = next) {
7491 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007492
Willy Tarreau24581ba2010-08-31 22:39:35 +02007493 /* find att_beg */
7494 att_beg = prev + 1;
7495 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7496 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007497
Willy Tarreau24581ba2010-08-31 22:39:35 +02007498 /* find att_end : this is the first character after the last non
7499 * space before the equal. It may be equal to hdr_end.
7500 */
7501 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007502
Willy Tarreau24581ba2010-08-31 22:39:35 +02007503 while (equal < hdr_end) {
7504 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7505 break;
7506 if (http_is_spht[(unsigned char)*equal++])
7507 continue;
7508 att_end = equal;
7509 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510
Willy Tarreau24581ba2010-08-31 22:39:35 +02007511 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7512 * is between <att_beg> and <equal>, both may be identical.
7513 */
7514
7515 /* look for end of cookie if there is an equal sign */
7516 if (equal < hdr_end && *equal == '=') {
7517 /* look for the beginning of the value */
7518 val_beg = equal + 1;
7519 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7520 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007521
Willy Tarreau24581ba2010-08-31 22:39:35 +02007522 /* find the end of the value, respecting quotes */
7523 next = find_cookie_value_end(val_beg, hdr_end);
7524
7525 /* make val_end point to the first white space or delimitor after the value */
7526 val_end = next;
7527 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7528 val_end--;
7529 } else {
7530 /* <equal> points to next comma, semi-colon or EOL */
7531 val_beg = val_end = next = equal;
7532 }
7533
7534 if (next < hdr_end) {
7535 /* Set-Cookie2 supports multiple cookies, and <next> points to
7536 * a colon or semi-colon before the end. So skip all attr-value
7537 * pairs and look for the next comma. For Set-Cookie, since
7538 * commas are permitted in values, skip to the end.
7539 */
7540 if (is_cookie2)
7541 next = find_hdr_value_end(next, hdr_end);
7542 else
7543 next = hdr_end;
7544 }
7545
7546 /* Now everything is as on the diagram above */
7547
7548 /* Ignore cookies with no equal sign */
7549 if (equal == val_end)
7550 continue;
7551
7552 /* If there are spaces around the equal sign, we need to
7553 * strip them otherwise we'll get trouble for cookie captures,
7554 * or even for rewrites. Since this happens extremely rarely,
7555 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7558 int stripped_before = 0;
7559 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007560
Willy Tarreau24581ba2010-08-31 22:39:35 +02007561 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007562 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007563 equal += stripped_before;
7564 val_beg += stripped_before;
7565 }
7566
7567 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007568 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007569 val_beg += stripped_after;
7570 stripped_before += stripped_after;
7571 }
7572
7573 val_end += stripped_before;
7574 next += stripped_before;
7575 hdr_end += stripped_before;
7576 hdr_next += stripped_before;
7577 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007578 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007579 }
7580
7581 /* First, let's see if we want to capture this cookie. We check
7582 * that we don't already have a server side cookie, because we
7583 * can only capture one. Also as an optimisation, we ignore
7584 * cookies shorter than the declared name.
7585 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007586 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007587 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007588 (val_end - att_beg >= t->fe->capture_namelen) &&
7589 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7590 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007591 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007592 Alert("HTTP logging : out of memory.\n");
7593 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007594 else {
7595 if (log_len > t->fe->capture_len)
7596 log_len = t->fe->capture_len;
7597 memcpy(txn->srv_cookie, att_beg, log_len);
7598 txn->srv_cookie[log_len] = 0;
7599 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 }
7601
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007602 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007604 if (!(t->flags & SN_IGNORE_PRST) &&
7605 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7606 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007607 /* assume passive cookie by default */
7608 txn->flags &= ~TX_SCK_MASK;
7609 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007610
7611 /* If the cookie is in insert mode on a known server, we'll delete
7612 * this occurrence because we'll insert another one later.
7613 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007614 * a direct access.
7615 */
Willy Tarreau67402132012-05-31 20:40:20 +02007616 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007617 /* The "preserve" flag was set, we don't want to touch the
7618 * server's cookie.
7619 */
7620 }
Willy Tarreau67402132012-05-31 20:40:20 +02007621 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7622 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007623 /* this cookie must be deleted */
7624 if (*prev == ':' && next == hdr_end) {
7625 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007626 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007627 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7628 txn->hdr_idx.used--;
7629 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007630 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007631 hdr_next += delta;
7632 http_msg_move_end(&txn->rsp, delta);
7633 /* note: while both invalid now, <next> and <hdr_end>
7634 * are still equal, so the for() will stop as expected.
7635 */
7636 } else {
7637 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007638 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007639 next = prev;
7640 hdr_end += delta;
7641 hdr_next += delta;
7642 cur_hdr->len += delta;
7643 http_msg_move_end(&txn->rsp, delta);
7644 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007645 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007646 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007647 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007648 }
Willy Tarreau67402132012-05-31 20:40:20 +02007649 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007650 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007651 * with this server since we know it.
7652 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007653 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007654 next += delta;
7655 hdr_end += delta;
7656 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007658 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659
Willy Tarreauf1348312010-10-07 15:54:11 +02007660 txn->flags &= ~TX_SCK_MASK;
7661 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007662 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007663 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007665 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007666 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007667 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007668 next += delta;
7669 hdr_end += delta;
7670 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007672 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007673
Willy Tarreau827aee92011-03-10 16:55:02 +01007674 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007675 txn->flags &= ~TX_SCK_MASK;
7676 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 }
7678 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007679 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7680 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007681 int cmp_len, value_len;
7682 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683
Cyril Bontéb21570a2009-11-29 20:04:48 +01007684 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007685 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7686 value_begin = att_beg + t->be->appsession_name_len;
7687 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007688 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007689 cmp_len = att_end - att_beg;
7690 value_begin = val_beg;
7691 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007692 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007693
Cyril Bonté17530c32010-04-06 21:11:10 +02007694 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007695 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7696 /* free a possibly previously allocated memory */
7697 pool_free2(apools.sessid, txn->sessid);
7698
Cyril Bontéb21570a2009-11-29 20:04:48 +01007699 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007700 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007701 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7702 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7703 return;
7704 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007705 memcpy(txn->sessid, value_begin, value_len);
7706 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007707 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007708 }
7709 /* that's done for this cookie, check the next one on the same
7710 * line when next != hdr_end (only if is_cookie2).
7711 */
7712 }
7713 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007714 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007715 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007716
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007717 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007718 appsess *asession = NULL;
7719 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007720 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007721 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007722 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007723 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7724 Alert("Not enough Memory process_srv():asession:calloc().\n");
7725 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7726 return;
7727 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007728 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7729
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007730 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7731 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7732 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007733 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007734 return;
7735 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007736 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007737 asession->sessid[t->be->appsession_len] = 0;
7738
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007739 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007740 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007741 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007742 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007743 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007744 return;
7745 }
7746 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007747 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007748
7749 asession->request_count = 0;
7750 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7751 }
7752
7753 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7754 asession->request_count++;
7755 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007756}
7757
7758
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759/*
7760 * Check if response is cacheable or not. Updates t->flags.
7761 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007762void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007763{
7764 struct http_txn *txn = &t->txn;
7765 char *p1, *p2;
7766
7767 char *cur_ptr, *cur_end, *cur_next;
7768 int cur_idx;
7769
Willy Tarreau5df51872007-11-25 16:20:08 +01007770 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007771 return;
7772
7773 /* Iterate through the headers.
7774 * we start with the start line.
7775 */
7776 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007777 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007778
7779 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7780 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007781 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007782
7783 cur_hdr = &txn->hdr_idx.v[cur_idx];
7784 cur_ptr = cur_next;
7785 cur_end = cur_ptr + cur_hdr->len;
7786 cur_next = cur_end + cur_hdr->cr + 1;
7787
7788 /* We have one full header between cur_ptr and cur_end, and the
7789 * next header starts at cur_next. We're only interested in
7790 * "Cookie:" headers.
7791 */
7792
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007793 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7794 if (val) {
7795 if ((cur_end - (cur_ptr + val) >= 8) &&
7796 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7797 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7798 return;
7799 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007800 }
7801
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007802 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7803 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007804 continue;
7805
7806 /* OK, right now we know we have a cache-control header at cur_ptr */
7807
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007808 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809
7810 if (p1 >= cur_end) /* no more info */
7811 continue;
7812
7813 /* p1 is at the beginning of the value */
7814 p2 = p1;
7815
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007816 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817 p2++;
7818
7819 /* we have a complete value between p1 and p2 */
7820 if (p2 < cur_end && *p2 == '=') {
7821 /* we have something of the form no-cache="set-cookie" */
7822 if ((cur_end - p1 >= 21) &&
7823 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7824 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007825 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826 continue;
7827 }
7828
7829 /* OK, so we know that either p2 points to the end of string or to a comma */
7830 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007831 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7833 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7834 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007835 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007836 return;
7837 }
7838
7839 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007840 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841 continue;
7842 }
7843 }
7844}
7845
7846
Willy Tarreau58f10d72006-12-04 02:26:12 +01007847/*
7848 * Try to retrieve a known appsession in the URI, then the associated server.
7849 * If the server is found, it's assigned to the session.
7850 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007851void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007852{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007853 char *end_params, *first_param, *cur_param, *next_param;
7854 char separator;
7855 int value_len;
7856
7857 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007858
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007859 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007860 (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 +01007861 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007862 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007863
Cyril Bontéb21570a2009-11-29 20:04:48 +01007864 first_param = NULL;
7865 switch (mode) {
7866 case PR_O2_AS_M_PP:
7867 first_param = memchr(begin, ';', len);
7868 break;
7869 case PR_O2_AS_M_QS:
7870 first_param = memchr(begin, '?', len);
7871 break;
7872 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007873
Cyril Bontéb21570a2009-11-29 20:04:48 +01007874 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007876 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007877
Cyril Bontéb21570a2009-11-29 20:04:48 +01007878 switch (mode) {
7879 case PR_O2_AS_M_PP:
7880 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7881 end_params = (char *) begin + len;
7882 }
7883 separator = ';';
7884 break;
7885 case PR_O2_AS_M_QS:
7886 end_params = (char *) begin + len;
7887 separator = '&';
7888 break;
7889 default:
7890 /* unknown mode, shouldn't happen */
7891 return;
7892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007893
Cyril Bontéb21570a2009-11-29 20:04:48 +01007894 cur_param = next_param = end_params;
7895 while (cur_param > first_param) {
7896 cur_param--;
7897 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7898 /* let's see if this is the appsession parameter */
7899 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7900 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7901 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7902 /* Cool... it's the right one */
7903 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7904 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7905 if (value_len > 0) {
7906 manage_client_side_appsession(t, cur_param, value_len);
7907 }
7908 break;
7909 }
7910 next_param = cur_param;
7911 }
7912 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007913#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007914 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007915 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007916#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007917}
7918
Willy Tarreaub2513902006-12-17 14:52:38 +01007919/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007920 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007921 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007922 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007923 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007924 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007925 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007926 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007927 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007928int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007929{
7930 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007931 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007932 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007933 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007934
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007935 if (!uri_auth)
7936 return 0;
7937
Cyril Bonté70be45d2010-10-12 00:14:35 +02007938 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007939 return 0;
7940
Willy Tarreau295a8372011-03-10 11:25:07 +01007941 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007942 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007943 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007944
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007945 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007946 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007947 return 0;
7948
Willy Tarreau3a215be2012-03-09 21:39:51 +01007949 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007950 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007951 return 0;
7952
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007953 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007954 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007955 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007956 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007957 break;
7958 }
7959 h++;
7960 }
7961
7962 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007963 h = uri + uri_auth->uri_len;
7964 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007965 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007966 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007967 break;
7968 }
7969 h++;
7970 }
7971 }
7972
Willy Tarreau3a215be2012-03-09 21:39:51 +01007973 h = uri + uri_auth->uri_len;
7974 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007975 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007976 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007977 break;
7978 }
7979 h++;
7980 }
7981
Willy Tarreau3a215be2012-03-09 21:39:51 +01007982 h = uri + uri_auth->uri_len;
7983 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007984 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007985 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007986 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007987 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007988 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7989 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7990 si->applet.ctx.stats.st_code = i;
7991 break;
7992 }
7993 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007994 break;
7995 }
7996 h++;
7997 }
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02007998
7999 si->applet.ctx.stats.scope_str = 0;
8000 si->applet.ctx.stats.scope_len = 0;
8001 h = uri + uri_auth->uri_len;
8002 while (h <= uri + msg->sl.rq.u_l - 8) {
8003 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
8004 int itx = 0;
8005 const char *h2;
8006 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
8007 const char *err;
8008
8009 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
8010 h2 = h;
8011 si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p;
8012 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
8013 itx++;
8014 h++;
8015 }
8016
8017 if (itx > STAT_SCOPE_TXT_MAXLEN)
8018 itx = STAT_SCOPE_TXT_MAXLEN;
8019 si->applet.ctx.stats.scope_len = itx;
8020
8021 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
8022 memcpy(scope_txt, h2, itx);
8023 scope_txt[itx] = '\0';
8024 err = invalid_char(scope_txt);
8025 if (err) {
8026 /* bad char in search text => clear scope */
8027 si->applet.ctx.stats.scope_str = 0;
8028 si->applet.ctx.stats.scope_len = 0;
8029 }
8030 break;
8031 }
8032 h++;
8033 }
8034
8035
Willy Tarreaub2513902006-12-17 14:52:38 +01008036 return 1;
8037}
8038
Willy Tarreau4076a152009-04-02 15:18:36 +02008039/*
8040 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008041 * By default it tries to report the error position as msg->err_pos. However if
8042 * this one is not set, it will then report msg->next, which is the last known
8043 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008044 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008045 */
8046void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008047 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01008048 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008049{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008050 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008051 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008052
Willy Tarreau9b28e032012-10-12 23:49:43 +02008053 es->len = MIN(chn->buf->i, sizeof(es->buf));
8054 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008055 len1 = MIN(len1, es->len);
8056 len2 = es->len - len1; /* remaining data if buffer wraps */
8057
Willy Tarreau9b28e032012-10-12 23:49:43 +02008058 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008059 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008060 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008061
Willy Tarreau4076a152009-04-02 15:18:36 +02008062 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008063 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008064 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008065 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008066
Willy Tarreau4076a152009-04-02 15:18:36 +02008067 es->when = date; // user-visible date
8068 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008069 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008070 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008071 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01008072 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008073 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008074 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008075 es->s_flags = s->flags;
8076 es->t_flags = s->txn.flags;
8077 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008078 es->b_out = chn->buf->o;
8079 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008080 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008081 es->m_clen = msg->chunk_len;
8082 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008083}
Willy Tarreaub2513902006-12-17 14:52:38 +01008084
Willy Tarreau294c4732011-12-16 21:35:50 +01008085/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8086 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8087 * performed over the whole headers. Otherwise it must contain a valid header
8088 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8089 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8090 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8091 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008092 * -1. The value fetch stops at commas, so this function is suited for use with
8093 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008094 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008095 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008096unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008097 struct hdr_idx *idx, int occ,
8098 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008099{
Willy Tarreau294c4732011-12-16 21:35:50 +01008100 struct hdr_ctx local_ctx;
8101 char *ptr_hist[MAX_HDR_HISTORY];
8102 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008103 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008104 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008105
Willy Tarreau294c4732011-12-16 21:35:50 +01008106 if (!ctx) {
8107 local_ctx.idx = 0;
8108 ctx = &local_ctx;
8109 }
8110
Willy Tarreaubce70882009-09-07 11:51:47 +02008111 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008112 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008113 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008114 occ--;
8115 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008116 *vptr = ctx->line + ctx->val;
8117 *vlen = ctx->vlen;
8118 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008119 }
8120 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008121 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008122 }
8123
8124 /* negative occurrence, we scan all the list then walk back */
8125 if (-occ > MAX_HDR_HISTORY)
8126 return 0;
8127
Willy Tarreau294c4732011-12-16 21:35:50 +01008128 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008129 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008130 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8131 len_hist[hist_ptr] = ctx->vlen;
8132 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008133 hist_ptr = 0;
8134 found++;
8135 }
8136 if (-occ > found)
8137 return 0;
8138 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008139 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8140 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8141 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008142 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008143 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008144 if (hist_ptr >= MAX_HDR_HISTORY)
8145 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008146 *vptr = ptr_hist[hist_ptr];
8147 *vlen = len_hist[hist_ptr];
8148 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008149}
8150
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008151/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8152 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8153 * performed over the whole headers. Otherwise it must contain a valid header
8154 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8155 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8156 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8157 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8158 * -1. This function differs from http_get_hdr() in that it only returns full
8159 * line header values and does not stop at commas.
8160 * The return value is 0 if nothing was found, or non-zero otherwise.
8161 */
8162unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8163 struct hdr_idx *idx, int occ,
8164 struct hdr_ctx *ctx, char **vptr, int *vlen)
8165{
8166 struct hdr_ctx local_ctx;
8167 char *ptr_hist[MAX_HDR_HISTORY];
8168 int len_hist[MAX_HDR_HISTORY];
8169 unsigned int hist_ptr;
8170 int found;
8171
8172 if (!ctx) {
8173 local_ctx.idx = 0;
8174 ctx = &local_ctx;
8175 }
8176
8177 if (occ >= 0) {
8178 /* search from the beginning */
8179 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8180 occ--;
8181 if (occ <= 0) {
8182 *vptr = ctx->line + ctx->val;
8183 *vlen = ctx->vlen;
8184 return 1;
8185 }
8186 }
8187 return 0;
8188 }
8189
8190 /* negative occurrence, we scan all the list then walk back */
8191 if (-occ > MAX_HDR_HISTORY)
8192 return 0;
8193
8194 found = hist_ptr = 0;
8195 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8196 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8197 len_hist[hist_ptr] = ctx->vlen;
8198 if (++hist_ptr >= MAX_HDR_HISTORY)
8199 hist_ptr = 0;
8200 found++;
8201 }
8202 if (-occ > found)
8203 return 0;
8204 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8205 * find occurrence -occ, so we have to check [hist_ptr+occ].
8206 */
8207 hist_ptr += occ;
8208 if (hist_ptr >= MAX_HDR_HISTORY)
8209 hist_ptr -= MAX_HDR_HISTORY;
8210 *vptr = ptr_hist[hist_ptr];
8211 *vlen = len_hist[hist_ptr];
8212 return 1;
8213}
8214
Willy Tarreaubaaee002006-06-26 02:48:02 +02008215/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008216 * Print a debug line with a header. Always stop at the first CR or LF char,
8217 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8218 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008219 */
8220void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8221{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008222 int max;
8223 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01008224 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
8225 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008226
8227 for (max = 0; start + max < end; max++)
8228 if (start[max] == '\r' || start[max] == '\n')
8229 break;
8230
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008231 UBOUND(max, trash.size - trash.len - 3);
8232 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8233 trash.str[trash.len++] = '\n';
8234 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008235}
8236
Willy Tarreau0937bc42009-12-22 15:03:09 +01008237/*
8238 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8239 * the required fields are properly allocated and that we only need to (re)init
8240 * them. This should be used before processing any new request.
8241 */
8242void http_init_txn(struct session *s)
8243{
8244 struct http_txn *txn = &s->txn;
8245 struct proxy *fe = s->fe;
8246
8247 txn->flags = 0;
8248 txn->status = -1;
8249
Willy Tarreauf64d1412010-10-07 20:06:11 +02008250 txn->cookie_first_date = 0;
8251 txn->cookie_last_date = 0;
8252
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008253 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008254 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008255 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008256 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008257 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008258 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008259 txn->req.chunk_len = 0LL;
8260 txn->req.body_len = 0LL;
8261 txn->rsp.chunk_len = 0LL;
8262 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008263 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8264 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008265 txn->req.chn = s->req;
8266 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008267
8268 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008269
8270 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8271 if (fe->options2 & PR_O2_REQBUG_OK)
8272 txn->req.err_pos = -1; /* let buggy requests pass */
8273
Willy Tarreau46023632010-01-07 22:51:47 +01008274 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008275 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8276
Willy Tarreau46023632010-01-07 22:51:47 +01008277 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008278 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8279
8280 if (txn->hdr_idx.v)
8281 hdr_idx_init(&txn->hdr_idx);
8282}
8283
8284/* to be used at the end of a transaction */
8285void http_end_txn(struct session *s)
8286{
8287 struct http_txn *txn = &s->txn;
8288
8289 /* these ones will have been dynamically allocated */
8290 pool_free2(pool2_requri, txn->uri);
8291 pool_free2(pool2_capture, txn->cli_cookie);
8292 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008293 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008294 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008295
William Lallemanda73203e2012-03-12 12:48:57 +01008296 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008297 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008298 txn->uri = NULL;
8299 txn->srv_cookie = NULL;
8300 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008301
8302 if (txn->req.cap) {
8303 struct cap_hdr *h;
8304 for (h = s->fe->req_cap; h; h = h->next)
8305 pool_free2(h->pool, txn->req.cap[h->index]);
8306 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8307 }
8308
8309 if (txn->rsp.cap) {
8310 struct cap_hdr *h;
8311 for (h = s->fe->rsp_cap; h; h = h->next)
8312 pool_free2(h->pool, txn->rsp.cap[h->index]);
8313 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8314 }
8315
Willy Tarreau0937bc42009-12-22 15:03:09 +01008316}
8317
8318/* to be used at the end of a transaction to prepare a new one */
8319void http_reset_txn(struct session *s)
8320{
8321 http_end_txn(s);
8322 http_init_txn(s);
8323
8324 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008325 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008326 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008327 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008328 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008329 /* re-init store persistence */
8330 s->store_count = 0;
8331
Willy Tarreau0937bc42009-12-22 15:03:09 +01008332 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008333
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008334 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008335
Willy Tarreau739cfba2010-01-25 23:11:14 +01008336 /* We must trim any excess data from the response buffer, because we
8337 * may have blocked an invalid response from a server that we don't
8338 * want to accidentely forward once we disable the analysers, nor do
8339 * we want those data to come along with next response. A typical
8340 * example of such data would be from a buggy server responding to
8341 * a HEAD with some data, or sending more than the advertised
8342 * content-length.
8343 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008344 if (unlikely(s->rep->buf->i))
8345 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008346
Willy Tarreau0937bc42009-12-22 15:03:09 +01008347 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008348 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008349
Willy Tarreaud04e8582010-05-31 12:31:35 +02008350 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008351 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008352
8353 s->req->rex = TICK_ETERNITY;
8354 s->req->wex = TICK_ETERNITY;
8355 s->req->analyse_exp = TICK_ETERNITY;
8356 s->rep->rex = TICK_ETERNITY;
8357 s->rep->wex = TICK_ETERNITY;
8358 s->rep->analyse_exp = TICK_ETERNITY;
8359}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008360
Willy Tarreauff011f22011-01-06 17:51:27 +01008361void free_http_req_rules(struct list *r) {
8362 struct http_req_rule *tr, *pr;
8363
8364 list_for_each_entry_safe(pr, tr, r, list) {
8365 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008366 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008367 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008368
8369 free(pr);
8370 }
8371}
8372
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008373/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008374struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8375{
8376 struct http_req_rule *rule;
8377 int cur_arg;
8378
8379 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8380 if (!rule) {
8381 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008382 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008383 }
8384
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008385 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008386 rule->action = HTTP_REQ_ACT_ALLOW;
8387 cur_arg = 1;
8388 } else if (!strcmp(args[0], "deny")) {
8389 rule->action = HTTP_REQ_ACT_DENY;
8390 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008391 } else if (!strcmp(args[0], "tarpit")) {
8392 rule->action = HTTP_REQ_ACT_TARPIT;
8393 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008394 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008395 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008396 cur_arg = 1;
8397
8398 while(*args[cur_arg]) {
8399 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008400 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008401 cur_arg+=2;
8402 continue;
8403 } else
8404 break;
8405 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008406 } else if (!strcmp(args[0], "set-nice")) {
8407 rule->action = HTTP_REQ_ACT_SET_NICE;
8408 cur_arg = 1;
8409
8410 if (!*args[cur_arg] ||
8411 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8412 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8413 file, linenum, args[0]);
8414 goto out_err;
8415 }
8416 rule->arg.nice = atoi(args[cur_arg]);
8417 if (rule->arg.nice < -1024)
8418 rule->arg.nice = -1024;
8419 else if (rule->arg.nice > 1024)
8420 rule->arg.nice = 1024;
8421 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008422 } else if (!strcmp(args[0], "set-tos")) {
8423#ifdef IP_TOS
8424 char *err;
8425 rule->action = HTTP_REQ_ACT_SET_TOS;
8426 cur_arg = 1;
8427
8428 if (!*args[cur_arg] ||
8429 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8430 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8431 file, linenum, args[0]);
8432 goto out_err;
8433 }
8434
8435 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8436 if (err && *err != '\0') {
8437 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8438 file, linenum, err, args[0]);
8439 goto out_err;
8440 }
8441 cur_arg++;
8442#else
8443 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8444 goto out_err;
8445#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008446 } else if (!strcmp(args[0], "set-mark")) {
8447#ifdef SO_MARK
8448 char *err;
8449 rule->action = HTTP_REQ_ACT_SET_MARK;
8450 cur_arg = 1;
8451
8452 if (!*args[cur_arg] ||
8453 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8454 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8455 file, linenum, args[0]);
8456 goto out_err;
8457 }
8458
8459 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8460 if (err && *err != '\0') {
8461 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8462 file, linenum, err, args[0]);
8463 goto out_err;
8464 }
8465 cur_arg++;
8466 global.last_checks |= LSTCHK_NETADM;
8467#else
8468 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8469 goto out_err;
8470#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008471 } else if (!strcmp(args[0], "set-log-level")) {
8472 rule->action = HTTP_REQ_ACT_SET_LOGL;
8473 cur_arg = 1;
8474
8475 if (!*args[cur_arg] ||
8476 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8477 bad_log_level:
8478 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8479 file, linenum, args[0]);
8480 goto out_err;
8481 }
8482 if (strcmp(args[cur_arg], "silent") == 0)
8483 rule->arg.loglevel = -1;
8484 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8485 goto bad_log_level;
8486 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008487 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8488 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8489 cur_arg = 1;
8490
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008491 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8492 (*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 +01008493 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8494 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008495 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008496 }
8497
8498 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8499 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8500 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008501
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008502 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008503 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8504 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008505 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008506 } else if (strcmp(args[0], "redirect") == 0) {
8507 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008508 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008509
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008510 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008511 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8512 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8513 goto out_err;
8514 }
8515
8516 /* this redirect rule might already contain a parsed condition which
8517 * we'll pass to the http-request rule.
8518 */
8519 rule->action = HTTP_REQ_ACT_REDIR;
8520 rule->arg.redir = redir;
8521 rule->cond = redir->cond;
8522 redir->cond = NULL;
8523 cur_arg = 2;
8524 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008525 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008526 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 +01008527 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008528 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008529 }
8530
8531 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8532 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008533 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008534
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008535 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8536 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8537 file, linenum, args[0], errmsg);
8538 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008539 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008540 }
8541 rule->cond = cond;
8542 }
8543 else if (*args[cur_arg]) {
8544 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8545 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8546 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008547 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008548 }
8549
8550 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008551 out_err:
8552 free(rule);
8553 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008554}
8555
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008556/* parse an "http-respose" rule */
8557struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8558{
8559 struct http_res_rule *rule;
8560 int cur_arg;
8561
8562 rule = calloc(1, sizeof(*rule));
8563 if (!rule) {
8564 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8565 goto out_err;
8566 }
8567
8568 if (!strcmp(args[0], "allow")) {
8569 rule->action = HTTP_RES_ACT_ALLOW;
8570 cur_arg = 1;
8571 } else if (!strcmp(args[0], "deny")) {
8572 rule->action = HTTP_RES_ACT_DENY;
8573 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008574 } else if (!strcmp(args[0], "set-nice")) {
8575 rule->action = HTTP_RES_ACT_SET_NICE;
8576 cur_arg = 1;
8577
8578 if (!*args[cur_arg] ||
8579 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8580 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8581 file, linenum, args[0]);
8582 goto out_err;
8583 }
8584 rule->arg.nice = atoi(args[cur_arg]);
8585 if (rule->arg.nice < -1024)
8586 rule->arg.nice = -1024;
8587 else if (rule->arg.nice > 1024)
8588 rule->arg.nice = 1024;
8589 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008590 } else if (!strcmp(args[0], "set-tos")) {
8591#ifdef IP_TOS
8592 char *err;
8593 rule->action = HTTP_RES_ACT_SET_TOS;
8594 cur_arg = 1;
8595
8596 if (!*args[cur_arg] ||
8597 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8598 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8599 file, linenum, args[0]);
8600 goto out_err;
8601 }
8602
8603 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8604 if (err && *err != '\0') {
8605 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8606 file, linenum, err, args[0]);
8607 goto out_err;
8608 }
8609 cur_arg++;
8610#else
8611 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8612 goto out_err;
8613#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008614 } else if (!strcmp(args[0], "set-mark")) {
8615#ifdef SO_MARK
8616 char *err;
8617 rule->action = HTTP_RES_ACT_SET_MARK;
8618 cur_arg = 1;
8619
8620 if (!*args[cur_arg] ||
8621 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8622 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8623 file, linenum, args[0]);
8624 goto out_err;
8625 }
8626
8627 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8628 if (err && *err != '\0') {
8629 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8630 file, linenum, err, args[0]);
8631 goto out_err;
8632 }
8633 cur_arg++;
8634 global.last_checks |= LSTCHK_NETADM;
8635#else
8636 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8637 goto out_err;
8638#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008639 } else if (!strcmp(args[0], "set-log-level")) {
8640 rule->action = HTTP_RES_ACT_SET_LOGL;
8641 cur_arg = 1;
8642
8643 if (!*args[cur_arg] ||
8644 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8645 bad_log_level:
8646 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8647 file, linenum, args[0]);
8648 goto out_err;
8649 }
8650 if (strcmp(args[cur_arg], "silent") == 0)
8651 rule->arg.loglevel = -1;
8652 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8653 goto bad_log_level;
8654 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008655 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8656 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8657 cur_arg = 1;
8658
8659 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8660 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8661 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8662 file, linenum, args[0]);
8663 goto out_err;
8664 }
8665
8666 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8667 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8668 LIST_INIT(&rule->arg.hdr_add.fmt);
8669
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008670 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008671 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8672 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8673 cur_arg += 2;
8674 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008675 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 +02008676 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8677 goto out_err;
8678 }
8679
8680 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8681 struct acl_cond *cond;
8682 char *errmsg = NULL;
8683
8684 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8685 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8686 file, linenum, args[0], errmsg);
8687 free(errmsg);
8688 goto out_err;
8689 }
8690 rule->cond = cond;
8691 }
8692 else if (*args[cur_arg]) {
8693 Alert("parsing [%s:%d]: 'http-response %s' expects"
8694 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8695 file, linenum, args[0], args[cur_arg]);
8696 goto out_err;
8697 }
8698
8699 return rule;
8700 out_err:
8701 free(rule);
8702 return NULL;
8703}
8704
Willy Tarreau4baae242012-12-27 12:00:31 +01008705/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008706 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8707 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008708 */
8709struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008710 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008711{
8712 struct redirect_rule *rule;
8713 int cur_arg;
8714 int type = REDIRECT_TYPE_NONE;
8715 int code = 302;
8716 const char *destination = NULL;
8717 const char *cookie = NULL;
8718 int cookie_set = 0;
8719 unsigned int flags = REDIRECT_FLAG_NONE;
8720 struct acl_cond *cond = NULL;
8721
8722 cur_arg = 0;
8723 while (*(args[cur_arg])) {
8724 if (strcmp(args[cur_arg], "location") == 0) {
8725 if (!*args[cur_arg + 1])
8726 goto missing_arg;
8727
8728 type = REDIRECT_TYPE_LOCATION;
8729 cur_arg++;
8730 destination = args[cur_arg];
8731 }
8732 else if (strcmp(args[cur_arg], "prefix") == 0) {
8733 if (!*args[cur_arg + 1])
8734 goto missing_arg;
8735
8736 type = REDIRECT_TYPE_PREFIX;
8737 cur_arg++;
8738 destination = args[cur_arg];
8739 }
8740 else if (strcmp(args[cur_arg], "scheme") == 0) {
8741 if (!*args[cur_arg + 1])
8742 goto missing_arg;
8743
8744 type = REDIRECT_TYPE_SCHEME;
8745 cur_arg++;
8746 destination = args[cur_arg];
8747 }
8748 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8749 if (!*args[cur_arg + 1])
8750 goto missing_arg;
8751
8752 cur_arg++;
8753 cookie = args[cur_arg];
8754 cookie_set = 1;
8755 }
8756 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8757 if (!*args[cur_arg + 1])
8758 goto missing_arg;
8759
8760 cur_arg++;
8761 cookie = args[cur_arg];
8762 cookie_set = 0;
8763 }
8764 else if (strcmp(args[cur_arg], "code") == 0) {
8765 if (!*args[cur_arg + 1])
8766 goto missing_arg;
8767
8768 cur_arg++;
8769 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008770 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008771 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008772 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008773 args[cur_arg - 1], args[cur_arg]);
8774 return NULL;
8775 }
8776 }
8777 else if (!strcmp(args[cur_arg],"drop-query")) {
8778 flags |= REDIRECT_FLAG_DROP_QS;
8779 }
8780 else if (!strcmp(args[cur_arg],"append-slash")) {
8781 flags |= REDIRECT_FLAG_APPEND_SLASH;
8782 }
8783 else if (strcmp(args[cur_arg], "if") == 0 ||
8784 strcmp(args[cur_arg], "unless") == 0) {
8785 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8786 if (!cond) {
8787 memprintf(errmsg, "error in condition: %s", *errmsg);
8788 return NULL;
8789 }
8790 break;
8791 }
8792 else {
8793 memprintf(errmsg,
8794 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8795 args[cur_arg]);
8796 return NULL;
8797 }
8798 cur_arg++;
8799 }
8800
8801 if (type == REDIRECT_TYPE_NONE) {
8802 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8803 return NULL;
8804 }
8805
8806 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8807 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008808 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008809
8810 if (!use_fmt) {
8811 /* old-style static redirect rule */
8812 rule->rdr_str = strdup(destination);
8813 rule->rdr_len = strlen(destination);
8814 }
8815 else {
8816 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008817
8818 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8819 * if prefix == "/", we don't want to add anything, otherwise it
8820 * makes it hard for the user to configure a self-redirection.
8821 */
8822 proxy->conf.args.ctx = ARGC_RDR;
8823 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8824 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8825 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8826 }
8827 }
8828
Willy Tarreau4baae242012-12-27 12:00:31 +01008829 if (cookie) {
8830 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8831 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8832 */
8833 rule->cookie_len = strlen(cookie);
8834 if (cookie_set) {
8835 rule->cookie_str = malloc(rule->cookie_len + 10);
8836 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8837 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8838 rule->cookie_len += 9;
8839 } else {
8840 rule->cookie_str = malloc(rule->cookie_len + 21);
8841 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8842 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8843 rule->cookie_len += 20;
8844 }
8845 }
8846 rule->type = type;
8847 rule->code = code;
8848 rule->flags = flags;
8849 LIST_INIT(&rule->list);
8850 return rule;
8851
8852 missing_arg:
8853 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8854 return NULL;
8855}
8856
Willy Tarreau8797c062007-05-07 00:55:35 +02008857/************************************************************************/
8858/* The code below is dedicated to ACL parsing and matching */
8859/************************************************************************/
8860
8861
Willy Tarreau14174bc2012-04-16 14:34:04 +02008862/* This function ensures that the prerequisites for an L7 fetch are ready,
8863 * which means that a request or response is ready. If some data is missing,
8864 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008865 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8866 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008867 *
8868 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008869 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8870 * decide whether or not an HTTP message is present ;
8871 * 0 if the requested data cannot be fetched or if it is certain that
8872 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008873 * 1 if an HTTP message is ready
8874 */
8875static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008876smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008877 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008878{
8879 struct http_txn *txn = l7;
8880 struct http_msg *msg = &txn->req;
8881
8882 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8883 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8884 */
8885
8886 if (unlikely(!s || !txn))
8887 return 0;
8888
8889 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008890 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008891
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008892 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008893 if (unlikely(!s->req))
8894 return 0;
8895
Willy Tarreauaae75e32013-03-29 12:31:49 +01008896 /* If the buffer does not leave enough free space at the end,
8897 * we must first realign it.
8898 */
8899 if (s->req->buf->p > s->req->buf->data &&
8900 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8901 buffer_slow_realign(s->req->buf);
8902
Willy Tarreau14174bc2012-04-16 14:34:04 +02008903 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008904 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008905 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008906
8907 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008908 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008909 http_msg_analyzer(msg, &txn->hdr_idx);
8910
8911 /* Still no valid request ? */
8912 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008913 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008914 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008915 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008916 }
8917 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008918 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008919 return 0;
8920 }
8921
8922 /* OK we just got a valid HTTP request. We have some minor
8923 * preparation to perform so that further checks can rely
8924 * on HTTP tests.
8925 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008926
8927 /* If the request was parsed but was too large, we must absolutely
8928 * return an error so that it is not processed. At the moment this
8929 * cannot happen, but if the parsers are to change in the future,
8930 * we want this check to be maintained.
8931 */
8932 if (unlikely(s->req->buf->i + s->req->buf->p >
8933 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8934 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008935 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008936 return 1;
8937 }
8938
Willy Tarreau9b28e032012-10-12 23:49:43 +02008939 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008940 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8941 s->flags |= SN_REDIRECTABLE;
8942
Willy Tarreau506d0502013-07-06 13:29:24 +02008943 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8944 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008945 }
8946
Willy Tarreau506d0502013-07-06 13:29:24 +02008947 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008948 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008949 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008950
8951 /* otherwise everything's ready for the request */
8952 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008953 else {
8954 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008955 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8956 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008957 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008958 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008959 }
8960
8961 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008962 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008963 return 1;
8964}
Willy Tarreau8797c062007-05-07 00:55:35 +02008965
Willy Tarreauc0239e02012-04-16 14:42:55 +02008966#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008967 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02008968
Willy Tarreau24e32d82012-04-23 23:55:44 +02008969#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008970 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02008971
Willy Tarreau8797c062007-05-07 00:55:35 +02008972
8973/* 1. Check on METHOD
8974 * We use the pre-parsed method if it is known, and store its number as an
8975 * integer. If it is unknown, we use the pointer and the length.
8976 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008977static int pat_parse_meth(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008978{
8979 int len, meth;
8980
Willy Tarreauae8b7962007-06-09 23:10:04 +02008981 len = strlen(*text);
8982 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008983
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01008984 pattern->smp = smp;
Willy Tarreau8797c062007-05-07 00:55:35 +02008985 pattern->val.i = meth;
8986 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008987 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008988 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008989 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008990 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008991 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008992 pattern->len = len;
8993 }
8994 return 1;
8995}
8996
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008997/* This function fetches the method of current HTTP request and stores
8998 * it in the global pattern struct as a chunk. There are two possibilities :
8999 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9000 * in <len> and <ptr> is NULL ;
9001 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9002 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009003 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009004 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009005static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009006smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009007 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009008{
9009 int meth;
9010 struct http_txn *txn = l7;
9011
Willy Tarreau24e32d82012-04-23 23:55:44 +02009012 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009013
Willy Tarreau8797c062007-05-07 00:55:35 +02009014 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02009015 smp->type = SMP_T_UINT;
9016 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009017 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009018 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9019 /* ensure the indexes are not affected */
9020 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009021 smp->type = SMP_T_CSTR;
9022 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009023 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009024 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009025 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009026 return 1;
9027}
9028
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009029/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01009030static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02009031{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009032 int icase;
9033
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009034
Willy Tarreauf853c462012-04-23 18:53:56 +02009035 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009036 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009037 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009038 return PAT_MATCH;
9039 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009040 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009041
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009042 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9043 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009044 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009045
9046 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009047 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009048 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009049
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009050 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009051 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9052 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009053 return PAT_NOMATCH;
9054 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009055}
9056
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009057static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009058smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009059 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009060{
9061 struct http_txn *txn = l7;
9062 char *ptr;
9063 int len;
9064
Willy Tarreauc0239e02012-04-16 14:42:55 +02009065 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009066
Willy Tarreau8797c062007-05-07 00:55:35 +02009067 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009068 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009069
9070 while ((len-- > 0) && (*ptr++ != '/'));
9071 if (len <= 0)
9072 return 0;
9073
Willy Tarreauf853c462012-04-23 18:53:56 +02009074 smp->type = SMP_T_CSTR;
9075 smp->data.str.str = ptr;
9076 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009077
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009078 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009079 return 1;
9080}
9081
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009082static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009083smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009084 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009085{
9086 struct http_txn *txn = l7;
9087 char *ptr;
9088 int len;
9089
Willy Tarreauc0239e02012-04-16 14:42:55 +02009090 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009091
Willy Tarreauf26b2522012-12-14 08:33:14 +01009092 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9093 return 0;
9094
Willy Tarreau8797c062007-05-07 00:55:35 +02009095 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009096 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009097
9098 while ((len-- > 0) && (*ptr++ != '/'));
9099 if (len <= 0)
9100 return 0;
9101
Willy Tarreauf853c462012-04-23 18:53:56 +02009102 smp->type = SMP_T_CSTR;
9103 smp->data.str.str = ptr;
9104 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009105
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009106 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009107 return 1;
9108}
9109
9110/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009111static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009112smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009113 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009114{
9115 struct http_txn *txn = l7;
9116 char *ptr;
9117 int len;
9118
Willy Tarreauc0239e02012-04-16 14:42:55 +02009119 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009120
Willy Tarreauf26b2522012-12-14 08:33:14 +01009121 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9122 return 0;
9123
Willy Tarreau8797c062007-05-07 00:55:35 +02009124 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009125 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009126
Willy Tarreauf853c462012-04-23 18:53:56 +02009127 smp->type = SMP_T_UINT;
9128 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009129 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009130 return 1;
9131}
9132
9133/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009134static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009135smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009136 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009137{
9138 struct http_txn *txn = l7;
9139
Willy Tarreauc0239e02012-04-16 14:42:55 +02009140 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009141
Willy Tarreauf853c462012-04-23 18:53:56 +02009142 smp->type = SMP_T_CSTR;
9143 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009144 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009145 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009146 return 1;
9147}
9148
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009149static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009150smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009151 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009152{
9153 struct http_txn *txn = l7;
9154
Willy Tarreauc0239e02012-04-16 14:42:55 +02009155 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009156
9157 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009158 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
9159 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009160 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009161 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009162 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009163
9164 /*
9165 * If we are parsing url in frontend space, we prepare backend stage
9166 * to not parse again the same url ! optimization lazyness...
9167 */
9168 if (px->options & PR_O_HTTP_PROXY)
9169 l4->flags |= SN_ADDR_SET;
9170
Willy Tarreau37406352012-04-23 16:16:37 +02009171 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009172 return 1;
9173}
9174
9175static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009176smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009177 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009178{
9179 struct http_txn *txn = l7;
9180
Willy Tarreauc0239e02012-04-16 14:42:55 +02009181 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009182
9183 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009184 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 +02009185 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009186 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009187
9188 if (px->options & PR_O_HTTP_PROXY)
9189 l4->flags |= SN_ADDR_SET;
9190
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009191 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009192 return 1;
9193}
9194
Willy Tarreau185b5c42012-04-26 15:11:51 +02009195/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9196 * Accepts an optional argument of type string containing the header field name,
9197 * and an optional argument of type signed or unsigned integer to request an
9198 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009199 * headers are considered from the first one. It does not stop on commas and
9200 * returns full lines instead (useful for User-Agent or Date for example).
9201 */
9202static int
9203smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009204 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009205{
9206 struct http_txn *txn = l7;
9207 struct hdr_idx *idx = &txn->hdr_idx;
9208 struct hdr_ctx *ctx = smp->ctx.a[0];
9209 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9210 int occ = 0;
9211 const char *name_str = NULL;
9212 int name_len = 0;
9213
9214 if (!ctx) {
9215 /* first call */
9216 ctx = &static_hdr_ctx;
9217 ctx->idx = 0;
9218 smp->ctx.a[0] = ctx;
9219 }
9220
9221 if (args) {
9222 if (args[0].type != ARGT_STR)
9223 return 0;
9224 name_str = args[0].data.str.str;
9225 name_len = args[0].data.str.len;
9226
9227 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9228 occ = args[1].data.uint;
9229 }
9230
9231 CHECK_HTTP_MESSAGE_FIRST();
9232
9233 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9234 /* search for header from the beginning */
9235 ctx->idx = 0;
9236
9237 if (!occ && !(opt & SMP_OPT_ITERATE))
9238 /* no explicit occurrence and single fetch => last header by default */
9239 occ = -1;
9240
9241 if (!occ)
9242 /* prepare to report multiple occurrences for ACL fetches */
9243 smp->flags |= SMP_F_NOT_LAST;
9244
9245 smp->type = SMP_T_CSTR;
9246 smp->flags |= SMP_F_VOL_HDR;
9247 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9248 return 1;
9249
9250 smp->flags &= ~SMP_F_NOT_LAST;
9251 return 0;
9252}
9253
9254/* 6. Check on HTTP header count. The number of occurrences is returned.
9255 * Accepts exactly 1 argument of type string. It does not stop on commas and
9256 * returns full lines instead (useful for User-Agent or Date for example).
9257 */
9258static int
9259smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009260 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009261{
9262 struct http_txn *txn = l7;
9263 struct hdr_idx *idx = &txn->hdr_idx;
9264 struct hdr_ctx ctx;
9265 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9266 int cnt;
9267
9268 if (!args || args->type != ARGT_STR)
9269 return 0;
9270
9271 CHECK_HTTP_MESSAGE_FIRST();
9272
9273 ctx.idx = 0;
9274 cnt = 0;
9275 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9276 cnt++;
9277
9278 smp->type = SMP_T_UINT;
9279 smp->data.uint = cnt;
9280 smp->flags = SMP_F_VOL_HDR;
9281 return 1;
9282}
9283
9284/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9285 * Accepts an optional argument of type string containing the header field name,
9286 * and an optional argument of type signed or unsigned integer to request an
9287 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009288 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009289 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009290static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009291smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009292 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009293{
9294 struct http_txn *txn = l7;
9295 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009296 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009297 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009298 int occ = 0;
9299 const char *name_str = NULL;
9300 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009301
Willy Tarreaua890d072013-04-02 12:01:06 +02009302 if (!ctx) {
9303 /* first call */
9304 ctx = &static_hdr_ctx;
9305 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009306 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009307 }
9308
Willy Tarreau185b5c42012-04-26 15:11:51 +02009309 if (args) {
9310 if (args[0].type != ARGT_STR)
9311 return 0;
9312 name_str = args[0].data.str.str;
9313 name_len = args[0].data.str.len;
9314
9315 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9316 occ = args[1].data.uint;
9317 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009318
Willy Tarreaue333ec92012-04-16 16:26:40 +02009319 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009320
Willy Tarreau185b5c42012-04-26 15:11:51 +02009321 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009322 /* search for header from the beginning */
9323 ctx->idx = 0;
9324
Willy Tarreau185b5c42012-04-26 15:11:51 +02009325 if (!occ && !(opt & SMP_OPT_ITERATE))
9326 /* no explicit occurrence and single fetch => last header by default */
9327 occ = -1;
9328
9329 if (!occ)
9330 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009331 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009332
Willy Tarreau185b5c42012-04-26 15:11:51 +02009333 smp->type = SMP_T_CSTR;
9334 smp->flags |= SMP_F_VOL_HDR;
9335 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 +02009336 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009337
Willy Tarreau37406352012-04-23 16:16:37 +02009338 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009339 return 0;
9340}
9341
Willy Tarreauc11416f2007-06-17 16:58:38 +02009342/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009343 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009344 */
9345static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009346smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009347 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009348{
9349 struct http_txn *txn = l7;
9350 struct hdr_idx *idx = &txn->hdr_idx;
9351 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009352 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009353 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009354
Willy Tarreau24e32d82012-04-23 23:55:44 +02009355 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009356 return 0;
9357
Willy Tarreaue333ec92012-04-16 16:26:40 +02009358 CHECK_HTTP_MESSAGE_FIRST();
9359
Willy Tarreau33a7e692007-06-10 19:45:56 +02009360 ctx.idx = 0;
9361 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009362 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 +02009363 cnt++;
9364
Willy Tarreauf853c462012-04-23 18:53:56 +02009365 smp->type = SMP_T_UINT;
9366 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009367 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009368 return 1;
9369}
9370
Willy Tarreau185b5c42012-04-26 15:11:51 +02009371/* Fetch an HTTP header's integer value. The integer value is returned. It
9372 * takes a mandatory argument of type string and an optional one of type int
9373 * to designate a specific occurrence. It returns an unsigned integer, which
9374 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009375 */
9376static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009377smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009378 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009379{
Willy Tarreauef38c392013-07-22 16:29:32 +02009380 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009381
Willy Tarreauf853c462012-04-23 18:53:56 +02009382 if (ret > 0) {
9383 smp->type = SMP_T_UINT;
9384 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9385 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009386
Willy Tarreaud53e2422012-04-16 17:21:11 +02009387 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009388}
9389
Cyril Bonté69fa9922012-10-25 00:01:06 +02009390/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9391 * and an optional one of type int to designate a specific occurrence.
9392 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009393 */
9394static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009395smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009396 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009397{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009398 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009399
Willy Tarreauef38c392013-07-22 16:29:32 +02009400 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009401 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9402 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009403 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009404 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009405 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009406 if (smp->data.str.len < temp->size - 1) {
9407 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9408 temp->str[smp->data.str.len] = '\0';
9409 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9410 smp->type = SMP_T_IPV6;
9411 break;
9412 }
9413 }
9414 }
9415
Willy Tarreaud53e2422012-04-16 17:21:11 +02009416 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009417 if (!(smp->flags & SMP_F_NOT_LAST))
9418 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009419 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009420 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009421}
9422
Willy Tarreau737b0c12007-06-10 21:28:46 +02009423/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9424 * the first '/' after the possible hostname, and ends before the possible '?'.
9425 */
9426static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009427smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009428 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009429{
9430 struct http_txn *txn = l7;
9431 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009432
Willy Tarreauc0239e02012-04-16 14:42:55 +02009433 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009434
Willy Tarreau9b28e032012-10-12 23:49:43 +02009435 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009436 ptr = http_get_path(txn);
9437 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009438 return 0;
9439
9440 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009441 smp->type = SMP_T_CSTR;
9442 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009443
9444 while (ptr < end && *ptr != '?')
9445 ptr++;
9446
Willy Tarreauf853c462012-04-23 18:53:56 +02009447 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009448 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009449 return 1;
9450}
9451
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009452/* This produces a concatenation of the first occurrence of the Host header
9453 * followed by the path component if it begins with a slash ('/'). This means
9454 * that '*' will not be added, resulting in exactly the first Host entry.
9455 * If no Host header is found, then the path is returned as-is. The returned
9456 * value is stored in the trash so it does not need to be marked constant.
9457 */
9458static int
9459smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009460 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009461{
9462 struct http_txn *txn = l7;
9463 char *ptr, *end, *beg;
9464 struct hdr_ctx ctx;
9465
9466 CHECK_HTTP_MESSAGE_FIRST();
9467
9468 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009469 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 +02009470 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009471 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009472
9473 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009474 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009475 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009476 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009477 smp->data.str.len = ctx.vlen;
9478
9479 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009480 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 +02009481 beg = http_get_path(txn);
9482 if (!beg)
9483 beg = end;
9484
9485 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9486
9487 if (beg < ptr && *beg == '/') {
9488 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9489 smp->data.str.len += ptr - beg;
9490 }
9491
9492 smp->flags = SMP_F_VOL_1ST;
9493 return 1;
9494}
9495
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009496/* This produces a 32-bit hash of the concatenation of the first occurrence of
9497 * the Host header followed by the path component if it begins with a slash ('/').
9498 * This means that '*' will not be added, resulting in exactly the first Host
9499 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009500 * is hashed using the path hash followed by a full avalanche hash and provides a
9501 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009502 * high-traffic sites without having to store whole paths.
9503 */
9504static int
9505smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009506 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009507{
9508 struct http_txn *txn = l7;
9509 struct hdr_ctx ctx;
9510 unsigned int hash = 0;
9511 char *ptr, *beg, *end;
9512 int len;
9513
9514 CHECK_HTTP_MESSAGE_FIRST();
9515
9516 ctx.idx = 0;
9517 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9518 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9519 ptr = ctx.line + ctx.val;
9520 len = ctx.vlen;
9521 while (len--)
9522 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9523 }
9524
9525 /* now retrieve the path */
9526 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9527 beg = http_get_path(txn);
9528 if (!beg)
9529 beg = end;
9530
9531 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9532
9533 if (beg < ptr && *beg == '/') {
9534 while (beg < ptr)
9535 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9536 }
9537 hash = full_hash(hash);
9538
9539 smp->type = SMP_T_UINT;
9540 smp->data.uint = hash;
9541 smp->flags = SMP_F_VOL_1ST;
9542 return 1;
9543}
9544
Willy Tarreau4a550602012-12-09 14:53:32 +01009545/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009546 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9547 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9548 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009549 * that in environments where IPv6 is insignificant, truncating the output to
9550 * 8 bytes would still work.
9551 */
9552static int
9553smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009554 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009555{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009556 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01009557
Willy Tarreauef38c392013-07-22 16:29:32 +02009558 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009559 return 0;
9560
Willy Tarreau47ca5452012-12-23 20:22:19 +01009561 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009562 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9563 temp->len += sizeof(smp->data.uint);
9564
9565 switch (l4->si[0].conn->addr.from.ss_family) {
9566 case AF_INET:
9567 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
9568 temp->len += 4;
9569 break;
9570 case AF_INET6:
9571 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
9572 temp->len += 16;
9573 break;
9574 default:
9575 return 0;
9576 }
9577
9578 smp->data.str = *temp;
9579 smp->type = SMP_T_BIN;
9580 return 1;
9581}
9582
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009583static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009584smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009585 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009586{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009587 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9588 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9589 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009590
Willy Tarreau24e32d82012-04-23 23:55:44 +02009591 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009592
Willy Tarreauf853c462012-04-23 18:53:56 +02009593 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009594 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009595 return 1;
9596}
9597
Willy Tarreau7f18e522010-10-22 20:04:13 +02009598/* return a valid test if the current request is the first one on the connection */
9599static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009600smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009601 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009602{
9603 if (!s)
9604 return 0;
9605
Willy Tarreauf853c462012-04-23 18:53:56 +02009606 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009607 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009608 return 1;
9609}
9610
Willy Tarreau34db1082012-04-19 17:16:54 +02009611/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009612static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009613smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009614 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009615{
9616
Willy Tarreau24e32d82012-04-23 23:55:44 +02009617 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009618 return 0;
9619
Willy Tarreauc0239e02012-04-16 14:42:55 +02009620 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009621
Willy Tarreauc0239e02012-04-16 14:42:55 +02009622 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009623 return 0;
9624
Willy Tarreauf853c462012-04-23 18:53:56 +02009625 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009626 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 +01009627 return 1;
9628}
Willy Tarreau8797c062007-05-07 00:55:35 +02009629
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009630/* Accepts exactly 1 argument of type userlist */
9631static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009632smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009633 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009634{
9635
9636 if (!args || args->type != ARGT_USR)
9637 return 0;
9638
9639 CHECK_HTTP_MESSAGE_FIRST();
9640
9641 if (!get_http_auth(l4))
9642 return 0;
9643
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009644 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009645 smp->ctx.a[0] = args->data.usr; /* user list */
9646 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9647 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9648
9649 /* if the user does not belong to the userlist or has a wrong password,
9650 * report that it unconditionally does not match. Otherwise we return
9651 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009652 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009653 */
9654 smp->type = SMP_T_BOOL;
9655 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9656 if (smp->data.uint)
9657 smp->type = SMP_T_UINT;
9658
9659 return 1;
9660}
9661
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009662/* Try to find the next occurrence of a cookie name in a cookie header value.
9663 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9664 * the cookie value is returned into *value and *value_l, and the function
9665 * returns a pointer to the next pointer to search from if the value was found.
9666 * Otherwise if the cookie was not found, NULL is returned and neither value
9667 * nor value_l are touched. The input <hdr> string should first point to the
9668 * header's value, and the <hdr_end> pointer must point to the first character
9669 * not part of the value. <list> must be non-zero if value may represent a list
9670 * of values (cookie headers). This makes it faster to abort parsing when no
9671 * list is expected.
9672 */
9673static char *
9674extract_cookie_value(char *hdr, const char *hdr_end,
9675 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009676 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009677{
9678 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9679 char *next;
9680
9681 /* we search at least a cookie name followed by an equal, and more
9682 * generally something like this :
9683 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9684 */
9685 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9686 /* Iterate through all cookies on this line */
9687
9688 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9689 att_beg++;
9690
9691 /* find att_end : this is the first character after the last non
9692 * space before the equal. It may be equal to hdr_end.
9693 */
9694 equal = att_end = att_beg;
9695
9696 while (equal < hdr_end) {
9697 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9698 break;
9699 if (http_is_spht[(unsigned char)*equal++])
9700 continue;
9701 att_end = equal;
9702 }
9703
9704 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9705 * is between <att_beg> and <equal>, both may be identical.
9706 */
9707
9708 /* look for end of cookie if there is an equal sign */
9709 if (equal < hdr_end && *equal == '=') {
9710 /* look for the beginning of the value */
9711 val_beg = equal + 1;
9712 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9713 val_beg++;
9714
9715 /* find the end of the value, respecting quotes */
9716 next = find_cookie_value_end(val_beg, hdr_end);
9717
9718 /* make val_end point to the first white space or delimitor after the value */
9719 val_end = next;
9720 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9721 val_end--;
9722 } else {
9723 val_beg = val_end = next = equal;
9724 }
9725
9726 /* We have nothing to do with attributes beginning with '$'. However,
9727 * they will automatically be removed if a header before them is removed,
9728 * since they're supposed to be linked together.
9729 */
9730 if (*att_beg == '$')
9731 continue;
9732
9733 /* Ignore cookies with no equal sign */
9734 if (equal == next)
9735 continue;
9736
9737 /* Now we have the cookie name between att_beg and att_end, and
9738 * its value between val_beg and val_end.
9739 */
9740
9741 if (att_end - att_beg == cookie_name_l &&
9742 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9743 /* let's return this value and indicate where to go on from */
9744 *value = val_beg;
9745 *value_l = val_end - val_beg;
9746 return next + 1;
9747 }
9748
9749 /* Set-Cookie headers only have the name in the first attr=value part */
9750 if (!list)
9751 break;
9752 }
9753
9754 return NULL;
9755}
9756
Willy Tarreaue333ec92012-04-16 16:26:40 +02009757/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009758 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009759 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009760 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009761 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009762 * Accepts exactly 1 argument of type string. If the input options indicate
9763 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009764 */
9765static int
Willy Tarreau51539362012-05-08 12:46:28 +02009766smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009767 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009768{
9769 struct http_txn *txn = l7;
9770 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009771 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009772 const struct http_msg *msg;
9773 const char *hdr_name;
9774 int hdr_name_len;
9775 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009776 int occ = 0;
9777 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009778
Willy Tarreau24e32d82012-04-23 23:55:44 +02009779 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009780 return 0;
9781
Willy Tarreaua890d072013-04-02 12:01:06 +02009782 if (!ctx) {
9783 /* first call */
9784 ctx = &static_hdr_ctx;
9785 ctx->idx = 0;
9786 smp->ctx.a[2] = ctx;
9787 }
9788
Willy Tarreaue333ec92012-04-16 16:26:40 +02009789 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009790
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009791 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009792 msg = &txn->req;
9793 hdr_name = "Cookie";
9794 hdr_name_len = 6;
9795 } else {
9796 msg = &txn->rsp;
9797 hdr_name = "Set-Cookie";
9798 hdr_name_len = 10;
9799 }
9800
Willy Tarreau28376d62012-04-26 21:26:10 +02009801 if (!occ && !(opt & SMP_OPT_ITERATE))
9802 /* no explicit occurrence and single fetch => last cookie by default */
9803 occ = -1;
9804
9805 /* OK so basically here, either we want only one value and it's the
9806 * last one, or we want to iterate over all of them and we fetch the
9807 * next one.
9808 */
9809
Willy Tarreau9b28e032012-10-12 23:49:43 +02009810 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009811 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009812 /* search for the header from the beginning, we must first initialize
9813 * the search parameters.
9814 */
Willy Tarreau37406352012-04-23 16:16:37 +02009815 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009816 ctx->idx = 0;
9817 }
9818
Willy Tarreau28376d62012-04-26 21:26:10 +02009819 smp->flags |= SMP_F_VOL_HDR;
9820
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009821 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009822 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9823 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009824 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9825 goto out;
9826
Willy Tarreau24e32d82012-04-23 23:55:44 +02009827 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009828 continue;
9829
Willy Tarreau37406352012-04-23 16:16:37 +02009830 smp->ctx.a[0] = ctx->line + ctx->val;
9831 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009832 }
9833
Willy Tarreauf853c462012-04-23 18:53:56 +02009834 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009835 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009836 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009837 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009838 &smp->data.str.str,
9839 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009840 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009841 found = 1;
9842 if (occ >= 0) {
9843 /* one value was returned into smp->data.str.{str,len} */
9844 smp->flags |= SMP_F_NOT_LAST;
9845 return 1;
9846 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009847 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009848 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009849 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009850 /* all cookie headers and values were scanned. If we're looking for the
9851 * last occurrence, we may return it now.
9852 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009853 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009854 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009855 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009856}
9857
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009858/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009859 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009860 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009861 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009862 */
9863static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009864smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009865 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009866{
9867 struct http_txn *txn = l7;
9868 struct hdr_idx *idx = &txn->hdr_idx;
9869 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009870 const struct http_msg *msg;
9871 const char *hdr_name;
9872 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009873 int cnt;
9874 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009875 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009876
Willy Tarreau24e32d82012-04-23 23:55:44 +02009877 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009878 return 0;
9879
Willy Tarreaue333ec92012-04-16 16:26:40 +02009880 CHECK_HTTP_MESSAGE_FIRST();
9881
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009882 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009883 msg = &txn->req;
9884 hdr_name = "Cookie";
9885 hdr_name_len = 6;
9886 } else {
9887 msg = &txn->rsp;
9888 hdr_name = "Set-Cookie";
9889 hdr_name_len = 10;
9890 }
9891
Willy Tarreau9b28e032012-10-12 23:49:43 +02009892 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009893 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009894 ctx.idx = 0;
9895 cnt = 0;
9896
9897 while (1) {
9898 /* Note: val_beg == NULL every time we need to fetch a new header */
9899 if (!val_beg) {
9900 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9901 break;
9902
Willy Tarreau24e32d82012-04-23 23:55:44 +02009903 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009904 continue;
9905
9906 val_beg = ctx.line + ctx.val;
9907 val_end = val_beg + ctx.vlen;
9908 }
9909
Willy Tarreauf853c462012-04-23 18:53:56 +02009910 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009911 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009912 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009913 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009914 &smp->data.str.str,
9915 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009916 cnt++;
9917 }
9918 }
9919
Willy Tarreauf853c462012-04-23 18:53:56 +02009920 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009921 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009922 return 1;
9923}
9924
Willy Tarreau51539362012-05-08 12:46:28 +02009925/* Fetch an cookie's integer value. The integer value is returned. It
9926 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9927 */
9928static int
9929smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009930 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009931{
Willy Tarreauef38c392013-07-22 16:29:32 +02009932 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009933
9934 if (ret > 0) {
9935 smp->type = SMP_T_UINT;
9936 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9937 }
9938
9939 return ret;
9940}
9941
Willy Tarreau8797c062007-05-07 00:55:35 +02009942/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009943/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009944/************************************************************************/
9945
David Cournapeau16023ee2010-12-23 20:55:41 +09009946/*
9947 * Given a path string and its length, find the position of beginning of the
9948 * query string. Returns NULL if no query string is found in the path.
9949 *
9950 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9951 *
9952 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9953 */
bedis4c75cca2012-10-05 08:38:24 +02009954static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009955{
9956 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009957
bedis4c75cca2012-10-05 08:38:24 +02009958 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009959 return p ? p + 1 : NULL;
9960}
9961
bedis4c75cca2012-10-05 08:38:24 +02009962static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009963{
bedis4c75cca2012-10-05 08:38:24 +02009964 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009965}
9966
9967/*
9968 * Given a url parameter, find the starting position of the first occurence,
9969 * or NULL if the parameter is not found.
9970 *
9971 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9972 * the function will return query_string+8.
9973 */
9974static char*
9975find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009976 char* url_param_name, size_t url_param_name_l,
9977 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009978{
9979 char *pos, *last;
9980
9981 pos = query_string;
9982 last = query_string + query_string_l - url_param_name_l - 1;
9983
9984 while (pos <= last) {
9985 if (pos[url_param_name_l] == '=') {
9986 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9987 return pos;
9988 pos += url_param_name_l + 1;
9989 }
bedis4c75cca2012-10-05 08:38:24 +02009990 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009991 pos++;
9992 pos++;
9993 }
9994 return NULL;
9995}
9996
9997/*
9998 * Given a url parameter name, returns its value and size into *value and
9999 * *value_l respectively, and returns non-zero. If the parameter is not found,
10000 * zero is returned and value/value_l are not touched.
10001 */
10002static int
10003find_url_param_value(char* path, size_t path_l,
10004 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010005 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010006{
10007 char *query_string, *qs_end;
10008 char *arg_start;
10009 char *value_start, *value_end;
10010
bedis4c75cca2012-10-05 08:38:24 +020010011 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010012 if (!query_string)
10013 return 0;
10014
10015 qs_end = path + path_l;
10016 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010017 url_param_name, url_param_name_l,
10018 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010019 if (!arg_start)
10020 return 0;
10021
10022 value_start = arg_start + url_param_name_l + 1;
10023 value_end = value_start;
10024
bedis4c75cca2012-10-05 08:38:24 +020010025 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010026 value_end++;
10027
10028 *value = value_start;
10029 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010030 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010031}
10032
10033static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010034smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010035 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010036{
bedis4c75cca2012-10-05 08:38:24 +020010037 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010038 struct http_txn *txn = l7;
10039 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010040
bedis4c75cca2012-10-05 08:38:24 +020010041 if (!args || args[0].type != ARGT_STR ||
10042 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010043 return 0;
10044
10045 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010046
bedis4c75cca2012-10-05 08:38:24 +020010047 if (args[1].type)
10048 delim = *args[1].data.str.str;
10049
Willy Tarreau9b28e032012-10-12 23:49:43 +020010050 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010051 args->data.str.str, args->data.str.len,
10052 &smp->data.str.str, &smp->data.str.len,
10053 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010054 return 0;
10055
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010056 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010057 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010058 return 1;
10059}
10060
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010061/* Return the signed integer value for the specified url parameter (see url_param
10062 * above).
10063 */
10064static int
10065smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010066 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010067{
Willy Tarreauef38c392013-07-22 16:29:32 +020010068 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010069
10070 if (ret > 0) {
10071 smp->type = SMP_T_UINT;
10072 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10073 }
10074
10075 return ret;
10076}
10077
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010078/* This produces a 32-bit hash of the concatenation of the first occurrence of
10079 * the Host header followed by the path component if it begins with a slash ('/').
10080 * This means that '*' will not be added, resulting in exactly the first Host
10081 * entry. If no Host header is found, then the path is used. The resulting value
10082 * is hashed using the url hash followed by a full avalanche hash and provides a
10083 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10084 * high-traffic sites without having to store whole paths.
10085 * this differs from the base32 functions in that it includes the url parameters
10086 * as well as the path
10087 */
10088static int
10089smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010090 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010091{
10092 struct http_txn *txn = l7;
10093 struct hdr_ctx ctx;
10094 unsigned int hash = 0;
10095 char *ptr, *beg, *end;
10096 int len;
10097
10098 CHECK_HTTP_MESSAGE_FIRST();
10099
10100 ctx.idx = 0;
10101 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10102 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10103 ptr = ctx.line + ctx.val;
10104 len = ctx.vlen;
10105 while (len--)
10106 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10107 }
10108
10109 /* now retrieve the path */
10110 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10111 beg = http_get_path(txn);
10112 if (!beg)
10113 beg = end;
10114
10115 for (ptr = beg; ptr < end ; ptr++);
10116
10117 if (beg < ptr && *beg == '/') {
10118 while (beg < ptr)
10119 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10120 }
10121 hash = full_hash(hash);
10122
10123 smp->type = SMP_T_UINT;
10124 smp->data.uint = hash;
10125 smp->flags = SMP_F_VOL_1ST;
10126 return 1;
10127}
10128
10129/* This concatenates the source address with the 32-bit hash of the Host and
10130 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10131 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10132 * on the source address length. The URL hash is stored before the address so
10133 * that in environments where IPv6 is insignificant, truncating the output to
10134 * 8 bytes would still work.
10135 */
10136static int
10137smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010138 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010139{
10140 struct chunk *temp;
10141
Willy Tarreaue155ec22013-11-18 18:33:22 +010010142 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010143 return 0;
10144
10145 temp = get_trash_chunk();
10146 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10147 temp->len += sizeof(smp->data.uint);
10148
10149 switch (l4->si[0].conn->addr.from.ss_family) {
10150 case AF_INET:
10151 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
10152 temp->len += 4;
10153 break;
10154 case AF_INET6:
10155 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
10156 temp->len += 16;
10157 break;
10158 default:
10159 return 0;
10160 }
10161
10162 smp->data.str = *temp;
10163 smp->type = SMP_T_BIN;
10164 return 1;
10165}
10166
Willy Tarreau185b5c42012-04-26 15:11:51 +020010167/* This function is used to validate the arguments passed to any "hdr" fetch
10168 * keyword. These keywords support an optional positive or negative occurrence
10169 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10170 * is assumed that the types are already the correct ones. Returns 0 on error,
10171 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10172 * error message in case of error, that the caller is responsible for freeing.
10173 * The initial location must either be freeable or NULL.
10174 */
10175static int val_hdr(struct arg *arg, char **err_msg)
10176{
10177 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010178 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010179 return 0;
10180 }
10181 return 1;
10182}
10183
Willy Tarreau276fae92013-07-25 14:36:01 +020010184/* takes an UINT value on input supposed to represent the time since EPOCH,
10185 * adds an optional offset found in args[0] and emits a string representing
10186 * the date in RFC-1123/5322 format.
10187 */
10188static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10189{
10190 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10191 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10192 struct chunk *temp;
10193 struct tm *tm;
10194 time_t curr_date = smp->data.uint;
10195
10196 /* add offset */
10197 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10198 curr_date += args[0].data.sint;
10199
10200 tm = gmtime(&curr_date);
10201
10202 temp = get_trash_chunk();
10203 temp->len = snprintf(temp->str, temp->size - temp->len,
10204 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10205 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10206 tm->tm_hour, tm->tm_min, tm->tm_sec);
10207
10208 smp->data.str = *temp;
10209 smp->type = SMP_T_STR;
10210 return 1;
10211}
10212
Willy Tarreau4a568972010-05-12 08:08:50 +020010213/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010214/* All supported ACL keywords must be declared here. */
10215/************************************************************************/
10216
10217/* Note: must not be declared <const> as its list will be overwritten.
10218 * Please take care of keeping this list alphabetically sorted.
10219 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010220static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010221 { "base", "base", pat_parse_str, pat_match_str },
10222 { "base_beg", "base", pat_parse_str, pat_match_beg },
10223 { "base_dir", "base", pat_parse_str, pat_match_dir },
10224 { "base_dom", "base", pat_parse_str, pat_match_dom },
10225 { "base_end", "base", pat_parse_str, pat_match_end },
10226 { "base_len", "base", pat_parse_int, pat_match_len },
10227 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10228 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010229
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010230 { "cook", "req.cook", pat_parse_str, pat_match_str },
10231 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10232 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10233 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10234 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10235 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10236 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10237 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010238
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010239 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10240 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10241 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10242 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10243 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10244 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10245 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10246 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010247
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010248 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010249
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010250 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010251
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010252 { "path", "path", pat_parse_str, pat_match_str },
10253 { "path_beg", "path", pat_parse_str, pat_match_beg },
10254 { "path_dir", "path", pat_parse_str, pat_match_dir },
10255 { "path_dom", "path", pat_parse_str, pat_match_dom },
10256 { "path_end", "path", pat_parse_str, pat_match_end },
10257 { "path_len", "path", pat_parse_int, pat_match_len },
10258 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10259 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010260
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010261 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10262 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010263
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010264 { "scook", "res.cook", pat_parse_str, pat_match_str },
10265 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10266 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10267 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10268 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10269 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10270 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10271 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010272
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010273 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10274 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10275 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10276 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10277 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10278 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10279 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10280 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010281
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010282 { "url", "url", pat_parse_str, pat_match_str },
10283 { "url_beg", "url", pat_parse_str, pat_match_beg },
10284 { "url_dir", "url", pat_parse_str, pat_match_dir },
10285 { "url_dom", "url", pat_parse_str, pat_match_dom },
10286 { "url_end", "url", pat_parse_str, pat_match_end },
10287 { "url_len", "url", pat_parse_int, pat_match_len },
10288 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10289 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010290
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010291 { "urlp", "urlp", pat_parse_str, pat_match_str },
10292 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10293 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10294 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10295 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10296 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10297 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10298 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010299
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010300 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010301}};
10302
10303/************************************************************************/
10304/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010305/************************************************************************/
10306/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010307static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010308 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10309 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10310 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10311
10312 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10313 * are only here to match the ACL's name, are request-only and are used
10314 * for ACL compatibility only.
10315 */
10316 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10317 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10318 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10319 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10320
10321 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10322 * only here to match the ACL's name, are request-only and are used for
10323 * ACL compatibility only.
10324 */
10325 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10326 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10327 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10328 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10329
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010330 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10331 { "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 +010010332 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10333 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10334 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010335
10336 /* HTTP protocol on the request path */
10337 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010338 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010339
10340 /* HTTP version on the request path */
10341 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010342 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010343
10344 /* HTTP version on the response path */
10345 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010346 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10347
Willy Tarreau18ed2562013-01-14 15:56:36 +010010348 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10349 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10350 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10351 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10352
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010353 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10354 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010355 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10356 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10357 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10358 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10359
10360 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10361 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10362 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10363 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10364
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010365 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10366 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010367 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10368 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10369 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10370 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10371
Willy Tarreau409bcde2013-01-08 00:31:00 +010010372 /* scook is valid only on the response and is used for ACL compatibility */
10373 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10374 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10375 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10376 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10377
10378 /* shdr is valid only on the response and is used for ACL compatibility */
10379 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10380 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10381 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10382 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10383
10384 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10385 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010386 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10387 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010388 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10389 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10390 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10391 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10392 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10393 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010394}};
10395
Willy Tarreau8797c062007-05-07 00:55:35 +020010396
Willy Tarreau276fae92013-07-25 14:36:01 +020010397/* Note: must not be declared <const> as its list will be overwritten */
10398static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10399 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10400 { NULL, NULL, 0, 0, 0 },
10401}};
10402
Willy Tarreau8797c062007-05-07 00:55:35 +020010403__attribute__((constructor))
10404static void __http_protocol_init(void)
10405{
10406 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010407 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010408 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010409}
10410
10411
Willy Tarreau58f10d72006-12-04 02:26:12 +010010412/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010413 * Local variables:
10414 * c-indent-level: 8
10415 * c-basic-offset: 8
10416 * End:
10417 */