blob: acef3711e024f6562622cd83b90a036060f837be [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;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100899 si->state = SI_ST_CLO;
900
901 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200902 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100903
904 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100905 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906}
907
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100908/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100909 * that the server side is closed. Note that err_type is actually a
910 * bitmask, where almost only aborts may be cumulated with other
911 * values. We consider that aborted operations are more important
912 * than timeouts or errors due to the fact that nobody else in the
913 * logs might explain incomplete retries. All others should avoid
914 * being cumulated. It should normally not be possible to have multiple
915 * aborts at once, but just in case, the first one in sequence is reported.
916 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100917void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100919 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100920
921 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100922 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200923 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100924 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100925 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200926 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100927 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100928 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200929 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200932 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100933 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100934 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200935 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100936 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100937 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200938 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100940 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200941 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942}
943
Willy Tarreau42250582007-04-01 01:30:43 +0200944extern const char sess_term_cond[8];
945extern const char sess_fin_state[8];
946extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200947struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100948struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100949struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100950
Willy Tarreau117f59e2007-03-04 18:17:17 +0100951/*
952 * Capture headers from message starting at <som> according to header list
953 * <cap_hdr>, and fill the <idx> structure appropriately.
954 */
955void capture_headers(char *som, struct hdr_idx *idx,
956 char **cap, struct cap_hdr *cap_hdr)
957{
958 char *eol, *sol, *col, *sov;
959 int cur_idx;
960 struct cap_hdr *h;
961 int len;
962
963 sol = som + hdr_idx_first_pos(idx);
964 cur_idx = hdr_idx_first_idx(idx);
965
966 while (cur_idx) {
967 eol = sol + idx->v[cur_idx].len;
968
969 col = sol;
970 while (col < eol && *col != ':')
971 col++;
972
973 sov = col + 1;
974 while (sov < eol && http_is_lws[(unsigned char)*sov])
975 sov++;
976
977 for (h = cap_hdr; h; h = h->next) {
978 if ((h->namelen == col - sol) &&
979 (strncasecmp(sol, h->name, h->namelen) == 0)) {
980 if (cap[h->index] == NULL)
981 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200982 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100983
984 if (cap[h->index] == NULL) {
985 Alert("HTTP capture : out of memory.\n");
986 continue;
987 }
988
989 len = eol - sov;
990 if (len > h->len)
991 len = h->len;
992
993 memcpy(cap[h->index], sov, len);
994 cap[h->index][len]=0;
995 }
996 }
997 sol = eol + idx->v[cur_idx].cr + 1;
998 cur_idx = idx->v[cur_idx].next;
999 }
1000}
1001
1002
Willy Tarreau42250582007-04-01 01:30:43 +02001003/* either we find an LF at <ptr> or we jump to <bad>.
1004 */
1005#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1006
1007/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1008 * otherwise to <http_msg_ood> with <state> set to <st>.
1009 */
1010#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1011 ptr++; \
1012 if (likely(ptr < end)) \
1013 goto good; \
1014 else { \
1015 state = (st); \
1016 goto http_msg_ood; \
1017 } \
1018 } while (0)
1019
1020
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001022 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1024 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1025 * will give undefined results.
1026 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1027 * and that msg->sol points to the beginning of the response.
1028 * If a complete line is found (which implies that at least one CR or LF is
1029 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1030 * returned indicating an incomplete line (which does not mean that parts have
1031 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1032 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1033 * upon next call.
1034 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001035 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1037 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001040const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001041 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001042 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001043{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001044 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001045
Willy Tarreau8973c702007-01-21 23:58:29 +01001046 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001047 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001048 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001049 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001050 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1051
1052 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001053 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001054 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1055 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001056 state = HTTP_MSG_ERROR;
1057 break;
1058
Willy Tarreau8973c702007-01-21 23:58:29 +01001059 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001060 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001061 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001062 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001063 goto http_msg_rpcode;
1064 }
1065 if (likely(HTTP_IS_SPHT(*ptr)))
1066 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1067 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001068 state = HTTP_MSG_ERROR;
1069 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001070
Willy Tarreau8973c702007-01-21 23:58:29 +01001071 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001072 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001073 if (likely(!HTTP_IS_LWS(*ptr)))
1074 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1075
1076 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001077 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001078 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1079 }
1080
1081 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001082 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001083 http_msg_rsp_reason:
1084 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001086 msg->sl.st.r_l = 0;
1087 goto http_msg_rpline_eol;
1088
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001090 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001091 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001093 goto http_msg_rpreason;
1094 }
1095 if (likely(HTTP_IS_SPHT(*ptr)))
1096 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1097 /* so it's a CR/LF, so there is no reason phrase */
1098 goto http_msg_rsp_reason;
1099
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001101 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 if (likely(!HTTP_IS_CRLF(*ptr)))
1103 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001104 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 http_msg_rpline_eol:
1106 /* We have seen the end of line. Note that we do not
1107 * necessarily have the \n yet, but at least we know that we
1108 * have EITHER \r OR \n, otherwise the response would not be
1109 * complete. We can then record the response length and return
1110 * to the caller which will be able to register it.
1111 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001112 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 return ptr;
1114
1115#ifdef DEBUG_FULL
1116 default:
1117 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1118 exit(1);
1119#endif
1120 }
1121
1122 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001123 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 if (ret_state)
1125 *ret_state = state;
1126 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001127 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129}
1130
Willy Tarreau8973c702007-01-21 23:58:29 +01001131/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 * This function parses a request line between <ptr> and <end>, starting with
1133 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1134 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1135 * will give undefined results.
1136 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1137 * and that msg->sol points to the beginning of the request.
1138 * If a complete line is found (which implies that at least one CR or LF is
1139 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1140 * returned indicating an incomplete line (which does not mean that parts have
1141 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1142 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1143 * upon next call.
1144 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001145 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1147 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001148 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001150const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001151 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001152 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001154 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001155
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001158 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 if (likely(HTTP_IS_TOKEN(*ptr)))
1160 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001163 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001164 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001167 if (likely(HTTP_IS_CRLF(*ptr))) {
1168 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001169 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001173 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 msg->sl.rq.v_l = 0;
1177 goto http_msg_rqline_eol;
1178 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001179 state = HTTP_MSG_ERROR;
1180 break;
1181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001183 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001185 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 goto http_msg_rquri;
1187 }
1188 if (likely(HTTP_IS_SPHT(*ptr)))
1189 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1190 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1191 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001192
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001194 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001195 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001197
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001198 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001199 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1201 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001202
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001203 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001204 /* non-ASCII chars are forbidden unless option
1205 * accept-invalid-http-request is enabled in the frontend.
1206 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001207 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001208 if (msg->err_pos < -1)
1209 goto invalid_char;
1210 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001211 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001212 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1213 }
1214
1215 if (likely(HTTP_IS_CRLF(*ptr))) {
1216 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1217 goto http_msg_req09_uri_e;
1218 }
1219
1220 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001221 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001222 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001223 state = HTTP_MSG_ERROR;
1224 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001227 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001229 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 goto http_msg_rqver;
1231 }
1232 if (likely(HTTP_IS_SPHT(*ptr)))
1233 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1234 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1235 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001239 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001241
1242 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001244 http_msg_rqline_eol:
1245 /* We have seen the end of line. Note that we do not
1246 * necessarily have the \n yet, but at least we know that we
1247 * have EITHER \r OR \n, otherwise the request would not be
1248 * complete. We can then record the request length and return
1249 * to the caller which will be able to register it.
1250 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001251 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001252 return ptr;
1253 }
1254
1255 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001256 state = HTTP_MSG_ERROR;
1257 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259#ifdef DEBUG_FULL
1260 default:
1261 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1262 exit(1);
1263#endif
1264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001274
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001275/*
1276 * Returns the data from Authorization header. Function may be called more
1277 * than once so data is stored in txn->auth_data. When no header is found
1278 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1279 * searching again for something we are unable to find anyway.
1280 */
1281
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001282char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001283
1284int
1285get_http_auth(struct session *s)
1286{
1287
1288 struct http_txn *txn = &s->txn;
1289 struct chunk auth_method;
1290 struct hdr_ctx ctx;
1291 char *h, *p;
1292 int len;
1293
1294#ifdef DEBUG_AUTH
1295 printf("Auth for session %p: %d\n", s, txn->auth.method);
1296#endif
1297
1298 if (txn->auth.method == HTTP_AUTH_WRONG)
1299 return 0;
1300
1301 if (txn->auth.method)
1302 return 1;
1303
1304 txn->auth.method = HTTP_AUTH_WRONG;
1305
1306 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001307
1308 if (txn->flags & TX_USE_PX_CONN) {
1309 h = "Proxy-Authorization";
1310 len = strlen(h);
1311 } else {
1312 h = "Authorization";
1313 len = strlen(h);
1314 }
1315
Willy Tarreau9b28e032012-10-12 23:49:43 +02001316 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317 return 0;
1318
1319 h = ctx.line + ctx.val;
1320
1321 p = memchr(h, ' ', ctx.vlen);
1322 if (!p || p == h)
1323 return 0;
1324
1325 chunk_initlen(&auth_method, h, 0, p-h);
1326 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1327
1328 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1329
1330 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001331 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001332
1333 if (len < 0)
1334 return 0;
1335
1336
1337 get_http_auth_buff[len] = '\0';
1338
1339 p = strchr(get_http_auth_buff, ':');
1340
1341 if (!p)
1342 return 0;
1343
1344 txn->auth.user = get_http_auth_buff;
1345 *p = '\0';
1346 txn->auth.pass = p+1;
1347
1348 txn->auth.method = HTTP_AUTH_BASIC;
1349 return 1;
1350 }
1351
1352 return 0;
1353}
1354
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Willy Tarreau8973c702007-01-21 23:58:29 +01001356/*
1357 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001358 * depending on the initial msg->msg_state. The caller is responsible for
1359 * ensuring that the message does not wrap. The function can be preempted
1360 * everywhere when data are missing and recalled at the exact same location
1361 * with no information loss. The message may even be realigned between two
1362 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001363 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001364 * fields. Note that msg->sol will be initialized after completing the first
1365 * state, so that none of the msg pointers has to be initialized prior to the
1366 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001368void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001370 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001372 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001373
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001374 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001375 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001376 ptr = buf->p + msg->next;
1377 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 if (unlikely(ptr >= end))
1380 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 /*
1384 * First, states that are specific to the response only.
1385 * We check them first so that request and headers are
1386 * closer to each other (accessed more often).
1387 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001388 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001389 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001391 /* we have a start of message, but we have to check
1392 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001393 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001394 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001395 if (unlikely(ptr != buf->p)) {
1396 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001397 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001398 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001399 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 }
Willy Tarreau26927362012-05-18 23:22:52 +02001401 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001402 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001403 hdr_idx_init(idx);
1404 state = HTTP_MSG_RPVER;
1405 goto http_msg_rpver;
1406 }
1407
1408 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1409 goto http_msg_invalid;
1410
1411 if (unlikely(*ptr == '\n'))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1413 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1414 /* stop here */
1415
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001417 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001418 EXPECT_LF_HERE(ptr, http_msg_invalid);
1419 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1420 /* stop here */
1421
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001424 case HTTP_MSG_RPVER_SP:
1425 case HTTP_MSG_RPCODE:
1426 case HTTP_MSG_RPCODE_SP:
1427 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001428 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001429 state, ptr, end,
1430 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 if (unlikely(!ptr))
1432 return;
1433
1434 /* we have a full response and we know that we have either a CR
1435 * or an LF at <ptr>.
1436 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1438
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001439 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 if (likely(*ptr == '\r'))
1441 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1442 goto http_msg_rpline_end;
1443
Willy Tarreau8973c702007-01-21 23:58:29 +01001444 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001445 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 /* msg->sol must point to the first of CR or LF. */
1447 EXPECT_LF_HERE(ptr, http_msg_invalid);
1448 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1449 /* stop here */
1450
1451 /*
1452 * Second, states that are specific to the request only
1453 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001455 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001457 /* we have a start of message, but we have to check
1458 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001459 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001460 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001461 if (likely(ptr != buf->p)) {
1462 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001463 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001464 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001465 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 }
Willy Tarreau26927362012-05-18 23:22:52 +02001467 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001468 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 state = HTTP_MSG_RQMETH;
1470 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1474 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 if (unlikely(*ptr == '\n'))
1477 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1478 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001482 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 EXPECT_LF_HERE(ptr, http_msg_invalid);
1484 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001485 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 case HTTP_MSG_RQMETH_SP:
1490 case HTTP_MSG_RQURI:
1491 case HTTP_MSG_RQURI_SP:
1492 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001493 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001494 state, ptr, end,
1495 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 if (unlikely(!ptr))
1497 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 /* we have a full request and we know that we have either a CR
1500 * or an LF at <ptr>.
1501 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001504 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 if (likely(*ptr == '\r'))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001510 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 /* check for HTTP/0.9 request : no version information available.
1512 * msg->sol must point to the first of CR or LF.
1513 */
1514 if (unlikely(msg->sl.rq.v_l == 0))
1515 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 EXPECT_LF_HERE(ptr, http_msg_invalid);
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001519 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 /*
1522 * Common states below
1523 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001526 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (likely(!HTTP_IS_CRLF(*ptr))) {
1528 goto http_msg_hdr_name;
1529 }
1530
1531 if (likely(*ptr == '\r'))
1532 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1533 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 /* assumes msg->sol points to the first char */
1538 if (likely(HTTP_IS_TOKEN(*ptr)))
1539 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001541 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001543
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001544 if (likely(msg->err_pos < -1) || *ptr == '\n')
1545 goto http_msg_invalid;
1546
1547 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001549
1550 /* and we still accept this non-token character */
1551 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001554 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001555 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (likely(HTTP_IS_SPHT(*ptr)))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 if (likely(!HTTP_IS_CRLF(*ptr))) {
1563 goto http_msg_hdr_val;
1564 }
1565
1566 if (likely(*ptr == '\r'))
1567 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1568 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 EXPECT_LF_HERE(ptr, http_msg_invalid);
1573 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (likely(HTTP_IS_SPHT(*ptr))) {
1578 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001579 for (; buf->p + msg->sov < ptr; msg->sov++)
1580 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 goto http_msg_hdr_l1_sp;
1582 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001583 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001584 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 goto http_msg_complete_header;
1586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001588 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001589 /* assumes msg->sol points to the first char, and msg->sov
1590 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 */
1592 if (likely(!HTTP_IS_CRLF(*ptr)))
1593 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001594
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001595 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 /* Note: we could also copy eol into ->eoh so that we have the
1597 * real header end in case it ends with lots of LWS, but is this
1598 * really needed ?
1599 */
1600 if (likely(*ptr == '\r'))
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1602 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001605 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 EXPECT_LF_HERE(ptr, http_msg_invalid);
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001610 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1612 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001613 for (; buf->p + msg->eol < ptr; msg->eol++)
1614 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 goto http_msg_hdr_val;
1616 }
1617 http_msg_complete_header:
1618 /*
1619 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001620 * Assumes msg->sol points to the first char, msg->sov points
1621 * to the first character of the value and msg->eol to the
1622 * first CR or LF so we know how the line ends. We insert last
1623 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001625 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 idx, idx->tail) < 0))
1627 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001628
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 if (likely(!HTTP_IS_CRLF(*ptr))) {
1631 goto http_msg_hdr_name;
1632 }
1633
1634 if (likely(*ptr == '\r'))
1635 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1636 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001639 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 /* Assumes msg->sol points to the first of either CR or LF */
1641 EXPECT_LF_HERE(ptr, http_msg_invalid);
1642 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001644 msg->eoh = msg->sol;
1645 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001646 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001648
1649 case HTTP_MSG_ERROR:
1650 /* this may only happen if we call http_msg_analyser() twice with an error */
1651 break;
1652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653#ifdef DEBUG_FULL
1654 default:
1655 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1656 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001657#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 }
1659 http_msg_ood:
1660 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001661 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001664
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 http_msg_invalid:
1666 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001668 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 return;
1670}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001671
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001672/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1673 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1674 * nothing is done and 1 is returned.
1675 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001676static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001677{
1678 int delta;
1679 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001680 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001681
1682 if (msg->sl.rq.v_l != 0)
1683 return 1;
1684
Willy Tarreau9b28e032012-10-12 23:49:43 +02001685 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001686 delta = 0;
1687
1688 if (msg->sl.rq.u_l == 0) {
1689 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001690 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001691 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001692 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001693 }
1694 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001695 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001696 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001697 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001698 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001699 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001700 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001701 NULL, NULL);
1702 if (unlikely(!cur_end))
1703 return 0;
1704
1705 /* we have a full HTTP/1.0 request now and we know that
1706 * we have either a CR or an LF at <ptr>.
1707 */
1708 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1709 return 1;
1710}
1711
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001712/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001713 * and "keep-alive" values. If we already know that some headers may safely
1714 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001715 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1716 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001717 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1719 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1720 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001721 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001722 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001723void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001724{
Willy Tarreau5b154472009-12-21 20:11:07 +01001725 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001726 const char *hdr_val = "Connection";
1727 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001728
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001729 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001730 return;
1731
Willy Tarreau88d349d2010-01-25 12:15:43 +01001732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection";
1734 hdr_len = 16;
1735 }
1736
Willy Tarreau5b154472009-12-21 20:11:07 +01001737 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001739 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001740 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1741 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001742 if (to_del & 2)
1743 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001744 else
1745 txn->flags |= TX_CON_KAL_SET;
1746 }
1747 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1748 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001749 if (to_del & 1)
1750 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001751 else
1752 txn->flags |= TX_CON_CLO_SET;
1753 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001754 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1755 txn->flags |= TX_HDR_CONN_UPG;
1756 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001757 }
1758
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001759 txn->flags |= TX_HDR_CONN_PRS;
1760 return;
1761}
Willy Tarreau5b154472009-12-21 20:11:07 +01001762
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001763/* Apply desired changes on the Connection: header. Values may be removed and/or
1764 * added depending on the <wanted> flags, which are exclusively composed of
1765 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1766 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1767 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001768void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001769{
1770 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001771 const char *hdr_val = "Connection";
1772 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001773
1774 ctx.idx = 0;
1775
Willy Tarreau88d349d2010-01-25 12:15:43 +01001776
1777 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1778 hdr_val = "Proxy-Connection";
1779 hdr_len = 16;
1780 }
1781
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001782 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001784 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1785 if (wanted & TX_CON_KAL_SET)
1786 txn->flags |= TX_CON_KAL_SET;
1787 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001788 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001789 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001790 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1791 if (wanted & TX_CON_CLO_SET)
1792 txn->flags |= TX_CON_CLO_SET;
1793 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001794 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001795 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001796 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001797
1798 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1799 return;
1800
1801 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1802 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001803 hdr_val = "Connection: close";
1804 hdr_len = 17;
1805 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1806 hdr_val = "Proxy-Connection: close";
1807 hdr_len = 23;
1808 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001809 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001810 }
1811
1812 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1813 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001814 hdr_val = "Connection: keep-alive";
1815 hdr_len = 22;
1816 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1817 hdr_val = "Proxy-Connection: keep-alive";
1818 hdr_len = 28;
1819 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001820 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821 }
1822 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001823}
1824
Willy Tarreaua458b672012-03-05 11:17:50 +01001825/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001827 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001829 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001830 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001831static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001832{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001833 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001835 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001836 const char *end = buf->data + buf->size;
1837 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 unsigned int chunk = 0;
1839
1840 /* The chunk size is in the following form, though we are only
1841 * interested in the size and CRLF :
1842 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1843 */
1844 while (1) {
1845 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001846 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001849 if (c < 0) /* not a hex digit anymore */
1850 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001851 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001852 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001853 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001854 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 chunk = (chunk << 4) + c;
1856 }
1857
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001859 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001860 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861
1862 while (http_is_spht[(unsigned char)*ptr]) {
1863 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001865 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001866 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 }
1868
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 /* Up to there, we know that at least one byte is present at *ptr. Check
1870 * for the end of chunk size.
1871 */
1872 while (1) {
1873 if (likely(HTTP_IS_CRLF(*ptr))) {
1874 /* we now have a CR or an LF at ptr */
1875 if (likely(*ptr == '\r')) {
1876 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001877 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001878 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 return 0;
1880 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001881
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001883 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001885 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 /* done */
1887 break;
1888 }
1889 else if (*ptr == ';') {
1890 /* chunk extension, ends at next CRLF */
1891 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001892 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001893 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001894 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
1896 while (!HTTP_IS_CRLF(*ptr)) {
1897 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001899 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 return 0;
1901 }
1902 /* we have a CRLF now, loop above */
1903 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001904 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001906 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 }
1908
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001910 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001912 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001913 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001914 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001915 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001917 msg->chunk_len = chunk;
1918 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001921 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001923 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001924}
1925
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001926/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001927 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 * the trailers is found, it is automatically scheduled to be forwarded,
1929 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1930 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001931 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001932 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001933 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001934 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1935 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001936 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001937 * matches the length of trailers already parsed and not forwarded. It is also
1938 * important to note that this function is designed to be able to parse wrapped
1939 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001941static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001943 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001944
Willy Tarreaua458b672012-03-05 11:17:50 +01001945 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001947 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001948 const char *ptr = b_ptr(buf, msg->next);
1949 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001950 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951
1952 /* scan current line and stop at LF or CRLF */
1953 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001954 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 return 0;
1956
1957 if (*ptr == '\n') {
1958 if (!p1)
1959 p1 = ptr;
1960 p2 = ptr;
1961 break;
1962 }
1963
1964 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001965 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001966 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001967 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001968 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001969 p1 = ptr;
1970 }
1971
1972 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001973 if (ptr >= buf->data + buf->size)
1974 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 }
1976
1977 /* after LF; point to beginning of next line */
1978 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001979 if (p2 >= buf->data + buf->size)
1980 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001982 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001983 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001985
1986 /* schedule this line for forwarding */
1987 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 if (msg->sov >= buf->size)
1989 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001991 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001992 /* LF/CRLF at beginning of line => end of trailers at p2.
1993 * Everything was scheduled for forwarding, there's nothing
1994 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001995 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001996 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 msg->msg_state = HTTP_MSG_DONE;
1998 return 1;
1999 }
2000 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002001 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002 }
2003}
2004
Willy Tarreau54d23df2012-10-25 19:04:45 +02002005/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002007 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002008 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2010 * not enough data are available, the function does not change anything and
2011 * returns zero. If a parse error is encountered, the function returns < 0 and
2012 * does not change anything. Note: this function is designed to parse wrapped
2013 * CRLF at the end of the buffer.
2014 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002015static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002016{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002017 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002019 int bytes;
2020
2021 /* NB: we'll check data availabilty at the end. It's not a
2022 * problem because whatever we match first will be checked
2023 * against the correct length.
2024 */
2025 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002026 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002027 if (*ptr == '\r') {
2028 bytes++;
2029 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002030 if (ptr >= buf->data + buf->size)
2031 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002032 }
2033
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002034 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002037 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002038 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002039 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002040 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041
2042 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002043 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002045 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2046 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002047 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2049 return 1;
2050}
Willy Tarreau5b154472009-12-21 20:11:07 +01002051
William Lallemand82fe75c2012-10-23 10:25:10 +02002052
2053/*
2054 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002055 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002056int select_compression_request_header(struct session *s, struct buffer *req)
2057{
2058 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002059 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002060 struct hdr_ctx ctx;
2061 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002062 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002063
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002064 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2065 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002066 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2067 */
2068 ctx.idx = 0;
2069 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2070 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002071 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2072 (ctx.vlen < 31 ||
2073 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2074 ctx.line[ctx.val + 30] < '6' ||
2075 (ctx.line[ctx.val + 30] == '6' &&
2076 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2077 s->comp_algo = NULL;
2078 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002079 }
2080
William Lallemand82fe75c2012-10-23 10:25:10 +02002081 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002082 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002083 ctx.idx = 0;
2084 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002085 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002086 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2087 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002088
2089 /* remove all occurrences of the header when "compression offload" is set */
2090
2091 if ((s->be->comp && s->be->comp->offload) ||
2092 (s->fe->comp && s->fe->comp->offload)) {
2093 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2094 ctx.idx = 0;
2095 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2096 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2097 }
2098 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002099 return 1;
2100 }
2101 }
2102 }
2103 }
2104
2105 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002106 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2107 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002108 if (comp_algo->add_data == identity_add_data) {
2109 s->comp_algo = comp_algo;
2110 return 1;
2111 }
2112 }
2113 }
2114
2115 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002116 return 0;
2117}
2118
2119/*
2120 * Selects a comression algorithm depending of the server response.
2121 */
2122int select_compression_response_header(struct session *s, struct buffer *res)
2123{
2124 struct http_txn *txn = &s->txn;
2125 struct http_msg *msg = &txn->rsp;
2126 struct hdr_ctx ctx;
2127 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002128
2129 /* no common compression algorithm was found in request header */
2130 if (s->comp_algo == NULL)
2131 goto fail;
2132
2133 /* HTTP < 1.1 should not be compressed */
2134 if (!(msg->flags & HTTP_MSGF_VER_11))
2135 goto fail;
2136
William Lallemandd3002612012-11-26 14:34:47 +01002137 /* 200 only */
2138 if (txn->status != 200)
2139 goto fail;
2140
William Lallemand82fe75c2012-10-23 10:25:10 +02002141 /* Content-Length is null */
2142 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2143 goto fail;
2144
Willy Tarreau667c2a32013-04-09 08:13:58 +02002145 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2146 if (msg->flags & HTTP_MSGF_TE_CHNK)
2147 goto fail;
2148
William Lallemand82fe75c2012-10-23 10:25:10 +02002149 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002150 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002151 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2152 goto fail;
2153
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002154 /* no compression when Cache-Control: no-transform is present in the message */
2155 ctx.idx = 0;
2156 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2157 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2158 goto fail;
2159 }
2160
William Lallemand82fe75c2012-10-23 10:25:10 +02002161 comp_type = NULL;
2162
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002163 /* we don't want to compress multipart content-types, nor content-types that are
2164 * not listed in the "compression type" directive if any. If no content-type was
2165 * found but configuration requires one, we don't compress either. Backend has
2166 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002167 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002168 ctx.idx = 0;
2169 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2170 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2171 goto fail;
2172
2173 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2174 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002175 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002176 if (ctx.vlen >= comp_type->name_len &&
2177 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002178 /* this Content-Type should be compressed */
2179 break;
2180 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002181 /* this Content-Type should not be compressed */
2182 if (comp_type == NULL)
2183 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002184 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002185 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002186 else { /* no content-type header */
2187 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2188 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002189 }
2190
William Lallemandd85f9172012-11-09 17:05:39 +01002191 /* limit compression rate */
2192 if (global.comp_rate_lim > 0)
2193 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2194 goto fail;
2195
William Lallemand072a2bf2012-11-20 17:01:01 +01002196 /* limit cpu usage */
2197 if (idle_pct < compress_min_idle)
2198 goto fail;
2199
William Lallemand4c49fae2012-11-07 15:00:23 +01002200 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002201 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002202 goto fail;
2203
William Lallemandec3e3892012-11-12 17:02:18 +01002204 s->flags |= SN_COMP_READY;
2205
William Lallemand82fe75c2012-10-23 10:25:10 +02002206 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002207 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002208 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2209 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2210
2211 /* add Transfer-Encoding header */
2212 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2213 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2214
2215 /*
2216 * Add Content-Encoding header when it's not identity encoding.
2217 * RFC 2616 : Identity encoding: This content-coding is used only in the
2218 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2219 * header.
2220 */
2221 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002222 trash.len = 18;
2223 memcpy(trash.str, "Content-Encoding: ", trash.len);
2224 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2225 trash.len += s->comp_algo->name_len;
2226 trash.str[trash.len] = '\0';
2227 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002228 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 return 1;
2230
2231fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002232 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002233 return 0;
2234}
2235
2236
Willy Tarreaud787e662009-07-07 10:14:51 +02002237/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2238 * processing can continue on next analysers, or zero if it either needs more
2239 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2240 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2241 * when it has nothing left to do, and may remove any analyser when it wants to
2242 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002243 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002244int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002245{
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 /*
2247 * We will parse the partial (or complete) lines.
2248 * We will check the request syntax, and also join multi-line
2249 * headers. An index of all the lines will be elaborated while
2250 * parsing.
2251 *
2252 * For the parsing, we use a 28 states FSM.
2253 *
2254 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002255 * req->buf->p = beginning of request
2256 * req->buf->p + msg->eoh = end of processed headers / start of current one
2257 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002258 * msg->eol = end of current header or line (LF or CRLF)
2259 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002260 *
2261 * At end of parsing, we may perform a capture of the error (if any), and
2262 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2263 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2264 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002266
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002268 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002269 struct http_txn *txn = &s->txn;
2270 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002271 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002272
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002273 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002274 now_ms, __FUNCTION__,
2275 s,
2276 req,
2277 req->rex, req->wex,
2278 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002279 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002280 req->analysers);
2281
Willy Tarreau52a0c602009-08-16 22:45:38 +02002282 /* we're speaking HTTP here, so let's speak HTTP to the client */
2283 s->srv_error = http_return_srv_error;
2284
Willy Tarreau83e3af02009-12-28 17:39:57 +01002285 /* There's a protected area at the end of the buffer for rewriting
2286 * purposes. We don't want to start to parse the request if the
2287 * protected area is affected, because we may have to move processed
2288 * data later, which is much more complicated.
2289 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002290 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002291 if (txn->flags & TX_NOT_FIRST) {
2292 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002293 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002294 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002295 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002296 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002297 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002298 return 0;
2299 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002300 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2301 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2302 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002303 }
2304
Willy Tarreau065e8332010-01-08 00:30:20 +01002305 /* Note that we have the same problem with the response ; we
2306 * may want to send a redirect, error or anything which requires
2307 * some spare space. So we'll ensure that we have at least
2308 * maxrewrite bytes available in the response buffer before
2309 * processing that one. This will only affect pipelined
2310 * keep-alive requests.
2311 */
2312 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002313 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002314 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2315 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2316 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002317 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002318 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002319 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002320 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002321 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002322 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002323 return 0;
2324 }
2325 }
2326
Willy Tarreau9b28e032012-10-12 23:49:43 +02002327 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002328 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002329 }
2330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /* 1: we might have to print this header in debug mode */
2332 if (unlikely((global.mode & MODE_DEBUG) &&
2333 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002334 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002336
Willy Tarreau9b28e032012-10-12 23:49:43 +02002337 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002338 /* this is a bit complex : in case of error on the request line,
2339 * we know that rq.l is still zero, so we display only the part
2340 * up to the end of the line (truncated by debug_hdr).
2341 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002342 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 sol += hdr_idx_first_pos(&txn->hdr_idx);
2346 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 while (cur_idx) {
2349 eol = sol + txn->hdr_idx.v[cur_idx].len;
2350 debug_hdr("clihdr", s, sol, eol);
2351 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2352 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002353 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 }
2355
Willy Tarreau58f10d72006-12-04 02:26:12 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 /*
2358 * Now we quickly check if we have found a full valid request.
2359 * If not so, we check the FD and buffer states before leaving.
2360 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002361 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002362 * requests are checked first. When waiting for a second request
2363 * on a keep-alive session, if we encounter and error, close, t/o,
2364 * we note the error in the session flags but don't set any state.
2365 * Since the error will be noted there, it will not be counted by
2366 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002367 * Last, we may increase some tracked counters' http request errors on
2368 * the cases that are deliberately the client's fault. For instance,
2369 * a timeout or connection reset is not counted as an error. However
2370 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002372
Willy Tarreau655dce92009-11-08 13:10:58 +01002373 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002374 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002376 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002377 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002378 session_inc_http_req_ctr(s);
2379 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002380 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002382 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002383
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 /* 1: Since we are in header mode, if there's no space
2385 * left for headers, we won't be able to free more
2386 * later, so the session will never terminate. We
2387 * must terminate it now.
2388 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002389 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* FIXME: check if URI is set and return Status
2391 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002392 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002393 session_inc_http_req_ctr(s);
2394 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002395 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002396 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002397 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002398 goto return_bad_req;
2399 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002400
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002402 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002403 if (!(s->flags & SN_ERR_MASK))
2404 s->flags |= SN_ERR_CLICL;
2405
Willy Tarreaufcffa692010-01-10 14:21:19 +01002406 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002407 goto failed_keep_alive;
2408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002410 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002411 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002412 session_inc_http_err_ctr(s);
2413 }
2414
Willy Tarreaudc979f22012-12-04 10:39:01 +01002415 txn->status = 400;
2416 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 msg->msg_state = HTTP_MSG_ERROR;
2418 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002419
Willy Tarreauda7ff642010-06-23 11:44:09 +02002420 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002421 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002422 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002423 if (s->listener->counters)
2424 s->listener->counters->failed_req++;
2425
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 if (!(s->flags & SN_FINST_MASK))
2427 s->flags |= SN_FINST_R;
2428 return 0;
2429 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002432 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002433 if (!(s->flags & SN_ERR_MASK))
2434 s->flags |= SN_ERR_CLITO;
2435
Willy Tarreaufcffa692010-01-10 14:21:19 +01002436 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002437 goto failed_keep_alive;
2438
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002440 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002441 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002442 session_inc_http_err_ctr(s);
2443 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002445 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002446 msg->msg_state = HTTP_MSG_ERROR;
2447 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002448
Willy Tarreauda7ff642010-06-23 11:44:09 +02002449 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002450 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002451 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002452 if (s->listener->counters)
2453 s->listener->counters->failed_req++;
2454
Willy Tarreau59234e92008-11-30 23:51:27 +01002455 if (!(s->flags & SN_FINST_MASK))
2456 s->flags |= SN_FINST_R;
2457 return 0;
2458 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002459
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002461 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002462 if (!(s->flags & SN_ERR_MASK))
2463 s->flags |= SN_ERR_CLICL;
2464
Willy Tarreaufcffa692010-01-10 14:21:19 +01002465 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002466 goto failed_keep_alive;
2467
Willy Tarreau4076a152009-04-02 15:18:36 +02002468 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002469 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002471 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 msg->msg_state = HTTP_MSG_ERROR;
2473 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002474
Willy Tarreauda7ff642010-06-23 11:44:09 +02002475 session_inc_http_err_ctr(s);
2476 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002477 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002478 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002479 if (s->listener->counters)
2480 s->listener->counters->failed_req++;
2481
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 if (!(s->flags & SN_FINST_MASK))
2483 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002484 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002485 }
2486
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002487 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002488 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2489 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002490#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002491 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002492 /* We need more data, we have to re-enable quick-ack in case we
2493 * previously disabled it, otherwise we might cause the client
2494 * to delay next data.
2495 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002496 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002497 }
2498#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002499
Willy Tarreaufcffa692010-01-10 14:21:19 +01002500 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2501 /* If the client starts to talk, let's fall back to
2502 * request timeout processing.
2503 */
2504 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002505 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002506 }
2507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002509 if (!tick_isset(req->analyse_exp)) {
2510 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2511 (txn->flags & TX_WAIT_NEXT_RQ) &&
2512 tick_isset(s->be->timeout.httpka))
2513 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2514 else
2515 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2516 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002517
Willy Tarreau59234e92008-11-30 23:51:27 +01002518 /* we're not ready yet */
2519 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002520
2521 failed_keep_alive:
2522 /* Here we process low-level errors for keep-alive requests. In
2523 * short, if the request is not the first one and it experiences
2524 * a timeout, read error or shutdown, we just silently close so
2525 * that the client can try again.
2526 */
2527 txn->status = 0;
2528 msg->msg_state = HTTP_MSG_RQBEFORE;
2529 req->analysers = 0;
2530 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002531 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002532 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002533 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002534 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536
Willy Tarreaud787e662009-07-07 10:14:51 +02002537 /* OK now we have a complete HTTP request with indexed headers. Let's
2538 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002539 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002540 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002541 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002542 * byte after the last LF. msg->sov points to the first byte of data.
2543 * msg->eol cannot be trusted because it may have been left uninitialized
2544 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002545 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002546
Willy Tarreauda7ff642010-06-23 11:44:09 +02002547 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002548 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2549
Willy Tarreaub16a5742010-01-10 14:46:16 +01002550 if (txn->flags & TX_WAIT_NEXT_RQ) {
2551 /* kill the pending keep-alive timeout */
2552 txn->flags &= ~TX_WAIT_NEXT_RQ;
2553 req->analyse_exp = TICK_ETERNITY;
2554 }
2555
2556
Willy Tarreaud787e662009-07-07 10:14:51 +02002557 /* Maybe we found in invalid header name while we were configured not
2558 * to block on that, so we have to capture it now.
2559 */
2560 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002561 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /*
2564 * 1: identify the method
2565 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002566 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002567
2568 /* we can make use of server redirect on GET and HEAD */
2569 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2570 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /*
2573 * 2: check if the URI matches the monitor_uri.
2574 * We have to do this for every request which gets in, because
2575 * the monitor-uri is defined by the frontend.
2576 */
2577 if (unlikely((s->fe->monitor_uri_len != 0) &&
2578 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002579 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 s->fe->monitor_uri,
2581 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002582 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002584 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002586
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002588 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002589
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002591 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002592 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002593
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 ret = acl_pass(ret);
2595 if (cond->pol == ACL_COND_UNLESS)
2596 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002597
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 if (ret) {
2599 /* we fail this request, let's return 503 service unavail */
2600 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002601 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002602 if (!(s->flags & SN_ERR_MASK))
2603 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002605 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002606 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 /* nothing to fail, let's reply normaly */
2609 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002610 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002611 if (!(s->flags & SN_ERR_MASK))
2612 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 goto return_prx_cond;
2614 }
2615
2616 /*
2617 * 3: Maybe we have to copy the original REQURI for the logs ?
2618 * Note: we cannot log anymore if the request has been
2619 * classified as invalid.
2620 */
2621 if (unlikely(s->logs.logwait & LW_REQ)) {
2622 /* we have a complete HTTP request that we must log */
2623 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2624 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 if (urilen >= REQURI_LEN)
2627 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002628 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002630
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002631 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 s->do_log(s);
2633 } else {
2634 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 }
Willy Tarreau06619262006-12-17 08:37:22 +01002637
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002639 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002640 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002641
Willy Tarreau5b154472009-12-21 20:11:07 +01002642 /* ... and check if the request is HTTP/1.1 or above */
2643 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002644 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2645 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2646 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002647 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002648
2649 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002650 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 +01002651
Willy Tarreau88d349d2010-01-25 12:15:43 +01002652 /* if the frontend has "option http-use-proxy-header", we'll check if
2653 * we have what looks like a proxied connection instead of a connection,
2654 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2655 * Note that this is *not* RFC-compliant, however browsers and proxies
2656 * happen to do that despite being non-standard :-(
2657 * We consider that a request not beginning with either '/' or '*' is
2658 * a proxied connection, which covers both "scheme://location" and
2659 * CONNECT ip:port.
2660 */
2661 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002662 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002663 txn->flags |= TX_USE_PX_CONN;
2664
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002665 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002666 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002669 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002670 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002672
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002673 /* 6: determine the transfer-length.
2674 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2675 * the presence of a message-body in a REQUEST and its transfer length
2676 * must be determined that way (in order of precedence) :
2677 * 1. The presence of a message-body in a request is signaled by the
2678 * inclusion of a Content-Length or Transfer-Encoding header field
2679 * in the request's header fields. When a request message contains
2680 * both a message-body of non-zero length and a method that does
2681 * not define any semantics for that request message-body, then an
2682 * origin server SHOULD either ignore the message-body or respond
2683 * with an appropriate error message (e.g., 413). A proxy or
2684 * gateway, when presented the same request, SHOULD either forward
2685 * the request inbound with the message- body or ignore the
2686 * message-body when determining a response.
2687 *
2688 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2689 * and the "chunked" transfer-coding (Section 6.2) is used, the
2690 * transfer-length is defined by the use of this transfer-coding.
2691 * If a Transfer-Encoding header field is present and the "chunked"
2692 * transfer-coding is not present, the transfer-length is defined
2693 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002694 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002695 * 3. If a Content-Length header field is present, its decimal value in
2696 * OCTETs represents both the entity-length and the transfer-length.
2697 * If a message is received with both a Transfer-Encoding header
2698 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002699 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002700 * 4. By the server closing the connection. (Closing the connection
2701 * cannot be used to indicate the end of a request body, since that
2702 * would leave no possibility for the server to send back a response.)
2703 *
2704 * Whenever a transfer-coding is applied to a message-body, the set of
2705 * transfer-codings MUST include "chunked", unless the message indicates
2706 * it is terminated by closing the connection. When the "chunked"
2707 * transfer-coding is used, it MUST be the last transfer-coding applied
2708 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002709 */
2710
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002711 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002712 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002713 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002714 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002716 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002717 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2718 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002719 /* bad transfer-encoding (chunked followed by something else) */
2720 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002721 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002722 break;
2723 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002724 }
2725
Willy Tarreau32b47f42009-10-18 20:55:02 +02002726 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002727 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002729 signed long long cl;
2730
Willy Tarreauad14f752011-09-02 20:33:27 +02002731 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002732 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002733 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002734 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002735
Willy Tarreauad14f752011-09-02 20:33:27 +02002736 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002737 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002738 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002739 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002740
Willy Tarreauad14f752011-09-02 20:33:27 +02002741 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002742 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002743 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002744 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002745
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002746 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002747 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002748 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002749 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002750
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002751 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002752 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002753 }
2754
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002755 /* bodyless requests have a known length */
2756 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002757 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002758
Willy Tarreaud787e662009-07-07 10:14:51 +02002759 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002760 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002761 req->analyse_exp = TICK_ETERNITY;
2762 return 1;
2763
2764 return_bad_req:
2765 /* We centralize bad requests processing here */
2766 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2767 /* we detected a parsing error. We want to archive this request
2768 * in the dedicated proxy area for later troubleshooting.
2769 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002770 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 }
2772
2773 txn->req.msg_state = HTTP_MSG_ERROR;
2774 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002775 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002776
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002777 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002778 if (s->listener->counters)
2779 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002780
2781 return_prx_cond:
2782 if (!(s->flags & SN_ERR_MASK))
2783 s->flags |= SN_ERR_PRXCOND;
2784 if (!(s->flags & SN_FINST_MASK))
2785 s->flags |= SN_FINST_R;
2786
2787 req->analysers = 0;
2788 req->analyse_exp = TICK_ETERNITY;
2789 return 0;
2790}
2791
Cyril Bonté70be45d2010-10-12 00:14:35 +02002792/* We reached the stats page through a POST request.
2793 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002794 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002795 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002796int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002797{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002798 struct proxy *px = NULL;
2799 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002800
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002801 char key[LINESIZE];
2802 int action = ST_ADM_ACTION_NONE;
2803 int reprocess = 0;
2804
2805 int total_servers = 0;
2806 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002807
2808 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002809 char *st_cur_param = NULL;
2810 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002811
Willy Tarreau9b28e032012-10-12 23:49:43 +02002812 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002813 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002814
2815 cur_param = next_param = end_params;
2816
Willy Tarreau9b28e032012-10-12 23:49:43 +02002817 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002818 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002819 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002820 return 1;
2821 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002822 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002823 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002824 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002825 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002826 }
2827
2828 *end_params = '\0';
2829
Willy Tarreau295a8372011-03-10 11:25:07 +01002830 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002831
2832 /*
2833 * Parse the parameters in reverse order to only store the last value.
2834 * From the html form, the backend and the action are at the end.
2835 */
2836 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002837 char *value;
2838 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002839
2840 cur_param--;
2841 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002842 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002843 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002844 poffset = (cur_param != first_param ? 1 : 0);
2845 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2846 if ((plen > 0) && (plen <= sizeof(key))) {
2847 strncpy(key, cur_param + poffset, plen);
2848 key[plen - 1] = '\0';
2849 } else {
2850 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2851 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002852 }
2853
2854 /* Parse the value */
2855 value = key;
2856 while (*value != '\0' && *value != '=') {
2857 value++;
2858 }
2859 if (*value == '=') {
2860 /* Ok, a value is found, we can mark the end of the key */
2861 *value++ = '\0';
2862 }
2863
Thierry FOURNIER5068d962013-10-04 16:27:27 +02002864 if (url_decode(key) < 0 || url_decode(value) < 0)
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002865 break;
2866
Cyril Bonté70be45d2010-10-12 00:14:35 +02002867 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002868 if (!px && (strcmp(key, "b") == 0)) {
2869 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2870 /* the backend name is unknown or ambiguous (duplicate names) */
2871 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2872 goto out;
2873 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002874 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002875 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002876 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002877 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002878 }
2879 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002880 action = ST_ADM_ACTION_ENABLE;
2881 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002882 else if (strcmp(value, "stop") == 0) {
2883 action = ST_ADM_ACTION_STOP;
2884 }
2885 else if (strcmp(value, "start") == 0) {
2886 action = ST_ADM_ACTION_START;
2887 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002888 else if (strcmp(value, "shutdown") == 0) {
2889 action = ST_ADM_ACTION_SHUTDOWN;
2890 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002891 else {
2892 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2893 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002894 }
2895 }
2896 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002897 if (!(px && action)) {
2898 /*
2899 * Indicates that we'll need to reprocess the parameters
2900 * as soon as backend and action are known
2901 */
2902 if (!reprocess) {
2903 st_cur_param = cur_param;
2904 st_next_param = next_param;
2905 }
2906 reprocess = 1;
2907 }
2908 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002909 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002910 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002911 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002912 /* Not already in maintenance, we can change the server state */
2913 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09002914 set_server_down(&sv->check);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002915 altered_servers++;
2916 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002917 }
2918 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002919 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002920 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002921 /* Already in maintenance, we can change the server state */
Simon Horman4a741432013-02-23 15:35:38 +09002922 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09002923 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002924 altered_servers++;
2925 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002926 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002927 break;
2928 case ST_ADM_ACTION_STOP:
2929 case ST_ADM_ACTION_START:
2930 if (action == ST_ADM_ACTION_START)
2931 sv->uweight = sv->iweight;
2932 else
2933 sv->uweight = 0;
2934
Willy Tarreau004e0452013-11-21 11:22:01 +01002935 server_recalc_eweight(sv);
Willy Tarreau0900bcb2013-12-04 00:05:29 +01002936 set_server_drain_state(sv);
Willy Tarreaud7282242012-06-04 00:22:44 +02002937
Willy Tarreaud7282242012-06-04 00:22:44 +02002938 altered_servers++;
2939 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002940 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002941 case ST_ADM_ACTION_SHUTDOWN:
2942 if (px->state != PR_STSTOPPED) {
2943 struct session *sess, *sess_bck;
2944
2945 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2946 if (sess->srv_conn == sv)
2947 session_shutdown(sess, SN_ERR_KILLED);
2948
2949 altered_servers++;
2950 total_servers++;
2951 }
2952 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002953 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002954 } else {
2955 /* the server name is unknown or ambiguous (duplicate names) */
2956 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002957 }
2958 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002959 if (reprocess && px && action) {
2960 /* Now, we know the backend and the action chosen by the user.
2961 * We can safely restart from the first server parameter
2962 * to reprocess them
2963 */
2964 cur_param = st_cur_param;
2965 next_param = st_next_param;
2966 reprocess = 0;
2967 goto reprocess_servers;
2968 }
2969
Cyril Bonté70be45d2010-10-12 00:14:35 +02002970 next_param = cur_param;
2971 }
2972 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002973
2974 if (total_servers == 0) {
2975 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2976 }
2977 else if (altered_servers == 0) {
2978 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2979 }
2980 else if (altered_servers == total_servers) {
2981 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2982 }
2983 else {
2984 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2985 }
2986 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002987 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002988}
2989
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002990/* This function checks whether we need to enable a POST analyser to parse a
2991 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002992 * if it needs to come back again, otherwise non-zero if it finishes. In the
2993 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002994 */
2995int http_handle_stats(struct session *s, struct channel *req)
2996{
2997 struct stats_admin_rule *stats_admin_rule;
2998 struct stream_interface *si = s->rep->prod;
2999 struct http_txn *txn = &s->txn;
3000 struct http_msg *msg = &txn->req;
3001 struct uri_auth *uri = s->be->uri_auth;
3002
3003 /* now check whether we have some admin rules for this request */
3004 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3005 int ret = 1;
3006
3007 if (stats_admin_rule->cond) {
3008 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3009 ret = acl_pass(ret);
3010 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3011 ret = !ret;
3012 }
3013
3014 if (ret) {
3015 /* no rule, or the rule matches */
3016 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
3017 break;
3018 }
3019 }
3020
3021 /* Was the status page requested with a POST ? */
3022 if (unlikely(txn->meth == HTTP_METH_POST)) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003023 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3024
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003025 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
3026 if (msg->msg_state < HTTP_MSG_100_SENT) {
3027 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3028 * send an HTTP/1.1 100 Continue intermediate response.
3029 */
3030 if (msg->flags & HTTP_MSGF_VER_11) {
3031 struct hdr_ctx ctx;
3032 ctx.idx = 0;
3033 /* Expect is allowed in 1.1, look for it */
3034 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3035 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3036 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3037 }
3038 }
3039 msg->msg_state = HTTP_MSG_100_SENT;
3040 s->logs.tv_request = now; /* update the request timer to reflect full request */
3041 }
3042 if (!http_process_req_stat_post(si, txn, req))
3043 return 0; /* we need more data */
3044 }
3045 else
3046 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003047 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3048 scope_txt[0] = 0;
3049 if (si->applet.ctx.stats.scope_len) {
3050 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3051 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(req->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3052 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3053 }
3054
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003055
3056 /* We don't want to land on the posted stats page because a refresh will
3057 * repost the data. We don't want this to happen on accident so we redirect
3058 * the browse to the stats page with a GET.
3059 */
3060 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04003061 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003062 "Cache-Control: no-cache\r\n"
3063 "Content-Type: text/plain\r\n"
3064 "Connection: close\r\n"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003065 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003066 "\r\n",
3067 uri->uri_prefix,
3068 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3069 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3070 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3071 stat_status_codes[si->applet.ctx.stats.st_code] :
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003072 stat_status_codes[STAT_STATUS_UNKN],
3073 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3074 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3075 scope_txt);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003076
3077 s->txn.status = 303;
3078 s->logs.tv_request = now;
3079 stream_int_retnclose(req->prod, &trash);
3080 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3081
3082 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3083 s->fe->fe_counters.intercepted_req++;
3084
3085 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003086 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003087 if (!(s->flags & SN_FINST_MASK))
3088 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003089 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 return 1;
3091 }
3092
3093 /* OK, let's go on now */
3094
3095 chunk_printf(&trash,
3096 "HTTP/1.0 200 OK\r\n"
3097 "Cache-Control: no-cache\r\n"
3098 "Connection: close\r\n"
3099 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003100 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003101
3102 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3103 chunk_appendf(&trash, "Refresh: %d\r\n",
3104 uri->refresh);
3105
3106 chunk_appendf(&trash, "\r\n");
3107
3108 s->txn.status = 200;
3109 s->logs.tv_request = now;
3110
3111 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3112 s->fe->fe_counters.intercepted_req++;
3113
3114 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
Willy Tarreau570f2212013-06-10 16:42:09 +02003115 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003116 if (!(s->flags & SN_FINST_MASK))
3117 s->flags |= SN_FINST_R;
3118
3119 if (s->txn.meth == HTTP_METH_HEAD) {
3120 /* that's all we return in case of HEAD request, so let's immediately close. */
3121 stream_int_retnclose(req->prod, &trash);
3122 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003123 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003124 return 1;
3125 }
3126
3127 /* OK, push the response and hand over to the stats I/O handler */
3128 bi_putchk(s->rep, &trash);
3129
3130 s->task->nice = -32; /* small boost for HTTP statistics */
3131 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3132 s->target = s->rep->prod->conn->target; // for logging only
3133 s->rep->prod->conn->xprt_ctx = s;
3134 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3135 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003136 return 1;
3137}
3138
Lukas Tribus67db8df2013-06-23 17:37:13 +02003139/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3140 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3141 */
3142static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3143{
3144#ifdef IP_TOS
3145 if (from.ss_family == AF_INET)
3146 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3147#endif
3148#ifdef IPV6_TCLASS
3149 if (from.ss_family == AF_INET6) {
3150 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3151 /* v4-mapped addresses need IP_TOS */
3152 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3153 else
3154 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3155 }
3156#endif
3157}
3158
Willy Tarreau20b0de52012-12-24 15:45:22 +01003159/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003160 * transaction <txn>. Returns the first rule that prevents further processing
3161 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3162 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3163 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003164 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003165static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003166http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003167{
Willy Tarreauff011f22011-01-06 17:51:27 +01003168 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003169 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003170
Willy Tarreauff011f22011-01-06 17:51:27 +01003171 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003172 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003173 continue;
3174
Willy Tarreau96257ec2012-12-27 10:46:37 +01003175 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003176 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003177 int ret;
3178
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003179 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003180 ret = acl_pass(ret);
3181
Willy Tarreauff011f22011-01-06 17:51:27 +01003182 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003183 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003184
3185 if (!ret) /* condition not matched */
3186 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003187 }
3188
Willy Tarreau20b0de52012-12-24 15:45:22 +01003189
Willy Tarreau96257ec2012-12-27 10:46:37 +01003190 switch (rule->action) {
3191 case HTTP_REQ_ACT_ALLOW:
3192 return NULL; /* "allow" rules are OK */
3193
3194 case HTTP_REQ_ACT_DENY:
3195 txn->flags |= TX_CLDENY;
3196 return rule;
3197
Willy Tarreauccbcc372012-12-27 12:37:57 +01003198 case HTTP_REQ_ACT_TARPIT:
3199 txn->flags |= TX_CLTARPIT;
3200 return rule;
3201
Willy Tarreau96257ec2012-12-27 10:46:37 +01003202 case HTTP_REQ_ACT_AUTH:
3203 return rule;
3204
Willy Tarreau81499eb2012-12-27 12:19:02 +01003205 case HTTP_REQ_ACT_REDIR:
3206 return rule;
3207
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003208 case HTTP_REQ_ACT_SET_NICE:
3209 s->task->nice = rule->arg.nice;
3210 break;
3211
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003212 case HTTP_REQ_ACT_SET_TOS:
Lukas Tribus67db8df2013-06-23 17:37:13 +02003213 inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003214 break;
3215
Willy Tarreau51347ed2013-06-11 19:34:13 +02003216 case HTTP_REQ_ACT_SET_MARK:
3217#ifdef SO_MARK
3218 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3219#endif
3220 break;
3221
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003222 case HTTP_REQ_ACT_SET_LOGL:
3223 s->logs.level = rule->arg.loglevel;
3224 break;
3225
Willy Tarreau96257ec2012-12-27 10:46:37 +01003226 case HTTP_REQ_ACT_SET_HDR:
3227 ctx.idx = 0;
3228 /* remove all occurrences of the header */
3229 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3230 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3231 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003232 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003233 /* now fall through to header addition */
3234
3235 case HTTP_REQ_ACT_ADD_HDR:
3236 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3237 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3238 trash.len = rule->arg.hdr_add.name_len;
3239 trash.str[trash.len++] = ':';
3240 trash.str[trash.len++] = ' ';
3241 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3242 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3243 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003244 }
3245 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003246
3247 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003248 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003249}
3250
Willy Tarreau71241ab2012-12-27 11:30:54 +01003251
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003252/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3253 * transaction <txn>. Returns the first rule that prevents further processing
3254 * of the response (deny, ...) or NULL if it executed all rules or stopped
3255 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3256 * rule.
3257 */
3258static struct http_res_rule *
3259http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3260{
3261 struct http_res_rule *rule;
3262 struct hdr_ctx ctx;
3263
3264 list_for_each_entry(rule, rules, list) {
3265 if (rule->action >= HTTP_RES_ACT_MAX)
3266 continue;
3267
3268 /* check optional condition */
3269 if (rule->cond) {
3270 int ret;
3271
3272 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3273 ret = acl_pass(ret);
3274
3275 if (rule->cond->pol == ACL_COND_UNLESS)
3276 ret = !ret;
3277
3278 if (!ret) /* condition not matched */
3279 continue;
3280 }
3281
3282
3283 switch (rule->action) {
3284 case HTTP_RES_ACT_ALLOW:
3285 return NULL; /* "allow" rules are OK */
3286
3287 case HTTP_RES_ACT_DENY:
3288 txn->flags |= TX_SVDENY;
3289 return rule;
3290
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003291 case HTTP_RES_ACT_SET_NICE:
3292 s->task->nice = rule->arg.nice;
3293 break;
3294
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003295 case HTTP_RES_ACT_SET_TOS:
Lukas Tribus67db8df2013-06-23 17:37:13 +02003296 inet_set_tos(s->req->prod->conn->t.sock.fd, s->req->prod->conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003297 break;
3298
Willy Tarreau51347ed2013-06-11 19:34:13 +02003299 case HTTP_RES_ACT_SET_MARK:
3300#ifdef SO_MARK
3301 setsockopt(s->req->prod->conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3302#endif
3303 break;
3304
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003305 case HTTP_RES_ACT_SET_LOGL:
3306 s->logs.level = rule->arg.loglevel;
3307 break;
3308
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003309 case HTTP_RES_ACT_SET_HDR:
3310 ctx.idx = 0;
3311 /* remove all occurrences of the header */
3312 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3313 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3314 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3315 }
3316 /* now fall through to header addition */
3317
3318 case HTTP_RES_ACT_ADD_HDR:
3319 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3320 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3321 trash.len = rule->arg.hdr_add.name_len;
3322 trash.str[trash.len++] = ':';
3323 trash.str[trash.len++] = ' ';
3324 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3325 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3326 break;
3327 }
3328 }
3329
3330 /* we reached the end of the rules, nothing to report */
3331 return NULL;
3332}
3333
3334
Willy Tarreau71241ab2012-12-27 11:30:54 +01003335/* Perform an HTTP redirect based on the information in <rule>. The function
3336 * returns non-zero on success, or zero in case of a, irrecoverable error such
3337 * as too large a request to build a valid response.
3338 */
3339static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3340{
3341 struct http_msg *msg = &txn->req;
3342 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003343 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003344
3345 /* build redirect message */
3346 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003347 case 308:
3348 msg_fmt = HTTP_308;
3349 break;
3350 case 307:
3351 msg_fmt = HTTP_307;
3352 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003353 case 303:
3354 msg_fmt = HTTP_303;
3355 break;
3356 case 301:
3357 msg_fmt = HTTP_301;
3358 break;
3359 case 302:
3360 default:
3361 msg_fmt = HTTP_302;
3362 break;
3363 }
3364
3365 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3366 return 0;
3367
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003368 location = trash.str + trash.len;
3369
Willy Tarreau71241ab2012-12-27 11:30:54 +01003370 switch(rule->type) {
3371 case REDIRECT_TYPE_SCHEME: {
3372 const char *path;
3373 const char *host;
3374 struct hdr_ctx ctx;
3375 int pathlen;
3376 int hostlen;
3377
3378 host = "";
3379 hostlen = 0;
3380 ctx.idx = 0;
3381 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3382 host = ctx.line + ctx.val;
3383 hostlen = ctx.vlen;
3384 }
3385
3386 path = http_get_path(txn);
3387 /* build message using path */
3388 if (path) {
3389 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3390 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3391 int qs = 0;
3392 while (qs < pathlen) {
3393 if (path[qs] == '?') {
3394 pathlen = qs;
3395 break;
3396 }
3397 qs++;
3398 }
3399 }
3400 } else {
3401 path = "/";
3402 pathlen = 1;
3403 }
3404
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003405 if (rule->rdr_str) { /* this is an old "redirect" rule */
3406 /* check if we can add scheme + "://" + host + path */
3407 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3408 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003409
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003410 /* add scheme */
3411 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3412 trash.len += rule->rdr_len;
3413 }
3414 else {
3415 /* add scheme with executing log format */
3416 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003417
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003418 /* check if we can add scheme + "://" + host + path */
3419 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3420 return 0;
3421 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003422 /* add "://" */
3423 memcpy(trash.str + trash.len, "://", 3);
3424 trash.len += 3;
3425
3426 /* add host */
3427 memcpy(trash.str + trash.len, host, hostlen);
3428 trash.len += hostlen;
3429
3430 /* add path */
3431 memcpy(trash.str + trash.len, path, pathlen);
3432 trash.len += pathlen;
3433
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003434 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003435 if (trash.len && trash.str[trash.len - 1] != '/' &&
3436 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3437 if (trash.len > trash.size - 5)
3438 return 0;
3439 trash.str[trash.len] = '/';
3440 trash.len++;
3441 }
3442
3443 break;
3444 }
3445 case REDIRECT_TYPE_PREFIX: {
3446 const char *path;
3447 int pathlen;
3448
3449 path = http_get_path(txn);
3450 /* build message using path */
3451 if (path) {
3452 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3453 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3454 int qs = 0;
3455 while (qs < pathlen) {
3456 if (path[qs] == '?') {
3457 pathlen = qs;
3458 break;
3459 }
3460 qs++;
3461 }
3462 }
3463 } else {
3464 path = "/";
3465 pathlen = 1;
3466 }
3467
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003468 if (rule->rdr_str) { /* this is an old "redirect" rule */
3469 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3470 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003471
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003472 /* add prefix. Note that if prefix == "/", we don't want to
3473 * add anything, otherwise it makes it hard for the user to
3474 * configure a self-redirection.
3475 */
3476 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3477 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3478 trash.len += rule->rdr_len;
3479 }
3480 }
3481 else {
3482 /* add prefix with executing log format */
3483 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3484
3485 /* Check length */
3486 if (trash.len + pathlen > trash.size - 4)
3487 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003488 }
3489
3490 /* add path */
3491 memcpy(trash.str + trash.len, path, pathlen);
3492 trash.len += pathlen;
3493
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003494 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003495 if (trash.len && trash.str[trash.len - 1] != '/' &&
3496 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3497 if (trash.len > trash.size - 5)
3498 return 0;
3499 trash.str[trash.len] = '/';
3500 trash.len++;
3501 }
3502
3503 break;
3504 }
3505 case REDIRECT_TYPE_LOCATION:
3506 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003507 if (rule->rdr_str) { /* this is an old "redirect" rule */
3508 if (trash.len + rule->rdr_len > trash.size - 4)
3509 return 0;
3510
3511 /* add location */
3512 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3513 trash.len += rule->rdr_len;
3514 }
3515 else {
3516 /* add location with executing log format */
3517 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003518
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003519 /* Check left length */
3520 if (trash.len > trash.size - 4)
3521 return 0;
3522 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003523 break;
3524 }
3525
3526 if (rule->cookie_len) {
3527 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3528 trash.len += 14;
3529 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3530 trash.len += rule->cookie_len;
3531 memcpy(trash.str + trash.len, "\r\n", 2);
3532 trash.len += 2;
3533 }
3534
3535 /* add end of headers and the keep-alive/close status.
3536 * We may choose to set keep-alive if the Location begins
3537 * with a slash, because the client will come back to the
3538 * same server.
3539 */
3540 txn->status = rule->code;
3541 /* let's log the request time */
3542 s->logs.tv_request = now;
3543
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003544 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003545 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3546 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3547 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3548 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3549 /* keep-alive possible */
3550 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3551 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3552 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3553 trash.len += 30;
3554 } else {
3555 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3556 trash.len += 24;
3557 }
3558 }
3559 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3560 trash.len += 4;
3561 bo_inject(txn->rsp.chn, trash.str, trash.len);
3562 /* "eat" the request */
3563 bi_fast_delete(txn->req.chn->buf, msg->sov);
3564 msg->sov = 0;
3565 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3566 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3567 txn->req.msg_state = HTTP_MSG_CLOSED;
3568 txn->rsp.msg_state = HTTP_MSG_DONE;
3569 } else {
3570 /* keep-alive not possible */
3571 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3572 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3573 trash.len += 29;
3574 } else {
3575 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3576 trash.len += 23;
3577 }
3578 stream_int_retnclose(txn->req.chn->prod, &trash);
3579 txn->req.chn->analysers = 0;
3580 }
3581
3582 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003583 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003584 if (!(s->flags & SN_FINST_MASK))
3585 s->flags |= SN_FINST_R;
3586
3587 return 1;
3588}
3589
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003590/* This stream analyser runs all HTTP request processing which is common to
3591 * frontends and backends, which means blocking ACLs, filters, connection-close,
3592 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003593 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003594 * either needs more data or wants to immediately abort the request (eg: deny,
3595 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003596 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003597int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003598{
Willy Tarreaud787e662009-07-07 10:14:51 +02003599 struct http_txn *txn = &s->txn;
3600 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003602 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003603 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003604 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003605 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003606
Willy Tarreau655dce92009-11-08 13:10:58 +01003607 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003608 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003609 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003610 return 0;
3611 }
3612
Willy Tarreau3a816292009-07-07 10:55:49 +02003613 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003614 req->analyse_exp = TICK_ETERNITY;
3615
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003616 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003617 now_ms, __FUNCTION__,
3618 s,
3619 req,
3620 req->rex, req->wex,
3621 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003622 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003623 req->analysers);
3624
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003625 /* first check whether we have some ACLs set to block this request */
3626 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003627 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003628
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003629 ret = acl_pass(ret);
3630 if (cond->pol == ACL_COND_UNLESS)
3631 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003632
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003633 if (ret) {
3634 txn->status = 403;
3635 /* let's log the request time */
3636 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003637 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003638 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003640 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003641 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003642
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003643 /* just in case we have some per-backend tracking */
3644 session_inc_be_http_req_ctr(s);
3645
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003646 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003647 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003648
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003649 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003650 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003651 do_stats = stats_check_uri(s->rep->prod, txn, px);
3652 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003653 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003654 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003655 else
3656 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003657
Willy Tarreau3b44e722013-11-16 10:28:23 +01003658 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3659 * blocked by an http-request rule.
3660 */
3661 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003662 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003663 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003664 }
Willy Tarreau06619262006-12-17 08:37:22 +01003665
Willy Tarreau3b44e722013-11-16 10:28:23 +01003666 /* return a 403 if either rule has blocked */
3667 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003668 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003669 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003670 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003671 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003672 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003673 s->fe->fe_counters.denied_req++;
3674 if (s->fe != s->be)
3675 s->be->be_counters.denied_req++;
3676 if (s->listener->counters)
3677 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003678 goto return_prx_cond;
3679 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003680
3681 /* When a connection is tarpitted, we use the tarpit timeout,
3682 * which may be the same as the connect timeout if unspecified.
3683 * If unset, then set it to zero because we really want it to
3684 * eventually expire. We build the tarpit as an analyser.
3685 */
3686 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003687 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003688 /* wipe the request out so that we can drop the connection early
3689 * if the client closes first.
3690 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003691 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003692 req->analysers = 0; /* remove switching rules etc... */
3693 req->analysers |= AN_REQ_HTTP_TARPIT;
3694 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3695 if (!req->analyse_exp)
3696 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003697 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003698 s->fe->fe_counters.denied_req++;
3699 if (s->fe != s->be)
3700 s->be->be_counters.denied_req++;
3701 if (s->listener->counters)
3702 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003703 return 1;
3704 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003705 }
Willy Tarreau06619262006-12-17 08:37:22 +01003706
Willy Tarreau5b154472009-12-21 20:11:07 +01003707 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3708 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003709 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3710 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003711 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3712 * avoid to redo the same work if FE and BE have the same settings (common).
3713 * The method consists in checking if options changed between the two calls
3714 * (implying that either one is non-null, or one of them is non-null and we
3715 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003716 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003717
Willy Tarreaudc008c52010-02-01 16:20:08 +01003718 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3719 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3720 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3721 (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01003722 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003723
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003724 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3725 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003726 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003727 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3728 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003729 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003730 tmp = TX_CON_WANT_CLO;
3731
Willy Tarreau5b154472009-12-21 20:11:07 +01003732 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3733 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003734
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003735 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3736 /* parse the Connection header and possibly clean it */
3737 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003738 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003739 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3740 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003741 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003742 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003743 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003744 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003745 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003746
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003747 /* check if client or config asks for explicit close in KAL/SCL */
3748 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3749 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3750 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003751 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003752 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003753 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003754 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003755 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3756 }
Willy Tarreau78599912009-10-17 20:12:21 +02003757
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003758 /* we can be blocked here because the request needs to be authenticated,
3759 * either to pass or to access stats.
3760 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003761 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003762 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003763
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003764 if (!realm)
3765 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3766
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003767 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003768 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003769 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003770 /* on 401 we still count one error, because normal browsing
3771 * won't significantly increase the counter but brute force
3772 * attempts will.
3773 */
3774 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003775 goto return_prx_cond;
3776 }
3777
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003778 /* add request headers from the rule sets in the same order */
3779 list_for_each_entry(wl, &px->req_add, list) {
3780 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003781 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003782 ret = acl_pass(ret);
3783 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3784 ret = !ret;
3785 if (!ret)
3786 continue;
3787 }
3788
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003789 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003790 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003791 }
3792
3793 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3794 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3795 goto return_bad_req;
3796 req->analyse_exp = TICK_ETERNITY;
3797 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003798 }
3799
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003800 if (unlikely(do_stats)) {
3801 /* process the stats request now */
3802 if (!http_handle_stats(s, req)) {
3803 /* we need more data, let's come back here later */
3804 req->analysers |= an_bit;
3805 channel_dont_connect(req);
Willy Tarreau7fe33002013-04-21 08:04:22 +02003806 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003807 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003808 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003809 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003810
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003811 /* check whether we have some ACLs set to redirect this request */
3812 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003813 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003814 int ret;
3815
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003816 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003817 ret = acl_pass(ret);
3818 if (rule->cond->pol == ACL_COND_UNLESS)
3819 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003820 if (!ret)
3821 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003822 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003823 if (!http_apply_redirect_rule(rule, s, txn))
3824 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003825
Willy Tarreau71241ab2012-12-27 11:30:54 +01003826 req->analyse_exp = TICK_ETERNITY;
3827 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003828 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003829
Willy Tarreau2be39392010-01-03 17:24:51 +01003830 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3831 * If this happens, then the data will not come immediately, so we must
3832 * send all what we have without waiting. Note that due to the small gain
3833 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003834 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003835 * itself once used.
3836 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003837 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003838
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003839 /* that's OK for us now, let's move on to next analysers */
3840 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003841
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003842 return_bad_req:
3843 /* We centralize bad requests processing here */
3844 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3845 /* we detected a parsing error. We want to archive this request
3846 * in the dedicated proxy area for later troubleshooting.
3847 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003848 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003849 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003850
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003851 txn->req.msg_state = HTTP_MSG_ERROR;
3852 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003853 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003854
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003855 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003856 if (s->listener->counters)
3857 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003858
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003859 return_prx_cond:
3860 if (!(s->flags & SN_ERR_MASK))
3861 s->flags |= SN_ERR_PRXCOND;
3862 if (!(s->flags & SN_FINST_MASK))
3863 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003864
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003865 req->analysers = 0;
3866 req->analyse_exp = TICK_ETERNITY;
3867 return 0;
3868}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003869
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003870/* This function performs all the processing enabled for the current request.
3871 * It returns 1 if the processing can continue on next analysers, or zero if it
3872 * needs more data, encounters an error, or wants to immediately abort the
3873 * request. It relies on buffers flags, and updates s->req->analysers.
3874 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003875int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003876{
3877 struct http_txn *txn = &s->txn;
3878 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003879
Willy Tarreau655dce92009-11-08 13:10:58 +01003880 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003881 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003882 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003883 return 0;
3884 }
3885
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003886 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003887 now_ms, __FUNCTION__,
3888 s,
3889 req,
3890 req->rex, req->wex,
3891 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003892 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003893 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003894
William Lallemand82fe75c2012-10-23 10:25:10 +02003895 if (s->fe->comp || s->be->comp)
3896 select_compression_request_header(s, req->buf);
3897
Willy Tarreau59234e92008-11-30 23:51:27 +01003898 /*
3899 * Right now, we know that we have processed the entire headers
3900 * and that unwanted requests have been filtered out. We can do
3901 * whatever we want with the remaining request. Also, now we
3902 * may have separate values for ->fe, ->be.
3903 */
Willy Tarreau06619262006-12-17 08:37:22 +01003904
Willy Tarreau59234e92008-11-30 23:51:27 +01003905 /*
3906 * If HTTP PROXY is set we simply get remote server address
3907 * parsing incoming request.
3908 */
3909 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003910 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003912
Willy Tarreau59234e92008-11-30 23:51:27 +01003913 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003914 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003915 * Note that doing so might move headers in the request, but
3916 * the fields will stay coherent and the URI will not move.
3917 * This should only be performed in the backend.
3918 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003919 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003920 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3921 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003922
Willy Tarreau59234e92008-11-30 23:51:27 +01003923 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003924 * 8: the appsession cookie was looked up very early in 1.2,
3925 * so let's do the same now.
3926 */
3927
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003928 /* It needs to look into the URI unless persistence must be ignored */
3929 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003930 get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003931 }
3932
William Lallemanda73203e2012-03-12 12:48:57 +01003933 /* add unique-id if "header-unique-id" is specified */
3934
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003935 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3936 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3937 goto return_bad_req;
3938 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003939 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003940 }
William Lallemanda73203e2012-03-12 12:48:57 +01003941
3942 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003943 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3944 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003945 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003946 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003947 goto return_bad_req;
3948 }
3949
Cyril Bontéb21570a2009-11-29 20:04:48 +01003950 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003951 * 9: add X-Forwarded-For if either the frontend or the backend
3952 * asks for it.
3953 */
3954 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003955 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003956 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003957 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3958 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003959 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003960 /* The header is set to be added only if none is present
3961 * and we found it, so don't do anything.
3962 */
3963 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003964 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 /* Add an X-Forwarded-For header unless the source IP is
3966 * in the 'except' network range.
3967 */
3968 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003969 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003970 != s->fe->except_net.s_addr) &&
3971 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003972 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003973 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003974 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003975 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003976 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003977
3978 /* Note: we rely on the backend to get the header name to be used for
3979 * x-forwarded-for, because the header is really meant for the backends.
3980 * However, if the backend did not specify any option, we have to rely
3981 * on the frontend's header name.
3982 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003983 if (s->be->fwdfor_hdr_len) {
3984 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003985 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003986 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003987 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003989 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003990 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003992 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003993 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003994 }
3995 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003996 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003997 /* FIXME: for the sake of completeness, we should also support
3998 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003999 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004000 int len;
4001 char pn[INET6_ADDRSTRLEN];
4002 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004003 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004004 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004005
Willy Tarreau59234e92008-11-30 23:51:27 +01004006 /* Note: we rely on the backend to get the header name to be used for
4007 * x-forwarded-for, because the header is really meant for the backends.
4008 * However, if the backend did not specify any option, we have to rely
4009 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004010 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004011 if (s->be->fwdfor_hdr_len) {
4012 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004013 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004014 } else {
4015 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004016 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004017 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004018 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004019
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004020 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004021 goto return_bad_req;
4022 }
4023 }
4024
4025 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004026 * 10: add X-Original-To if either the frontend or the backend
4027 * asks for it.
4028 */
4029 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4030
4031 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004032 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004033 /* Add an X-Original-To header unless the destination IP is
4034 * in the 'except' network range.
4035 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004036 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004037
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004038 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004039 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004040 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004041 != s->fe->except_to.s_addr) &&
4042 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004043 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004044 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004045 int len;
4046 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004047 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004048
4049 /* Note: we rely on the backend to get the header name to be used for
4050 * x-original-to, because the header is really meant for the backends.
4051 * However, if the backend did not specify any option, we have to rely
4052 * on the frontend's header name.
4053 */
4054 if (s->be->orgto_hdr_len) {
4055 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004056 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004057 } else {
4058 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004059 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004060 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004061 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004062
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004063 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004064 goto return_bad_req;
4065 }
4066 }
4067 }
4068
Willy Tarreau50fc7772012-11-11 22:19:57 +01004069 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4070 * If an "Upgrade" token is found, the header is left untouched in order not to have
4071 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4072 * "Upgrade" is present in the Connection header.
4073 */
4074 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4075 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4076 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004077 unsigned int want_flags = 0;
4078
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004079 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004080 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
4081 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
4082 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004083 want_flags |= TX_CON_CLO_SET;
4084 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004085 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
4086 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
4087 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004088 want_flags |= TX_CON_KAL_SET;
4089 }
4090
4091 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004092 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004093 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004094
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004095
Willy Tarreau522d6c02009-12-06 18:49:18 +01004096 /* If we have no server assigned yet and we're balancing on url_param
4097 * with a POST request, we may be interested in checking the body for
4098 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004099 */
4100 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4101 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004102 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004103 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004104 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004105 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004106 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004107
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004108 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004109 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004110#ifdef TCP_QUICKACK
4111 /* We expect some data from the client. Unless we know for sure
4112 * we already have a full request, we have to re-enable quick-ack
4113 * in case we previously disabled it, otherwise we might cause
4114 * the client to delay further data.
4115 */
4116 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004117 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004118 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004119 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004120#endif
4121 }
Willy Tarreau03945942009-12-22 16:50:27 +01004122
Willy Tarreau59234e92008-11-30 23:51:27 +01004123 /*************************************************************
4124 * OK, that's finished for the headers. We have done what we *
4125 * could. Let's switch to the DATA state. *
4126 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004127 req->analyse_exp = TICK_ETERNITY;
4128 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004129
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004130 /* if the server closes the connection, we want to immediately react
4131 * and close the socket to save packets and syscalls.
4132 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004133 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4134 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004135
Willy Tarreau59234e92008-11-30 23:51:27 +01004136 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004137 /* OK let's go on with the BODY now */
4138 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004139
Willy Tarreau59234e92008-11-30 23:51:27 +01004140 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004141 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004142 /* we detected a parsing error. We want to archive this request
4143 * in the dedicated proxy area for later troubleshooting.
4144 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004145 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004146 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004147
Willy Tarreau59234e92008-11-30 23:51:27 +01004148 txn->req.msg_state = HTTP_MSG_ERROR;
4149 txn->status = 400;
4150 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004151 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004152
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004153 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004154 if (s->listener->counters)
4155 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004156
Willy Tarreau59234e92008-11-30 23:51:27 +01004157 if (!(s->flags & SN_ERR_MASK))
4158 s->flags |= SN_ERR_PRXCOND;
4159 if (!(s->flags & SN_FINST_MASK))
4160 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004161 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004162}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004163
Willy Tarreau60b85b02008-11-30 23:28:40 +01004164/* This function is an analyser which processes the HTTP tarpit. It always
4165 * returns zero, at the beginning because it prevents any other processing
4166 * from occurring, and at the end because it terminates the request.
4167 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004168int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004169{
4170 struct http_txn *txn = &s->txn;
4171
4172 /* This connection is being tarpitted. The CLIENT side has
4173 * already set the connect expiration date to the right
4174 * timeout. We just have to check that the client is still
4175 * there and that the timeout has not expired.
4176 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004177 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004178 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004179 !tick_is_expired(req->analyse_exp, now_ms))
4180 return 0;
4181
4182 /* We will set the queue timer to the time spent, just for
4183 * logging purposes. We fake a 500 server error, so that the
4184 * attacker will not suspect his connection has been tarpitted.
4185 * It will not cause trouble to the logs because we can exclude
4186 * the tarpitted connections by filtering on the 'PT' status flags.
4187 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004188 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4189
4190 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004191 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004192 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004193
4194 req->analysers = 0;
4195 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004196
Willy Tarreau60b85b02008-11-30 23:28:40 +01004197 if (!(s->flags & SN_ERR_MASK))
4198 s->flags |= SN_ERR_PRXCOND;
4199 if (!(s->flags & SN_FINST_MASK))
4200 s->flags |= SN_FINST_T;
4201 return 0;
4202}
4203
Willy Tarreaud34af782008-11-30 23:36:37 +01004204/* This function is an analyser which processes the HTTP request body. It looks
4205 * for parameters to be used for the load balancing algorithm (url_param). It
4206 * must only be called after the standard HTTP request processing has occurred,
4207 * because it expects the request to be parsed. It returns zero if it needs to
4208 * read more data, or 1 once it has completed its analysis.
4209 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004210int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004211{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004212 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004213 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004214 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004215
4216 /* We have to parse the HTTP request body to find any required data.
4217 * "balance url_param check_post" should have been the only way to get
4218 * into this. We were brought here after HTTP header analysis, so all
4219 * related structures are ready.
4220 */
4221
Willy Tarreau522d6c02009-12-06 18:49:18 +01004222 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4223 goto missing_data;
4224
4225 if (msg->msg_state < HTTP_MSG_100_SENT) {
4226 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4227 * send an HTTP/1.1 100 Continue intermediate response.
4228 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004229 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004230 struct hdr_ctx ctx;
4231 ctx.idx = 0;
4232 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004233 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004234 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004235 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004236 }
4237 }
4238 msg->msg_state = HTTP_MSG_100_SENT;
4239 }
4240
4241 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004242 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004243 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004244 * is still null. We must save the body in msg->next because it
4245 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004247 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004248
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004249 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004250 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4251 else
4252 msg->msg_state = HTTP_MSG_DATA;
4253 }
4254
4255 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004256 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004257 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004258 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004259 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004260 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004261
Willy Tarreau115acb92009-12-26 13:56:06 +01004262 if (!ret)
4263 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004264 else if (ret < 0) {
4265 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004266 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004267 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004268 }
4269
Willy Tarreaud98cf932009-12-27 22:54:55 +01004270 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004271 * We have the first data byte is in msg->sov. We're waiting for at
4272 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004273 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004274
Willy Tarreau124d9912011-03-01 20:30:48 +01004275 if (msg->body_len < limit)
4276 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004277
Willy Tarreau9b28e032012-10-12 23:49:43 +02004278 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004279 goto http_end;
4280
4281 missing_data:
4282 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004283 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004284 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004285 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004286 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004287
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004288 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004289 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004290 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004291
4292 if (!(s->flags & SN_ERR_MASK))
4293 s->flags |= SN_ERR_CLITO;
4294 if (!(s->flags & SN_FINST_MASK))
4295 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004296 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004297 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004298
4299 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004300 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004301 /* Not enough data. We'll re-use the http-request
4302 * timeout here. Ideally, we should set the timeout
4303 * relative to the accept() date. We just set the
4304 * request timeout once at the beginning of the
4305 * request.
4306 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004307 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004308 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004309 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004310 return 0;
4311 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004312
4313 http_end:
4314 /* The situation will not evolve, so let's give up on the analysis. */
4315 s->logs.tv_request = now; /* update the request timer to reflect full request */
4316 req->analysers &= ~an_bit;
4317 req->analyse_exp = TICK_ETERNITY;
4318 return 1;
4319
4320 return_bad_req: /* let's centralize all bad requests */
4321 txn->req.msg_state = HTTP_MSG_ERROR;
4322 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004323 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004324
Willy Tarreau79ebac62010-06-07 13:47:49 +02004325 if (!(s->flags & SN_ERR_MASK))
4326 s->flags |= SN_ERR_PRXCOND;
4327 if (!(s->flags & SN_FINST_MASK))
4328 s->flags |= SN_FINST_R;
4329
Willy Tarreau522d6c02009-12-06 18:49:18 +01004330 return_err_msg:
4331 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004332 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004333 if (s->listener->counters)
4334 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004335 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004336}
4337
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004338/* send a server's name with an outgoing request over an established connection.
4339 * Note: this function is designed to be called once the request has been scheduled
4340 * for being forwarded. This is the reason why it rewinds the buffer before
4341 * proceeding.
4342 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004343int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004344
4345 struct hdr_ctx ctx;
4346
Mark Lamourinec2247f02012-01-04 13:02:01 -05004347 char *hdr_name = be->server_id_hdr_name;
4348 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004349 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004350 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004351 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004352
William Lallemandd9e90662012-01-30 17:27:17 +01004353 ctx.idx = 0;
4354
Willy Tarreau9b28e032012-10-12 23:49:43 +02004355 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004356 if (old_o) {
4357 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004358 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004359 }
4360
Willy Tarreau9b28e032012-10-12 23:49:43 +02004361 old_i = chn->buf->i;
4362 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004363 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004364 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004365 }
4366
4367 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004368 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004369 memcpy(hdr_val, hdr_name, hdr_name_len);
4370 hdr_val += hdr_name_len;
4371 *hdr_val++ = ':';
4372 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004373 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4374 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004375
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004376 if (old_o) {
4377 /* If this was a forwarded request, we must readjust the amount of
4378 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004379 * variations. Note that if the request was already scheduled for
4380 * forwarding, it had its req->sol pointing to the body, which
4381 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004382 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004383 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004384 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004385 }
4386
Mark Lamourinec2247f02012-01-04 13:02:01 -05004387 return 0;
4388}
4389
Willy Tarreau610ecce2010-01-04 21:15:02 +01004390/* Terminate current transaction and prepare a new one. This is very tricky
4391 * right now but it works.
4392 */
4393void http_end_txn_clean_session(struct session *s)
4394{
4395 /* FIXME: We need a more portable way of releasing a backend's and a
4396 * server's connections. We need a safer way to reinitialize buffer
4397 * flags. We also need a more accurate method for computing per-request
4398 * data.
4399 */
4400 http_silent_debug(__LINE__, s);
4401
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004402 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004403 si_shutr(s->req->cons);
4404 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405
4406 http_silent_debug(__LINE__, s);
4407
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004408 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004410 if (unlikely(s->srv_conn))
4411 sess_change_server(s, NULL);
4412 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413
4414 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4415 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004416 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417
4418 if (s->txn.status) {
4419 int n;
4420
4421 n = s->txn.status / 100;
4422 if (n < 1 || n > 5)
4423 n = 0;
4424
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004425 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004426 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004427 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004428 s->fe->fe_counters.p.http.comp_rsp++;
4429 }
Willy Tarreau24657792010-02-26 10:30:28 +01004430 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004431 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004432 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004433 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004434 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004435 s->be->be_counters.p.http.comp_rsp++;
4436 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004437 }
4438
4439 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004440 s->logs.bytes_in -= s->req->buf->i;
4441 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442
4443 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004444 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004445 !(s->flags & SN_MONITOR) &&
4446 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4447 s->do_log(s);
4448 }
4449
4450 s->logs.accept_date = date; /* user-visible date for logging */
4451 s->logs.tv_accept = now; /* corrected date for internal use */
4452 tv_zero(&s->logs.tv_request);
4453 s->logs.t_queue = -1;
4454 s->logs.t_connect = -1;
4455 s->logs.t_data = -1;
4456 s->logs.t_close = 0;
4457 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4458 s->logs.srv_queue_size = 0; /* we will get this number soon */
4459
Willy Tarreau9b28e032012-10-12 23:49:43 +02004460 s->logs.bytes_in = s->req->total = s->req->buf->i;
4461 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462
4463 if (s->pend_pos)
4464 pendconn_free(s->pend_pos);
4465
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004466 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 if (s->flags & SN_CURR_SESS) {
4468 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004469 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004470 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004471 if (may_dequeue_tasks(objt_server(s->target), s->be))
4472 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004473 }
4474
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004475 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004476
4477 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004478 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4479 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004480 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004482 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004483 s->req->cons->exp = TICK_ETERNITY;
4484 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004485 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4486 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 +02004487 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 +01004488 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004489
4490 if (s->flags & SN_COMP_READY)
4491 s->comp_algo->end(&s->comp_ctx);
4492 s->comp_algo = NULL;
4493 s->flags &= ~SN_COMP_READY;
4494
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495 s->txn.meth = 0;
4496 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004497 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004498 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004499 s->req->cons->flags |= SI_FL_INDEP_STR;
4500
Willy Tarreau96e31212011-05-30 18:10:30 +02004501 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004502 s->req->flags |= CF_NEVER_WAIT;
4503 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004504 }
4505
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 /* if the request buffer is not empty, it means we're
4507 * about to process another request, so send pending
4508 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004509 * Just don't do this if the buffer is close to be full,
4510 * because the request will wait for it to flush a little
4511 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004512 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004513 if (s->req->buf->i) {
4514 if (s->rep->buf->o &&
4515 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4516 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004517 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004518 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004519
4520 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004521 channel_auto_read(s->req);
4522 channel_auto_close(s->req);
4523 channel_auto_read(s->rep);
4524 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004525
Willy Tarreau342b11c2010-11-24 16:22:09 +01004526 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004527 s->rep->analysers = 0;
4528
4529 http_silent_debug(__LINE__, s);
4530}
4531
4532
4533/* This function updates the request state machine according to the response
4534 * state machine and buffer flags. It returns 1 if it changes anything (flag
4535 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4536 * it is only used to find when a request/response couple is complete. Both
4537 * this function and its equivalent should loop until both return zero. It
4538 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4539 */
4540int http_sync_req_state(struct session *s)
4541{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004542 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004543 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004544 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 unsigned int old_state = txn->req.msg_state;
4546
4547 http_silent_debug(__LINE__, s);
4548 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4549 return 0;
4550
4551 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004552 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004553 * We can shut the read side unless we want to abort_on_close,
4554 * or we have a POST request. The issue with POST requests is
4555 * that some browsers still send a CRLF after the request, and
4556 * this CRLF must be read so that it does not remain in the kernel
4557 * buffers, otherwise a close could cause an RST on some systems
4558 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004559 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004560 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004561 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562
Willy Tarreau40f151a2012-12-20 12:10:09 +01004563 /* if the server closes the connection, we want to immediately react
4564 * and close the socket to save packets and syscalls.
4565 */
4566 chn->cons->flags |= SI_FL_NOHALF;
4567
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4569 goto wait_other_side;
4570
4571 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4572 /* The server has not finished to respond, so we
4573 * don't want to move in order not to upset it.
4574 */
4575 goto wait_other_side;
4576 }
4577
4578 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4579 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004580 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004581 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004582 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583 goto wait_other_side;
4584 }
4585
4586 /* When we get here, it means that both the request and the
4587 * response have finished receiving. Depending on the connection
4588 * mode, we'll have to wait for the last bytes to leave in either
4589 * direction, and sometimes for a close to be effective.
4590 */
4591
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004592 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4593 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004594 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4595 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004596 }
4597 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4598 /* Option forceclose is set, or either side wants to close,
4599 * let's enforce it now that we're not expecting any new
4600 * data to come. The caller knows the session is complete
4601 * once both states are CLOSED.
4602 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004603 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4604 channel_shutr_now(chn);
4605 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004606 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004607 }
4608 else {
4609 /* The last possible modes are keep-alive and tunnel. Since tunnel
4610 * mode does not set the body analyser, we can't reach this place
4611 * in tunnel mode, so we're left with keep-alive only.
4612 * This mode is currently not implemented, we switch to tunnel mode.
4613 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004614 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004615 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004616 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617 }
4618
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004619 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004621 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004622
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004623 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004624 txn->req.msg_state = HTTP_MSG_CLOSING;
4625 goto http_msg_closing;
4626 }
4627 else {
4628 txn->req.msg_state = HTTP_MSG_CLOSED;
4629 goto http_msg_closed;
4630 }
4631 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004632 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 }
4634
4635 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4636 http_msg_closing:
4637 /* nothing else to forward, just waiting for the output buffer
4638 * to be empty and for the shutw_now to take effect.
4639 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004640 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 txn->req.msg_state = HTTP_MSG_CLOSED;
4642 goto http_msg_closed;
4643 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004644 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645 txn->req.msg_state = HTTP_MSG_ERROR;
4646 goto wait_other_side;
4647 }
4648 }
4649
4650 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4651 http_msg_closed:
4652 goto wait_other_side;
4653 }
4654
4655 wait_other_side:
4656 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004657 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658}
4659
4660
4661/* This function updates the response state machine according to the request
4662 * state machine and buffer flags. It returns 1 if it changes anything (flag
4663 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4664 * it is only used to find when a request/response couple is complete. Both
4665 * this function and its equivalent should loop until both return zero. It
4666 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4667 */
4668int http_sync_res_state(struct session *s)
4669{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004670 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004672 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004673 unsigned int old_state = txn->rsp.msg_state;
4674
4675 http_silent_debug(__LINE__, s);
4676 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4677 return 0;
4678
4679 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4680 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004681 * still monitor the server connection for a possible close
4682 * while the request is being uploaded, so we don't disable
4683 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004685 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004686
4687 if (txn->req.msg_state == HTTP_MSG_ERROR)
4688 goto wait_other_side;
4689
4690 if (txn->req.msg_state < HTTP_MSG_DONE) {
4691 /* The client seems to still be sending data, probably
4692 * because we got an error response during an upload.
4693 * We have the choice of either breaking the connection
4694 * or letting it pass through. Let's do the later.
4695 */
4696 goto wait_other_side;
4697 }
4698
4699 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4700 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004701 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004702 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004703 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704 goto wait_other_side;
4705 }
4706
4707 /* When we get here, it means that both the request and the
4708 * response have finished receiving. Depending on the connection
4709 * mode, we'll have to wait for the last bytes to leave in either
4710 * direction, and sometimes for a close to be effective.
4711 */
4712
4713 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4714 /* Server-close mode : shut read and wait for the request
4715 * side to close its output buffer. The caller will detect
4716 * when we're in DONE and the other is in CLOSED and will
4717 * catch that for the final cleanup.
4718 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004719 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4720 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004721 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004722 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4723 /* Option forceclose is set, or either side wants to close,
4724 * let's enforce it now that we're not expecting any new
4725 * data to come. The caller knows the session is complete
4726 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004727 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004728 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4729 channel_shutr_now(chn);
4730 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004731 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 }
4733 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004734 /* The last possible modes are keep-alive and tunnel. Since tunnel
4735 * mode does not set the body analyser, we can't reach this place
4736 * in tunnel mode, so we're left with keep-alive only.
4737 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004739 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004740 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004741 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 }
4743
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004744 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004746 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004747 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4748 goto http_msg_closing;
4749 }
4750 else {
4751 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4752 goto http_msg_closed;
4753 }
4754 }
4755 goto wait_other_side;
4756 }
4757
4758 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4759 http_msg_closing:
4760 /* nothing else to forward, just waiting for the output buffer
4761 * to be empty and for the shutw_now to take effect.
4762 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004763 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004764 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4765 goto http_msg_closed;
4766 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004767 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004768 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004769 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004770 if (objt_server(s->target))
4771 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004772 goto wait_other_side;
4773 }
4774 }
4775
4776 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4777 http_msg_closed:
4778 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004779 bi_erase(chn);
4780 channel_auto_close(chn);
4781 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 goto wait_other_side;
4783 }
4784
4785 wait_other_side:
4786 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004787 /* We force the response to leave immediately if we're waiting for the
4788 * other side, since there is no pending shutdown to push it out.
4789 */
4790 if (!channel_is_empty(chn))
4791 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004792 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793}
4794
4795
4796/* Resync the request and response state machines. Return 1 if either state
4797 * changes.
4798 */
4799int http_resync_states(struct session *s)
4800{
4801 struct http_txn *txn = &s->txn;
4802 int old_req_state = txn->req.msg_state;
4803 int old_res_state = txn->rsp.msg_state;
4804
4805 http_silent_debug(__LINE__, s);
4806 http_sync_req_state(s);
4807 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004808 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809 if (!http_sync_res_state(s))
4810 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004811 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812 if (!http_sync_req_state(s))
4813 break;
4814 }
4815 http_silent_debug(__LINE__, s);
4816 /* OK, both state machines agree on a compatible state.
4817 * There are a few cases we're interested in :
4818 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4819 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4820 * directions, so let's simply disable both analysers.
4821 * - HTTP_MSG_CLOSED on the response only means we must abort the
4822 * request.
4823 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4824 * with server-close mode means we've completed one request and we
4825 * must re-initialize the server connection.
4826 */
4827
4828 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4829 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4830 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4831 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4832 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004833 channel_auto_close(s->req);
4834 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004835 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004836 channel_auto_close(s->rep);
4837 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004839 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4840 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004841 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004842 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004843 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004844 channel_auto_close(s->rep);
4845 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004846 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004847 channel_abort(s->req);
4848 channel_auto_close(s->req);
4849 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004850 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004851 }
4852 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4853 txn->rsp.msg_state == HTTP_MSG_DONE &&
4854 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4855 /* server-close: terminate this server connection and
4856 * reinitialize a fresh-new transaction.
4857 */
4858 http_end_txn_clean_session(s);
4859 }
4860
4861 http_silent_debug(__LINE__, s);
4862 return txn->req.msg_state != old_req_state ||
4863 txn->rsp.msg_state != old_res_state;
4864}
4865
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866/* This function is an analyser which forwards request body (including chunk
4867 * sizes if any). It is called as soon as we must forward, even if we forward
4868 * zero byte. The only situation where it must not be called is when we're in
4869 * tunnel mode and we want to forward till the close. It's used both to forward
4870 * remaining data and to resync after end of body. It expects the msg_state to
4871 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4872 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004873 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004874 * bytes of pending data + the headers if not already done (between sol and sov).
4875 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004876 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004877int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004878{
4879 struct http_txn *txn = &s->txn;
4880 struct http_msg *msg = &s->txn.req;
4881
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004882 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4883 return 0;
4884
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004885 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004886 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004887 /* Output closed while we were sending data. We must abort and
4888 * wake the other side up.
4889 */
4890 msg->msg_state = HTTP_MSG_ERROR;
4891 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004892 return 1;
4893 }
4894
Willy Tarreau4fe41902010-06-07 22:27:41 +02004895 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004896 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004897
4898 /* Note that we don't have to send 100-continue back because we don't
4899 * need the data to complete our job, and it's up to the server to
4900 * decide whether to return 100, 417 or anything else in return of
4901 * an "Expect: 100-continue" header.
4902 */
4903
4904 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004905 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004906 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004907 * is still null. We must save the body in msg->next because it
4908 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004909 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004910 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004911
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004912 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004913 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004914 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004915 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004916 }
4917
Willy Tarreaud98cf932009-12-27 22:54:55 +01004918 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004919 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004920
Willy Tarreau610ecce2010-01-04 21:15:02 +01004921 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004922 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004923 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004924 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004925 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004926 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004927 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004928 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004929 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004930
Willy Tarreaucaabe412010-01-03 23:08:28 +01004931 if (msg->msg_state == HTTP_MSG_DATA) {
4932 /* must still forward */
4933 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004934 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004935
4936 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004937 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004938 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004940 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004941 }
4942 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004943 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004944 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004945 * TRAILERS state.
4946 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004947 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004948
Willy Tarreau54d23df2012-10-25 19:04:45 +02004949 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004950 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004951 else if (ret < 0) {
4952 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004953 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004954 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004955 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004956 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004957 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004958 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004959 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004960 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004961 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004962
4963 if (ret == 0)
4964 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004965 else if (ret < 0) {
4966 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004967 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004968 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004969 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004970 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004971 /* we're in MSG_CHUNK_SIZE now */
4972 }
4973 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004974 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004975
4976 if (ret == 0)
4977 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004978 else if (ret < 0) {
4979 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004980 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004981 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004982 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004983 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984 /* we're in HTTP_MSG_DONE now */
4985 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004987 int old_state = msg->msg_state;
4988
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004990 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004991 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4992 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004993 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994 if (http_resync_states(s)) {
4995 /* some state changes occurred, maybe the analyser
4996 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004997 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004998 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004999 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005000 /* request errors are most likely due to
5001 * the server aborting the transfer.
5002 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005003 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005004 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005005 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005006 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005007 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005008 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005010 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005011
5012 /* If "option abortonclose" is set on the backend, we
5013 * want to monitor the client's connection and forward
5014 * any shutdown notification to the server, which will
5015 * decide whether to close or to go on processing the
5016 * request.
5017 */
5018 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005019 channel_auto_read(req);
5020 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005021 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005022 else if (s->txn.meth == HTTP_METH_POST) {
5023 /* POST requests may require to read extra CRLF
5024 * sent by broken browsers and which could cause
5025 * an RST to be sent upon close on some systems
5026 * (eg: Linux).
5027 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005028 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005029 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005030
Willy Tarreau610ecce2010-01-04 21:15:02 +01005031 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005032 }
5033 }
5034
Willy Tarreaud98cf932009-12-27 22:54:55 +01005035 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005036 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005037 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005038 if (!(s->flags & SN_ERR_MASK))
5039 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005040 if (!(s->flags & SN_FINST_MASK)) {
5041 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5042 s->flags |= SN_FINST_H;
5043 else
5044 s->flags |= SN_FINST_D;
5045 }
5046
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005047 s->fe->fe_counters.cli_aborts++;
5048 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005049 if (objt_server(s->target))
5050 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005051
5052 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005053 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005054
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005055 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005056 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005057 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005059 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005060 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005061 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005062 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005063 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005064
Willy Tarreau5c620922011-05-11 19:56:11 +02005065 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005066 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005067 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005068 * modes are already handled by the stream sock layer. We must not do
5069 * this in content-length mode because it could present the MSG_MORE
5070 * flag with the last block of forwarded data, which would cause an
5071 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005072 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005073 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005074 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005075
Willy Tarreau610ecce2010-01-04 21:15:02 +01005076 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005077 return 0;
5078
Willy Tarreaud98cf932009-12-27 22:54:55 +01005079 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005080 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005081 if (s->listener->counters)
5082 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005083 return_bad_req_stats_ok:
5084 txn->req.msg_state = HTTP_MSG_ERROR;
5085 if (txn->status) {
5086 /* Note: we don't send any error if some data were already sent */
5087 stream_int_retnclose(req->prod, NULL);
5088 } else {
5089 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005090 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005091 }
5092 req->analysers = 0;
5093 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005094
5095 if (!(s->flags & SN_ERR_MASK))
5096 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005097 if (!(s->flags & SN_FINST_MASK)) {
5098 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5099 s->flags |= SN_FINST_H;
5100 else
5101 s->flags |= SN_FINST_D;
5102 }
5103 return 0;
5104
5105 aborted_xfer:
5106 txn->req.msg_state = HTTP_MSG_ERROR;
5107 if (txn->status) {
5108 /* Note: we don't send any error if some data were already sent */
5109 stream_int_retnclose(req->prod, NULL);
5110 } else {
5111 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005112 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005113 }
5114 req->analysers = 0;
5115 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5116
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005117 s->fe->fe_counters.srv_aborts++;
5118 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005119 if (objt_server(s->target))
5120 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005121
5122 if (!(s->flags & SN_ERR_MASK))
5123 s->flags |= SN_ERR_SRVCL;
5124 if (!(s->flags & SN_FINST_MASK)) {
5125 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5126 s->flags |= SN_FINST_H;
5127 else
5128 s->flags |= SN_FINST_D;
5129 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005130 return 0;
5131}
5132
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005133/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5134 * processing can continue on next analysers, or zero if it either needs more
5135 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5136 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5137 * when it has nothing left to do, and may remove any analyser when it wants to
5138 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005139 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005140int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005141{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142 struct http_txn *txn = &s->txn;
5143 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005144 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005145 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005147 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005148
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005149 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 +02005150 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005152 rep,
5153 rep->rex, rep->wex,
5154 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005155 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005156 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005157
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005158 /*
5159 * Now parse the partial (or complete) lines.
5160 * We will check the response syntax, and also join multi-line
5161 * headers. An index of all the lines will be elaborated while
5162 * parsing.
5163 *
5164 * For the parsing, we use a 28 states FSM.
5165 *
5166 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005167 * rep->buf->p = beginning of response
5168 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5169 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005170 * msg->eol = end of current header or line (LF or CRLF)
5171 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005172 */
5173
Willy Tarreau83e3af02009-12-28 17:39:57 +01005174 /* There's a protected area at the end of the buffer for rewriting
5175 * purposes. We don't want to start to parse the request if the
5176 * protected area is affected, because we may have to move processed
5177 * data later, which is much more complicated.
5178 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005179 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005180 if (unlikely(!channel_reserved(rep))) {
5181 /* some data has still not left the buffer, wake us once that's done */
5182 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5183 goto abort_response;
5184 channel_dont_close(rep);
5185 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5186 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005187 }
5188
Willy Tarreau379357a2013-06-08 12:55:46 +02005189 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5190 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5191 buffer_slow_realign(rep->buf);
5192
Willy Tarreau9b28e032012-10-12 23:49:43 +02005193 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005194 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005195 }
5196
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005197 /* 1: we might have to print this header in debug mode */
5198 if (unlikely((global.mode & MODE_DEBUG) &&
5199 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005200 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005201 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005202
Willy Tarreau9b28e032012-10-12 23:49:43 +02005203 sol = rep->buf->p;
5204 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005205 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005206
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005207 sol += hdr_idx_first_pos(&txn->hdr_idx);
5208 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005209
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005210 while (cur_idx) {
5211 eol = sol + txn->hdr_idx.v[cur_idx].len;
5212 debug_hdr("srvhdr", s, sol, eol);
5213 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5214 cur_idx = txn->hdr_idx.v[cur_idx].next;
5215 }
5216 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005217
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005218 /*
5219 * Now we quickly check if we have found a full valid response.
5220 * If not so, we check the FD and buffer states before leaving.
5221 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005222 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005223 * responses are checked first.
5224 *
5225 * Depending on whether the client is still there or not, we
5226 * may send an error response back or not. Note that normally
5227 * we should only check for HTTP status there, and check I/O
5228 * errors somewhere else.
5229 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005230
Willy Tarreau655dce92009-11-08 13:10:58 +01005231 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005232 /* Invalid response */
5233 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5234 /* we detected a parsing error. We want to archive this response
5235 * in the dedicated proxy area for later troubleshooting.
5236 */
5237 hdr_response_bad:
5238 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005239 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005240
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005241 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005242 if (objt_server(s->target)) {
5243 objt_server(s->target)->counters.failed_resp++;
5244 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005245 }
Willy Tarreau64648412010-03-05 10:41:54 +01005246 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005247 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005248 rep->analysers = 0;
5249 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005250 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005251 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005252 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253
5254 if (!(s->flags & SN_ERR_MASK))
5255 s->flags |= SN_ERR_PRXCOND;
5256 if (!(s->flags & SN_FINST_MASK))
5257 s->flags |= SN_FINST_H;
5258
5259 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005260 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005261
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005262 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005263 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005264 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005265 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005266 goto hdr_response_bad;
5267 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005269 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005270 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005271 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005272 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005273
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005274 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005275 if (objt_server(s->target)) {
5276 objt_server(s->target)->counters.failed_resp++;
5277 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005278 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005279
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005280 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005281 rep->analysers = 0;
5282 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005283 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005284 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005285 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005286
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005287 if (!(s->flags & SN_ERR_MASK))
5288 s->flags |= SN_ERR_SRVCL;
5289 if (!(s->flags & SN_FINST_MASK))
5290 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005291 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005292 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005293
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005294 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005295 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005296 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005297 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005298
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005299 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005300 if (objt_server(s->target)) {
5301 objt_server(s->target)->counters.failed_resp++;
5302 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005303 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005304
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005305 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005306 rep->analysers = 0;
5307 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005308 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005309 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005310 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005311
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005312 if (!(s->flags & SN_ERR_MASK))
5313 s->flags |= SN_ERR_SRVTO;
5314 if (!(s->flags & SN_FINST_MASK))
5315 s->flags |= SN_FINST_H;
5316 return 0;
5317 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005318
Willy Tarreauf003d372012-11-26 13:35:37 +01005319 /* client abort with an abortonclose */
5320 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5321 s->fe->fe_counters.cli_aborts++;
5322 s->be->be_counters.cli_aborts++;
5323 if (objt_server(s->target))
5324 objt_server(s->target)->counters.cli_aborts++;
5325
5326 rep->analysers = 0;
5327 channel_auto_close(rep);
5328
5329 txn->status = 400;
5330 bi_erase(rep);
5331 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5332
5333 if (!(s->flags & SN_ERR_MASK))
5334 s->flags |= SN_ERR_CLICL;
5335 if (!(s->flags & SN_FINST_MASK))
5336 s->flags |= SN_FINST_H;
5337
5338 /* process_session() will take care of the error */
5339 return 0;
5340 }
5341
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005342 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005343 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005344 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005345 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005346
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005347 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005348 if (objt_server(s->target)) {
5349 objt_server(s->target)->counters.failed_resp++;
5350 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005351 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005352
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005353 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005354 rep->analysers = 0;
5355 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005356 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005357 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005358 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005359
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005360 if (!(s->flags & SN_ERR_MASK))
5361 s->flags |= SN_ERR_SRVCL;
5362 if (!(s->flags & SN_FINST_MASK))
5363 s->flags |= SN_FINST_H;
5364 return 0;
5365 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005366
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005367 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005368 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005369 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005370 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005371
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005372 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005373 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005374 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005375
5376 if (!(s->flags & SN_ERR_MASK))
5377 s->flags |= SN_ERR_CLICL;
5378 if (!(s->flags & SN_FINST_MASK))
5379 s->flags |= SN_FINST_H;
5380
5381 /* process_session() will take care of the error */
5382 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005383 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005384
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005385 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005386 return 0;
5387 }
5388
5389 /* More interesting part now : we know that we have a complete
5390 * response which at least looks like HTTP. We have an indicator
5391 * of each header's length, so we can parse them quickly.
5392 */
5393
5394 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005395 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005396
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005397 /*
5398 * 1: get the status code
5399 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005400 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005401 if (n < 1 || n > 5)
5402 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005403 /* when the client triggers a 4xx from the server, it's most often due
5404 * to a missing object or permission. These events should be tracked
5405 * because if they happen often, it may indicate a brute force or a
5406 * vulnerability scan.
5407 */
5408 if (n == 4)
5409 session_inc_http_err_ctr(s);
5410
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005411 if (objt_server(s->target))
5412 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413
Willy Tarreau5b154472009-12-21 20:11:07 +01005414 /* check if the response is HTTP/1.1 or above */
5415 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005416 ((rep->buf->p[5] > '1') ||
5417 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005418 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005419
5420 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005421 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 +01005422
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005423 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005424 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005425
Willy Tarreau9b28e032012-10-12 23:49:43 +02005426 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005427
Willy Tarreau39650402010-03-15 19:44:39 +01005428 /* Adjust server's health based on status code. Note: status codes 501
5429 * and 505 are triggered on demand by client request, so we must not
5430 * count them as server failures.
5431 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005432 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005433 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005434 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005435 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005436 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005437 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005438
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005439 /*
5440 * 2: check for cacheability.
5441 */
5442
5443 switch (txn->status) {
5444 case 200:
5445 case 203:
5446 case 206:
5447 case 300:
5448 case 301:
5449 case 410:
5450 /* RFC2616 @13.4:
5451 * "A response received with a status code of
5452 * 200, 203, 206, 300, 301 or 410 MAY be stored
5453 * by a cache (...) unless a cache-control
5454 * directive prohibits caching."
5455 *
5456 * RFC2616 @9.5: POST method :
5457 * "Responses to this method are not cacheable,
5458 * unless the response includes appropriate
5459 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005460 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005461 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005462 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5464 break;
5465 default:
5466 break;
5467 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005468
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005469 /*
5470 * 3: we may need to capture headers
5471 */
5472 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005473 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005474 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005475 txn->rsp.cap, s->fe->rsp_cap);
5476
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005477 /* 4: determine the transfer-length.
5478 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5479 * the presence of a message-body in a RESPONSE and its transfer length
5480 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005481 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005482 * All responses to the HEAD request method MUST NOT include a
5483 * message-body, even though the presence of entity-header fields
5484 * might lead one to believe they do. All 1xx (informational), 204
5485 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5486 * message-body. All other responses do include a message-body,
5487 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005488 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005489 * 1. Any response which "MUST NOT" include a message-body (such as the
5490 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5491 * always terminated by the first empty line after the header fields,
5492 * regardless of the entity-header fields present in the message.
5493 *
5494 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5495 * the "chunked" transfer-coding (Section 6.2) is used, the
5496 * transfer-length is defined by the use of this transfer-coding.
5497 * If a Transfer-Encoding header field is present and the "chunked"
5498 * transfer-coding is not present, the transfer-length is defined by
5499 * the sender closing the connection.
5500 *
5501 * 3. If a Content-Length header field is present, its decimal value in
5502 * OCTETs represents both the entity-length and the transfer-length.
5503 * If a message is received with both a Transfer-Encoding header
5504 * field and a Content-Length header field, the latter MUST be ignored.
5505 *
5506 * 4. If the message uses the media type "multipart/byteranges", and
5507 * the transfer-length is not otherwise specified, then this self-
5508 * delimiting media type defines the transfer-length. This media
5509 * type MUST NOT be used unless the sender knows that the recipient
5510 * can parse it; the presence in a request of a Range header with
5511 * multiple byte-range specifiers from a 1.1 client implies that the
5512 * client can parse multipart/byteranges responses.
5513 *
5514 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005515 */
5516
5517 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005518 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005519 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005520 * FIXME: should we parse anyway and return an error on chunked encoding ?
5521 */
5522 if (txn->meth == HTTP_METH_HEAD ||
5523 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005524 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005525 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005526 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005527 goto skip_content_length;
5528 }
5529
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005530 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005531 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005532 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005533 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005534 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005535 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5536 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005537 /* bad transfer-encoding (chunked followed by something else) */
5538 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005539 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005540 break;
5541 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005542 }
5543
5544 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5545 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005546 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005547 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005548 signed long long cl;
5549
Willy Tarreauad14f752011-09-02 20:33:27 +02005550 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005551 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005552 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005553 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005554
Willy Tarreauad14f752011-09-02 20:33:27 +02005555 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005556 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005557 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005558 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005559
Willy Tarreauad14f752011-09-02 20:33:27 +02005560 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005561 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005562 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005563 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005564
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005565 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005566 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005567 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005568 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005569
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005570 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005571 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572 }
5573
William Lallemand82fe75c2012-10-23 10:25:10 +02005574 if (s->fe->comp || s->be->comp)
5575 select_compression_response_header(s, rep->buf);
5576
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005577 /* FIXME: we should also implement the multipart/byterange method.
5578 * For now on, we resort to close mode in this case (unknown length).
5579 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005580skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005581
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005582 /* end of job, return OK */
5583 rep->analysers &= ~an_bit;
5584 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005585 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005586 return 1;
5587}
5588
5589/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005590 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5591 * and updates t->rep->analysers. It might make sense to explode it into several
5592 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005593 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005594int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005595{
5596 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005597 struct http_msg *msg = &txn->rsp;
5598 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005599 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005600 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005601
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005602 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 +02005603 now_ms, __FUNCTION__,
5604 t,
5605 rep,
5606 rep->rex, rep->wex,
5607 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005608 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005609 rep->analysers);
5610
Willy Tarreau655dce92009-11-08 13:10:58 +01005611 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005612 return 0;
5613
5614 rep->analysers &= ~an_bit;
5615 rep->analyse_exp = TICK_ETERNITY;
5616
Willy Tarreau5b154472009-12-21 20:11:07 +01005617 /* Now we have to check if we need to modify the Connection header.
5618 * This is more difficult on the response than it is on the request,
5619 * because we can have two different HTTP versions and we don't know
5620 * how the client will interprete a response. For instance, let's say
5621 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5622 * HTTP/1.1 response without any header. Maybe it will bound itself to
5623 * HTTP/1.0 because it only knows about it, and will consider the lack
5624 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5625 * the lack of header as a keep-alive. Thus we will use two flags
5626 * indicating how a request MAY be understood by the client. In case
5627 * of multiple possibilities, we'll fix the header to be explicit. If
5628 * ambiguous cases such as both close and keepalive are seen, then we
5629 * will fall back to explicit close. Note that we won't take risks with
5630 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005631 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005632 */
5633
Willy Tarreaudc008c52010-02-01 16:20:08 +01005634 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5635 txn->status == 101)) {
5636 /* Either we've established an explicit tunnel, or we're
5637 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005638 * to understand the next protocols. We have to switch to tunnel
5639 * mode, so that we transfer the request and responses then let
5640 * this protocol pass unmodified. When we later implement specific
5641 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005642 * header which contains information about that protocol for
5643 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005644 */
5645 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5646 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005647 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5648 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5649 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005650 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005651
Willy Tarreau60466522010-01-18 19:08:45 +01005652 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005653 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005654 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5655 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005656
Willy Tarreau60466522010-01-18 19:08:45 +01005657 /* now adjust header transformations depending on current state */
5658 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5659 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5660 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005661 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005662 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005663 }
Willy Tarreau60466522010-01-18 19:08:45 +01005664 else { /* SCL / KAL */
5665 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005666 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005667 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005668 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005669
Willy Tarreau60466522010-01-18 19:08:45 +01005670 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005671 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005672
Willy Tarreau60466522010-01-18 19:08:45 +01005673 /* Some keep-alive responses are converted to Server-close if
5674 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005675 */
Willy Tarreau60466522010-01-18 19:08:45 +01005676 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5677 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005678 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005679 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005680 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005681 }
5682
Willy Tarreau7959a552013-09-23 16:44:27 +02005683 /* we want to have the response time before we start processing it */
5684 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5685
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005686 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005687 /*
5688 * 3: we will have to evaluate the filters.
5689 * As opposed to version 1.2, now they will be evaluated in the
5690 * filters order and not in the header order. This means that
5691 * each filter has to be validated among all headers.
5692 *
5693 * Filters are tried with ->be first, then with ->fe if it is
5694 * different from ->be.
5695 */
5696
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005697 cur_proxy = t->be;
5698 while (1) {
5699 struct proxy *rule_set = cur_proxy;
5700
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005701 /* evaluate http-response rules */
5702 if (!http_res_last_rule)
5703 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5704
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005705 /* try headers filters */
5706 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005707 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005708 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005709 if (objt_server(t->target)) {
5710 objt_server(t->target)->counters.failed_resp++;
5711 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005712 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005713 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005714 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005715 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005716 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005717 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005718 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005719 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005720 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005721 if (!(t->flags & SN_ERR_MASK))
5722 t->flags |= SN_ERR_PRXCOND;
5723 if (!(t->flags & SN_FINST_MASK))
5724 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005725 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005726 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005727 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005728
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005729 /* has the response been denied ? */
5730 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005731 if (objt_server(t->target))
5732 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005733
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005734 t->be->be_counters.denied_resp++;
5735 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005736 if (t->listener->counters)
5737 t->listener->counters->denied_resp++;
5738
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005739 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005740 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005741
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005742 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005743 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005744 if (txn->status < 200)
5745 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005746 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005747 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005748 ret = acl_pass(ret);
5749 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5750 ret = !ret;
5751 if (!ret)
5752 continue;
5753 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005754 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005755 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005756 }
5757
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005758 /* check whether we're already working on the frontend */
5759 if (cur_proxy == t->fe)
5760 break;
5761 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005762 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005763
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005764 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005765 * We may be facing a 100-continue response, in which case this
5766 * is not the right response, and we're waiting for the next one.
5767 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005768 * next one.
5769 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005770 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005771 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005772 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005773 msg->msg_state = HTTP_MSG_RPBEFORE;
5774 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005775 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005776 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5777 return 1;
5778 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005779 else if (unlikely(txn->status < 200))
5780 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005781
5782 /* we don't have any 1xx status code now */
5783
5784 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005785 * 4: check for server cookie.
5786 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005787 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5788 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005789 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005790
Willy Tarreaubaaee002006-06-26 02:48:02 +02005791
Willy Tarreaua15645d2007-03-18 16:22:39 +01005792 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005793 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 */
Willy Tarreau67402132012-05-31 20:40:20 +02005795 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005796 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005797
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005798 /*
5799 * 6: add server cookie in the response if needed
5800 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005801 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005802 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005803 (!(t->flags & SN_DIRECT) ||
5804 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5805 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5806 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5807 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005808 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005809 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005810 /* the server is known, it's not the one the client requested, or the
5811 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005812 * insert a set-cookie here, except if we want to insert only on POST
5813 * requests and this one isn't. Note that servers which don't have cookies
5814 * (eg: some backup servers) will return a full cookie removal request.
5815 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005816 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005817 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005818 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5819 t->be->cookie_name);
5820 }
5821 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005822 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005823
5824 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5825 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005826 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5827 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5828 trash.len += 5;
5829
Willy Tarreauef4f3912010-10-07 21:00:29 +02005830 if (t->be->cookie_maxlife) {
5831 /* emit first_date, which is either the original one or
5832 * the current date.
5833 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005834 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005835 s30tob64(txn->cookie_first_date ?
5836 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005837 (date.tv_sec+3) >> 2, trash.str + trash.len);
5838 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005839 }
5840 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005841 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005842 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005843
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005844 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005845 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005846
Willy Tarreau4992dd22012-05-31 21:02:17 +02005847 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005848 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005849
5850 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005851 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005852
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005853 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005854 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005855
Willy Tarreauf1348312010-10-07 15:54:11 +02005856 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005857 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005858 /* the server did not change, only the date was updated */
5859 txn->flags |= TX_SCK_UPDATED;
5860 else
5861 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005862
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005863 /* Here, we will tell an eventual cache on the client side that we don't
5864 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5865 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5866 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5867 */
Willy Tarreau67402132012-05-31 20:40:20 +02005868 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005869
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005870 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5871
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005872 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005873 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005874 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005875 }
5876 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005877
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005878 /*
5879 * 7: check if result will be cacheable with a cookie.
5880 * We'll block the response if security checks have caught
5881 * nasty things such as a cacheable cookie.
5882 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005883 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5884 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005885 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005886
5887 /* we're in presence of a cacheable response containing
5888 * a set-cookie header. We'll block it as requested by
5889 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005890 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005891 if (objt_server(t->target))
5892 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005893
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005894 t->be->be_counters.denied_resp++;
5895 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005896 if (t->listener->counters)
5897 t->listener->counters->denied_resp++;
5898
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005899 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005900 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005901 send_log(t->be, LOG_ALERT,
5902 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005903 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005904 goto return_srv_prx_502;
5905 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906
5907 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005908 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005909 * If an "Upgrade" token is found, the header is left untouched in order
5910 * not to have to deal with some client bugs : some of them fail an upgrade
5911 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005912 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005913 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5914 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5915 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005916 unsigned int want_flags = 0;
5917
5918 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5919 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5920 /* we want a keep-alive response here. Keep-alive header
5921 * required if either side is not 1.1.
5922 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005923 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005924 want_flags |= TX_CON_KAL_SET;
5925 }
5926 else {
5927 /* we want a close response here. Close header required if
5928 * the server is 1.1, regardless of the client.
5929 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005930 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005931 want_flags |= TX_CON_CLO_SET;
5932 }
5933
5934 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005935 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005936 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005937
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005938 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005939 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005940 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005941 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005942
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005943 /*************************************************************
5944 * OK, that's finished for the headers. We have done what we *
5945 * could. Let's switch to the DATA state. *
5946 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005948 /* if the user wants to log as soon as possible, without counting
5949 * bytes from the server, then this is the right moment. We have
5950 * to temporarily assign bytes_out to log what we currently have.
5951 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005952 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005953 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5954 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005955 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005956 t->logs.bytes_out = 0;
5957 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005959 /* Note: we must not try to cheat by jumping directly to DATA,
5960 * otherwise we would not let the client side wake up.
5961 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005962
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005963 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005964 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005965 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005966}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005967
Willy Tarreaud98cf932009-12-27 22:54:55 +01005968/* This function is an analyser which forwards response body (including chunk
5969 * sizes if any). It is called as soon as we must forward, even if we forward
5970 * zero byte. The only situation where it must not be called is when we're in
5971 * tunnel mode and we want to forward till the close. It's used both to forward
5972 * remaining data and to resync after end of body. It expects the msg_state to
5973 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5974 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005975 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005976 * bytes of pending data + the headers if not already done (between sol and sov).
5977 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005978 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005979int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005980{
5981 struct http_txn *txn = &s->txn;
5982 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005983 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005984 static struct buffer *tmpbuf = NULL;
5985 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005986 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005987 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005988
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005989 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5990 return 0;
5991
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005992 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005993 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005994 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005995 /* Output closed while we were sending data. We must abort and
5996 * wake the other side up.
5997 */
5998 msg->msg_state = HTTP_MSG_ERROR;
5999 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006000 return 1;
6001 }
6002
Willy Tarreau4fe41902010-06-07 22:27:41 +02006003 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006004 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006005
William Lallemand82fe75c2012-10-23 10:25:10 +02006006 /* this is the first time we need the compression buffer */
6007 if (s->comp_algo != NULL && tmpbuf == NULL) {
6008 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6009 goto aborted_xfer; /* no memory */
6010 }
6011
Willy Tarreaud98cf932009-12-27 22:54:55 +01006012 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006013 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006014 * rep->buf.p still points to the beginning of the message and msg->sol
6015 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006016 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006017 channel_forward(res, msg->sov);
6018 msg->next = 0;
6019 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006020
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006021 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006022 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006023 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006024 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006025 }
6026
William Lallemand82fe75c2012-10-23 10:25:10 +02006027 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006028 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02006029 if (ret < 0)
6030 goto missing_data; /* not enough spaces in buffers */
6031 compressing = 1;
6032 }
6033
Willy Tarreaud98cf932009-12-27 22:54:55 +01006034 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006035 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006036 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006037 if (s->comp_algo == NULL) {
6038 bytes = msg->sov - msg->sol;
6039 if (msg->chunk_len || bytes) {
6040 msg->sol = msg->sov;
6041 msg->next -= bytes; /* will be forwarded */
6042 msg->chunk_len += bytes;
6043 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6044 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006045 }
6046
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006047 switch (msg->msg_state - HTTP_MSG_DATA) {
6048 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006049 if (compressing) {
6050 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6051 if (ret < 0)
6052 goto aborted_xfer;
6053 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006054
6055 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006056 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01006057
6058 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006059 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006060 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006061 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006062 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006063 if (compressing && consumed_data) {
6064 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6065 compressing = 0;
6066 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006067 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006068 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006069 /* fall through for HTTP_MSG_CHUNK_CRLF */
6070
6071 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6072 /* we want the CRLF after the data */
6073
6074 ret = http_skip_chunk_crlf(msg);
6075 if (ret == 0)
6076 goto missing_data;
6077 else if (ret < 0) {
6078 if (msg->err_pos >= 0)
6079 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6080 goto return_bad_res;
6081 }
6082 /* skipping data in buffer for compression */
6083 if (compressing) {
6084 b_adv(res->buf, msg->next);
6085 msg->next = 0;
6086 msg->sov = 0;
6087 msg->sol = 0;
6088 }
6089 /* we're in MSG_CHUNK_SIZE now, fall through */
6090
6091 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006092 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006093 * set ->sov and ->next to point to the body and switch to DATA or
6094 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006095 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006096
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006097 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006098 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006099 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006100 else if (ret < 0) {
6101 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006102 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006103 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006104 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006105 if (compressing) {
6106 if (likely(msg->chunk_len > 0)) {
6107 /* skipping data if we are in compression mode */
6108 b_adv(res->buf, msg->next);
6109 msg->next = 0;
6110 msg->sov = 0;
6111 msg->sol = 0;
6112 } else {
6113 if (consumed_data) {
6114 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6115 compressing = 0;
6116 }
6117 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006118 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006119 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006120 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006121
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006122 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6123 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006124 if (ret == 0)
6125 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006126 else if (ret < 0) {
6127 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006128 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006129 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006130 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006131 if (s->comp_algo != NULL) {
6132 /* forwarding trailers */
6133 channel_forward(res, msg->next);
6134 msg->next = 0;
6135 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006136 /* we're in HTTP_MSG_DONE now, but we might still have
6137 * some data pending, so let's loop over once.
6138 */
6139 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006140
6141 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006142 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006143
6144 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006145 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006146 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6147 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006148 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006149 if (http_resync_states(s)) {
6150 http_silent_debug(__LINE__, s);
6151 /* some state changes occurred, maybe the analyser
6152 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006153 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006154 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006155 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006156 /* response errors are most likely due to
6157 * the client aborting the transfer.
6158 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006159 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006160 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006161 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006162 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006163 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006164 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006165 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006166 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006167 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006168 }
6169 }
6170
Willy Tarreaud98cf932009-12-27 22:54:55 +01006171 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006172 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006173 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6174 compressing = 0;
6175 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006176
6177 if (res->flags & CF_SHUTW)
6178 goto aborted_xfer;
6179
6180 /* stop waiting for data if the input is closed before the end. If the
6181 * client side was already closed, it means that the client has aborted,
6182 * so we don't want to count this as a server abort. Otherwise it's a
6183 * server abort.
6184 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006185 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006186 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6187 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006188 if (!(s->flags & SN_ERR_MASK))
6189 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006190 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006191 if (objt_server(s->target))
6192 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006193 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006194 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006195
Willy Tarreau40dba092010-03-04 18:14:51 +01006196 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006197 if (!s->req->analysers)
6198 goto return_bad_res;
6199
Willy Tarreauea953162012-05-18 23:41:28 +02006200 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006201 if (s->comp_algo == NULL) {
6202 bytes = msg->sov - msg->sol;
6203 if (msg->chunk_len || bytes) {
6204 msg->sol = msg->sov;
6205 msg->next -= bytes; /* will be forwarded */
6206 msg->chunk_len += bytes;
6207 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6208 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006209 }
6210
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006211 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006212 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006213 * Similarly, with keep-alive on the client side, we don't want to forward a
6214 * close.
6215 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006216 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006217 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6218 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006219 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006220
Willy Tarreau5c620922011-05-11 19:56:11 +02006221 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006222 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006223 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006224 * modes are already handled by the stream sock layer. We must not do
6225 * this in content-length mode because it could present the MSG_MORE
6226 * flag with the last block of forwarded data, which would cause an
6227 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006228 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006229 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006230 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006231
Willy Tarreaud98cf932009-12-27 22:54:55 +01006232 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006233 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006234 return 0;
6235
Willy Tarreau40dba092010-03-04 18:14:51 +01006236 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006237 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006238 if (objt_server(s->target))
6239 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006240
6241 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006242 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006243 /* don't send any error message as we're in the body */
6244 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006245 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006246 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006247 if (objt_server(s->target))
6248 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006249
6250 if (!(s->flags & SN_ERR_MASK))
6251 s->flags |= SN_ERR_PRXCOND;
6252 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006253 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006254 return 0;
6255
6256 aborted_xfer:
6257 txn->rsp.msg_state = HTTP_MSG_ERROR;
6258 /* don't send any error message as we're in the body */
6259 stream_int_retnclose(res->cons, NULL);
6260 res->analysers = 0;
6261 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6262
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006263 s->fe->fe_counters.cli_aborts++;
6264 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006265 if (objt_server(s->target))
6266 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006267
6268 if (!(s->flags & SN_ERR_MASK))
6269 s->flags |= SN_ERR_CLICL;
6270 if (!(s->flags & SN_FINST_MASK))
6271 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006272 return 0;
6273}
6274
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006275/* Iterate the same filter through all request headers.
6276 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006277 * Since it can manage the switch to another backend, it updates the per-proxy
6278 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006280int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006282 char term;
6283 char *cur_ptr, *cur_end, *cur_next;
6284 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006285 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006286 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006287 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006289 last_hdr = 0;
6290
Willy Tarreau9b28e032012-10-12 23:49:43 +02006291 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006292 old_idx = 0;
6293
6294 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006295 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006296 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006297 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006298 (exp->action == ACT_ALLOW ||
6299 exp->action == ACT_DENY ||
6300 exp->action == ACT_TARPIT))
6301 return 0;
6302
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006303 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006304 if (!cur_idx)
6305 break;
6306
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006307 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006308 cur_ptr = cur_next;
6309 cur_end = cur_ptr + cur_hdr->len;
6310 cur_next = cur_end + cur_hdr->cr + 1;
6311
6312 /* Now we have one header between cur_ptr and cur_end,
6313 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006314 */
6315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006316 /* The annoying part is that pattern matching needs
6317 * that we modify the contents to null-terminate all
6318 * strings before testing them.
6319 */
6320
6321 term = *cur_end;
6322 *cur_end = '\0';
6323
6324 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6325 switch (exp->action) {
6326 case ACT_SETBE:
6327 /* It is not possible to jump a second time.
6328 * FIXME: should we return an HTTP/500 here so that
6329 * the admin knows there's a problem ?
6330 */
6331 if (t->be != t->fe)
6332 break;
6333
6334 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006335 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006336 last_hdr = 1;
6337 break;
6338
6339 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006340 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006341 last_hdr = 1;
6342 break;
6343
6344 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006345 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006346 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 break;
6348
6349 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006350 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006351 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352 break;
6353
6354 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006355 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6356 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 /* FIXME: if the user adds a newline in the replacement, the
6358 * index will not be recalculated for now, and the new line
6359 * will not be counted as a new header.
6360 */
6361
6362 cur_end += delta;
6363 cur_next += delta;
6364 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006365 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006366 break;
6367
6368 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006369 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 cur_next += delta;
6371
Willy Tarreaufa355d42009-11-29 18:12:29 +01006372 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006373 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6374 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006375 cur_hdr->len = 0;
6376 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006377 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006378 break;
6379
6380 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382 if (cur_end)
6383 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006385 /* keep the link from this header to next one in case of later
6386 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006387 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006388 old_idx = cur_idx;
6389 }
6390 return 0;
6391}
6392
6393
6394/* Apply the filter to the request line.
6395 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6396 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006397 * Since it can manage the switch to another backend, it updates the per-proxy
6398 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006399 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006400int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006401{
6402 char term;
6403 char *cur_ptr, *cur_end;
6404 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006405 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006406 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006407
Willy Tarreau3d300592007-03-18 18:34:41 +01006408 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006409 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006410 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006411 (exp->action == ACT_ALLOW ||
6412 exp->action == ACT_DENY ||
6413 exp->action == ACT_TARPIT))
6414 return 0;
6415 else if (exp->action == ACT_REMOVE)
6416 return 0;
6417
6418 done = 0;
6419
Willy Tarreau9b28e032012-10-12 23:49:43 +02006420 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006421 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006422
6423 /* Now we have the request line between cur_ptr and cur_end */
6424
6425 /* The annoying part is that pattern matching needs
6426 * that we modify the contents to null-terminate all
6427 * strings before testing them.
6428 */
6429
6430 term = *cur_end;
6431 *cur_end = '\0';
6432
6433 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6434 switch (exp->action) {
6435 case ACT_SETBE:
6436 /* It is not possible to jump a second time.
6437 * FIXME: should we return an HTTP/500 here so that
6438 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006439 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006440 if (t->be != t->fe)
6441 break;
6442
6443 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006444 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006445 done = 1;
6446 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006448 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006449 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006450 done = 1;
6451 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006453 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006454 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006455 done = 1;
6456 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006458 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006459 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006460 done = 1;
6461 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 case ACT_REPLACE:
6464 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006465 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6466 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 /* FIXME: if the user adds a newline in the replacement, the
6468 * index will not be recalculated for now, and the new line
6469 * will not be counted as a new header.
6470 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006471
Willy Tarreaufa355d42009-11-29 18:12:29 +01006472 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006473 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006474 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 HTTP_MSG_RQMETH,
6476 cur_ptr, cur_end + 1,
6477 NULL, NULL);
6478 if (unlikely(!cur_end))
6479 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006481 /* we have a full request and we know that we have either a CR
6482 * or an LF at <ptr>.
6483 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006484 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6485 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 /* there is no point trying this regex on headers */
6487 return 1;
6488 }
6489 }
6490 *cur_end = term; /* restore the string terminator */
6491 return done;
6492}
Willy Tarreau97de6242006-12-27 17:18:38 +01006493
Willy Tarreau58f10d72006-12-04 02:26:12 +01006494
Willy Tarreau58f10d72006-12-04 02:26:12 +01006495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006497 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006498 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499 * unparsable request. Since it can manage the switch to another backend, it
6500 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006501 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006502int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006504 struct http_txn *txn = &s->txn;
6505 struct hdr_exp *exp;
6506
6507 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006508 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006510 /*
6511 * The interleaving of transformations and verdicts
6512 * makes it difficult to decide to continue or stop
6513 * the evaluation.
6514 */
6515
Willy Tarreau6c123b12010-01-28 20:22:06 +01006516 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6517 break;
6518
Willy Tarreau3d300592007-03-18 18:34:41 +01006519 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006521 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006523
6524 /* if this filter had a condition, evaluate it now and skip to
6525 * next filter if the condition does not match.
6526 */
6527 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006528 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006529 ret = acl_pass(ret);
6530 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6531 ret = !ret;
6532
6533 if (!ret)
6534 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006535 }
6536
6537 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006538 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 if (unlikely(ret < 0))
6540 return -1;
6541
6542 if (likely(ret == 0)) {
6543 /* The filter did not match the request, it can be
6544 * iterated through all headers.
6545 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006546 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006548 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006549 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006550}
6551
6552
Willy Tarreaua15645d2007-03-18 16:22:39 +01006553
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006555 * Try to retrieve the server associated to the appsession.
6556 * If the server is found, it's assigned to the session.
6557 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006558void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006559 struct http_txn *txn = &t->txn;
6560 appsess *asession = NULL;
6561 char *sessid_temp = NULL;
6562
Cyril Bontéb21570a2009-11-29 20:04:48 +01006563 if (len > t->be->appsession_len) {
6564 len = t->be->appsession_len;
6565 }
6566
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006567 if (t->be->options2 & PR_O2_AS_REQL) {
6568 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006569 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006570 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006571 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006572 }
6573
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006574 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006575 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6576 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6577 return;
6578 }
6579
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006580 memcpy(txn->sessid, buf, len);
6581 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006582 }
6583
6584 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6585 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6586 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6587 return;
6588 }
6589
Cyril Bontéb21570a2009-11-29 20:04:48 +01006590 memcpy(sessid_temp, buf, len);
6591 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006592
6593 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6594 /* free previously allocated memory */
6595 pool_free2(apools.sessid, sessid_temp);
6596
6597 if (asession != NULL) {
6598 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6599 if (!(t->be->options2 & PR_O2_AS_REQL))
6600 asession->request_count++;
6601
6602 if (asession->serverid != NULL) {
6603 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006604
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006605 while (srv) {
6606 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006607 if ((srv->state & SRV_RUNNING) ||
6608 (t->be->options & PR_O_PERSIST) ||
6609 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006610 /* we found the server and it's usable */
6611 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006612 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006613 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006614 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006615
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006616 break;
6617 } else {
6618 txn->flags &= ~TX_CK_MASK;
6619 txn->flags |= TX_CK_DOWN;
6620 }
6621 }
6622 srv = srv->next;
6623 }
6624 }
6625 }
6626}
6627
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628/* Find the end of a cookie value contained between <s> and <e>. It works the
6629 * same way as with headers above except that the semi-colon also ends a token.
6630 * See RFC2965 for more information. Note that it requires a valid header to
6631 * return a valid result.
6632 */
6633char *find_cookie_value_end(char *s, const char *e)
6634{
6635 int quoted, qdpair;
6636
6637 quoted = qdpair = 0;
6638 for (; s < e; s++) {
6639 if (qdpair) qdpair = 0;
6640 else if (quoted) {
6641 if (*s == '\\') qdpair = 1;
6642 else if (*s == '"') quoted = 0;
6643 }
6644 else if (*s == '"') quoted = 1;
6645 else if (*s == ',' || *s == ';') return s;
6646 }
6647 return s;
6648}
6649
6650/* Delete a value in a header between delimiters <from> and <next> in buffer
6651 * <buf>. The number of characters displaced is returned, and the pointer to
6652 * the first delimiter is updated if required. The function tries as much as
6653 * possible to respect the following principles :
6654 * - replace <from> delimiter by the <next> one unless <from> points to a
6655 * colon, in which case <next> is simply removed
6656 * - set exactly one space character after the new first delimiter, unless
6657 * there are not enough characters in the block being moved to do so.
6658 * - remove unneeded spaces before the previous delimiter and after the new
6659 * one.
6660 *
6661 * It is the caller's responsibility to ensure that :
6662 * - <from> points to a valid delimiter or the colon ;
6663 * - <next> points to a valid delimiter or the final CR/LF ;
6664 * - there are non-space chars before <from> ;
6665 * - there is a CR/LF at or after <next>.
6666 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006667int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006668{
6669 char *prev = *from;
6670
6671 if (*prev == ':') {
6672 /* We're removing the first value, preserve the colon and add a
6673 * space if possible.
6674 */
6675 if (!http_is_crlf[(unsigned char)*next])
6676 next++;
6677 prev++;
6678 if (prev < next)
6679 *prev++ = ' ';
6680
6681 while (http_is_spht[(unsigned char)*next])
6682 next++;
6683 } else {
6684 /* Remove useless spaces before the old delimiter. */
6685 while (http_is_spht[(unsigned char)*(prev-1)])
6686 prev--;
6687 *from = prev;
6688
6689 /* copy the delimiter and if possible a space if we're
6690 * not at the end of the line.
6691 */
6692 if (!http_is_crlf[(unsigned char)*next]) {
6693 *prev++ = *next++;
6694 if (prev + 1 < next)
6695 *prev++ = ' ';
6696 while (http_is_spht[(unsigned char)*next])
6697 next++;
6698 }
6699 }
6700 return buffer_replace2(buf, prev, next, NULL, 0);
6701}
6702
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006703/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006704 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006705 * desirable to call it only when needed. This code is quite complex because
6706 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6707 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006708 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006709void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006711 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006712 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006713 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006714 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6715 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006716
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006717 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006718 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006719 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006720
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006721 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006722 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006723 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006724
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006725 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006726 hdr_beg = hdr_next;
6727 hdr_end = hdr_beg + cur_hdr->len;
6728 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006729
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006730 /* We have one full header between hdr_beg and hdr_end, and the
6731 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006732 * "Cookie:" headers.
6733 */
6734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006735 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006736 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006737 old_idx = cur_idx;
6738 continue;
6739 }
6740
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006741 del_from = NULL; /* nothing to be deleted */
6742 preserve_hdr = 0; /* assume we may kill the whole header */
6743
Willy Tarreau58f10d72006-12-04 02:26:12 +01006744 /* Now look for cookies. Conforming to RFC2109, we have to support
6745 * attributes whose name begin with a '$', and associate them with
6746 * the right cookie, if we want to delete this cookie.
6747 * So there are 3 cases for each cookie read :
6748 * 1) it's a special attribute, beginning with a '$' : ignore it.
6749 * 2) it's a server id cookie that we *MAY* want to delete : save
6750 * some pointers on it (last semi-colon, beginning of cookie...)
6751 * 3) it's an application cookie : we *MAY* have to delete a previous
6752 * "special" cookie.
6753 * At the end of loop, if a "special" cookie remains, we may have to
6754 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 * *MUST* delete it.
6756 *
6757 * Note: RFC2965 is unclear about the processing of spaces around
6758 * the equal sign in the ATTR=VALUE form. A careful inspection of
6759 * the RFC explicitly allows spaces before it, and not within the
6760 * tokens (attrs or values). An inspection of RFC2109 allows that
6761 * too but section 10.1.3 lets one think that spaces may be allowed
6762 * after the equal sign too, resulting in some (rare) buggy
6763 * implementations trying to do that. So let's do what servers do.
6764 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6765 * allowed quoted strings in values, with any possible character
6766 * after a backslash, including control chars and delimitors, which
6767 * causes parsing to become ambiguous. Browsers also allow spaces
6768 * within values even without quotes.
6769 *
6770 * We have to keep multiple pointers in order to support cookie
6771 * removal at the beginning, middle or end of header without
6772 * corrupting the header. All of these headers are valid :
6773 *
6774 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6775 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6776 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6777 * | | | | | | | | |
6778 * | | | | | | | | hdr_end <--+
6779 * | | | | | | | +--> next
6780 * | | | | | | +----> val_end
6781 * | | | | | +-----------> val_beg
6782 * | | | | +--------------> equal
6783 * | | | +----------------> att_end
6784 * | | +---------------------> att_beg
6785 * | +--------------------------> prev
6786 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006787 */
6788
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006789 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6790 /* Iterate through all cookies on this line */
6791
6792 /* find att_beg */
6793 att_beg = prev + 1;
6794 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6795 att_beg++;
6796
6797 /* find att_end : this is the first character after the last non
6798 * space before the equal. It may be equal to hdr_end.
6799 */
6800 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006801
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006802 while (equal < hdr_end) {
6803 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006804 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006805 if (http_is_spht[(unsigned char)*equal++])
6806 continue;
6807 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006808 }
6809
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006810 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6811 * is between <att_beg> and <equal>, both may be identical.
6812 */
6813
6814 /* look for end of cookie if there is an equal sign */
6815 if (equal < hdr_end && *equal == '=') {
6816 /* look for the beginning of the value */
6817 val_beg = equal + 1;
6818 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6819 val_beg++;
6820
6821 /* find the end of the value, respecting quotes */
6822 next = find_cookie_value_end(val_beg, hdr_end);
6823
6824 /* make val_end point to the first white space or delimitor after the value */
6825 val_end = next;
6826 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6827 val_end--;
6828 } else {
6829 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006830 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006831
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006832 /* We have nothing to do with attributes beginning with '$'. However,
6833 * they will automatically be removed if a header before them is removed,
6834 * since they're supposed to be linked together.
6835 */
6836 if (*att_beg == '$')
6837 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006839 /* Ignore cookies with no equal sign */
6840 if (equal == next) {
6841 /* This is not our cookie, so we must preserve it. But if we already
6842 * scheduled another cookie for removal, we cannot remove the
6843 * complete header, but we can remove the previous block itself.
6844 */
6845 preserve_hdr = 1;
6846 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006847 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006848 val_end += delta;
6849 next += delta;
6850 hdr_end += delta;
6851 hdr_next += delta;
6852 cur_hdr->len += delta;
6853 http_msg_move_end(&txn->req, delta);
6854 prev = del_from;
6855 del_from = NULL;
6856 }
6857 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006859
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006860 /* if there are spaces around the equal sign, we need to
6861 * strip them otherwise we'll get trouble for cookie captures,
6862 * or even for rewrites. Since this happens extremely rarely,
6863 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006864 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6866 int stripped_before = 0;
6867 int stripped_after = 0;
6868
6869 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006870 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 equal += stripped_before;
6872 val_beg += stripped_before;
6873 }
6874
6875 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006876 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 val_beg += stripped_after;
6878 stripped_before += stripped_after;
6879 }
6880
6881 val_end += stripped_before;
6882 next += stripped_before;
6883 hdr_end += stripped_before;
6884 hdr_next += stripped_before;
6885 cur_hdr->len += stripped_before;
6886 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006887 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006888 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 /* First, let's see if we want to capture this cookie. We check
6891 * that we don't already have a client side cookie, because we
6892 * can only capture one. Also as an optimisation, we ignore
6893 * cookies shorter than the declared name.
6894 */
6895 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6896 (val_end - att_beg >= t->fe->capture_namelen) &&
6897 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6898 int log_len = val_end - att_beg;
6899
6900 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6901 Alert("HTTP logging : out of memory.\n");
6902 } else {
6903 if (log_len > t->fe->capture_len)
6904 log_len = t->fe->capture_len;
6905 memcpy(txn->cli_cookie, att_beg, log_len);
6906 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006907 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006908 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006909
Willy Tarreaubca99692010-10-06 19:25:55 +02006910 /* Persistence cookies in passive, rewrite or insert mode have the
6911 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006912 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006913 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006915 * For cookies in prefix mode, the form is :
6916 *
6917 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006918 */
6919 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6920 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6921 struct server *srv = t->be->srv;
6922 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6925 * have the server ID between val_beg and delim, and the original cookie between
6926 * delim+1 and val_end. Otherwise, delim==val_end :
6927 *
6928 * Cookie: NAME=SRV; # in all but prefix modes
6929 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6930 * | || || | |+-> next
6931 * | || || | +--> val_end
6932 * | || || +---------> delim
6933 * | || |+------------> val_beg
6934 * | || +-------------> att_end = equal
6935 * | |+-----------------> att_beg
6936 * | +------------------> prev
6937 * +-------------------------> hdr_beg
6938 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006939
Willy Tarreau67402132012-05-31 20:40:20 +02006940 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 for (delim = val_beg; delim < val_end; delim++)
6942 if (*delim == COOKIE_DELIM)
6943 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006944 } else {
6945 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006947 /* Now check if the cookie contains a date field, which would
6948 * appear after a vertical bar ('|') just after the server name
6949 * and before the delimiter.
6950 */
6951 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6952 if (vbar1) {
6953 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006954 * right is the last seen date. It is a base64 encoded
6955 * 30-bit value representing the UNIX date since the
6956 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006957 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006958 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006959 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006960 if (val_end - vbar1 >= 5) {
6961 val = b64tos30(vbar1);
6962 if (val > 0)
6963 txn->cookie_last_date = val << 2;
6964 }
6965 /* look for a second vertical bar */
6966 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6967 if (vbar1 && (val_end - vbar1 > 5)) {
6968 val = b64tos30(vbar1 + 1);
6969 if (val > 0)
6970 txn->cookie_first_date = val << 2;
6971 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006972 }
6973 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006974
Willy Tarreauf64d1412010-10-07 20:06:11 +02006975 /* if the cookie has an expiration date and the proxy wants to check
6976 * it, then we do that now. We first check if the cookie is too old,
6977 * then only if it has expired. We detect strict overflow because the
6978 * time resolution here is not great (4 seconds). Cookies with dates
6979 * in the future are ignored if their offset is beyond one day. This
6980 * allows an admin to fix timezone issues without expiring everyone
6981 * and at the same time avoids keeping unwanted side effects for too
6982 * long.
6983 */
6984 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006985 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6986 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006987 txn->flags &= ~TX_CK_MASK;
6988 txn->flags |= TX_CK_OLD;
6989 delim = val_beg; // let's pretend we have not found the cookie
6990 txn->cookie_first_date = 0;
6991 txn->cookie_last_date = 0;
6992 }
6993 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006994 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6995 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006996 txn->flags &= ~TX_CK_MASK;
6997 txn->flags |= TX_CK_EXPIRED;
6998 delim = val_beg; // let's pretend we have not found the cookie
6999 txn->cookie_first_date = 0;
7000 txn->cookie_last_date = 0;
7001 }
7002
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007003 /* Here, we'll look for the first running server which supports the cookie.
7004 * This allows to share a same cookie between several servers, for example
7005 * to dedicate backup servers to specific servers only.
7006 * However, to prevent clients from sticking to cookie-less backup server
7007 * when they have incidentely learned an empty cookie, we simply ignore
7008 * empty cookies and mark them as invalid.
7009 * The same behaviour is applied when persistence must be ignored.
7010 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007011 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007012 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007013
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007014 while (srv) {
7015 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7016 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7017 if ((srv->state & SRV_RUNNING) ||
7018 (t->be->options & PR_O_PERSIST) ||
7019 (t->flags & SN_FORCE_PRST)) {
7020 /* we found the server and we can use it */
7021 txn->flags &= ~TX_CK_MASK;
7022 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7023 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007024 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007025 break;
7026 } else {
7027 /* we found a server, but it's down,
7028 * mark it as such and go on in case
7029 * another one is available.
7030 */
7031 txn->flags &= ~TX_CK_MASK;
7032 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007033 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007034 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007035 srv = srv->next;
7036 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037
Willy Tarreauf64d1412010-10-07 20:06:11 +02007038 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007039 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007040 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007041 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7042 txn->flags |= TX_CK_UNUSED;
7043 else
7044 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007045 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007046
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007047 /* depending on the cookie mode, we may have to either :
7048 * - delete the complete cookie if we're in insert+indirect mode, so that
7049 * the server never sees it ;
7050 * - remove the server id from the cookie value, and tag the cookie as an
7051 * application cookie so that it does not get accidentely removed later,
7052 * if we're in cookie prefix mode
7053 */
Willy Tarreau67402132012-05-31 20:40:20 +02007054 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007056
Willy Tarreau9b28e032012-10-12 23:49:43 +02007057 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007058 val_end += delta;
7059 next += delta;
7060 hdr_end += delta;
7061 hdr_next += delta;
7062 cur_hdr->len += delta;
7063 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007065 del_from = NULL;
7066 preserve_hdr = 1; /* we want to keep this cookie */
7067 }
7068 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007069 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007070 del_from = prev;
7071 }
7072 } else {
7073 /* This is not our cookie, so we must preserve it. But if we already
7074 * scheduled another cookie for removal, we cannot remove the
7075 * complete header, but we can remove the previous block itself.
7076 */
7077 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007078
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007079 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007080 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007081 if (att_beg >= del_from)
7082 att_beg += delta;
7083 if (att_end >= del_from)
7084 att_end += delta;
7085 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007086 val_end += delta;
7087 next += delta;
7088 hdr_end += delta;
7089 hdr_next += delta;
7090 cur_hdr->len += delta;
7091 http_msg_move_end(&txn->req, delta);
7092 prev = del_from;
7093 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007095 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 /* Look for the appsession cookie unless persistence must be ignored */
7098 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7099 int cmp_len, value_len;
7100 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007101
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007102 if (t->be->options2 & PR_O2_AS_PFX) {
7103 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7104 value_begin = att_beg + t->be->appsession_name_len;
7105 value_len = val_end - att_beg - t->be->appsession_name_len;
7106 } else {
7107 cmp_len = att_end - att_beg;
7108 value_begin = val_beg;
7109 value_len = val_end - val_beg;
7110 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007111
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007112 /* let's see if the cookie is our appcookie */
7113 if (cmp_len == t->be->appsession_name_len &&
7114 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7115 manage_client_side_appsession(t, value_begin, value_len);
7116 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007117 }
7118
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007119 /* continue with next cookie on this header line */
7120 att_beg = next;
7121 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007123 /* There are no more cookies on this line.
7124 * We may still have one (or several) marked for deletion at the
7125 * end of the line. We must do this now in two ways :
7126 * - if some cookies must be preserved, we only delete from the
7127 * mark to the end of line ;
7128 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007129 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007130 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007131 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007132 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007133 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007134 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007135 cur_hdr->len += delta;
7136 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007137 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007138
7139 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007140 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7141 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007142 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007143 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007144 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007145 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007146 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147 }
7148
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007149 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007150 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007151 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152}
7153
7154
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155/* Iterate the same filter through all response headers contained in <rtr>.
7156 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7157 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007158int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159{
7160 char term;
7161 char *cur_ptr, *cur_end, *cur_next;
7162 int cur_idx, old_idx, last_hdr;
7163 struct http_txn *txn = &t->txn;
7164 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007165 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166
7167 last_hdr = 0;
7168
Willy Tarreau9b28e032012-10-12 23:49:43 +02007169 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170 old_idx = 0;
7171
7172 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007173 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007175 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 (exp->action == ACT_ALLOW ||
7177 exp->action == ACT_DENY))
7178 return 0;
7179
7180 cur_idx = txn->hdr_idx.v[old_idx].next;
7181 if (!cur_idx)
7182 break;
7183
7184 cur_hdr = &txn->hdr_idx.v[cur_idx];
7185 cur_ptr = cur_next;
7186 cur_end = cur_ptr + cur_hdr->len;
7187 cur_next = cur_end + cur_hdr->cr + 1;
7188
7189 /* Now we have one header between cur_ptr and cur_end,
7190 * and the next header starts at cur_next.
7191 */
7192
7193 /* The annoying part is that pattern matching needs
7194 * that we modify the contents to null-terminate all
7195 * strings before testing them.
7196 */
7197
7198 term = *cur_end;
7199 *cur_end = '\0';
7200
7201 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7202 switch (exp->action) {
7203 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007204 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 last_hdr = 1;
7206 break;
7207
7208 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007209 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007210 last_hdr = 1;
7211 break;
7212
7213 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007214 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7215 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007216 /* FIXME: if the user adds a newline in the replacement, the
7217 * index will not be recalculated for now, and the new line
7218 * will not be counted as a new header.
7219 */
7220
7221 cur_end += delta;
7222 cur_next += delta;
7223 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007224 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225 break;
7226
7227 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007228 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 cur_next += delta;
7230
Willy Tarreaufa355d42009-11-29 18:12:29 +01007231 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7233 txn->hdr_idx.used--;
7234 cur_hdr->len = 0;
7235 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007236 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 break;
7238
7239 }
7240 }
7241 if (cur_end)
7242 *cur_end = term; /* restore the string terminator */
7243
7244 /* keep the link from this header to next one in case of later
7245 * removal of next header.
7246 */
7247 old_idx = cur_idx;
7248 }
7249 return 0;
7250}
7251
7252
7253/* Apply the filter to the status line in the response buffer <rtr>.
7254 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7255 * or -1 if a replacement resulted in an invalid status line.
7256 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007257int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258{
7259 char term;
7260 char *cur_ptr, *cur_end;
7261 int done;
7262 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007263 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264
7265
Willy Tarreau3d300592007-03-18 18:34:41 +01007266 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 (exp->action == ACT_ALLOW ||
7270 exp->action == ACT_DENY))
7271 return 0;
7272 else if (exp->action == ACT_REMOVE)
7273 return 0;
7274
7275 done = 0;
7276
Willy Tarreau9b28e032012-10-12 23:49:43 +02007277 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007278 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279
7280 /* Now we have the status line between cur_ptr and cur_end */
7281
7282 /* The annoying part is that pattern matching needs
7283 * that we modify the contents to null-terminate all
7284 * strings before testing them.
7285 */
7286
7287 term = *cur_end;
7288 *cur_end = '\0';
7289
7290 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7291 switch (exp->action) {
7292 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007293 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 done = 1;
7295 break;
7296
7297 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007298 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 done = 1;
7300 break;
7301
7302 case ACT_REPLACE:
7303 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007304 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7305 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 /* FIXME: if the user adds a newline in the replacement, the
7307 * index will not be recalculated for now, and the new line
7308 * will not be counted as a new header.
7309 */
7310
Willy Tarreaufa355d42009-11-29 18:12:29 +01007311 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007313 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007314 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315 cur_ptr, cur_end + 1,
7316 NULL, NULL);
7317 if (unlikely(!cur_end))
7318 return -1;
7319
7320 /* we have a full respnse and we know that we have either a CR
7321 * or an LF at <ptr>.
7322 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007323 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007324 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007325 /* there is no point trying this regex on headers */
7326 return 1;
7327 }
7328 }
7329 *cur_end = term; /* restore the string terminator */
7330 return done;
7331}
7332
7333
7334
7335/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007336 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7338 * unparsable response.
7339 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007340int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007342 struct http_txn *txn = &s->txn;
7343 struct hdr_exp *exp;
7344
7345 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007346 int ret;
7347
7348 /*
7349 * The interleaving of transformations and verdicts
7350 * makes it difficult to decide to continue or stop
7351 * the evaluation.
7352 */
7353
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007354 if (txn->flags & TX_SVDENY)
7355 break;
7356
Willy Tarreau3d300592007-03-18 18:34:41 +01007357 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007358 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7359 exp->action == ACT_PASS)) {
7360 exp = exp->next;
7361 continue;
7362 }
7363
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007364 /* if this filter had a condition, evaluate it now and skip to
7365 * next filter if the condition does not match.
7366 */
7367 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007368 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007369 ret = acl_pass(ret);
7370 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7371 ret = !ret;
7372 if (!ret)
7373 continue;
7374 }
7375
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007377 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007378 if (unlikely(ret < 0))
7379 return -1;
7380
7381 if (likely(ret == 0)) {
7382 /* The filter did not match the response, it can be
7383 * iterated through all headers.
7384 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007385 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007386 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387 }
7388 return 0;
7389}
7390
7391
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007393 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007394 * desirable to call it only when needed. This function is also used when we
7395 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007397void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398{
7399 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007400 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007401 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007402 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007403 char *hdr_beg, *hdr_end, *hdr_next;
7404 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 /* Iterate through the headers.
7407 * we start with the start line.
7408 */
7409 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007410 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007411
7412 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7413 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007414 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415
7416 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007417 hdr_beg = hdr_next;
7418 hdr_end = hdr_beg + cur_hdr->len;
7419 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007420
Willy Tarreau24581ba2010-08-31 22:39:35 +02007421 /* We have one full header between hdr_beg and hdr_end, and the
7422 * next header starts at hdr_next. We're only interested in
7423 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007424 */
7425
Willy Tarreau24581ba2010-08-31 22:39:35 +02007426 is_cookie2 = 0;
7427 prev = hdr_beg + 10;
7428 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007429 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007430 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7431 if (!val) {
7432 old_idx = cur_idx;
7433 continue;
7434 }
7435 is_cookie2 = 1;
7436 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007437 }
7438
Willy Tarreau24581ba2010-08-31 22:39:35 +02007439 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7440 * <prev> points to the colon.
7441 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007442 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443
Willy Tarreau24581ba2010-08-31 22:39:35 +02007444 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7445 * check-cache is enabled) and we are not interested in checking
7446 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007447 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007448 if (t->be->cookie_name == NULL &&
7449 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007450 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007451 return;
7452
Willy Tarreau24581ba2010-08-31 22:39:35 +02007453 /* OK so now we know we have to process this response cookie.
7454 * The format of the Set-Cookie header is slightly different
7455 * from the format of the Cookie header in that it does not
7456 * support the comma as a cookie delimiter (thus the header
7457 * cannot be folded) because the Expires attribute described in
7458 * the original Netscape's spec may contain an unquoted date
7459 * with a comma inside. We have to live with this because
7460 * many browsers don't support Max-Age and some browsers don't
7461 * support quoted strings. However the Set-Cookie2 header is
7462 * clean.
7463 *
7464 * We have to keep multiple pointers in order to support cookie
7465 * removal at the beginning, middle or end of header without
7466 * corrupting the header (in case of set-cookie2). A special
7467 * pointer, <scav> points to the beginning of the set-cookie-av
7468 * fields after the first semi-colon. The <next> pointer points
7469 * either to the end of line (set-cookie) or next unquoted comma
7470 * (set-cookie2). All of these headers are valid :
7471 *
7472 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7473 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7474 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7475 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7476 * | | | | | | | | | |
7477 * | | | | | | | | +-> next hdr_end <--+
7478 * | | | | | | | +------------> scav
7479 * | | | | | | +--------------> val_end
7480 * | | | | | +--------------------> val_beg
7481 * | | | | +----------------------> equal
7482 * | | | +------------------------> att_end
7483 * | | +----------------------------> att_beg
7484 * | +------------------------------> prev
7485 * +-----------------------------------------> hdr_beg
7486 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007487
Willy Tarreau24581ba2010-08-31 22:39:35 +02007488 for (; prev < hdr_end; prev = next) {
7489 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490
Willy Tarreau24581ba2010-08-31 22:39:35 +02007491 /* find att_beg */
7492 att_beg = prev + 1;
7493 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7494 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007495
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 /* find att_end : this is the first character after the last non
7497 * space before the equal. It may be equal to hdr_end.
7498 */
7499 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007500
Willy Tarreau24581ba2010-08-31 22:39:35 +02007501 while (equal < hdr_end) {
7502 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7503 break;
7504 if (http_is_spht[(unsigned char)*equal++])
7505 continue;
7506 att_end = equal;
7507 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007508
Willy Tarreau24581ba2010-08-31 22:39:35 +02007509 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7510 * is between <att_beg> and <equal>, both may be identical.
7511 */
7512
7513 /* look for end of cookie if there is an equal sign */
7514 if (equal < hdr_end && *equal == '=') {
7515 /* look for the beginning of the value */
7516 val_beg = equal + 1;
7517 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7518 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519
Willy Tarreau24581ba2010-08-31 22:39:35 +02007520 /* find the end of the value, respecting quotes */
7521 next = find_cookie_value_end(val_beg, hdr_end);
7522
7523 /* make val_end point to the first white space or delimitor after the value */
7524 val_end = next;
7525 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7526 val_end--;
7527 } else {
7528 /* <equal> points to next comma, semi-colon or EOL */
7529 val_beg = val_end = next = equal;
7530 }
7531
7532 if (next < hdr_end) {
7533 /* Set-Cookie2 supports multiple cookies, and <next> points to
7534 * a colon or semi-colon before the end. So skip all attr-value
7535 * pairs and look for the next comma. For Set-Cookie, since
7536 * commas are permitted in values, skip to the end.
7537 */
7538 if (is_cookie2)
7539 next = find_hdr_value_end(next, hdr_end);
7540 else
7541 next = hdr_end;
7542 }
7543
7544 /* Now everything is as on the diagram above */
7545
7546 /* Ignore cookies with no equal sign */
7547 if (equal == val_end)
7548 continue;
7549
7550 /* If there are spaces around the equal sign, we need to
7551 * strip them otherwise we'll get trouble for cookie captures,
7552 * or even for rewrites. Since this happens extremely rarely,
7553 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007554 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007555 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7556 int stripped_before = 0;
7557 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007558
Willy Tarreau24581ba2010-08-31 22:39:35 +02007559 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007560 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007561 equal += stripped_before;
7562 val_beg += stripped_before;
7563 }
7564
7565 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007566 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007567 val_beg += stripped_after;
7568 stripped_before += stripped_after;
7569 }
7570
7571 val_end += stripped_before;
7572 next += stripped_before;
7573 hdr_end += stripped_before;
7574 hdr_next += stripped_before;
7575 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007576 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 }
7578
7579 /* First, let's see if we want to capture this cookie. We check
7580 * that we don't already have a server side cookie, because we
7581 * can only capture one. Also as an optimisation, we ignore
7582 * cookies shorter than the declared name.
7583 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007584 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007585 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007586 (val_end - att_beg >= t->fe->capture_namelen) &&
7587 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7588 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007589 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007590 Alert("HTTP logging : out of memory.\n");
7591 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007592 else {
7593 if (log_len > t->fe->capture_len)
7594 log_len = t->fe->capture_len;
7595 memcpy(txn->srv_cookie, att_beg, log_len);
7596 txn->srv_cookie[log_len] = 0;
7597 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598 }
7599
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007600 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007602 if (!(t->flags & SN_IGNORE_PRST) &&
7603 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7604 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007605 /* assume passive cookie by default */
7606 txn->flags &= ~TX_SCK_MASK;
7607 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608
7609 /* If the cookie is in insert mode on a known server, we'll delete
7610 * this occurrence because we'll insert another one later.
7611 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007612 * a direct access.
7613 */
Willy Tarreau67402132012-05-31 20:40:20 +02007614 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007615 /* The "preserve" flag was set, we don't want to touch the
7616 * server's cookie.
7617 */
7618 }
Willy Tarreau67402132012-05-31 20:40:20 +02007619 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7620 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007621 /* this cookie must be deleted */
7622 if (*prev == ':' && next == hdr_end) {
7623 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007624 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007625 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7626 txn->hdr_idx.used--;
7627 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007628 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007629 hdr_next += delta;
7630 http_msg_move_end(&txn->rsp, delta);
7631 /* note: while both invalid now, <next> and <hdr_end>
7632 * are still equal, so the for() will stop as expected.
7633 */
7634 } else {
7635 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007636 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007637 next = prev;
7638 hdr_end += delta;
7639 hdr_next += delta;
7640 cur_hdr->len += delta;
7641 http_msg_move_end(&txn->rsp, delta);
7642 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007643 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007644 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007645 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007646 }
Willy Tarreau67402132012-05-31 20:40:20 +02007647 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007648 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 * with this server since we know it.
7650 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007651 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007652 next += delta;
7653 hdr_end += delta;
7654 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007656 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657
Willy Tarreauf1348312010-10-07 15:54:11 +02007658 txn->flags &= ~TX_SCK_MASK;
7659 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007660 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007661 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007662 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007663 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007665 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007666 next += delta;
7667 hdr_end += delta;
7668 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007669 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007670 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671
Willy Tarreau827aee92011-03-10 16:55:02 +01007672 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007673 txn->flags &= ~TX_SCK_MASK;
7674 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007675 }
7676 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007677 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7678 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007679 int cmp_len, value_len;
7680 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681
Cyril Bontéb21570a2009-11-29 20:04:48 +01007682 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007683 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7684 value_begin = att_beg + t->be->appsession_name_len;
7685 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007686 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007687 cmp_len = att_end - att_beg;
7688 value_begin = val_beg;
7689 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007690 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007691
Cyril Bonté17530c32010-04-06 21:11:10 +02007692 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007693 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7694 /* free a possibly previously allocated memory */
7695 pool_free2(apools.sessid, txn->sessid);
7696
Cyril Bontéb21570a2009-11-29 20:04:48 +01007697 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007698 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007699 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7700 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7701 return;
7702 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007703 memcpy(txn->sessid, value_begin, value_len);
7704 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007706 }
7707 /* that's done for this cookie, check the next one on the same
7708 * line when next != hdr_end (only if is_cookie2).
7709 */
7710 }
7711 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007713 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007714
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007715 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007716 appsess *asession = NULL;
7717 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007718 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007719 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007720 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007721 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7722 Alert("Not enough Memory process_srv():asession:calloc().\n");
7723 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7724 return;
7725 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007726 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7727
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007728 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7729 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7730 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007731 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007732 return;
7733 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007734 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007735 asession->sessid[t->be->appsession_len] = 0;
7736
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007737 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007738 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007739 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007740 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007741 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007742 return;
7743 }
7744 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007745 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007746
7747 asession->request_count = 0;
7748 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7749 }
7750
7751 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7752 asession->request_count++;
7753 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007754}
7755
7756
Willy Tarreaua15645d2007-03-18 16:22:39 +01007757/*
7758 * Check if response is cacheable or not. Updates t->flags.
7759 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007760void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761{
7762 struct http_txn *txn = &t->txn;
7763 char *p1, *p2;
7764
7765 char *cur_ptr, *cur_end, *cur_next;
7766 int cur_idx;
7767
Willy Tarreau5df51872007-11-25 16:20:08 +01007768 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769 return;
7770
7771 /* Iterate through the headers.
7772 * we start with the start line.
7773 */
7774 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007775 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776
7777 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7778 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007779 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007780
7781 cur_hdr = &txn->hdr_idx.v[cur_idx];
7782 cur_ptr = cur_next;
7783 cur_end = cur_ptr + cur_hdr->len;
7784 cur_next = cur_end + cur_hdr->cr + 1;
7785
7786 /* We have one full header between cur_ptr and cur_end, and the
7787 * next header starts at cur_next. We're only interested in
7788 * "Cookie:" headers.
7789 */
7790
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007791 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7792 if (val) {
7793 if ((cur_end - (cur_ptr + val) >= 8) &&
7794 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7795 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7796 return;
7797 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007798 }
7799
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007800 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7801 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007802 continue;
7803
7804 /* OK, right now we know we have a cache-control header at cur_ptr */
7805
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007806 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007807
7808 if (p1 >= cur_end) /* no more info */
7809 continue;
7810
7811 /* p1 is at the beginning of the value */
7812 p2 = p1;
7813
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007814 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815 p2++;
7816
7817 /* we have a complete value between p1 and p2 */
7818 if (p2 < cur_end && *p2 == '=') {
7819 /* we have something of the form no-cache="set-cookie" */
7820 if ((cur_end - p1 >= 21) &&
7821 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7822 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007823 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 continue;
7825 }
7826
7827 /* OK, so we know that either p2 points to the end of string or to a comma */
7828 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007829 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007830 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7831 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7832 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007833 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007834 return;
7835 }
7836
7837 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007838 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839 continue;
7840 }
7841 }
7842}
7843
7844
Willy Tarreau58f10d72006-12-04 02:26:12 +01007845/*
7846 * Try to retrieve a known appsession in the URI, then the associated server.
7847 * If the server is found, it's assigned to the session.
7848 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007849void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007850{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007851 char *end_params, *first_param, *cur_param, *next_param;
7852 char separator;
7853 int value_len;
7854
7855 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007856
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007857 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007858 (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 +01007859 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007861
Cyril Bontéb21570a2009-11-29 20:04:48 +01007862 first_param = NULL;
7863 switch (mode) {
7864 case PR_O2_AS_M_PP:
7865 first_param = memchr(begin, ';', len);
7866 break;
7867 case PR_O2_AS_M_QS:
7868 first_param = memchr(begin, '?', len);
7869 break;
7870 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007871
Cyril Bontéb21570a2009-11-29 20:04:48 +01007872 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007873 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007874 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875
Cyril Bontéb21570a2009-11-29 20:04:48 +01007876 switch (mode) {
7877 case PR_O2_AS_M_PP:
7878 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7879 end_params = (char *) begin + len;
7880 }
7881 separator = ';';
7882 break;
7883 case PR_O2_AS_M_QS:
7884 end_params = (char *) begin + len;
7885 separator = '&';
7886 break;
7887 default:
7888 /* unknown mode, shouldn't happen */
7889 return;
7890 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007891
Cyril Bontéb21570a2009-11-29 20:04:48 +01007892 cur_param = next_param = end_params;
7893 while (cur_param > first_param) {
7894 cur_param--;
7895 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7896 /* let's see if this is the appsession parameter */
7897 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7898 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7899 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7900 /* Cool... it's the right one */
7901 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7902 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7903 if (value_len > 0) {
7904 manage_client_side_appsession(t, cur_param, value_len);
7905 }
7906 break;
7907 }
7908 next_param = cur_param;
7909 }
7910 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007911#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007912 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007913 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007914#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007915}
7916
Willy Tarreaub2513902006-12-17 14:52:38 +01007917/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007918 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007919 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007920 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007921 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007922 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007923 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007924 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007925 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007926int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007927{
7928 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007929 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007930 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007931 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007932
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007933 if (!uri_auth)
7934 return 0;
7935
Cyril Bonté70be45d2010-10-12 00:14:35 +02007936 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007937 return 0;
7938
Willy Tarreau295a8372011-03-10 11:25:07 +01007939 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007940 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007941 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007942
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007943 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007944 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007945 return 0;
7946
Willy Tarreau3a215be2012-03-09 21:39:51 +01007947 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007948 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007949 return 0;
7950
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007951 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007952 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007953 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007954 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007955 break;
7956 }
7957 h++;
7958 }
7959
7960 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007961 h = uri + uri_auth->uri_len;
7962 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007963 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007964 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007965 break;
7966 }
7967 h++;
7968 }
7969 }
7970
Willy Tarreau3a215be2012-03-09 21:39:51 +01007971 h = uri + uri_auth->uri_len;
7972 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007973 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007974 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007975 break;
7976 }
7977 h++;
7978 }
7979
Willy Tarreau3a215be2012-03-09 21:39:51 +01007980 h = uri + uri_auth->uri_len;
7981 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007982 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007983 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007984 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007985 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007986 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7987 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7988 si->applet.ctx.stats.st_code = i;
7989 break;
7990 }
7991 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007992 break;
7993 }
7994 h++;
7995 }
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02007996
7997 si->applet.ctx.stats.scope_str = 0;
7998 si->applet.ctx.stats.scope_len = 0;
7999 h = uri + uri_auth->uri_len;
8000 while (h <= uri + msg->sl.rq.u_l - 8) {
8001 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
8002 int itx = 0;
8003 const char *h2;
8004 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
8005 const char *err;
8006
8007 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
8008 h2 = h;
8009 si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p;
8010 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
8011 itx++;
8012 h++;
8013 }
8014
8015 if (itx > STAT_SCOPE_TXT_MAXLEN)
8016 itx = STAT_SCOPE_TXT_MAXLEN;
8017 si->applet.ctx.stats.scope_len = itx;
8018
8019 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
8020 memcpy(scope_txt, h2, itx);
8021 scope_txt[itx] = '\0';
8022 err = invalid_char(scope_txt);
8023 if (err) {
8024 /* bad char in search text => clear scope */
8025 si->applet.ctx.stats.scope_str = 0;
8026 si->applet.ctx.stats.scope_len = 0;
8027 }
8028 break;
8029 }
8030 h++;
8031 }
8032
8033
Willy Tarreaub2513902006-12-17 14:52:38 +01008034 return 1;
8035}
8036
Willy Tarreau4076a152009-04-02 15:18:36 +02008037/*
8038 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008039 * By default it tries to report the error position as msg->err_pos. However if
8040 * this one is not set, it will then report msg->next, which is the last known
8041 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008042 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008043 */
8044void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008045 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01008046 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008047{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008048 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008049 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008050
Willy Tarreau9b28e032012-10-12 23:49:43 +02008051 es->len = MIN(chn->buf->i, sizeof(es->buf));
8052 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008053 len1 = MIN(len1, es->len);
8054 len2 = es->len - len1; /* remaining data if buffer wraps */
8055
Willy Tarreau9b28e032012-10-12 23:49:43 +02008056 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008057 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008058 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008059
Willy Tarreau4076a152009-04-02 15:18:36 +02008060 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008061 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008062 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008063 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008064
Willy Tarreau4076a152009-04-02 15:18:36 +02008065 es->when = date; // user-visible date
8066 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008067 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008068 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008069 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01008070 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008071 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008072 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008073 es->s_flags = s->flags;
8074 es->t_flags = s->txn.flags;
8075 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008076 es->b_out = chn->buf->o;
8077 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008078 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008079 es->m_clen = msg->chunk_len;
8080 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008081}
Willy Tarreaub2513902006-12-17 14:52:38 +01008082
Willy Tarreau294c4732011-12-16 21:35:50 +01008083/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8084 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8085 * performed over the whole headers. Otherwise it must contain a valid header
8086 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8087 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8088 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8089 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008090 * -1. The value fetch stops at commas, so this function is suited for use with
8091 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008092 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008093 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008094unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008095 struct hdr_idx *idx, int occ,
8096 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008097{
Willy Tarreau294c4732011-12-16 21:35:50 +01008098 struct hdr_ctx local_ctx;
8099 char *ptr_hist[MAX_HDR_HISTORY];
8100 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008101 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008102 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008103
Willy Tarreau294c4732011-12-16 21:35:50 +01008104 if (!ctx) {
8105 local_ctx.idx = 0;
8106 ctx = &local_ctx;
8107 }
8108
Willy Tarreaubce70882009-09-07 11:51:47 +02008109 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008110 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008111 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008112 occ--;
8113 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008114 *vptr = ctx->line + ctx->val;
8115 *vlen = ctx->vlen;
8116 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008117 }
8118 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008119 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008120 }
8121
8122 /* negative occurrence, we scan all the list then walk back */
8123 if (-occ > MAX_HDR_HISTORY)
8124 return 0;
8125
Willy Tarreau294c4732011-12-16 21:35:50 +01008126 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008127 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008128 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8129 len_hist[hist_ptr] = ctx->vlen;
8130 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008131 hist_ptr = 0;
8132 found++;
8133 }
8134 if (-occ > found)
8135 return 0;
8136 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008137 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8138 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8139 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008140 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008141 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008142 if (hist_ptr >= MAX_HDR_HISTORY)
8143 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008144 *vptr = ptr_hist[hist_ptr];
8145 *vlen = len_hist[hist_ptr];
8146 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008147}
8148
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008149/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8150 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8151 * performed over the whole headers. Otherwise it must contain a valid header
8152 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8153 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8154 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8155 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8156 * -1. This function differs from http_get_hdr() in that it only returns full
8157 * line header values and does not stop at commas.
8158 * The return value is 0 if nothing was found, or non-zero otherwise.
8159 */
8160unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8161 struct hdr_idx *idx, int occ,
8162 struct hdr_ctx *ctx, char **vptr, int *vlen)
8163{
8164 struct hdr_ctx local_ctx;
8165 char *ptr_hist[MAX_HDR_HISTORY];
8166 int len_hist[MAX_HDR_HISTORY];
8167 unsigned int hist_ptr;
8168 int found;
8169
8170 if (!ctx) {
8171 local_ctx.idx = 0;
8172 ctx = &local_ctx;
8173 }
8174
8175 if (occ >= 0) {
8176 /* search from the beginning */
8177 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8178 occ--;
8179 if (occ <= 0) {
8180 *vptr = ctx->line + ctx->val;
8181 *vlen = ctx->vlen;
8182 return 1;
8183 }
8184 }
8185 return 0;
8186 }
8187
8188 /* negative occurrence, we scan all the list then walk back */
8189 if (-occ > MAX_HDR_HISTORY)
8190 return 0;
8191
8192 found = hist_ptr = 0;
8193 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8194 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8195 len_hist[hist_ptr] = ctx->vlen;
8196 if (++hist_ptr >= MAX_HDR_HISTORY)
8197 hist_ptr = 0;
8198 found++;
8199 }
8200 if (-occ > found)
8201 return 0;
8202 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8203 * find occurrence -occ, so we have to check [hist_ptr+occ].
8204 */
8205 hist_ptr += occ;
8206 if (hist_ptr >= MAX_HDR_HISTORY)
8207 hist_ptr -= MAX_HDR_HISTORY;
8208 *vptr = ptr_hist[hist_ptr];
8209 *vlen = len_hist[hist_ptr];
8210 return 1;
8211}
8212
Willy Tarreaubaaee002006-06-26 02:48:02 +02008213/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008214 * Print a debug line with a header. Always stop at the first CR or LF char,
8215 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8216 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008217 */
8218void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8219{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008220 int max;
8221 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01008222 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
8223 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008224
8225 for (max = 0; start + max < end; max++)
8226 if (start[max] == '\r' || start[max] == '\n')
8227 break;
8228
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008229 UBOUND(max, trash.size - trash.len - 3);
8230 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8231 trash.str[trash.len++] = '\n';
8232 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008233}
8234
Willy Tarreau0937bc42009-12-22 15:03:09 +01008235/*
8236 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8237 * the required fields are properly allocated and that we only need to (re)init
8238 * them. This should be used before processing any new request.
8239 */
8240void http_init_txn(struct session *s)
8241{
8242 struct http_txn *txn = &s->txn;
8243 struct proxy *fe = s->fe;
8244
8245 txn->flags = 0;
8246 txn->status = -1;
8247
Willy Tarreauf64d1412010-10-07 20:06:11 +02008248 txn->cookie_first_date = 0;
8249 txn->cookie_last_date = 0;
8250
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008251 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008252 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008253 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008254 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008255 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008256 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008257 txn->req.chunk_len = 0LL;
8258 txn->req.body_len = 0LL;
8259 txn->rsp.chunk_len = 0LL;
8260 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008261 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8262 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008263 txn->req.chn = s->req;
8264 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008265
8266 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008267
8268 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8269 if (fe->options2 & PR_O2_REQBUG_OK)
8270 txn->req.err_pos = -1; /* let buggy requests pass */
8271
Willy Tarreau46023632010-01-07 22:51:47 +01008272 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008273 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8274
Willy Tarreau46023632010-01-07 22:51:47 +01008275 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008276 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8277
8278 if (txn->hdr_idx.v)
8279 hdr_idx_init(&txn->hdr_idx);
8280}
8281
8282/* to be used at the end of a transaction */
8283void http_end_txn(struct session *s)
8284{
8285 struct http_txn *txn = &s->txn;
8286
8287 /* these ones will have been dynamically allocated */
8288 pool_free2(pool2_requri, txn->uri);
8289 pool_free2(pool2_capture, txn->cli_cookie);
8290 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008291 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008292 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008293
William Lallemanda73203e2012-03-12 12:48:57 +01008294 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008295 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008296 txn->uri = NULL;
8297 txn->srv_cookie = NULL;
8298 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008299
8300 if (txn->req.cap) {
8301 struct cap_hdr *h;
8302 for (h = s->fe->req_cap; h; h = h->next)
8303 pool_free2(h->pool, txn->req.cap[h->index]);
8304 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8305 }
8306
8307 if (txn->rsp.cap) {
8308 struct cap_hdr *h;
8309 for (h = s->fe->rsp_cap; h; h = h->next)
8310 pool_free2(h->pool, txn->rsp.cap[h->index]);
8311 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8312 }
8313
Willy Tarreau0937bc42009-12-22 15:03:09 +01008314}
8315
8316/* to be used at the end of a transaction to prepare a new one */
8317void http_reset_txn(struct session *s)
8318{
8319 http_end_txn(s);
8320 http_init_txn(s);
8321
8322 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008323 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008324 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008325 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008326 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008327 /* re-init store persistence */
8328 s->store_count = 0;
8329
Willy Tarreau0937bc42009-12-22 15:03:09 +01008330 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008331
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008332 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008333
Willy Tarreau739cfba2010-01-25 23:11:14 +01008334 /* We must trim any excess data from the response buffer, because we
8335 * may have blocked an invalid response from a server that we don't
8336 * want to accidentely forward once we disable the analysers, nor do
8337 * we want those data to come along with next response. A typical
8338 * example of such data would be from a buggy server responding to
8339 * a HEAD with some data, or sending more than the advertised
8340 * content-length.
8341 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008342 if (unlikely(s->rep->buf->i))
8343 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008344
Willy Tarreau0937bc42009-12-22 15:03:09 +01008345 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008346 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008347
Willy Tarreaud04e8582010-05-31 12:31:35 +02008348 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008349 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008350
8351 s->req->rex = TICK_ETERNITY;
8352 s->req->wex = TICK_ETERNITY;
8353 s->req->analyse_exp = TICK_ETERNITY;
8354 s->rep->rex = TICK_ETERNITY;
8355 s->rep->wex = TICK_ETERNITY;
8356 s->rep->analyse_exp = TICK_ETERNITY;
8357}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008358
Willy Tarreauff011f22011-01-06 17:51:27 +01008359void free_http_req_rules(struct list *r) {
8360 struct http_req_rule *tr, *pr;
8361
8362 list_for_each_entry_safe(pr, tr, r, list) {
8363 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008364 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008365 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008366
8367 free(pr);
8368 }
8369}
8370
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008371/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008372struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8373{
8374 struct http_req_rule *rule;
8375 int cur_arg;
8376
8377 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8378 if (!rule) {
8379 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008380 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008381 }
8382
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008383 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008384 rule->action = HTTP_REQ_ACT_ALLOW;
8385 cur_arg = 1;
8386 } else if (!strcmp(args[0], "deny")) {
8387 rule->action = HTTP_REQ_ACT_DENY;
8388 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008389 } else if (!strcmp(args[0], "tarpit")) {
8390 rule->action = HTTP_REQ_ACT_TARPIT;
8391 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008392 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008393 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008394 cur_arg = 1;
8395
8396 while(*args[cur_arg]) {
8397 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008398 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008399 cur_arg+=2;
8400 continue;
8401 } else
8402 break;
8403 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008404 } else if (!strcmp(args[0], "set-nice")) {
8405 rule->action = HTTP_REQ_ACT_SET_NICE;
8406 cur_arg = 1;
8407
8408 if (!*args[cur_arg] ||
8409 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8410 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8411 file, linenum, args[0]);
8412 goto out_err;
8413 }
8414 rule->arg.nice = atoi(args[cur_arg]);
8415 if (rule->arg.nice < -1024)
8416 rule->arg.nice = -1024;
8417 else if (rule->arg.nice > 1024)
8418 rule->arg.nice = 1024;
8419 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008420 } else if (!strcmp(args[0], "set-tos")) {
8421#ifdef IP_TOS
8422 char *err;
8423 rule->action = HTTP_REQ_ACT_SET_TOS;
8424 cur_arg = 1;
8425
8426 if (!*args[cur_arg] ||
8427 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8428 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8429 file, linenum, args[0]);
8430 goto out_err;
8431 }
8432
8433 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8434 if (err && *err != '\0') {
8435 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8436 file, linenum, err, args[0]);
8437 goto out_err;
8438 }
8439 cur_arg++;
8440#else
8441 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8442 goto out_err;
8443#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008444 } else if (!strcmp(args[0], "set-mark")) {
8445#ifdef SO_MARK
8446 char *err;
8447 rule->action = HTTP_REQ_ACT_SET_MARK;
8448 cur_arg = 1;
8449
8450 if (!*args[cur_arg] ||
8451 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8452 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8453 file, linenum, args[0]);
8454 goto out_err;
8455 }
8456
8457 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8458 if (err && *err != '\0') {
8459 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8460 file, linenum, err, args[0]);
8461 goto out_err;
8462 }
8463 cur_arg++;
8464 global.last_checks |= LSTCHK_NETADM;
8465#else
8466 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8467 goto out_err;
8468#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008469 } else if (!strcmp(args[0], "set-log-level")) {
8470 rule->action = HTTP_REQ_ACT_SET_LOGL;
8471 cur_arg = 1;
8472
8473 if (!*args[cur_arg] ||
8474 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8475 bad_log_level:
8476 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8477 file, linenum, args[0]);
8478 goto out_err;
8479 }
8480 if (strcmp(args[cur_arg], "silent") == 0)
8481 rule->arg.loglevel = -1;
8482 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8483 goto bad_log_level;
8484 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008485 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8486 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8487 cur_arg = 1;
8488
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008489 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8490 (*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 +01008491 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8492 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008493 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008494 }
8495
8496 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8497 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8498 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008499
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008500 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008501 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8502 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008503 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008504 } else if (strcmp(args[0], "redirect") == 0) {
8505 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008506 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008507
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008508 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008509 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8510 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8511 goto out_err;
8512 }
8513
8514 /* this redirect rule might already contain a parsed condition which
8515 * we'll pass to the http-request rule.
8516 */
8517 rule->action = HTTP_REQ_ACT_REDIR;
8518 rule->arg.redir = redir;
8519 rule->cond = redir->cond;
8520 redir->cond = NULL;
8521 cur_arg = 2;
8522 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008523 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008524 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 +01008525 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008526 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008527 }
8528
8529 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8530 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008531 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008532
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008533 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8534 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8535 file, linenum, args[0], errmsg);
8536 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008537 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008538 }
8539 rule->cond = cond;
8540 }
8541 else if (*args[cur_arg]) {
8542 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8543 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8544 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008545 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008546 }
8547
8548 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008549 out_err:
8550 free(rule);
8551 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008552}
8553
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008554/* parse an "http-respose" rule */
8555struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8556{
8557 struct http_res_rule *rule;
8558 int cur_arg;
8559
8560 rule = calloc(1, sizeof(*rule));
8561 if (!rule) {
8562 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8563 goto out_err;
8564 }
8565
8566 if (!strcmp(args[0], "allow")) {
8567 rule->action = HTTP_RES_ACT_ALLOW;
8568 cur_arg = 1;
8569 } else if (!strcmp(args[0], "deny")) {
8570 rule->action = HTTP_RES_ACT_DENY;
8571 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008572 } else if (!strcmp(args[0], "set-nice")) {
8573 rule->action = HTTP_RES_ACT_SET_NICE;
8574 cur_arg = 1;
8575
8576 if (!*args[cur_arg] ||
8577 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8578 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8579 file, linenum, args[0]);
8580 goto out_err;
8581 }
8582 rule->arg.nice = atoi(args[cur_arg]);
8583 if (rule->arg.nice < -1024)
8584 rule->arg.nice = -1024;
8585 else if (rule->arg.nice > 1024)
8586 rule->arg.nice = 1024;
8587 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008588 } else if (!strcmp(args[0], "set-tos")) {
8589#ifdef IP_TOS
8590 char *err;
8591 rule->action = HTTP_RES_ACT_SET_TOS;
8592 cur_arg = 1;
8593
8594 if (!*args[cur_arg] ||
8595 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8596 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8597 file, linenum, args[0]);
8598 goto out_err;
8599 }
8600
8601 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8602 if (err && *err != '\0') {
8603 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8604 file, linenum, err, args[0]);
8605 goto out_err;
8606 }
8607 cur_arg++;
8608#else
8609 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8610 goto out_err;
8611#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008612 } else if (!strcmp(args[0], "set-mark")) {
8613#ifdef SO_MARK
8614 char *err;
8615 rule->action = HTTP_RES_ACT_SET_MARK;
8616 cur_arg = 1;
8617
8618 if (!*args[cur_arg] ||
8619 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8620 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8621 file, linenum, args[0]);
8622 goto out_err;
8623 }
8624
8625 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8626 if (err && *err != '\0') {
8627 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8628 file, linenum, err, args[0]);
8629 goto out_err;
8630 }
8631 cur_arg++;
8632 global.last_checks |= LSTCHK_NETADM;
8633#else
8634 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8635 goto out_err;
8636#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008637 } else if (!strcmp(args[0], "set-log-level")) {
8638 rule->action = HTTP_RES_ACT_SET_LOGL;
8639 cur_arg = 1;
8640
8641 if (!*args[cur_arg] ||
8642 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8643 bad_log_level:
8644 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8645 file, linenum, args[0]);
8646 goto out_err;
8647 }
8648 if (strcmp(args[cur_arg], "silent") == 0)
8649 rule->arg.loglevel = -1;
8650 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8651 goto bad_log_level;
8652 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008653 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8654 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8655 cur_arg = 1;
8656
8657 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8658 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8659 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8660 file, linenum, args[0]);
8661 goto out_err;
8662 }
8663
8664 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8665 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8666 LIST_INIT(&rule->arg.hdr_add.fmt);
8667
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008668 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008669 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8670 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8671 cur_arg += 2;
8672 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008673 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 +02008674 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8675 goto out_err;
8676 }
8677
8678 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8679 struct acl_cond *cond;
8680 char *errmsg = NULL;
8681
8682 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8683 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8684 file, linenum, args[0], errmsg);
8685 free(errmsg);
8686 goto out_err;
8687 }
8688 rule->cond = cond;
8689 }
8690 else if (*args[cur_arg]) {
8691 Alert("parsing [%s:%d]: 'http-response %s' expects"
8692 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8693 file, linenum, args[0], args[cur_arg]);
8694 goto out_err;
8695 }
8696
8697 return rule;
8698 out_err:
8699 free(rule);
8700 return NULL;
8701}
8702
Willy Tarreau4baae242012-12-27 12:00:31 +01008703/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008704 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8705 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008706 */
8707struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008708 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008709{
8710 struct redirect_rule *rule;
8711 int cur_arg;
8712 int type = REDIRECT_TYPE_NONE;
8713 int code = 302;
8714 const char *destination = NULL;
8715 const char *cookie = NULL;
8716 int cookie_set = 0;
8717 unsigned int flags = REDIRECT_FLAG_NONE;
8718 struct acl_cond *cond = NULL;
8719
8720 cur_arg = 0;
8721 while (*(args[cur_arg])) {
8722 if (strcmp(args[cur_arg], "location") == 0) {
8723 if (!*args[cur_arg + 1])
8724 goto missing_arg;
8725
8726 type = REDIRECT_TYPE_LOCATION;
8727 cur_arg++;
8728 destination = args[cur_arg];
8729 }
8730 else if (strcmp(args[cur_arg], "prefix") == 0) {
8731 if (!*args[cur_arg + 1])
8732 goto missing_arg;
8733
8734 type = REDIRECT_TYPE_PREFIX;
8735 cur_arg++;
8736 destination = args[cur_arg];
8737 }
8738 else if (strcmp(args[cur_arg], "scheme") == 0) {
8739 if (!*args[cur_arg + 1])
8740 goto missing_arg;
8741
8742 type = REDIRECT_TYPE_SCHEME;
8743 cur_arg++;
8744 destination = args[cur_arg];
8745 }
8746 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8747 if (!*args[cur_arg + 1])
8748 goto missing_arg;
8749
8750 cur_arg++;
8751 cookie = args[cur_arg];
8752 cookie_set = 1;
8753 }
8754 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8755 if (!*args[cur_arg + 1])
8756 goto missing_arg;
8757
8758 cur_arg++;
8759 cookie = args[cur_arg];
8760 cookie_set = 0;
8761 }
8762 else if (strcmp(args[cur_arg], "code") == 0) {
8763 if (!*args[cur_arg + 1])
8764 goto missing_arg;
8765
8766 cur_arg++;
8767 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008768 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008769 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008770 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008771 args[cur_arg - 1], args[cur_arg]);
8772 return NULL;
8773 }
8774 }
8775 else if (!strcmp(args[cur_arg],"drop-query")) {
8776 flags |= REDIRECT_FLAG_DROP_QS;
8777 }
8778 else if (!strcmp(args[cur_arg],"append-slash")) {
8779 flags |= REDIRECT_FLAG_APPEND_SLASH;
8780 }
8781 else if (strcmp(args[cur_arg], "if") == 0 ||
8782 strcmp(args[cur_arg], "unless") == 0) {
8783 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8784 if (!cond) {
8785 memprintf(errmsg, "error in condition: %s", *errmsg);
8786 return NULL;
8787 }
8788 break;
8789 }
8790 else {
8791 memprintf(errmsg,
8792 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8793 args[cur_arg]);
8794 return NULL;
8795 }
8796 cur_arg++;
8797 }
8798
8799 if (type == REDIRECT_TYPE_NONE) {
8800 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8801 return NULL;
8802 }
8803
8804 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8805 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008806 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008807
8808 if (!use_fmt) {
8809 /* old-style static redirect rule */
8810 rule->rdr_str = strdup(destination);
8811 rule->rdr_len = strlen(destination);
8812 }
8813 else {
8814 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008815
8816 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8817 * if prefix == "/", we don't want to add anything, otherwise it
8818 * makes it hard for the user to configure a self-redirection.
8819 */
8820 proxy->conf.args.ctx = ARGC_RDR;
8821 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8822 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8823 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8824 }
8825 }
8826
Willy Tarreau4baae242012-12-27 12:00:31 +01008827 if (cookie) {
8828 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8829 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8830 */
8831 rule->cookie_len = strlen(cookie);
8832 if (cookie_set) {
8833 rule->cookie_str = malloc(rule->cookie_len + 10);
8834 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8835 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8836 rule->cookie_len += 9;
8837 } else {
8838 rule->cookie_str = malloc(rule->cookie_len + 21);
8839 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8840 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8841 rule->cookie_len += 20;
8842 }
8843 }
8844 rule->type = type;
8845 rule->code = code;
8846 rule->flags = flags;
8847 LIST_INIT(&rule->list);
8848 return rule;
8849
8850 missing_arg:
8851 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8852 return NULL;
8853}
8854
Willy Tarreau8797c062007-05-07 00:55:35 +02008855/************************************************************************/
8856/* The code below is dedicated to ACL parsing and matching */
8857/************************************************************************/
8858
8859
Willy Tarreau14174bc2012-04-16 14:34:04 +02008860/* This function ensures that the prerequisites for an L7 fetch are ready,
8861 * which means that a request or response is ready. If some data is missing,
8862 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008863 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8864 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008865 *
8866 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008867 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8868 * decide whether or not an HTTP message is present ;
8869 * 0 if the requested data cannot be fetched or if it is certain that
8870 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008871 * 1 if an HTTP message is ready
8872 */
8873static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008874smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008875 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008876{
8877 struct http_txn *txn = l7;
8878 struct http_msg *msg = &txn->req;
8879
8880 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8881 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8882 */
8883
8884 if (unlikely(!s || !txn))
8885 return 0;
8886
8887 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008888 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008889
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008890 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008891 if (unlikely(!s->req))
8892 return 0;
8893
Willy Tarreauaae75e32013-03-29 12:31:49 +01008894 /* If the buffer does not leave enough free space at the end,
8895 * we must first realign it.
8896 */
8897 if (s->req->buf->p > s->req->buf->data &&
8898 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8899 buffer_slow_realign(s->req->buf);
8900
Willy Tarreau14174bc2012-04-16 14:34:04 +02008901 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008902 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008903 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008904
8905 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008906 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008907 http_msg_analyzer(msg, &txn->hdr_idx);
8908
8909 /* Still no valid request ? */
8910 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008911 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008912 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008913 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008914 }
8915 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008916 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008917 return 0;
8918 }
8919
8920 /* OK we just got a valid HTTP request. We have some minor
8921 * preparation to perform so that further checks can rely
8922 * on HTTP tests.
8923 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008924
8925 /* If the request was parsed but was too large, we must absolutely
8926 * return an error so that it is not processed. At the moment this
8927 * cannot happen, but if the parsers are to change in the future,
8928 * we want this check to be maintained.
8929 */
8930 if (unlikely(s->req->buf->i + s->req->buf->p >
8931 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8932 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008933 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008934 return 1;
8935 }
8936
Willy Tarreau9b28e032012-10-12 23:49:43 +02008937 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008938 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8939 s->flags |= SN_REDIRECTABLE;
8940
Willy Tarreau506d0502013-07-06 13:29:24 +02008941 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8942 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008943 }
8944
Willy Tarreau506d0502013-07-06 13:29:24 +02008945 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008946 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008947 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008948
8949 /* otherwise everything's ready for the request */
8950 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008951 else {
8952 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008953 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8954 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008955 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008956 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008957 }
8958
8959 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008960 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008961 return 1;
8962}
Willy Tarreau8797c062007-05-07 00:55:35 +02008963
Willy Tarreauc0239e02012-04-16 14:42:55 +02008964#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008965 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 +02008966
Willy Tarreau24e32d82012-04-23 23:55:44 +02008967#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008968 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 +02008969
Willy Tarreau8797c062007-05-07 00:55:35 +02008970
8971/* 1. Check on METHOD
8972 * We use the pre-parsed method if it is known, and store its number as an
8973 * integer. If it is unknown, we use the pointer and the length.
8974 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008975static 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 +02008976{
8977 int len, meth;
8978
Willy Tarreauae8b7962007-06-09 23:10:04 +02008979 len = strlen(*text);
8980 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008981
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01008982 pattern->smp = smp;
Willy Tarreau8797c062007-05-07 00:55:35 +02008983 pattern->val.i = meth;
8984 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008985 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008986 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008987 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008988 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008989 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008990 pattern->len = len;
8991 }
8992 return 1;
8993}
8994
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008995/* This function fetches the method of current HTTP request and stores
8996 * it in the global pattern struct as a chunk. There are two possibilities :
8997 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8998 * in <len> and <ptr> is NULL ;
8999 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9000 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009001 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009002 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009003static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009004smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009005 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009006{
9007 int meth;
9008 struct http_txn *txn = l7;
9009
Willy Tarreau24e32d82012-04-23 23:55:44 +02009010 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009011
Willy Tarreau8797c062007-05-07 00:55:35 +02009012 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02009013 smp->type = SMP_T_UINT;
9014 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009015 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009016 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9017 /* ensure the indexes are not affected */
9018 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009019 smp->type = SMP_T_CSTR;
9020 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009021 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009022 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009023 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009024 return 1;
9025}
9026
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009027/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01009028static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02009029{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009030 int icase;
9031
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009032
Willy Tarreauf853c462012-04-23 18:53:56 +02009033 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009034 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009035 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009036 return PAT_MATCH;
9037 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009038 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009039
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009040 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9041 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009042 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009043
9044 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009045 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009046 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009047
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009048 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009049 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9050 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009051 return PAT_NOMATCH;
9052 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009053}
9054
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009055static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009056smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009057 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009058{
9059 struct http_txn *txn = l7;
9060 char *ptr;
9061 int len;
9062
Willy Tarreauc0239e02012-04-16 14:42:55 +02009063 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009064
Willy Tarreau8797c062007-05-07 00:55:35 +02009065 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009066 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009067
9068 while ((len-- > 0) && (*ptr++ != '/'));
9069 if (len <= 0)
9070 return 0;
9071
Willy Tarreauf853c462012-04-23 18:53:56 +02009072 smp->type = SMP_T_CSTR;
9073 smp->data.str.str = ptr;
9074 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009075
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009076 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009077 return 1;
9078}
9079
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009080static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009081smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009082 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009083{
9084 struct http_txn *txn = l7;
9085 char *ptr;
9086 int len;
9087
Willy Tarreauc0239e02012-04-16 14:42:55 +02009088 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009089
Willy Tarreauf26b2522012-12-14 08:33:14 +01009090 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9091 return 0;
9092
Willy Tarreau8797c062007-05-07 00:55:35 +02009093 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009094 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009095
9096 while ((len-- > 0) && (*ptr++ != '/'));
9097 if (len <= 0)
9098 return 0;
9099
Willy Tarreauf853c462012-04-23 18:53:56 +02009100 smp->type = SMP_T_CSTR;
9101 smp->data.str.str = ptr;
9102 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009103
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009104 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009105 return 1;
9106}
9107
9108/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009109static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009110smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009111 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009112{
9113 struct http_txn *txn = l7;
9114 char *ptr;
9115 int len;
9116
Willy Tarreauc0239e02012-04-16 14:42:55 +02009117 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009118
Willy Tarreauf26b2522012-12-14 08:33:14 +01009119 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9120 return 0;
9121
Willy Tarreau8797c062007-05-07 00:55:35 +02009122 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009123 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009124
Willy Tarreauf853c462012-04-23 18:53:56 +02009125 smp->type = SMP_T_UINT;
9126 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009127 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009128 return 1;
9129}
9130
9131/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009132static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009133smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009134 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009135{
9136 struct http_txn *txn = l7;
9137
Willy Tarreauc0239e02012-04-16 14:42:55 +02009138 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009139
Willy Tarreauf853c462012-04-23 18:53:56 +02009140 smp->type = SMP_T_CSTR;
9141 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009142 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009143 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009144 return 1;
9145}
9146
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009147static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009148smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009149 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009150{
9151 struct http_txn *txn = l7;
9152
Willy Tarreauc0239e02012-04-16 14:42:55 +02009153 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009154
9155 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009156 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
9157 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009158 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009159 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009160 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009161
9162 /*
9163 * If we are parsing url in frontend space, we prepare backend stage
9164 * to not parse again the same url ! optimization lazyness...
9165 */
9166 if (px->options & PR_O_HTTP_PROXY)
9167 l4->flags |= SN_ADDR_SET;
9168
Willy Tarreau37406352012-04-23 16:16:37 +02009169 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009170 return 1;
9171}
9172
9173static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009174smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009175 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009176{
9177 struct http_txn *txn = l7;
9178
Willy Tarreauc0239e02012-04-16 14:42:55 +02009179 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009180
9181 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009182 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 +02009183 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02009184 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009185
9186 if (px->options & PR_O_HTTP_PROXY)
9187 l4->flags |= SN_ADDR_SET;
9188
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009189 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009190 return 1;
9191}
9192
Willy Tarreau185b5c42012-04-26 15:11:51 +02009193/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9194 * Accepts an optional argument of type string containing the header field name,
9195 * and an optional argument of type signed or unsigned integer to request an
9196 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009197 * headers are considered from the first one. It does not stop on commas and
9198 * returns full lines instead (useful for User-Agent or Date for example).
9199 */
9200static int
9201smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009202 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009203{
9204 struct http_txn *txn = l7;
9205 struct hdr_idx *idx = &txn->hdr_idx;
9206 struct hdr_ctx *ctx = smp->ctx.a[0];
9207 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9208 int occ = 0;
9209 const char *name_str = NULL;
9210 int name_len = 0;
9211
9212 if (!ctx) {
9213 /* first call */
9214 ctx = &static_hdr_ctx;
9215 ctx->idx = 0;
9216 smp->ctx.a[0] = ctx;
9217 }
9218
9219 if (args) {
9220 if (args[0].type != ARGT_STR)
9221 return 0;
9222 name_str = args[0].data.str.str;
9223 name_len = args[0].data.str.len;
9224
9225 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9226 occ = args[1].data.uint;
9227 }
9228
9229 CHECK_HTTP_MESSAGE_FIRST();
9230
9231 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9232 /* search for header from the beginning */
9233 ctx->idx = 0;
9234
9235 if (!occ && !(opt & SMP_OPT_ITERATE))
9236 /* no explicit occurrence and single fetch => last header by default */
9237 occ = -1;
9238
9239 if (!occ)
9240 /* prepare to report multiple occurrences for ACL fetches */
9241 smp->flags |= SMP_F_NOT_LAST;
9242
9243 smp->type = SMP_T_CSTR;
9244 smp->flags |= SMP_F_VOL_HDR;
9245 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9246 return 1;
9247
9248 smp->flags &= ~SMP_F_NOT_LAST;
9249 return 0;
9250}
9251
9252/* 6. Check on HTTP header count. The number of occurrences is returned.
9253 * Accepts exactly 1 argument of type string. It does not stop on commas and
9254 * returns full lines instead (useful for User-Agent or Date for example).
9255 */
9256static int
9257smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009258 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009259{
9260 struct http_txn *txn = l7;
9261 struct hdr_idx *idx = &txn->hdr_idx;
9262 struct hdr_ctx ctx;
9263 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9264 int cnt;
9265
9266 if (!args || args->type != ARGT_STR)
9267 return 0;
9268
9269 CHECK_HTTP_MESSAGE_FIRST();
9270
9271 ctx.idx = 0;
9272 cnt = 0;
9273 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9274 cnt++;
9275
9276 smp->type = SMP_T_UINT;
9277 smp->data.uint = cnt;
9278 smp->flags = SMP_F_VOL_HDR;
9279 return 1;
9280}
9281
9282/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9283 * Accepts an optional argument of type string containing the header field name,
9284 * and an optional argument of type signed or unsigned integer to request an
9285 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009286 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009287 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009288static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009289smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009290 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009291{
9292 struct http_txn *txn = l7;
9293 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009294 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009295 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009296 int occ = 0;
9297 const char *name_str = NULL;
9298 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009299
Willy Tarreaua890d072013-04-02 12:01:06 +02009300 if (!ctx) {
9301 /* first call */
9302 ctx = &static_hdr_ctx;
9303 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009304 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009305 }
9306
Willy Tarreau185b5c42012-04-26 15:11:51 +02009307 if (args) {
9308 if (args[0].type != ARGT_STR)
9309 return 0;
9310 name_str = args[0].data.str.str;
9311 name_len = args[0].data.str.len;
9312
9313 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9314 occ = args[1].data.uint;
9315 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009316
Willy Tarreaue333ec92012-04-16 16:26:40 +02009317 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009318
Willy Tarreau185b5c42012-04-26 15:11:51 +02009319 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009320 /* search for header from the beginning */
9321 ctx->idx = 0;
9322
Willy Tarreau185b5c42012-04-26 15:11:51 +02009323 if (!occ && !(opt & SMP_OPT_ITERATE))
9324 /* no explicit occurrence and single fetch => last header by default */
9325 occ = -1;
9326
9327 if (!occ)
9328 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009329 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009330
Willy Tarreau185b5c42012-04-26 15:11:51 +02009331 smp->type = SMP_T_CSTR;
9332 smp->flags |= SMP_F_VOL_HDR;
9333 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 +02009334 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009335
Willy Tarreau37406352012-04-23 16:16:37 +02009336 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009337 return 0;
9338}
9339
Willy Tarreauc11416f2007-06-17 16:58:38 +02009340/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009341 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009342 */
9343static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009344smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009345 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009346{
9347 struct http_txn *txn = l7;
9348 struct hdr_idx *idx = &txn->hdr_idx;
9349 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009350 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009351 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009352
Willy Tarreau24e32d82012-04-23 23:55:44 +02009353 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009354 return 0;
9355
Willy Tarreaue333ec92012-04-16 16:26:40 +02009356 CHECK_HTTP_MESSAGE_FIRST();
9357
Willy Tarreau33a7e692007-06-10 19:45:56 +02009358 ctx.idx = 0;
9359 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009360 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 +02009361 cnt++;
9362
Willy Tarreauf853c462012-04-23 18:53:56 +02009363 smp->type = SMP_T_UINT;
9364 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009365 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009366 return 1;
9367}
9368
Willy Tarreau185b5c42012-04-26 15:11:51 +02009369/* Fetch an HTTP header's integer value. The integer value is returned. It
9370 * takes a mandatory argument of type string and an optional one of type int
9371 * to designate a specific occurrence. It returns an unsigned integer, which
9372 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009373 */
9374static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009375smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009376 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009377{
Willy Tarreauef38c392013-07-22 16:29:32 +02009378 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009379
Willy Tarreauf853c462012-04-23 18:53:56 +02009380 if (ret > 0) {
9381 smp->type = SMP_T_UINT;
9382 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9383 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009384
Willy Tarreaud53e2422012-04-16 17:21:11 +02009385 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009386}
9387
Cyril Bonté69fa9922012-10-25 00:01:06 +02009388/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9389 * and an optional one of type int to designate a specific occurrence.
9390 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009391 */
9392static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009393smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009394 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009395{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009396 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009397
Willy Tarreauef38c392013-07-22 16:29:32 +02009398 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009399 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9400 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009401 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009402 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009403 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009404 if (smp->data.str.len < temp->size - 1) {
9405 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9406 temp->str[smp->data.str.len] = '\0';
9407 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9408 smp->type = SMP_T_IPV6;
9409 break;
9410 }
9411 }
9412 }
9413
Willy Tarreaud53e2422012-04-16 17:21:11 +02009414 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009415 if (!(smp->flags & SMP_F_NOT_LAST))
9416 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009417 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009418 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009419}
9420
Willy Tarreau737b0c12007-06-10 21:28:46 +02009421/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9422 * the first '/' after the possible hostname, and ends before the possible '?'.
9423 */
9424static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009425smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009426 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009427{
9428 struct http_txn *txn = l7;
9429 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009430
Willy Tarreauc0239e02012-04-16 14:42:55 +02009431 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009432
Willy Tarreau9b28e032012-10-12 23:49:43 +02009433 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009434 ptr = http_get_path(txn);
9435 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009436 return 0;
9437
9438 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009439 smp->type = SMP_T_CSTR;
9440 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009441
9442 while (ptr < end && *ptr != '?')
9443 ptr++;
9444
Willy Tarreauf853c462012-04-23 18:53:56 +02009445 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009446 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009447 return 1;
9448}
9449
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009450/* This produces a concatenation of the first occurrence of the Host header
9451 * followed by the path component if it begins with a slash ('/'). This means
9452 * that '*' will not be added, resulting in exactly the first Host entry.
9453 * If no Host header is found, then the path is returned as-is. The returned
9454 * value is stored in the trash so it does not need to be marked constant.
9455 */
9456static int
9457smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009458 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009459{
9460 struct http_txn *txn = l7;
9461 char *ptr, *end, *beg;
9462 struct hdr_ctx ctx;
9463
9464 CHECK_HTTP_MESSAGE_FIRST();
9465
9466 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009467 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 +02009468 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009469 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009470
9471 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009472 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009473 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009474 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009475 smp->data.str.len = ctx.vlen;
9476
9477 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009478 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 +02009479 beg = http_get_path(txn);
9480 if (!beg)
9481 beg = end;
9482
9483 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9484
9485 if (beg < ptr && *beg == '/') {
9486 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9487 smp->data.str.len += ptr - beg;
9488 }
9489
9490 smp->flags = SMP_F_VOL_1ST;
9491 return 1;
9492}
9493
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009494/* This produces a 32-bit hash of the concatenation of the first occurrence of
9495 * the Host header followed by the path component if it begins with a slash ('/').
9496 * This means that '*' will not be added, resulting in exactly the first Host
9497 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009498 * is hashed using the path hash followed by a full avalanche hash and provides a
9499 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009500 * high-traffic sites without having to store whole paths.
9501 */
9502static int
9503smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009504 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009505{
9506 struct http_txn *txn = l7;
9507 struct hdr_ctx ctx;
9508 unsigned int hash = 0;
9509 char *ptr, *beg, *end;
9510 int len;
9511
9512 CHECK_HTTP_MESSAGE_FIRST();
9513
9514 ctx.idx = 0;
9515 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9516 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9517 ptr = ctx.line + ctx.val;
9518 len = ctx.vlen;
9519 while (len--)
9520 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9521 }
9522
9523 /* now retrieve the path */
9524 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9525 beg = http_get_path(txn);
9526 if (!beg)
9527 beg = end;
9528
9529 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9530
9531 if (beg < ptr && *beg == '/') {
9532 while (beg < ptr)
9533 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9534 }
9535 hash = full_hash(hash);
9536
9537 smp->type = SMP_T_UINT;
9538 smp->data.uint = hash;
9539 smp->flags = SMP_F_VOL_1ST;
9540 return 1;
9541}
9542
Willy Tarreau4a550602012-12-09 14:53:32 +01009543/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009544 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9545 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9546 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009547 * that in environments where IPv6 is insignificant, truncating the output to
9548 * 8 bytes would still work.
9549 */
9550static int
9551smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009552 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009553{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009554 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01009555
Willy Tarreauef38c392013-07-22 16:29:32 +02009556 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009557 return 0;
9558
Willy Tarreau47ca5452012-12-23 20:22:19 +01009559 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009560 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9561 temp->len += sizeof(smp->data.uint);
9562
9563 switch (l4->si[0].conn->addr.from.ss_family) {
9564 case AF_INET:
9565 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
9566 temp->len += 4;
9567 break;
9568 case AF_INET6:
9569 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
9570 temp->len += 16;
9571 break;
9572 default:
9573 return 0;
9574 }
9575
9576 smp->data.str = *temp;
9577 smp->type = SMP_T_BIN;
9578 return 1;
9579}
9580
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009581static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009582smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009583 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009584{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009585 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9586 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9587 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009588
Willy Tarreau24e32d82012-04-23 23:55:44 +02009589 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009590
Willy Tarreauf853c462012-04-23 18:53:56 +02009591 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009592 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009593 return 1;
9594}
9595
Willy Tarreau7f18e522010-10-22 20:04:13 +02009596/* return a valid test if the current request is the first one on the connection */
9597static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009598smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009599 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009600{
9601 if (!s)
9602 return 0;
9603
Willy Tarreauf853c462012-04-23 18:53:56 +02009604 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009605 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009606 return 1;
9607}
9608
Willy Tarreau34db1082012-04-19 17:16:54 +02009609/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009610static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009611smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009612 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009613{
9614
Willy Tarreau24e32d82012-04-23 23:55:44 +02009615 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009616 return 0;
9617
Willy Tarreauc0239e02012-04-16 14:42:55 +02009618 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009619
Willy Tarreauc0239e02012-04-16 14:42:55 +02009620 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009621 return 0;
9622
Willy Tarreauf853c462012-04-23 18:53:56 +02009623 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009624 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 +01009625 return 1;
9626}
Willy Tarreau8797c062007-05-07 00:55:35 +02009627
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009628/* Accepts exactly 1 argument of type userlist */
9629static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009630smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009631 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009632{
9633
9634 if (!args || args->type != ARGT_USR)
9635 return 0;
9636
9637 CHECK_HTTP_MESSAGE_FIRST();
9638
9639 if (!get_http_auth(l4))
9640 return 0;
9641
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009642 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009643 smp->ctx.a[0] = args->data.usr; /* user list */
9644 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9645 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9646
9647 /* if the user does not belong to the userlist or has a wrong password,
9648 * report that it unconditionally does not match. Otherwise we return
9649 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009650 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009651 */
9652 smp->type = SMP_T_BOOL;
9653 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9654 if (smp->data.uint)
9655 smp->type = SMP_T_UINT;
9656
9657 return 1;
9658}
9659
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009660/* Try to find the next occurrence of a cookie name in a cookie header value.
9661 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9662 * the cookie value is returned into *value and *value_l, and the function
9663 * returns a pointer to the next pointer to search from if the value was found.
9664 * Otherwise if the cookie was not found, NULL is returned and neither value
9665 * nor value_l are touched. The input <hdr> string should first point to the
9666 * header's value, and the <hdr_end> pointer must point to the first character
9667 * not part of the value. <list> must be non-zero if value may represent a list
9668 * of values (cookie headers). This makes it faster to abort parsing when no
9669 * list is expected.
9670 */
9671static char *
9672extract_cookie_value(char *hdr, const char *hdr_end,
9673 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009674 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009675{
9676 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9677 char *next;
9678
9679 /* we search at least a cookie name followed by an equal, and more
9680 * generally something like this :
9681 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9682 */
9683 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9684 /* Iterate through all cookies on this line */
9685
9686 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9687 att_beg++;
9688
9689 /* find att_end : this is the first character after the last non
9690 * space before the equal. It may be equal to hdr_end.
9691 */
9692 equal = att_end = att_beg;
9693
9694 while (equal < hdr_end) {
9695 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9696 break;
9697 if (http_is_spht[(unsigned char)*equal++])
9698 continue;
9699 att_end = equal;
9700 }
9701
9702 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9703 * is between <att_beg> and <equal>, both may be identical.
9704 */
9705
9706 /* look for end of cookie if there is an equal sign */
9707 if (equal < hdr_end && *equal == '=') {
9708 /* look for the beginning of the value */
9709 val_beg = equal + 1;
9710 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9711 val_beg++;
9712
9713 /* find the end of the value, respecting quotes */
9714 next = find_cookie_value_end(val_beg, hdr_end);
9715
9716 /* make val_end point to the first white space or delimitor after the value */
9717 val_end = next;
9718 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9719 val_end--;
9720 } else {
9721 val_beg = val_end = next = equal;
9722 }
9723
9724 /* We have nothing to do with attributes beginning with '$'. However,
9725 * they will automatically be removed if a header before them is removed,
9726 * since they're supposed to be linked together.
9727 */
9728 if (*att_beg == '$')
9729 continue;
9730
9731 /* Ignore cookies with no equal sign */
9732 if (equal == next)
9733 continue;
9734
9735 /* Now we have the cookie name between att_beg and att_end, and
9736 * its value between val_beg and val_end.
9737 */
9738
9739 if (att_end - att_beg == cookie_name_l &&
9740 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9741 /* let's return this value and indicate where to go on from */
9742 *value = val_beg;
9743 *value_l = val_end - val_beg;
9744 return next + 1;
9745 }
9746
9747 /* Set-Cookie headers only have the name in the first attr=value part */
9748 if (!list)
9749 break;
9750 }
9751
9752 return NULL;
9753}
9754
Willy Tarreaue333ec92012-04-16 16:26:40 +02009755/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009756 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009757 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009758 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009759 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009760 * Accepts exactly 1 argument of type string. If the input options indicate
9761 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009762 */
9763static int
Willy Tarreau51539362012-05-08 12:46:28 +02009764smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009765 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009766{
9767 struct http_txn *txn = l7;
9768 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009769 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009770 const struct http_msg *msg;
9771 const char *hdr_name;
9772 int hdr_name_len;
9773 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009774 int occ = 0;
9775 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009776
Willy Tarreau24e32d82012-04-23 23:55:44 +02009777 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009778 return 0;
9779
Willy Tarreaua890d072013-04-02 12:01:06 +02009780 if (!ctx) {
9781 /* first call */
9782 ctx = &static_hdr_ctx;
9783 ctx->idx = 0;
9784 smp->ctx.a[2] = ctx;
9785 }
9786
Willy Tarreaue333ec92012-04-16 16:26:40 +02009787 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009788
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009789 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009790 msg = &txn->req;
9791 hdr_name = "Cookie";
9792 hdr_name_len = 6;
9793 } else {
9794 msg = &txn->rsp;
9795 hdr_name = "Set-Cookie";
9796 hdr_name_len = 10;
9797 }
9798
Willy Tarreau28376d62012-04-26 21:26:10 +02009799 if (!occ && !(opt & SMP_OPT_ITERATE))
9800 /* no explicit occurrence and single fetch => last cookie by default */
9801 occ = -1;
9802
9803 /* OK so basically here, either we want only one value and it's the
9804 * last one, or we want to iterate over all of them and we fetch the
9805 * next one.
9806 */
9807
Willy Tarreau9b28e032012-10-12 23:49:43 +02009808 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009809 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009810 /* search for the header from the beginning, we must first initialize
9811 * the search parameters.
9812 */
Willy Tarreau37406352012-04-23 16:16:37 +02009813 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009814 ctx->idx = 0;
9815 }
9816
Willy Tarreau28376d62012-04-26 21:26:10 +02009817 smp->flags |= SMP_F_VOL_HDR;
9818
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009819 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009820 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9821 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009822 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9823 goto out;
9824
Willy Tarreau24e32d82012-04-23 23:55:44 +02009825 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009826 continue;
9827
Willy Tarreau37406352012-04-23 16:16:37 +02009828 smp->ctx.a[0] = ctx->line + ctx->val;
9829 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009830 }
9831
Willy Tarreauf853c462012-04-23 18:53:56 +02009832 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009833 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009834 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009835 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009836 &smp->data.str.str,
9837 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009838 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009839 found = 1;
9840 if (occ >= 0) {
9841 /* one value was returned into smp->data.str.{str,len} */
9842 smp->flags |= SMP_F_NOT_LAST;
9843 return 1;
9844 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009845 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009846 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009847 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009848 /* all cookie headers and values were scanned. If we're looking for the
9849 * last occurrence, we may return it now.
9850 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009851 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009852 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009853 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009854}
9855
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009856/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009857 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009858 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009859 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009860 */
9861static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009862smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009863 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009864{
9865 struct http_txn *txn = l7;
9866 struct hdr_idx *idx = &txn->hdr_idx;
9867 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009868 const struct http_msg *msg;
9869 const char *hdr_name;
9870 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009871 int cnt;
9872 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009873 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009874
Willy Tarreau24e32d82012-04-23 23:55:44 +02009875 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009876 return 0;
9877
Willy Tarreaue333ec92012-04-16 16:26:40 +02009878 CHECK_HTTP_MESSAGE_FIRST();
9879
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009880 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009881 msg = &txn->req;
9882 hdr_name = "Cookie";
9883 hdr_name_len = 6;
9884 } else {
9885 msg = &txn->rsp;
9886 hdr_name = "Set-Cookie";
9887 hdr_name_len = 10;
9888 }
9889
Willy Tarreau9b28e032012-10-12 23:49:43 +02009890 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009891 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009892 ctx.idx = 0;
9893 cnt = 0;
9894
9895 while (1) {
9896 /* Note: val_beg == NULL every time we need to fetch a new header */
9897 if (!val_beg) {
9898 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9899 break;
9900
Willy Tarreau24e32d82012-04-23 23:55:44 +02009901 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009902 continue;
9903
9904 val_beg = ctx.line + ctx.val;
9905 val_end = val_beg + ctx.vlen;
9906 }
9907
Willy Tarreauf853c462012-04-23 18:53:56 +02009908 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009909 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009910 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009911 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009912 &smp->data.str.str,
9913 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009914 cnt++;
9915 }
9916 }
9917
Willy Tarreauf853c462012-04-23 18:53:56 +02009918 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009919 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009920 return 1;
9921}
9922
Willy Tarreau51539362012-05-08 12:46:28 +02009923/* Fetch an cookie's integer value. The integer value is returned. It
9924 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9925 */
9926static int
9927smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009928 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009929{
Willy Tarreauef38c392013-07-22 16:29:32 +02009930 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009931
9932 if (ret > 0) {
9933 smp->type = SMP_T_UINT;
9934 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9935 }
9936
9937 return ret;
9938}
9939
Willy Tarreau8797c062007-05-07 00:55:35 +02009940/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009941/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009942/************************************************************************/
9943
David Cournapeau16023ee2010-12-23 20:55:41 +09009944/*
9945 * Given a path string and its length, find the position of beginning of the
9946 * query string. Returns NULL if no query string is found in the path.
9947 *
9948 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9949 *
9950 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9951 */
bedis4c75cca2012-10-05 08:38:24 +02009952static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009953{
9954 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009955
bedis4c75cca2012-10-05 08:38:24 +02009956 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009957 return p ? p + 1 : NULL;
9958}
9959
bedis4c75cca2012-10-05 08:38:24 +02009960static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009961{
bedis4c75cca2012-10-05 08:38:24 +02009962 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009963}
9964
9965/*
9966 * Given a url parameter, find the starting position of the first occurence,
9967 * or NULL if the parameter is not found.
9968 *
9969 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9970 * the function will return query_string+8.
9971 */
9972static char*
9973find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009974 char* url_param_name, size_t url_param_name_l,
9975 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009976{
9977 char *pos, *last;
9978
9979 pos = query_string;
9980 last = query_string + query_string_l - url_param_name_l - 1;
9981
9982 while (pos <= last) {
9983 if (pos[url_param_name_l] == '=') {
9984 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9985 return pos;
9986 pos += url_param_name_l + 1;
9987 }
bedis4c75cca2012-10-05 08:38:24 +02009988 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009989 pos++;
9990 pos++;
9991 }
9992 return NULL;
9993}
9994
9995/*
9996 * Given a url parameter name, returns its value and size into *value and
9997 * *value_l respectively, and returns non-zero. If the parameter is not found,
9998 * zero is returned and value/value_l are not touched.
9999 */
10000static int
10001find_url_param_value(char* path, size_t path_l,
10002 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010003 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010004{
10005 char *query_string, *qs_end;
10006 char *arg_start;
10007 char *value_start, *value_end;
10008
bedis4c75cca2012-10-05 08:38:24 +020010009 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010010 if (!query_string)
10011 return 0;
10012
10013 qs_end = path + path_l;
10014 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010015 url_param_name, url_param_name_l,
10016 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010017 if (!arg_start)
10018 return 0;
10019
10020 value_start = arg_start + url_param_name_l + 1;
10021 value_end = value_start;
10022
bedis4c75cca2012-10-05 08:38:24 +020010023 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010024 value_end++;
10025
10026 *value = value_start;
10027 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010028 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010029}
10030
10031static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010032smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010033 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010034{
bedis4c75cca2012-10-05 08:38:24 +020010035 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010036 struct http_txn *txn = l7;
10037 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010038
bedis4c75cca2012-10-05 08:38:24 +020010039 if (!args || args[0].type != ARGT_STR ||
10040 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010041 return 0;
10042
10043 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010044
bedis4c75cca2012-10-05 08:38:24 +020010045 if (args[1].type)
10046 delim = *args[1].data.str.str;
10047
Willy Tarreau9b28e032012-10-12 23:49:43 +020010048 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010049 args->data.str.str, args->data.str.len,
10050 &smp->data.str.str, &smp->data.str.len,
10051 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010052 return 0;
10053
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010054 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010055 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010056 return 1;
10057}
10058
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010059/* Return the signed integer value for the specified url parameter (see url_param
10060 * above).
10061 */
10062static int
10063smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010064 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010065{
Willy Tarreauef38c392013-07-22 16:29:32 +020010066 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010067
10068 if (ret > 0) {
10069 smp->type = SMP_T_UINT;
10070 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10071 }
10072
10073 return ret;
10074}
10075
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010076/* This produces a 32-bit hash of the concatenation of the first occurrence of
10077 * the Host header followed by the path component if it begins with a slash ('/').
10078 * This means that '*' will not be added, resulting in exactly the first Host
10079 * entry. If no Host header is found, then the path is used. The resulting value
10080 * is hashed using the url hash followed by a full avalanche hash and provides a
10081 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10082 * high-traffic sites without having to store whole paths.
10083 * this differs from the base32 functions in that it includes the url parameters
10084 * as well as the path
10085 */
10086static int
10087smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010088 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010089{
10090 struct http_txn *txn = l7;
10091 struct hdr_ctx ctx;
10092 unsigned int hash = 0;
10093 char *ptr, *beg, *end;
10094 int len;
10095
10096 CHECK_HTTP_MESSAGE_FIRST();
10097
10098 ctx.idx = 0;
10099 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10100 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10101 ptr = ctx.line + ctx.val;
10102 len = ctx.vlen;
10103 while (len--)
10104 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10105 }
10106
10107 /* now retrieve the path */
10108 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10109 beg = http_get_path(txn);
10110 if (!beg)
10111 beg = end;
10112
10113 for (ptr = beg; ptr < end ; ptr++);
10114
10115 if (beg < ptr && *beg == '/') {
10116 while (beg < ptr)
10117 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10118 }
10119 hash = full_hash(hash);
10120
10121 smp->type = SMP_T_UINT;
10122 smp->data.uint = hash;
10123 smp->flags = SMP_F_VOL_1ST;
10124 return 1;
10125}
10126
10127/* This concatenates the source address with the 32-bit hash of the Host and
10128 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10129 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10130 * on the source address length. The URL hash is stored before the address so
10131 * that in environments where IPv6 is insignificant, truncating the output to
10132 * 8 bytes would still work.
10133 */
10134static int
10135smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010136 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010137{
10138 struct chunk *temp;
10139
Willy Tarreaue155ec22013-11-18 18:33:22 +010010140 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010141 return 0;
10142
10143 temp = get_trash_chunk();
10144 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10145 temp->len += sizeof(smp->data.uint);
10146
10147 switch (l4->si[0].conn->addr.from.ss_family) {
10148 case AF_INET:
10149 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
10150 temp->len += 4;
10151 break;
10152 case AF_INET6:
10153 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
10154 temp->len += 16;
10155 break;
10156 default:
10157 return 0;
10158 }
10159
10160 smp->data.str = *temp;
10161 smp->type = SMP_T_BIN;
10162 return 1;
10163}
10164
Willy Tarreau185b5c42012-04-26 15:11:51 +020010165/* This function is used to validate the arguments passed to any "hdr" fetch
10166 * keyword. These keywords support an optional positive or negative occurrence
10167 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10168 * is assumed that the types are already the correct ones. Returns 0 on error,
10169 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10170 * error message in case of error, that the caller is responsible for freeing.
10171 * The initial location must either be freeable or NULL.
10172 */
10173static int val_hdr(struct arg *arg, char **err_msg)
10174{
10175 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010176 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010177 return 0;
10178 }
10179 return 1;
10180}
10181
Willy Tarreau276fae92013-07-25 14:36:01 +020010182/* takes an UINT value on input supposed to represent the time since EPOCH,
10183 * adds an optional offset found in args[0] and emits a string representing
10184 * the date in RFC-1123/5322 format.
10185 */
10186static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10187{
10188 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10189 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10190 struct chunk *temp;
10191 struct tm *tm;
10192 time_t curr_date = smp->data.uint;
10193
10194 /* add offset */
10195 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10196 curr_date += args[0].data.sint;
10197
10198 tm = gmtime(&curr_date);
10199
10200 temp = get_trash_chunk();
10201 temp->len = snprintf(temp->str, temp->size - temp->len,
10202 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10203 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10204 tm->tm_hour, tm->tm_min, tm->tm_sec);
10205
10206 smp->data.str = *temp;
10207 smp->type = SMP_T_STR;
10208 return 1;
10209}
10210
Willy Tarreau4a568972010-05-12 08:08:50 +020010211/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010212/* All supported ACL keywords must be declared here. */
10213/************************************************************************/
10214
10215/* Note: must not be declared <const> as its list will be overwritten.
10216 * Please take care of keeping this list alphabetically sorted.
10217 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010218static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010219 { "base", "base", pat_parse_str, pat_match_str },
10220 { "base_beg", "base", pat_parse_str, pat_match_beg },
10221 { "base_dir", "base", pat_parse_str, pat_match_dir },
10222 { "base_dom", "base", pat_parse_str, pat_match_dom },
10223 { "base_end", "base", pat_parse_str, pat_match_end },
10224 { "base_len", "base", pat_parse_int, pat_match_len },
10225 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10226 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010227
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010228 { "cook", "req.cook", pat_parse_str, pat_match_str },
10229 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10230 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10231 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10232 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10233 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10234 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10235 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010236
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010237 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10238 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10239 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10240 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10241 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10242 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10243 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10244 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010245
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010246 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010247
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010248 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010249
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010250 { "path", "path", pat_parse_str, pat_match_str },
10251 { "path_beg", "path", pat_parse_str, pat_match_beg },
10252 { "path_dir", "path", pat_parse_str, pat_match_dir },
10253 { "path_dom", "path", pat_parse_str, pat_match_dom },
10254 { "path_end", "path", pat_parse_str, pat_match_end },
10255 { "path_len", "path", pat_parse_int, pat_match_len },
10256 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10257 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010258
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010259 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10260 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010261
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010262 { "scook", "res.cook", pat_parse_str, pat_match_str },
10263 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10264 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10265 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10266 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10267 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10268 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10269 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010270
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010271 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10272 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10273 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10274 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10275 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10276 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10277 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10278 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010279
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010280 { "url", "url", pat_parse_str, pat_match_str },
10281 { "url_beg", "url", pat_parse_str, pat_match_beg },
10282 { "url_dir", "url", pat_parse_str, pat_match_dir },
10283 { "url_dom", "url", pat_parse_str, pat_match_dom },
10284 { "url_end", "url", pat_parse_str, pat_match_end },
10285 { "url_len", "url", pat_parse_int, pat_match_len },
10286 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10287 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010288
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010289 { "urlp", "urlp", pat_parse_str, pat_match_str },
10290 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10291 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10292 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10293 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10294 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10295 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10296 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010297
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010298 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010299}};
10300
10301/************************************************************************/
10302/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010303/************************************************************************/
10304/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010305static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010306 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10307 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10308 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10309
10310 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10311 * are only here to match the ACL's name, are request-only and are used
10312 * for ACL compatibility only.
10313 */
10314 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10315 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10316 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10317 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10318
10319 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10320 * only here to match the ACL's name, are request-only and are used for
10321 * ACL compatibility only.
10322 */
10323 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10324 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10325 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10326 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10327
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010328 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10329 { "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 +010010330 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10331 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10332 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010333
10334 /* HTTP protocol on the request path */
10335 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010336 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010337
10338 /* HTTP version on the request path */
10339 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010340 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010341
10342 /* HTTP version on the response path */
10343 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010344 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10345
Willy Tarreau18ed2562013-01-14 15:56:36 +010010346 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10347 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10348 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10349 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10350
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010351 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10352 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010353 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10354 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10355 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10356 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10357
10358 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10359 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10360 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10361 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10362
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010363 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10364 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010365 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10366 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10367 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10368 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10369
Willy Tarreau409bcde2013-01-08 00:31:00 +010010370 /* scook is valid only on the response and is used for ACL compatibility */
10371 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10372 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10373 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10374 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10375
10376 /* shdr is valid only on the response and is used for ACL compatibility */
10377 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10378 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10379 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10380 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10381
10382 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10383 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010384 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10385 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010386 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10387 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10388 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10389 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10390 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10391 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010392}};
10393
Willy Tarreau8797c062007-05-07 00:55:35 +020010394
Willy Tarreau276fae92013-07-25 14:36:01 +020010395/* Note: must not be declared <const> as its list will be overwritten */
10396static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10397 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10398 { NULL, NULL, 0, 0, 0 },
10399}};
10400
Willy Tarreau8797c062007-05-07 00:55:35 +020010401__attribute__((constructor))
10402static void __http_protocol_init(void)
10403{
10404 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010405 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010406 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010407}
10408
10409
Willy Tarreau58f10d72006-12-04 02:26:12 +010010410/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010411 * Local variables:
10412 * c-indent-level: 8
10413 * c-basic-offset: 8
10414 * End:
10415 */