blob: a0717585a389afa78872b18872bdc883e6f515be [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 {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100297 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100298 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 */
Willy Tarreauc8987b32013-12-06 23:43:17 +0100770static enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100771{
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 Tarreau2d400bb2012-05-14 12:11:47 +0200939 else if (err_type & SI_ET_CONN_RES)
940 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
941 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100943 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200944 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100945}
946
Willy Tarreau42250582007-04-01 01:30:43 +0200947extern const char sess_term_cond[8];
948extern const char sess_fin_state[8];
949extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200950struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100951struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100952struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100953
Willy Tarreau117f59e2007-03-04 18:17:17 +0100954/*
955 * Capture headers from message starting at <som> according to header list
956 * <cap_hdr>, and fill the <idx> structure appropriately.
957 */
958void capture_headers(char *som, struct hdr_idx *idx,
959 char **cap, struct cap_hdr *cap_hdr)
960{
961 char *eol, *sol, *col, *sov;
962 int cur_idx;
963 struct cap_hdr *h;
964 int len;
965
966 sol = som + hdr_idx_first_pos(idx);
967 cur_idx = hdr_idx_first_idx(idx);
968
969 while (cur_idx) {
970 eol = sol + idx->v[cur_idx].len;
971
972 col = sol;
973 while (col < eol && *col != ':')
974 col++;
975
976 sov = col + 1;
977 while (sov < eol && http_is_lws[(unsigned char)*sov])
978 sov++;
979
980 for (h = cap_hdr; h; h = h->next) {
981 if ((h->namelen == col - sol) &&
982 (strncasecmp(sol, h->name, h->namelen) == 0)) {
983 if (cap[h->index] == NULL)
984 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200985 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100986
987 if (cap[h->index] == NULL) {
988 Alert("HTTP capture : out of memory.\n");
989 continue;
990 }
991
992 len = eol - sov;
993 if (len > h->len)
994 len = h->len;
995
996 memcpy(cap[h->index], sov, len);
997 cap[h->index][len]=0;
998 }
999 }
1000 sol = eol + idx->v[cur_idx].cr + 1;
1001 cur_idx = idx->v[cur_idx].next;
1002 }
1003}
1004
1005
Willy Tarreau42250582007-04-01 01:30:43 +02001006/* either we find an LF at <ptr> or we jump to <bad>.
1007 */
1008#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1009
1010/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1011 * otherwise to <http_msg_ood> with <state> set to <st>.
1012 */
1013#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1014 ptr++; \
1015 if (likely(ptr < end)) \
1016 goto good; \
1017 else { \
1018 state = (st); \
1019 goto http_msg_ood; \
1020 } \
1021 } while (0)
1022
1023
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001025 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1027 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1028 * will give undefined results.
1029 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1030 * and that msg->sol points to the beginning of the response.
1031 * If a complete line is found (which implies that at least one CR or LF is
1032 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1033 * returned indicating an incomplete line (which does not mean that parts have
1034 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1035 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1036 * upon next call.
1037 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001038 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1040 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001041 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001042 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001043const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001044 enum ht_state state, const char *ptr, const char *end,
1045 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001046{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001047 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001048
Willy Tarreau8973c702007-01-21 23:58:29 +01001049 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001050 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001051 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001052 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001053 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1054
1055 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001056 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001057 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1058 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001059 state = HTTP_MSG_ERROR;
1060 break;
1061
Willy Tarreau8973c702007-01-21 23:58:29 +01001062 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001063 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001065 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001066 goto http_msg_rpcode;
1067 }
1068 if (likely(HTTP_IS_SPHT(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1070 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001071 state = HTTP_MSG_ERROR;
1072 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001073
Willy Tarreau8973c702007-01-21 23:58:29 +01001074 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001075 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001076 if (likely(!HTTP_IS_LWS(*ptr)))
1077 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1078
1079 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001081 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1082 }
1083
1084 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001086 http_msg_rsp_reason:
1087 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001088 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 msg->sl.st.r_l = 0;
1090 goto http_msg_rpline_eol;
1091
Willy Tarreau8973c702007-01-21 23:58:29 +01001092 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001093 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001094 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001095 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001096 goto http_msg_rpreason;
1097 }
1098 if (likely(HTTP_IS_SPHT(*ptr)))
1099 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1100 /* so it's a CR/LF, so there is no reason phrase */
1101 goto http_msg_rsp_reason;
1102
Willy Tarreau8973c702007-01-21 23:58:29 +01001103 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001104 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 if (likely(!HTTP_IS_CRLF(*ptr)))
1106 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001107 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001108 http_msg_rpline_eol:
1109 /* We have seen the end of line. Note that we do not
1110 * necessarily have the \n yet, but at least we know that we
1111 * have EITHER \r OR \n, otherwise the response would not be
1112 * complete. We can then record the response length and return
1113 * to the caller which will be able to register it.
1114 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001115 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 return ptr;
1117
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001119#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001120 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1121 exit(1);
1122#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001123 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 }
1125
1126 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001127 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 if (ret_state)
1129 *ret_state = state;
1130 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001131 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001132 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133}
1134
Willy Tarreau8973c702007-01-21 23:58:29 +01001135/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 * This function parses a request line between <ptr> and <end>, starting with
1137 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1138 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1139 * will give undefined results.
1140 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1141 * and that msg->sol points to the beginning of the request.
1142 * If a complete line is found (which implies that at least one CR or LF is
1143 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1144 * returned indicating an incomplete line (which does not mean that parts have
1145 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1146 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1147 * upon next call.
1148 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001149 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001150 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1151 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001152 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001154const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001155 enum ht_state state, const char *ptr, const char *end,
1156 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001157{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001158 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001159
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001160 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001162 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 if (likely(HTTP_IS_TOKEN(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001167 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1169 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 if (likely(HTTP_IS_CRLF(*ptr))) {
1172 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001173 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001177 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001179 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 msg->sl.rq.v_l = 0;
1181 goto http_msg_rqline_eol;
1182 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001183 state = HTTP_MSG_ERROR;
1184 break;
1185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001187 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001189 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190 goto http_msg_rquri;
1191 }
1192 if (likely(HTTP_IS_SPHT(*ptr)))
1193 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1194 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1195 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001198 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001199 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001203 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001206
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001207 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001208 /* non-ASCII chars are forbidden unless option
1209 * accept-invalid-http-request is enabled in the frontend.
1210 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001211 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001212 if (msg->err_pos < -1)
1213 goto invalid_char;
1214 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001215 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001216 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1217 }
1218
1219 if (likely(HTTP_IS_CRLF(*ptr))) {
1220 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1221 goto http_msg_req09_uri_e;
1222 }
1223
1224 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001225 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001226 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001227 state = HTTP_MSG_ERROR;
1228 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001231 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001232 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001233 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 goto http_msg_rqver;
1235 }
1236 if (likely(HTTP_IS_SPHT(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1238 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1239 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001242 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001243 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001245
1246 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001248 http_msg_rqline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the request would not be
1252 * complete. We can then record the request length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001256 return ptr;
1257 }
1258
1259 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001260 state = HTTP_MSG_ERROR;
1261 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001262
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001264#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1266 exit(1);
1267#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001268 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001272 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 if (ret_state)
1274 *ret_state = state;
1275 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001276 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001280/*
1281 * Returns the data from Authorization header. Function may be called more
1282 * than once so data is stored in txn->auth_data. When no header is found
1283 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1284 * searching again for something we are unable to find anyway.
1285 */
1286
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001287char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001288
1289int
1290get_http_auth(struct session *s)
1291{
1292
1293 struct http_txn *txn = &s->txn;
1294 struct chunk auth_method;
1295 struct hdr_ctx ctx;
1296 char *h, *p;
1297 int len;
1298
1299#ifdef DEBUG_AUTH
1300 printf("Auth for session %p: %d\n", s, txn->auth.method);
1301#endif
1302
1303 if (txn->auth.method == HTTP_AUTH_WRONG)
1304 return 0;
1305
1306 if (txn->auth.method)
1307 return 1;
1308
1309 txn->auth.method = HTTP_AUTH_WRONG;
1310
1311 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001312
1313 if (txn->flags & TX_USE_PX_CONN) {
1314 h = "Proxy-Authorization";
1315 len = strlen(h);
1316 } else {
1317 h = "Authorization";
1318 len = strlen(h);
1319 }
1320
Willy Tarreau9b28e032012-10-12 23:49:43 +02001321 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001322 return 0;
1323
1324 h = ctx.line + ctx.val;
1325
1326 p = memchr(h, ' ', ctx.vlen);
1327 if (!p || p == h)
1328 return 0;
1329
1330 chunk_initlen(&auth_method, h, 0, p-h);
1331 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1332
1333 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1334
1335 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001336 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001337
1338 if (len < 0)
1339 return 0;
1340
1341
1342 get_http_auth_buff[len] = '\0';
1343
1344 p = strchr(get_http_auth_buff, ':');
1345
1346 if (!p)
1347 return 0;
1348
1349 txn->auth.user = get_http_auth_buff;
1350 *p = '\0';
1351 txn->auth.pass = p+1;
1352
1353 txn->auth.method = HTTP_AUTH_BASIC;
1354 return 1;
1355 }
1356
1357 return 0;
1358}
1359
Willy Tarreau58f10d72006-12-04 02:26:12 +01001360
Willy Tarreau8973c702007-01-21 23:58:29 +01001361/*
1362 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001363 * depending on the initial msg->msg_state. The caller is responsible for
1364 * ensuring that the message does not wrap. The function can be preempted
1365 * everywhere when data are missing and recalled at the exact same location
1366 * with no information loss. The message may even be realigned between two
1367 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001368 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001369 * fields. Note that msg->sol will be initialized after completing the first
1370 * state, so that none of the msg pointers has to be initialized prior to the
1371 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001373void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374{
Willy Tarreau3770f232013-12-07 00:01:53 +01001375 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001379 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001380 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001381 ptr = buf->p + msg->next;
1382 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (unlikely(ptr >= end))
1385 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001388 /*
1389 * First, states that are specific to the response only.
1390 * We check them first so that request and headers are
1391 * closer to each other (accessed more often).
1392 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001394 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001396 /* we have a start of message, but we have to check
1397 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001398 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001399 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001400 if (unlikely(ptr != buf->p)) {
1401 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001402 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001403 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001404 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001405 }
Willy Tarreau26927362012-05-18 23:22:52 +02001406 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001407 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 hdr_idx_init(idx);
1409 state = HTTP_MSG_RPVER;
1410 goto http_msg_rpver;
1411 }
1412
1413 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1414 goto http_msg_invalid;
1415
1416 if (unlikely(*ptr == '\n'))
1417 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1419 /* stop here */
1420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001423 EXPECT_LF_HERE(ptr, http_msg_invalid);
1424 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1425 /* stop here */
1426
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001428 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 case HTTP_MSG_RPVER_SP:
1430 case HTTP_MSG_RPCODE:
1431 case HTTP_MSG_RPCODE_SP:
1432 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001433 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001434 state, ptr, end,
1435 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 if (unlikely(!ptr))
1437 return;
1438
1439 /* we have a full response and we know that we have either a CR
1440 * or an LF at <ptr>.
1441 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1443
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001444 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001445 if (likely(*ptr == '\r'))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1447 goto http_msg_rpline_end;
1448
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001450 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 /* msg->sol must point to the first of CR or LF. */
1452 EXPECT_LF_HERE(ptr, http_msg_invalid);
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1454 /* stop here */
1455
1456 /*
1457 * Second, states that are specific to the request only
1458 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001460 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001462 /* we have a start of message, but we have to check
1463 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001464 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001465 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 if (likely(ptr != buf->p)) {
1467 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001468 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001469 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 }
Willy Tarreau26927362012-05-18 23:22:52 +02001472 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001473 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001474 state = HTTP_MSG_RQMETH;
1475 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1479 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 if (unlikely(*ptr == '\n'))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1483 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 case HTTP_MSG_RQMETH_SP:
1495 case HTTP_MSG_RQURI:
1496 case HTTP_MSG_RQURI_SP:
1497 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001498 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001499 state, ptr, end,
1500 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 if (unlikely(!ptr))
1502 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 /* we have a full request and we know that we have either a CR
1505 * or an LF at <ptr>.
1506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001509 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 if (likely(*ptr == '\r'))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001515 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 /* check for HTTP/0.9 request : no version information available.
1517 * msg->sol must point to the first of CR or LF.
1518 */
1519 if (unlikely(msg->sl.rq.v_l == 0))
1520 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8973c702007-01-21 23:58:29 +01001526 /*
1527 * Common states below
1528 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001530 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001531 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (likely(!HTTP_IS_CRLF(*ptr))) {
1533 goto http_msg_hdr_name;
1534 }
1535
1536 if (likely(*ptr == '\r'))
1537 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1538 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001541 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 /* assumes msg->sol points to the first char */
1543 if (likely(HTTP_IS_TOKEN(*ptr)))
1544 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001546 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001549 if (likely(msg->err_pos < -1) || *ptr == '\n')
1550 goto http_msg_invalid;
1551
1552 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001553 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001554
1555 /* and we still accept this non-token character */
1556 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001559 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001560 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(HTTP_IS_SPHT(*ptr)))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001565 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (likely(!HTTP_IS_CRLF(*ptr))) {
1568 goto http_msg_hdr_val;
1569 }
1570
1571 if (likely(*ptr == '\r'))
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1573 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001581 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 if (likely(HTTP_IS_SPHT(*ptr))) {
1583 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 for (; buf->p + msg->sov < ptr; msg->sov++)
1585 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 goto http_msg_hdr_l1_sp;
1587 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001588 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001589 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_complete_header;
1591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001594 /* assumes msg->sol points to the first char, and msg->sov
1595 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 */
1597 if (likely(!HTTP_IS_CRLF(*ptr)))
1598 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001600 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 /* Note: we could also copy eol into ->eoh so that we have the
1602 * real header end in case it ends with lots of LWS, but is this
1603 * really needed ?
1604 */
1605 if (likely(*ptr == '\r'))
1606 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1607 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001610 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001615 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1617 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001618 for (; buf->p + msg->eol < ptr; msg->eol++)
1619 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 goto http_msg_hdr_val;
1621 }
1622 http_msg_complete_header:
1623 /*
1624 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001625 * Assumes msg->sol points to the first char, msg->sov points
1626 * to the first character of the value and msg->eol to the
1627 * first CR or LF so we know how the line ends. We insert last
1628 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 idx, idx->tail) < 0))
1632 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001633
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001634 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 if (likely(!HTTP_IS_CRLF(*ptr))) {
1636 goto http_msg_hdr_name;
1637 }
1638
1639 if (likely(*ptr == '\r'))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1641 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001644 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 /* Assumes msg->sol points to the first of either CR or LF */
1646 EXPECT_LF_HERE(ptr, http_msg_invalid);
1647 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001648 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001649 msg->eoh = msg->sol;
1650 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001651 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001653
1654 case HTTP_MSG_ERROR:
1655 /* this may only happen if we call http_msg_analyser() twice with an error */
1656 break;
1657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001659#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1661 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001662#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001663 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 }
1665 http_msg_ood:
1666 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001668 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 http_msg_invalid:
1672 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001673 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001674 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 return;
1676}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001677
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001678/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1679 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1680 * nothing is done and 1 is returned.
1681 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001682static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001683{
1684 int delta;
1685 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001686 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001687
1688 if (msg->sl.rq.v_l != 0)
1689 return 1;
1690
Willy Tarreau9b28e032012-10-12 23:49:43 +02001691 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001692 delta = 0;
1693
1694 if (msg->sl.rq.u_l == 0) {
1695 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001696 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001697 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001698 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001699 }
1700 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001701 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001702 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001703 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001704 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001705 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001706 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001707 NULL, NULL);
1708 if (unlikely(!cur_end))
1709 return 0;
1710
1711 /* we have a full HTTP/1.0 request now and we know that
1712 * we have either a CR or an LF at <ptr>.
1713 */
1714 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1715 return 1;
1716}
1717
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001719 * and "keep-alive" values. If we already know that some headers may safely
1720 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001721 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1722 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001723 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001724 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1725 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1726 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001727 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001728 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001729void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001730{
Willy Tarreau5b154472009-12-21 20:11:07 +01001731 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001732 const char *hdr_val = "Connection";
1733 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001734
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001735 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001736 return;
1737
Willy Tarreau88d349d2010-01-25 12:15:43 +01001738 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1739 hdr_val = "Proxy-Connection";
1740 hdr_len = 16;
1741 }
1742
Willy Tarreau5b154472009-12-21 20:11:07 +01001743 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001744 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001745 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001746 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1747 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001748 if (to_del & 2)
1749 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001750 else
1751 txn->flags |= TX_CON_KAL_SET;
1752 }
1753 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1754 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001755 if (to_del & 1)
1756 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001757 else
1758 txn->flags |= TX_CON_CLO_SET;
1759 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001760 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1761 txn->flags |= TX_HDR_CONN_UPG;
1762 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001763 }
1764
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001765 txn->flags |= TX_HDR_CONN_PRS;
1766 return;
1767}
Willy Tarreau5b154472009-12-21 20:11:07 +01001768
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001769/* Apply desired changes on the Connection: header. Values may be removed and/or
1770 * added depending on the <wanted> flags, which are exclusively composed of
1771 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1772 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1773 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001774void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001775{
1776 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001777 const char *hdr_val = "Connection";
1778 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001779
1780 ctx.idx = 0;
1781
Willy Tarreau88d349d2010-01-25 12:15:43 +01001782
1783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1784 hdr_val = "Proxy-Connection";
1785 hdr_len = 16;
1786 }
1787
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001788 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001789 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001790 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1791 if (wanted & TX_CON_KAL_SET)
1792 txn->flags |= TX_CON_KAL_SET;
1793 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001794 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001795 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001796 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1797 if (wanted & TX_CON_CLO_SET)
1798 txn->flags |= TX_CON_CLO_SET;
1799 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001801 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001802 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803
1804 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1805 return;
1806
1807 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1808 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001809 hdr_val = "Connection: close";
1810 hdr_len = 17;
1811 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1812 hdr_val = "Proxy-Connection: close";
1813 hdr_len = 23;
1814 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001815 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001816 }
1817
1818 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1819 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001820 hdr_val = "Connection: keep-alive";
1821 hdr_len = 22;
1822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection: keep-alive";
1824 hdr_len = 28;
1825 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001826 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001827 }
1828 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001829}
1830
Willy Tarreaua458b672012-03-05 11:17:50 +01001831/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001833 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001834 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001836 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001837static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001838{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001839 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001841 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001842 const char *end = buf->data + buf->size;
1843 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001844 unsigned int chunk = 0;
1845
1846 /* The chunk size is in the following form, though we are only
1847 * interested in the size and CRLF :
1848 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1849 */
1850 while (1) {
1851 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001852 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001853 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001855 if (c < 0) /* not a hex digit anymore */
1856 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001857 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001858 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001859 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001860 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001861 chunk = (chunk << 4) + c;
1862 }
1863
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001865 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001866 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867
1868 while (http_is_spht[(unsigned char)*ptr]) {
1869 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001870 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001871 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001872 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001873 }
1874
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 /* Up to there, we know that at least one byte is present at *ptr. Check
1876 * for the end of chunk size.
1877 */
1878 while (1) {
1879 if (likely(HTTP_IS_CRLF(*ptr))) {
1880 /* we now have a CR or an LF at ptr */
1881 if (likely(*ptr == '\r')) {
1882 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001883 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001884 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 return 0;
1886 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001887
Willy Tarreaud98cf932009-12-27 22:54:55 +01001888 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001889 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001890 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001891 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001892 /* done */
1893 break;
1894 }
1895 else if (*ptr == ';') {
1896 /* chunk extension, ends at next CRLF */
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 Tarreau115acb92009-12-26 13:56:06 +01001900 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901
1902 while (!HTTP_IS_CRLF(*ptr)) {
1903 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001905 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 return 0;
1907 }
1908 /* we have a CRLF now, loop above */
1909 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001910 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001912 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001913 }
1914
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001916 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001919 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001920 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001921 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001923 msg->chunk_len = chunk;
1924 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001927 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001929 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001930}
1931
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001932/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001933 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 * the trailers is found, it is automatically scheduled to be forwarded,
1935 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1936 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001937 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001938 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001939 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001940 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1941 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001942 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001943 * matches the length of trailers already parsed and not forwarded. It is also
1944 * important to note that this function is designed to be able to parse wrapped
1945 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001947static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001949 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001950
Willy Tarreaua458b672012-03-05 11:17:50 +01001951 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001953 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 const char *ptr = b_ptr(buf, msg->next);
1955 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001956 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957
1958 /* scan current line and stop at LF or CRLF */
1959 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001960 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 return 0;
1962
1963 if (*ptr == '\n') {
1964 if (!p1)
1965 p1 = ptr;
1966 p2 = ptr;
1967 break;
1968 }
1969
1970 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001971 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001972 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001973 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001974 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 p1 = ptr;
1976 }
1977
1978 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001979 if (ptr >= buf->data + buf->size)
1980 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001981 }
1982
1983 /* after LF; point to beginning of next line */
1984 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 if (p2 >= buf->data + buf->size)
1986 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001989 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001991
1992 /* schedule this line for forwarding */
1993 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001994 if (msg->sov >= buf->size)
1995 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001996
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001997 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001998 /* LF/CRLF at beginning of line => end of trailers at p2.
1999 * Everything was scheduled for forwarding, there's nothing
2000 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002001 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003 msg->msg_state = HTTP_MSG_DONE;
2004 return 1;
2005 }
2006 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002007 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 }
2009}
2010
Willy Tarreau54d23df2012-10-25 19:04:45 +02002011/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002013 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002014 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002015 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2016 * not enough data are available, the function does not change anything and
2017 * returns zero. If a parse error is encountered, the function returns < 0 and
2018 * does not change anything. Note: this function is designed to parse wrapped
2019 * CRLF at the end of the buffer.
2020 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002021static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002022{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002023 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002024 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 int bytes;
2026
2027 /* NB: we'll check data availabilty at the end. It's not a
2028 * problem because whatever we match first will be checked
2029 * against the correct length.
2030 */
2031 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002032 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 if (*ptr == '\r') {
2034 bytes++;
2035 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002036 if (ptr >= buf->data + buf->size)
2037 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 }
2039
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041 return 0;
2042
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002043 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002046 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047
2048 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002049 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002050 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002051 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2052 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002053 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002054 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2055 return 1;
2056}
Willy Tarreau5b154472009-12-21 20:11:07 +01002057
William Lallemand82fe75c2012-10-23 10:25:10 +02002058
2059/*
2060 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002061 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002062int select_compression_request_header(struct session *s, struct buffer *req)
2063{
2064 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002065 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002066 struct hdr_ctx ctx;
2067 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002068 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002069
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002070 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2071 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002072 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2073 */
2074 ctx.idx = 0;
2075 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2076 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002077 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2078 (ctx.vlen < 31 ||
2079 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2080 ctx.line[ctx.val + 30] < '6' ||
2081 (ctx.line[ctx.val + 30] == '6' &&
2082 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2083 s->comp_algo = NULL;
2084 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002085 }
2086
William Lallemand82fe75c2012-10-23 10:25:10 +02002087 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002088 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 +02002089 ctx.idx = 0;
2090 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002091 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002092 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2093 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002094
2095 /* remove all occurrences of the header when "compression offload" is set */
2096
2097 if ((s->be->comp && s->be->comp->offload) ||
2098 (s->fe->comp && s->fe->comp->offload)) {
2099 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2100 ctx.idx = 0;
2101 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2102 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2103 }
2104 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002105 return 1;
2106 }
2107 }
2108 }
2109 }
2110
2111 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002112 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2113 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002114 if (comp_algo->add_data == identity_add_data) {
2115 s->comp_algo = comp_algo;
2116 return 1;
2117 }
2118 }
2119 }
2120
2121 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002122 return 0;
2123}
2124
2125/*
2126 * Selects a comression algorithm depending of the server response.
2127 */
2128int select_compression_response_header(struct session *s, struct buffer *res)
2129{
2130 struct http_txn *txn = &s->txn;
2131 struct http_msg *msg = &txn->rsp;
2132 struct hdr_ctx ctx;
2133 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002134
2135 /* no common compression algorithm was found in request header */
2136 if (s->comp_algo == NULL)
2137 goto fail;
2138
2139 /* HTTP < 1.1 should not be compressed */
2140 if (!(msg->flags & HTTP_MSGF_VER_11))
2141 goto fail;
2142
William Lallemandd3002612012-11-26 14:34:47 +01002143 /* 200 only */
2144 if (txn->status != 200)
2145 goto fail;
2146
William Lallemand82fe75c2012-10-23 10:25:10 +02002147 /* Content-Length is null */
2148 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2149 goto fail;
2150
Willy Tarreau667c2a32013-04-09 08:13:58 +02002151 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2152 if (msg->flags & HTTP_MSGF_TE_CHNK)
2153 goto fail;
2154
William Lallemand82fe75c2012-10-23 10:25:10 +02002155 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002156 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002157 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2158 goto fail;
2159
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002160 /* no compression when Cache-Control: no-transform is present in the message */
2161 ctx.idx = 0;
2162 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2163 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2164 goto fail;
2165 }
2166
William Lallemand82fe75c2012-10-23 10:25:10 +02002167 comp_type = NULL;
2168
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002169 /* we don't want to compress multipart content-types, nor content-types that are
2170 * not listed in the "compression type" directive if any. If no content-type was
2171 * found but configuration requires one, we don't compress either. Backend has
2172 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002173 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002174 ctx.idx = 0;
2175 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2176 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2177 goto fail;
2178
2179 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2180 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002181 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002182 if (ctx.vlen >= comp_type->name_len &&
2183 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002184 /* this Content-Type should be compressed */
2185 break;
2186 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002187 /* this Content-Type should not be compressed */
2188 if (comp_type == NULL)
2189 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002190 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002191 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002192 else { /* no content-type header */
2193 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2194 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002195 }
2196
William Lallemandd85f9172012-11-09 17:05:39 +01002197 /* limit compression rate */
2198 if (global.comp_rate_lim > 0)
2199 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2200 goto fail;
2201
William Lallemand072a2bf2012-11-20 17:01:01 +01002202 /* limit cpu usage */
2203 if (idle_pct < compress_min_idle)
2204 goto fail;
2205
William Lallemand4c49fae2012-11-07 15:00:23 +01002206 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002207 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002208 goto fail;
2209
William Lallemandec3e3892012-11-12 17:02:18 +01002210 s->flags |= SN_COMP_READY;
2211
William Lallemand82fe75c2012-10-23 10:25:10 +02002212 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002213 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002214 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2215 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2216
2217 /* add Transfer-Encoding header */
2218 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2219 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2220
2221 /*
2222 * Add Content-Encoding header when it's not identity encoding.
2223 * RFC 2616 : Identity encoding: This content-coding is used only in the
2224 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2225 * header.
2226 */
2227 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002228 trash.len = 18;
2229 memcpy(trash.str, "Content-Encoding: ", trash.len);
2230 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2231 trash.len += s->comp_algo->name_len;
2232 trash.str[trash.len] = '\0';
2233 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002234 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002235 return 1;
2236
2237fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002238 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002239 return 0;
2240}
2241
2242
Willy Tarreaud787e662009-07-07 10:14:51 +02002243/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2244 * processing can continue on next analysers, or zero if it either needs more
2245 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2246 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2247 * when it has nothing left to do, and may remove any analyser when it wants to
2248 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002249 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002250int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002251{
Willy Tarreau59234e92008-11-30 23:51:27 +01002252 /*
2253 * We will parse the partial (or complete) lines.
2254 * We will check the request syntax, and also join multi-line
2255 * headers. An index of all the lines will be elaborated while
2256 * parsing.
2257 *
2258 * For the parsing, we use a 28 states FSM.
2259 *
2260 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002261 * req->buf->p = beginning of request
2262 * req->buf->p + msg->eoh = end of processed headers / start of current one
2263 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002264 * msg->eol = end of current header or line (LF or CRLF)
2265 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002266 *
2267 * At end of parsing, we may perform a capture of the error (if any), and
2268 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2269 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2270 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002271 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002274 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 struct http_txn *txn = &s->txn;
2276 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002277 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002278
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002279 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 +01002280 now_ms, __FUNCTION__,
2281 s,
2282 req,
2283 req->rex, req->wex,
2284 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002285 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002286 req->analysers);
2287
Willy Tarreau52a0c602009-08-16 22:45:38 +02002288 /* we're speaking HTTP here, so let's speak HTTP to the client */
2289 s->srv_error = http_return_srv_error;
2290
Willy Tarreau83e3af02009-12-28 17:39:57 +01002291 /* There's a protected area at the end of the buffer for rewriting
2292 * purposes. We don't want to start to parse the request if the
2293 * protected area is affected, because we may have to move processed
2294 * data later, which is much more complicated.
2295 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002296 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002297 if (txn->flags & TX_NOT_FIRST) {
2298 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002299 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002300 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002301 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002302 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002303 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002304 return 0;
2305 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002306 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2307 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2308 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002309 }
2310
Willy Tarreau065e8332010-01-08 00:30:20 +01002311 /* Note that we have the same problem with the response ; we
2312 * may want to send a redirect, error or anything which requires
2313 * some spare space. So we'll ensure that we have at least
2314 * maxrewrite bytes available in the response buffer before
2315 * processing that one. This will only affect pipelined
2316 * keep-alive requests.
2317 */
2318 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002319 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002320 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2321 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2322 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002323 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002324 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002325 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002326 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002327 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002328 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002329 return 0;
2330 }
2331 }
2332
Willy Tarreau9b28e032012-10-12 23:49:43 +02002333 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002334 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002335 }
2336
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 /* 1: we might have to print this header in debug mode */
2338 if (unlikely((global.mode & MODE_DEBUG) &&
2339 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002340 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342
Willy Tarreau9b28e032012-10-12 23:49:43 +02002343 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002344 /* this is a bit complex : in case of error on the request line,
2345 * we know that rq.l is still zero, so we display only the part
2346 * up to the end of the line (truncated by debug_hdr).
2347 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002348 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 sol += hdr_idx_first_pos(&txn->hdr_idx);
2352 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002353
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 while (cur_idx) {
2355 eol = sol + txn->hdr_idx.v[cur_idx].len;
2356 debug_hdr("clihdr", s, sol, eol);
2357 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2358 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 }
2361
Willy Tarreau58f10d72006-12-04 02:26:12 +01002362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 /*
2364 * Now we quickly check if we have found a full valid request.
2365 * If not so, we check the FD and buffer states before leaving.
2366 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002367 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002368 * requests are checked first. When waiting for a second request
2369 * on a keep-alive session, if we encounter and error, close, t/o,
2370 * we note the error in the session flags but don't set any state.
2371 * Since the error will be noted there, it will not be counted by
2372 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002373 * Last, we may increase some tracked counters' http request errors on
2374 * the cases that are deliberately the client's fault. For instance,
2375 * a timeout or connection reset is not counted as an error. However
2376 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002378
Willy Tarreau655dce92009-11-08 13:10:58 +01002379 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002380 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002382 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002383 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002384 session_inc_http_req_ctr(s);
2385 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002386 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002388 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* 1: Since we are in header mode, if there's no space
2391 * left for headers, we won't be able to free more
2392 * later, so the session will never terminate. We
2393 * must terminate it now.
2394 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002395 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 /* FIXME: check if URI is set and return Status
2397 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002398 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002399 session_inc_http_req_ctr(s);
2400 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002401 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002402 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002403 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 goto return_bad_req;
2405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002408 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002409 if (!(s->flags & SN_ERR_MASK))
2410 s->flags |= SN_ERR_CLICL;
2411
Willy Tarreaufcffa692010-01-10 14:21:19 +01002412 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002413 goto failed_keep_alive;
2414
Willy Tarreau59234e92008-11-30 23:51:27 +01002415 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002416 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002417 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002418 session_inc_http_err_ctr(s);
2419 }
2420
Willy Tarreaudc979f22012-12-04 10:39:01 +01002421 txn->status = 400;
2422 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 msg->msg_state = HTTP_MSG_ERROR;
2424 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002425
Willy Tarreauda7ff642010-06-23 11:44:09 +02002426 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002427 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002428 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002429 if (s->listener->counters)
2430 s->listener->counters->failed_req++;
2431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 if (!(s->flags & SN_FINST_MASK))
2433 s->flags |= SN_FINST_R;
2434 return 0;
2435 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002438 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002439 if (!(s->flags & SN_ERR_MASK))
2440 s->flags |= SN_ERR_CLITO;
2441
Willy Tarreaufcffa692010-01-10 14:21:19 +01002442 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002443 goto failed_keep_alive;
2444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002446 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002447 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002448 session_inc_http_err_ctr(s);
2449 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002450 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002451 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 msg->msg_state = HTTP_MSG_ERROR;
2453 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002454
Willy Tarreauda7ff642010-06-23 11:44:09 +02002455 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002456 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002457 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002458 if (s->listener->counters)
2459 s->listener->counters->failed_req++;
2460
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 if (!(s->flags & SN_FINST_MASK))
2462 s->flags |= SN_FINST_R;
2463 return 0;
2464 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002465
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002467 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002468 if (!(s->flags & SN_ERR_MASK))
2469 s->flags |= SN_ERR_CLICL;
2470
Willy Tarreaufcffa692010-01-10 14:21:19 +01002471 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002472 goto failed_keep_alive;
2473
Willy Tarreau4076a152009-04-02 15:18:36 +02002474 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002475 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002477 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 msg->msg_state = HTTP_MSG_ERROR;
2479 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480
Willy Tarreauda7ff642010-06-23 11:44:09 +02002481 session_inc_http_err_ctr(s);
2482 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002483 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002484 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002485 if (s->listener->counters)
2486 s->listener->counters->failed_req++;
2487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 if (!(s->flags & SN_FINST_MASK))
2489 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002490 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002491 }
2492
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002493 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002494 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2495 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002496#ifdef TCP_QUICKACK
Willy Tarreauf79c8172013-10-21 16:30:56 +02002497 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && (__objt_conn(s->req->prod->end)->flags & CO_FL_CTRL_READY)) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002498 /* We need more data, we have to re-enable quick-ack in case we
2499 * previously disabled it, otherwise we might cause the client
2500 * to delay next data.
2501 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002502 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002503 }
2504#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002505
Willy Tarreaufcffa692010-01-10 14:21:19 +01002506 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2507 /* If the client starts to talk, let's fall back to
2508 * request timeout processing.
2509 */
2510 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002511 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002512 }
2513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002515 if (!tick_isset(req->analyse_exp)) {
2516 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2517 (txn->flags & TX_WAIT_NEXT_RQ) &&
2518 tick_isset(s->be->timeout.httpka))
2519 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2520 else
2521 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* we're not ready yet */
2525 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002526
2527 failed_keep_alive:
2528 /* Here we process low-level errors for keep-alive requests. In
2529 * short, if the request is not the first one and it experiences
2530 * a timeout, read error or shutdown, we just silently close so
2531 * that the client can try again.
2532 */
2533 txn->status = 0;
2534 msg->msg_state = HTTP_MSG_RQBEFORE;
2535 req->analysers = 0;
2536 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002537 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002538 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002539 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002540 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002542
Willy Tarreaud787e662009-07-07 10:14:51 +02002543 /* OK now we have a complete HTTP request with indexed headers. Let's
2544 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002545 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002546 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002547 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002548 * byte after the last LF. msg->sov points to the first byte of data.
2549 * msg->eol cannot be trusted because it may have been left uninitialized
2550 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002551 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002552
Willy Tarreauda7ff642010-06-23 11:44:09 +02002553 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002554 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2555
Willy Tarreaub16a5742010-01-10 14:46:16 +01002556 if (txn->flags & TX_WAIT_NEXT_RQ) {
2557 /* kill the pending keep-alive timeout */
2558 txn->flags &= ~TX_WAIT_NEXT_RQ;
2559 req->analyse_exp = TICK_ETERNITY;
2560 }
2561
2562
Willy Tarreaud787e662009-07-07 10:14:51 +02002563 /* Maybe we found in invalid header name while we were configured not
2564 * to block on that, so we have to capture it now.
2565 */
2566 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002567 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 /*
2570 * 1: identify the method
2571 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002572 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002573
2574 /* we can make use of server redirect on GET and HEAD */
2575 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2576 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002577
Willy Tarreau59234e92008-11-30 23:51:27 +01002578 /*
2579 * 2: check if the URI matches the monitor_uri.
2580 * We have to do this for every request which gets in, because
2581 * the monitor-uri is defined by the frontend.
2582 */
2583 if (unlikely((s->fe->monitor_uri_len != 0) &&
2584 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002585 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 s->fe->monitor_uri,
2587 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002588 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002590 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002594 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002595
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002597 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002598 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 ret = acl_pass(ret);
2601 if (cond->pol == ACL_COND_UNLESS)
2602 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 if (ret) {
2605 /* we fail this request, let's return 503 service unavail */
2606 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002607 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002608 if (!(s->flags & SN_ERR_MASK))
2609 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002611 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002612 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002613
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 /* nothing to fail, let's reply normaly */
2615 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002616 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002617 if (!(s->flags & SN_ERR_MASK))
2618 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 goto return_prx_cond;
2620 }
2621
2622 /*
2623 * 3: Maybe we have to copy the original REQURI for the logs ?
2624 * Note: we cannot log anymore if the request has been
2625 * classified as invalid.
2626 */
2627 if (unlikely(s->logs.logwait & LW_REQ)) {
2628 /* we have a complete HTTP request that we must log */
2629 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2630 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 if (urilen >= REQURI_LEN)
2633 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002634 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002636
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002637 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 s->do_log(s);
2639 } else {
2640 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002641 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 }
Willy Tarreau06619262006-12-17 08:37:22 +01002643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002645 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002646 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647
Willy Tarreau5b154472009-12-21 20:11:07 +01002648 /* ... and check if the request is HTTP/1.1 or above */
2649 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002650 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2651 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2652 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002653 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002654
2655 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002656 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 +01002657
Willy Tarreau88d349d2010-01-25 12:15:43 +01002658 /* if the frontend has "option http-use-proxy-header", we'll check if
2659 * we have what looks like a proxied connection instead of a connection,
2660 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2661 * Note that this is *not* RFC-compliant, however browsers and proxies
2662 * happen to do that despite being non-standard :-(
2663 * We consider that a request not beginning with either '/' or '*' is
2664 * a proxied connection, which covers both "scheme://location" and
2665 * CONNECT ip:port.
2666 */
2667 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002668 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002669 txn->flags |= TX_USE_PX_CONN;
2670
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002671 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002672 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002673
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002675 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002678
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002679 /* 6: determine the transfer-length.
2680 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2681 * the presence of a message-body in a REQUEST and its transfer length
2682 * must be determined that way (in order of precedence) :
2683 * 1. The presence of a message-body in a request is signaled by the
2684 * inclusion of a Content-Length or Transfer-Encoding header field
2685 * in the request's header fields. When a request message contains
2686 * both a message-body of non-zero length and a method that does
2687 * not define any semantics for that request message-body, then an
2688 * origin server SHOULD either ignore the message-body or respond
2689 * with an appropriate error message (e.g., 413). A proxy or
2690 * gateway, when presented the same request, SHOULD either forward
2691 * the request inbound with the message- body or ignore the
2692 * message-body when determining a response.
2693 *
2694 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2695 * and the "chunked" transfer-coding (Section 6.2) is used, the
2696 * transfer-length is defined by the use of this transfer-coding.
2697 * If a Transfer-Encoding header field is present and the "chunked"
2698 * transfer-coding is not present, the transfer-length is defined
2699 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002700 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002701 * 3. If a Content-Length header field is present, its decimal value in
2702 * OCTETs represents both the entity-length and the transfer-length.
2703 * If a message is received with both a Transfer-Encoding header
2704 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002705 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002706 * 4. By the server closing the connection. (Closing the connection
2707 * cannot be used to indicate the end of a request body, since that
2708 * would leave no possibility for the server to send back a response.)
2709 *
2710 * Whenever a transfer-coding is applied to a message-body, the set of
2711 * transfer-codings MUST include "chunked", unless the message indicates
2712 * it is terminated by closing the connection. When the "chunked"
2713 * transfer-coding is used, it MUST be the last transfer-coding applied
2714 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002715 */
2716
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002717 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002718 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002719 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002720 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002721 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002722 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002723 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2724 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002725 /* bad transfer-encoding (chunked followed by something else) */
2726 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002727 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002728 break;
2729 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002730 }
2731
Willy Tarreau32b47f42009-10-18 20:55:02 +02002732 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002733 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002734 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002735 signed long long cl;
2736
Willy Tarreauad14f752011-09-02 20:33:27 +02002737 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002738 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002739 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002740 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002741
Willy Tarreauad14f752011-09-02 20:33:27 +02002742 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002743 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002744 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002745 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746
Willy Tarreauad14f752011-09-02 20:33:27 +02002747 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002748 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002749 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002750 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002751
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002752 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002753 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002754 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002755 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002756
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002757 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002758 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002759 }
2760
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002761 /* bodyless requests have a known length */
2762 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002763 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764
Willy Tarreaud787e662009-07-07 10:14:51 +02002765 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002766 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002767 req->analyse_exp = TICK_ETERNITY;
2768 return 1;
2769
2770 return_bad_req:
2771 /* We centralize bad requests processing here */
2772 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2773 /* we detected a parsing error. We want to archive this request
2774 * in the dedicated proxy area for later troubleshooting.
2775 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002776 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 }
2778
2779 txn->req.msg_state = HTTP_MSG_ERROR;
2780 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002781 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002782
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002783 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002784 if (s->listener->counters)
2785 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002786
2787 return_prx_cond:
2788 if (!(s->flags & SN_ERR_MASK))
2789 s->flags |= SN_ERR_PRXCOND;
2790 if (!(s->flags & SN_FINST_MASK))
2791 s->flags |= SN_FINST_R;
2792
2793 req->analysers = 0;
2794 req->analyse_exp = TICK_ETERNITY;
2795 return 0;
2796}
2797
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002798
Willy Tarreau347a35d2013-11-22 17:51:09 +01002799/* This function prepares an applet to handle the stats. It can deal with the
2800 * "100-continue" expectation, check that admin rules are met for POST requests,
2801 * and program a response message if something was unexpected. It cannot fail
2802 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002803 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002804 * s->target which is supposed to already point to the stats applet. The caller
2805 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002806 */
2807int http_handle_stats(struct session *s, struct channel *req)
2808{
2809 struct stats_admin_rule *stats_admin_rule;
2810 struct stream_interface *si = s->rep->prod;
2811 struct http_txn *txn = &s->txn;
2812 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002813 struct uri_auth *uri_auth = s->be->uri_auth;
2814 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002815 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002817 appctx = si_appctx(si);
2818 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2819 appctx->st1 = appctx->st2 = 0;
2820 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2821 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002822
2823 uri = msg->chn->buf->p + msg->sl.rq.u;
2824 lookup = uri + uri_auth->uri_len;
2825
2826 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2827 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002828 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002829 break;
2830 }
2831 }
2832
2833 if (uri_auth->refresh) {
2834 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2835 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002836 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002837 break;
2838 }
2839 }
2840 }
2841
2842 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2843 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002844 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002845 break;
2846 }
2847 }
2848
2849 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2850 if (memcmp(h, ";st=", 4) == 0) {
2851 int i;
2852 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002853 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002854 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2855 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002857 break;
2858 }
2859 }
2860 break;
2861 }
2862 }
2863
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002864 appctx->ctx.stats.scope_str = 0;
2865 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002866 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2867 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2868 int itx = 0;
2869 const char *h2;
2870 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2871 const char *err;
2872
2873 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2874 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002875 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002876 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2877 itx++;
2878 h++;
2879 }
2880
2881 if (itx > STAT_SCOPE_TXT_MAXLEN)
2882 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002883 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002884
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002885 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002886 memcpy(scope_txt, h2, itx);
2887 scope_txt[itx] = '\0';
2888 err = invalid_char(scope_txt);
2889 if (err) {
2890 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 appctx->ctx.stats.scope_str = 0;
2892 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002893 }
2894 break;
2895 }
2896 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002897
2898 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002899 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002900 int ret = 1;
2901
2902 if (stats_admin_rule->cond) {
2903 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2904 ret = acl_pass(ret);
2905 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2906 ret = !ret;
2907 }
2908
2909 if (ret) {
2910 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002911 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002912 break;
2913 }
2914 }
2915
2916 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002917 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002918 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002919 if (msg->msg_state < HTTP_MSG_100_SENT) {
2920 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2921 * send an HTTP/1.1 100 Continue intermediate response.
2922 */
2923 if (msg->flags & HTTP_MSGF_VER_11) {
2924 struct hdr_ctx ctx;
2925 ctx.idx = 0;
2926 /* Expect is allowed in 1.1, look for it */
2927 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2928 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2929 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2930 }
2931 }
2932 msg->msg_state = HTTP_MSG_100_SENT;
2933 s->logs.tv_request = now; /* update the request timer to reflect full request */
2934 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002935 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002936 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002937 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002938 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2939 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002940 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002941 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002942 else {
2943 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002945 }
2946
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002947 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002948 return 1;
2949}
2950
Lukas Tribus67db8df2013-06-23 17:37:13 +02002951/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2952 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2953 */
2954static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
2955{
2956#ifdef IP_TOS
2957 if (from.ss_family == AF_INET)
2958 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2959#endif
2960#ifdef IPV6_TCLASS
2961 if (from.ss_family == AF_INET6) {
2962 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
2963 /* v4-mapped addresses need IP_TOS */
2964 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2965 else
2966 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2967 }
2968#endif
2969}
2970
Willy Tarreau20b0de52012-12-24 15:45:22 +01002971/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01002972 * transaction <txn>. Returns the first rule that prevents further processing
2973 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
2974 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
2975 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002976 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01002977static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01002978http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002979{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002980 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01002981 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002982 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002983
Willy Tarreauff011f22011-01-06 17:51:27 +01002984 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002985 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002986 continue;
2987
Willy Tarreau96257ec2012-12-27 10:46:37 +01002988 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002990 int ret;
2991
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002992 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002993 ret = acl_pass(ret);
2994
Willy Tarreauff011f22011-01-06 17:51:27 +01002995 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002996 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002997
2998 if (!ret) /* condition not matched */
2999 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003000 }
3001
Willy Tarreau20b0de52012-12-24 15:45:22 +01003002
Willy Tarreau96257ec2012-12-27 10:46:37 +01003003 switch (rule->action) {
3004 case HTTP_REQ_ACT_ALLOW:
3005 return NULL; /* "allow" rules are OK */
3006
3007 case HTTP_REQ_ACT_DENY:
3008 txn->flags |= TX_CLDENY;
3009 return rule;
3010
Willy Tarreauccbcc372012-12-27 12:37:57 +01003011 case HTTP_REQ_ACT_TARPIT:
3012 txn->flags |= TX_CLTARPIT;
3013 return rule;
3014
Willy Tarreau96257ec2012-12-27 10:46:37 +01003015 case HTTP_REQ_ACT_AUTH:
3016 return rule;
3017
Willy Tarreau81499eb2012-12-27 12:19:02 +01003018 case HTTP_REQ_ACT_REDIR:
3019 return rule;
3020
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003021 case HTTP_REQ_ACT_SET_NICE:
3022 s->task->nice = rule->arg.nice;
3023 break;
3024
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003025 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003026 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003027 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003028 break;
3029
Willy Tarreau51347ed2013-06-11 19:34:13 +02003030 case HTTP_REQ_ACT_SET_MARK:
3031#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003032 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003033 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003034#endif
3035 break;
3036
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003037 case HTTP_REQ_ACT_SET_LOGL:
3038 s->logs.level = rule->arg.loglevel;
3039 break;
3040
Willy Tarreau96257ec2012-12-27 10:46:37 +01003041 case HTTP_REQ_ACT_SET_HDR:
3042 ctx.idx = 0;
3043 /* remove all occurrences of the header */
3044 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3045 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3046 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003047 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003048 /* now fall through to header addition */
3049
3050 case HTTP_REQ_ACT_ADD_HDR:
3051 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3052 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3053 trash.len = rule->arg.hdr_add.name_len;
3054 trash.str[trash.len++] = ':';
3055 trash.str[trash.len++] = ' ';
3056 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3057 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3058 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003059 }
3060 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003061
3062 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003063 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003064}
3065
Willy Tarreau71241ab2012-12-27 11:30:54 +01003066
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003067/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3068 * transaction <txn>. Returns the first rule that prevents further processing
3069 * of the response (deny, ...) or NULL if it executed all rules or stopped
3070 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3071 * rule.
3072 */
3073static struct http_res_rule *
3074http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3075{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003076 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003077 struct http_res_rule *rule;
3078 struct hdr_ctx ctx;
3079
3080 list_for_each_entry(rule, rules, list) {
3081 if (rule->action >= HTTP_RES_ACT_MAX)
3082 continue;
3083
3084 /* check optional condition */
3085 if (rule->cond) {
3086 int ret;
3087
3088 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3089 ret = acl_pass(ret);
3090
3091 if (rule->cond->pol == ACL_COND_UNLESS)
3092 ret = !ret;
3093
3094 if (!ret) /* condition not matched */
3095 continue;
3096 }
3097
3098
3099 switch (rule->action) {
3100 case HTTP_RES_ACT_ALLOW:
3101 return NULL; /* "allow" rules are OK */
3102
3103 case HTTP_RES_ACT_DENY:
3104 txn->flags |= TX_SVDENY;
3105 return rule;
3106
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003107 case HTTP_RES_ACT_SET_NICE:
3108 s->task->nice = rule->arg.nice;
3109 break;
3110
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003111 case HTTP_RES_ACT_SET_TOS:
Willy Tarreauf79c8172013-10-21 16:30:56 +02003112 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003113 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003114 break;
3115
Willy Tarreau51347ed2013-06-11 19:34:13 +02003116 case HTTP_RES_ACT_SET_MARK:
3117#ifdef SO_MARK
Willy Tarreauf79c8172013-10-21 16:30:56 +02003118 if ((cli_conn = objt_conn(s->req->prod->end)) && (cli_conn->flags & CO_FL_CTRL_READY))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003119 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003120#endif
3121 break;
3122
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003123 case HTTP_RES_ACT_SET_LOGL:
3124 s->logs.level = rule->arg.loglevel;
3125 break;
3126
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003127 case HTTP_RES_ACT_SET_HDR:
3128 ctx.idx = 0;
3129 /* remove all occurrences of the header */
3130 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3131 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3132 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3133 }
3134 /* now fall through to header addition */
3135
3136 case HTTP_RES_ACT_ADD_HDR:
3137 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3138 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3139 trash.len = rule->arg.hdr_add.name_len;
3140 trash.str[trash.len++] = ':';
3141 trash.str[trash.len++] = ' ';
3142 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3143 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3144 break;
3145 }
3146 }
3147
3148 /* we reached the end of the rules, nothing to report */
3149 return NULL;
3150}
3151
3152
Willy Tarreau71241ab2012-12-27 11:30:54 +01003153/* Perform an HTTP redirect based on the information in <rule>. The function
3154 * returns non-zero on success, or zero in case of a, irrecoverable error such
3155 * as too large a request to build a valid response.
3156 */
3157static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3158{
3159 struct http_msg *msg = &txn->req;
3160 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003161 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003162
3163 /* build redirect message */
3164 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003165 case 308:
3166 msg_fmt = HTTP_308;
3167 break;
3168 case 307:
3169 msg_fmt = HTTP_307;
3170 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003171 case 303:
3172 msg_fmt = HTTP_303;
3173 break;
3174 case 301:
3175 msg_fmt = HTTP_301;
3176 break;
3177 case 302:
3178 default:
3179 msg_fmt = HTTP_302;
3180 break;
3181 }
3182
3183 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3184 return 0;
3185
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003186 location = trash.str + trash.len;
3187
Willy Tarreau71241ab2012-12-27 11:30:54 +01003188 switch(rule->type) {
3189 case REDIRECT_TYPE_SCHEME: {
3190 const char *path;
3191 const char *host;
3192 struct hdr_ctx ctx;
3193 int pathlen;
3194 int hostlen;
3195
3196 host = "";
3197 hostlen = 0;
3198 ctx.idx = 0;
3199 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3200 host = ctx.line + ctx.val;
3201 hostlen = ctx.vlen;
3202 }
3203
3204 path = http_get_path(txn);
3205 /* build message using path */
3206 if (path) {
3207 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3208 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3209 int qs = 0;
3210 while (qs < pathlen) {
3211 if (path[qs] == '?') {
3212 pathlen = qs;
3213 break;
3214 }
3215 qs++;
3216 }
3217 }
3218 } else {
3219 path = "/";
3220 pathlen = 1;
3221 }
3222
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003223 if (rule->rdr_str) { /* this is an old "redirect" rule */
3224 /* check if we can add scheme + "://" + host + path */
3225 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3226 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003228 /* add scheme */
3229 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3230 trash.len += rule->rdr_len;
3231 }
3232 else {
3233 /* add scheme with executing log format */
3234 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003236 /* check if we can add scheme + "://" + host + path */
3237 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3238 return 0;
3239 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003240 /* add "://" */
3241 memcpy(trash.str + trash.len, "://", 3);
3242 trash.len += 3;
3243
3244 /* add host */
3245 memcpy(trash.str + trash.len, host, hostlen);
3246 trash.len += hostlen;
3247
3248 /* add path */
3249 memcpy(trash.str + trash.len, path, pathlen);
3250 trash.len += pathlen;
3251
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003252 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003253 if (trash.len && trash.str[trash.len - 1] != '/' &&
3254 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3255 if (trash.len > trash.size - 5)
3256 return 0;
3257 trash.str[trash.len] = '/';
3258 trash.len++;
3259 }
3260
3261 break;
3262 }
3263 case REDIRECT_TYPE_PREFIX: {
3264 const char *path;
3265 int pathlen;
3266
3267 path = http_get_path(txn);
3268 /* build message using path */
3269 if (path) {
3270 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3271 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3272 int qs = 0;
3273 while (qs < pathlen) {
3274 if (path[qs] == '?') {
3275 pathlen = qs;
3276 break;
3277 }
3278 qs++;
3279 }
3280 }
3281 } else {
3282 path = "/";
3283 pathlen = 1;
3284 }
3285
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003286 if (rule->rdr_str) { /* this is an old "redirect" rule */
3287 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3288 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003289
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003290 /* add prefix. Note that if prefix == "/", we don't want to
3291 * add anything, otherwise it makes it hard for the user to
3292 * configure a self-redirection.
3293 */
3294 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3295 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3296 trash.len += rule->rdr_len;
3297 }
3298 }
3299 else {
3300 /* add prefix with executing log format */
3301 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3302
3303 /* Check length */
3304 if (trash.len + pathlen > trash.size - 4)
3305 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003306 }
3307
3308 /* add path */
3309 memcpy(trash.str + trash.len, path, pathlen);
3310 trash.len += pathlen;
3311
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003312 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003313 if (trash.len && trash.str[trash.len - 1] != '/' &&
3314 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3315 if (trash.len > trash.size - 5)
3316 return 0;
3317 trash.str[trash.len] = '/';
3318 trash.len++;
3319 }
3320
3321 break;
3322 }
3323 case REDIRECT_TYPE_LOCATION:
3324 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003325 if (rule->rdr_str) { /* this is an old "redirect" rule */
3326 if (trash.len + rule->rdr_len > trash.size - 4)
3327 return 0;
3328
3329 /* add location */
3330 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3331 trash.len += rule->rdr_len;
3332 }
3333 else {
3334 /* add location with executing log format */
3335 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003337 /* Check left length */
3338 if (trash.len > trash.size - 4)
3339 return 0;
3340 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003341 break;
3342 }
3343
3344 if (rule->cookie_len) {
3345 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3346 trash.len += 14;
3347 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3348 trash.len += rule->cookie_len;
3349 memcpy(trash.str + trash.len, "\r\n", 2);
3350 trash.len += 2;
3351 }
3352
3353 /* add end of headers and the keep-alive/close status.
3354 * We may choose to set keep-alive if the Location begins
3355 * with a slash, because the client will come back to the
3356 * same server.
3357 */
3358 txn->status = rule->code;
3359 /* let's log the request time */
3360 s->logs.tv_request = now;
3361
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003362 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003363 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3364 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3365 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3366 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3367 /* keep-alive possible */
3368 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3369 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3370 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3371 trash.len += 30;
3372 } else {
3373 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3374 trash.len += 24;
3375 }
3376 }
3377 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3378 trash.len += 4;
3379 bo_inject(txn->rsp.chn, trash.str, trash.len);
3380 /* "eat" the request */
3381 bi_fast_delete(txn->req.chn->buf, msg->sov);
3382 msg->sov = 0;
3383 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3384 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3385 txn->req.msg_state = HTTP_MSG_CLOSED;
3386 txn->rsp.msg_state = HTTP_MSG_DONE;
3387 } else {
3388 /* keep-alive not possible */
3389 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3390 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3391 trash.len += 29;
3392 } else {
3393 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3394 trash.len += 23;
3395 }
3396 stream_int_retnclose(txn->req.chn->prod, &trash);
3397 txn->req.chn->analysers = 0;
3398 }
3399
3400 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003401 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003402 if (!(s->flags & SN_FINST_MASK))
3403 s->flags |= SN_FINST_R;
3404
3405 return 1;
3406}
3407
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003408/* This stream analyser runs all HTTP request processing which is common to
3409 * frontends and backends, which means blocking ACLs, filters, connection-close,
3410 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003411 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003412 * either needs more data or wants to immediately abort the request (eg: deny,
3413 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003414 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003415int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003416{
Willy Tarreaud787e662009-07-07 10:14:51 +02003417 struct http_txn *txn = &s->txn;
3418 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003419 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003420 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003421 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003422 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003423
Willy Tarreau655dce92009-11-08 13:10:58 +01003424 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003425 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003426 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003427 return 0;
3428 }
3429
Willy Tarreau3a816292009-07-07 10:55:49 +02003430 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003431 req->analyse_exp = TICK_ETERNITY;
3432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003433 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 +02003434 now_ms, __FUNCTION__,
3435 s,
3436 req,
3437 req->rex, req->wex,
3438 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003439 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003440 req->analysers);
3441
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003442 /* first check whether we have some ACLs set to block this request */
3443 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003444 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003445
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003446 ret = acl_pass(ret);
3447 if (cond->pol == ACL_COND_UNLESS)
3448 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 if (ret) {
3451 txn->status = 403;
3452 /* let's log the request time */
3453 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003454 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003455 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003457 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003458 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003459
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003460 /* just in case we have some per-backend tracking */
3461 session_inc_be_http_req_ctr(s);
3462
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003463 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003464 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003465
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003466 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003467 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003468 if (stats_check_uri(s->rep->prod, txn, px)) {
3469 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003470 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3471 txn->status = 500;
3472 s->logs.tv_request = now;
3473 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003474
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003475 if (!(s->flags & SN_ERR_MASK))
3476 s->flags |= SN_ERR_RESOURCE;
3477 goto return_prx_cond;
3478 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003479 /* parse the whole stats request and extract the relevant information */
3480 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003481 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003482 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003483 }
3484
Willy Tarreau3b44e722013-11-16 10:28:23 +01003485 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3486 * blocked by an http-request rule.
3487 */
3488 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003489 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003490 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003491 }
Willy Tarreau06619262006-12-17 08:37:22 +01003492
Willy Tarreau3b44e722013-11-16 10:28:23 +01003493 /* return a 403 if either rule has blocked */
3494 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003495 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003496 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003497 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003498 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003499 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003500 s->fe->fe_counters.denied_req++;
3501 if (s->fe != s->be)
3502 s->be->be_counters.denied_req++;
3503 if (s->listener->counters)
3504 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003505 goto return_prx_cond;
3506 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003507
3508 /* When a connection is tarpitted, we use the tarpit timeout,
3509 * which may be the same as the connect timeout if unspecified.
3510 * If unset, then set it to zero because we really want it to
3511 * eventually expire. We build the tarpit as an analyser.
3512 */
3513 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003514 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003515 /* wipe the request out so that we can drop the connection early
3516 * if the client closes first.
3517 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003518 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003519 req->analysers = 0; /* remove switching rules etc... */
3520 req->analysers |= AN_REQ_HTTP_TARPIT;
3521 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3522 if (!req->analyse_exp)
3523 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003524 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003525 s->fe->fe_counters.denied_req++;
3526 if (s->fe != s->be)
3527 s->be->be_counters.denied_req++;
3528 if (s->listener->counters)
3529 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003530 return 1;
3531 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003532 }
Willy Tarreau06619262006-12-17 08:37:22 +01003533
Willy Tarreau5b154472009-12-21 20:11:07 +01003534 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3535 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003536 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3537 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003538 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3539 * avoid to redo the same work if FE and BE have the same settings (common).
3540 * The method consists in checking if options changed between the two calls
3541 * (implying that either one is non-null, or one of them is non-null and we
3542 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003543 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003544
Willy Tarreaudc008c52010-02-01 16:20:08 +01003545 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3546 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3547 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3548 (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 +01003549 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003550
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003551 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3552 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003553 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003554 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3555 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003556 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003557 tmp = TX_CON_WANT_CLO;
3558
Willy Tarreau5b154472009-12-21 20:11:07 +01003559 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3560 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003561
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003562 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3563 /* parse the Connection header and possibly clean it */
3564 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003565 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003566 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3567 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003568 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003569 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003570 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003571 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003572 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003573
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003574 /* check if client or config asks for explicit close in KAL/SCL */
3575 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3576 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3577 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003578 (!(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 +01003579 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003580 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003581 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003582 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3583 }
Willy Tarreau78599912009-10-17 20:12:21 +02003584
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003585 /* we can be blocked here because the request needs to be authenticated,
3586 * either to pass or to access stats.
3587 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003588 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003589 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003590
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003591 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003592 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003593
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003594 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003595 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003596 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003597 /* on 401 we still count one error, because normal browsing
3598 * won't significantly increase the counter but brute force
3599 * attempts will.
3600 */
3601 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003602 goto return_prx_cond;
3603 }
3604
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003605 /* add request headers from the rule sets in the same order */
3606 list_for_each_entry(wl, &px->req_add, list) {
3607 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003608 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003609 ret = acl_pass(ret);
3610 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3611 ret = !ret;
3612 if (!ret)
3613 continue;
3614 }
3615
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003616 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003617 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003618 }
3619
3620 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3621 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3622 goto return_bad_req;
3623 req->analyse_exp = TICK_ETERNITY;
3624 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003625 }
3626
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003627 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003628 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003629 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3630 s->fe->fe_counters.intercepted_req++;
3631
3632 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3633 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3634 if (!(s->flags & SN_FINST_MASK))
3635 s->flags |= SN_FINST_R;
3636
3637 req->analyse_exp = TICK_ETERNITY;
3638 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003639 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003640 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003641
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003642 /* check whether we have some ACLs set to redirect this request */
3643 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003644 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003645 int ret;
3646
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003647 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003648 ret = acl_pass(ret);
3649 if (rule->cond->pol == ACL_COND_UNLESS)
3650 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003651 if (!ret)
3652 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003653 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003654 if (!http_apply_redirect_rule(rule, s, txn))
3655 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003656
Willy Tarreau71241ab2012-12-27 11:30:54 +01003657 req->analyse_exp = TICK_ETERNITY;
3658 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003659 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003660
Willy Tarreau2be39392010-01-03 17:24:51 +01003661 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3662 * If this happens, then the data will not come immediately, so we must
3663 * send all what we have without waiting. Note that due to the small gain
3664 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003665 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003666 * itself once used.
3667 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003668 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003669
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 /* that's OK for us now, let's move on to next analysers */
3671 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003672
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003673 return_bad_req:
3674 /* We centralize bad requests processing here */
3675 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3676 /* we detected a parsing error. We want to archive this request
3677 * in the dedicated proxy area for later troubleshooting.
3678 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003679 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003680 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003681
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003682 txn->req.msg_state = HTTP_MSG_ERROR;
3683 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003684 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003685
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003686 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003687 if (s->listener->counters)
3688 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003689
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003690 return_prx_cond:
3691 if (!(s->flags & SN_ERR_MASK))
3692 s->flags |= SN_ERR_PRXCOND;
3693 if (!(s->flags & SN_FINST_MASK))
3694 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003695
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003696 req->analysers = 0;
3697 req->analyse_exp = TICK_ETERNITY;
3698 return 0;
3699}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003700
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003701/* This function performs all the processing enabled for the current request.
3702 * It returns 1 if the processing can continue on next analysers, or zero if it
3703 * needs more data, encounters an error, or wants to immediately abort the
3704 * request. It relies on buffers flags, and updates s->req->analysers.
3705 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003706int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003707{
3708 struct http_txn *txn = &s->txn;
3709 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003710 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003711
Willy Tarreau655dce92009-11-08 13:10:58 +01003712 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003713 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003714 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003715 return 0;
3716 }
3717
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003718 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 +02003719 now_ms, __FUNCTION__,
3720 s,
3721 req,
3722 req->rex, req->wex,
3723 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003724 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003725 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003726
William Lallemand82fe75c2012-10-23 10:25:10 +02003727 if (s->fe->comp || s->be->comp)
3728 select_compression_request_header(s, req->buf);
3729
Willy Tarreau59234e92008-11-30 23:51:27 +01003730 /*
3731 * Right now, we know that we have processed the entire headers
3732 * and that unwanted requests have been filtered out. We can do
3733 * whatever we want with the remaining request. Also, now we
3734 * may have separate values for ->fe, ->be.
3735 */
Willy Tarreau06619262006-12-17 08:37:22 +01003736
Willy Tarreau59234e92008-11-30 23:51:27 +01003737 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003738 * If HTTP PROXY is set we simply get remote server address parsing
3739 * incoming request. Note that this requires that a connection is
3740 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003741 */
3742 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003743 struct connection *conn;
3744
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003745 /* Note that for now we don't reuse existing proxy connections */
3746 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003747 txn->req.msg_state = HTTP_MSG_ERROR;
3748 txn->status = 500;
3749 req->analysers = 0;
3750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3751
3752 if (!(s->flags & SN_ERR_MASK))
3753 s->flags |= SN_ERR_RESOURCE;
3754 if (!(s->flags & SN_FINST_MASK))
3755 s->flags |= SN_FINST_R;
3756
3757 return 0;
3758 }
3759 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003760 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003761
Willy Tarreau59234e92008-11-30 23:51:27 +01003762 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003763 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003764 * Note that doing so might move headers in the request, but
3765 * the fields will stay coherent and the URI will not move.
3766 * This should only be performed in the backend.
3767 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003768 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3770 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003771
Willy Tarreau59234e92008-11-30 23:51:27 +01003772 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003773 * 8: the appsession cookie was looked up very early in 1.2,
3774 * so let's do the same now.
3775 */
3776
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003777 /* It needs to look into the URI unless persistence must be ignored */
3778 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003779 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 +01003780 }
3781
William Lallemanda73203e2012-03-12 12:48:57 +01003782 /* add unique-id if "header-unique-id" is specified */
3783
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003784 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3785 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3786 goto return_bad_req;
3787 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003788 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003789 }
William Lallemanda73203e2012-03-12 12:48:57 +01003790
3791 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003792 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3793 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003794 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003795 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003796 goto return_bad_req;
3797 }
3798
Cyril Bontéb21570a2009-11-29 20:04:48 +01003799 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003800 * 9: add X-Forwarded-For if either the frontend or the backend
3801 * asks for it.
3802 */
3803 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003804 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003805 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003806 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3807 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003808 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003809 /* The header is set to be added only if none is present
3810 * and we found it, so don't do anything.
3811 */
3812 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003813 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003814 /* Add an X-Forwarded-For header unless the source IP is
3815 * in the 'except' network range.
3816 */
3817 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003818 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003819 != s->fe->except_net.s_addr) &&
3820 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003821 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003822 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003823 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003824 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003825 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003826
3827 /* Note: we rely on the backend to get the header name to be used for
3828 * x-forwarded-for, because the header is really meant for the backends.
3829 * However, if the backend did not specify any option, we have to rely
3830 * on the frontend's header name.
3831 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003832 if (s->be->fwdfor_hdr_len) {
3833 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003834 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003835 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003836 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003837 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003838 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003839 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003840
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003841 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003842 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003843 }
3844 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003845 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003846 /* FIXME: for the sake of completeness, we should also support
3847 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003848 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003849 int len;
3850 char pn[INET6_ADDRSTRLEN];
3851 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003852 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003853 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003854
Willy Tarreau59234e92008-11-30 23:51:27 +01003855 /* Note: we rely on the backend to get the header name to be used for
3856 * x-forwarded-for, because the header is really meant for the backends.
3857 * However, if the backend did not specify any option, we have to rely
3858 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003859 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003860 if (s->be->fwdfor_hdr_len) {
3861 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003862 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003863 } else {
3864 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003865 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003866 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003867 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003869 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003870 goto return_bad_req;
3871 }
3872 }
3873
3874 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003875 * 10: add X-Original-To if either the frontend or the backend
3876 * asks for it.
3877 */
3878 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3879
3880 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003881 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003882 /* Add an X-Original-To header unless the destination IP is
3883 * in the 'except' network range.
3884 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003885 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003886
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003887 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003888 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003889 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003890 != s->fe->except_to.s_addr) &&
3891 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003892 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003893 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003894 int len;
3895 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003896 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003897
3898 /* Note: we rely on the backend to get the header name to be used for
3899 * x-original-to, because the header is really meant for the backends.
3900 * However, if the backend did not specify any option, we have to rely
3901 * on the frontend's header name.
3902 */
3903 if (s->be->orgto_hdr_len) {
3904 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003905 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003906 } else {
3907 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003908 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003909 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003910 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003911
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003912 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003913 goto return_bad_req;
3914 }
3915 }
3916 }
3917
Willy Tarreau50fc7772012-11-11 22:19:57 +01003918 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3919 * If an "Upgrade" token is found, the header is left untouched in order not to have
3920 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3921 * "Upgrade" is present in the Connection header.
3922 */
3923 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3924 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3925 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003926 unsigned int want_flags = 0;
3927
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003928 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003929 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3930 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3931 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003932 want_flags |= TX_CON_CLO_SET;
3933 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003934 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3935 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3936 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003937 want_flags |= TX_CON_KAL_SET;
3938 }
3939
3940 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003941 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003942 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003943
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003944
Willy Tarreau522d6c02009-12-06 18:49:18 +01003945 /* If we have no server assigned yet and we're balancing on url_param
3946 * with a POST request, we may be interested in checking the body for
3947 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003948 */
3949 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3950 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003951 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003952 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003953 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003954 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003955 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003956
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003957 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003958 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003959#ifdef TCP_QUICKACK
3960 /* We expect some data from the client. Unless we know for sure
3961 * we already have a full request, we have to re-enable quick-ack
3962 * in case we previously disabled it, otherwise we might cause
3963 * the client to delay further data.
3964 */
3965 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreauf79c8172013-10-21 16:30:56 +02003966 cli_conn && (cli_conn->flags & CO_FL_CTRL_READY) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003967 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003968 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003969 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003970#endif
3971 }
Willy Tarreau03945942009-12-22 16:50:27 +01003972
Willy Tarreau59234e92008-11-30 23:51:27 +01003973 /*************************************************************
3974 * OK, that's finished for the headers. We have done what we *
3975 * could. Let's switch to the DATA state. *
3976 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003977 req->analyse_exp = TICK_ETERNITY;
3978 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003979
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003980 /* if the server closes the connection, we want to immediately react
3981 * and close the socket to save packets and syscalls.
3982 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003983 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3984 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003985
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003987 /* OK let's go on with the BODY now */
3988 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003989
Willy Tarreau59234e92008-11-30 23:51:27 +01003990 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003991 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003992 /* we detected a parsing error. We want to archive this request
3993 * in the dedicated proxy area for later troubleshooting.
3994 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003995 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003996 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003997
Willy Tarreau59234e92008-11-30 23:51:27 +01003998 txn->req.msg_state = HTTP_MSG_ERROR;
3999 txn->status = 400;
4000 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004001 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004002
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004003 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004004 if (s->listener->counters)
4005 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004006
Willy Tarreau59234e92008-11-30 23:51:27 +01004007 if (!(s->flags & SN_ERR_MASK))
4008 s->flags |= SN_ERR_PRXCOND;
4009 if (!(s->flags & SN_FINST_MASK))
4010 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004011 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004012}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004013
Willy Tarreau60b85b02008-11-30 23:28:40 +01004014/* This function is an analyser which processes the HTTP tarpit. It always
4015 * returns zero, at the beginning because it prevents any other processing
4016 * from occurring, and at the end because it terminates the request.
4017 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004018int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004019{
4020 struct http_txn *txn = &s->txn;
4021
4022 /* This connection is being tarpitted. The CLIENT side has
4023 * already set the connect expiration date to the right
4024 * timeout. We just have to check that the client is still
4025 * there and that the timeout has not expired.
4026 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004027 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004028 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004029 !tick_is_expired(req->analyse_exp, now_ms))
4030 return 0;
4031
4032 /* We will set the queue timer to the time spent, just for
4033 * logging purposes. We fake a 500 server error, so that the
4034 * attacker will not suspect his connection has been tarpitted.
4035 * It will not cause trouble to the logs because we can exclude
4036 * the tarpitted connections by filtering on the 'PT' status flags.
4037 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004038 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4039
4040 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004041 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004042 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004043
4044 req->analysers = 0;
4045 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004046
Willy Tarreau60b85b02008-11-30 23:28:40 +01004047 if (!(s->flags & SN_ERR_MASK))
4048 s->flags |= SN_ERR_PRXCOND;
4049 if (!(s->flags & SN_FINST_MASK))
4050 s->flags |= SN_FINST_T;
4051 return 0;
4052}
4053
Willy Tarreaud34af782008-11-30 23:36:37 +01004054/* This function is an analyser which processes the HTTP request body. It looks
4055 * for parameters to be used for the load balancing algorithm (url_param). It
4056 * must only be called after the standard HTTP request processing has occurred,
4057 * because it expects the request to be parsed. It returns zero if it needs to
4058 * read more data, or 1 once it has completed its analysis.
4059 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004060int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004061{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004062 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004063 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004064 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004065
4066 /* We have to parse the HTTP request body to find any required data.
4067 * "balance url_param check_post" should have been the only way to get
4068 * into this. We were brought here after HTTP header analysis, so all
4069 * related structures are ready.
4070 */
4071
Willy Tarreau522d6c02009-12-06 18:49:18 +01004072 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4073 goto missing_data;
4074
4075 if (msg->msg_state < HTTP_MSG_100_SENT) {
4076 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4077 * send an HTTP/1.1 100 Continue intermediate response.
4078 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004079 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004080 struct hdr_ctx ctx;
4081 ctx.idx = 0;
4082 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004083 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004084 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004085 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004086 }
4087 }
4088 msg->msg_state = HTTP_MSG_100_SENT;
4089 }
4090
4091 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004092 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004093 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004094 * is still null. We must save the body in msg->next because it
4095 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004096 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004097 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004098
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004099 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004100 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4101 else
4102 msg->msg_state = HTTP_MSG_DATA;
4103 }
4104
4105 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004106 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004107 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004108 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004109 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004110 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004111
Willy Tarreau115acb92009-12-26 13:56:06 +01004112 if (!ret)
4113 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004114 else if (ret < 0) {
4115 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004116 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004117 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004118 }
4119
Willy Tarreaud98cf932009-12-27 22:54:55 +01004120 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004121 * We have the first data byte is in msg->sov. We're waiting for at
4122 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004123 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004124
Willy Tarreau124d9912011-03-01 20:30:48 +01004125 if (msg->body_len < limit)
4126 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004127
Willy Tarreau9b28e032012-10-12 23:49:43 +02004128 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004129 goto http_end;
4130
4131 missing_data:
4132 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004133 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004134 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004135 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004136 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004137
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004138 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004139 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004140 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004141
4142 if (!(s->flags & SN_ERR_MASK))
4143 s->flags |= SN_ERR_CLITO;
4144 if (!(s->flags & SN_FINST_MASK))
4145 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004146 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004147 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004148
4149 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004150 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004151 /* Not enough data. We'll re-use the http-request
4152 * timeout here. Ideally, we should set the timeout
4153 * relative to the accept() date. We just set the
4154 * request timeout once at the beginning of the
4155 * request.
4156 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004157 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004158 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004159 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004160 return 0;
4161 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004162
4163 http_end:
4164 /* The situation will not evolve, so let's give up on the analysis. */
4165 s->logs.tv_request = now; /* update the request timer to reflect full request */
4166 req->analysers &= ~an_bit;
4167 req->analyse_exp = TICK_ETERNITY;
4168 return 1;
4169
4170 return_bad_req: /* let's centralize all bad requests */
4171 txn->req.msg_state = HTTP_MSG_ERROR;
4172 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004173 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004174
Willy Tarreau79ebac62010-06-07 13:47:49 +02004175 if (!(s->flags & SN_ERR_MASK))
4176 s->flags |= SN_ERR_PRXCOND;
4177 if (!(s->flags & SN_FINST_MASK))
4178 s->flags |= SN_FINST_R;
4179
Willy Tarreau522d6c02009-12-06 18:49:18 +01004180 return_err_msg:
4181 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004182 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004183 if (s->listener->counters)
4184 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004185 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004186}
4187
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004188/* send a server's name with an outgoing request over an established connection.
4189 * Note: this function is designed to be called once the request has been scheduled
4190 * for being forwarded. This is the reason why it rewinds the buffer before
4191 * proceeding.
4192 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004193int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004194
4195 struct hdr_ctx ctx;
4196
Mark Lamourinec2247f02012-01-04 13:02:01 -05004197 char *hdr_name = be->server_id_hdr_name;
4198 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004199 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004200 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004201 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004202
William Lallemandd9e90662012-01-30 17:27:17 +01004203 ctx.idx = 0;
4204
Willy Tarreau9b28e032012-10-12 23:49:43 +02004205 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004206 if (old_o) {
4207 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004208 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004209 }
4210
Willy Tarreau9b28e032012-10-12 23:49:43 +02004211 old_i = chn->buf->i;
4212 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 -05004213 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004214 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004215 }
4216
4217 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004218 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004219 memcpy(hdr_val, hdr_name, hdr_name_len);
4220 hdr_val += hdr_name_len;
4221 *hdr_val++ = ':';
4222 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004223 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4224 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004225
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004226 if (old_o) {
4227 /* If this was a forwarded request, we must readjust the amount of
4228 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004229 * variations. Note that if the request was already scheduled for
4230 * forwarding, it had its req->sol pointing to the body, which
4231 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004232 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004233 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004234 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004235 }
4236
Mark Lamourinec2247f02012-01-04 13:02:01 -05004237 return 0;
4238}
4239
Willy Tarreau610ecce2010-01-04 21:15:02 +01004240/* Terminate current transaction and prepare a new one. This is very tricky
4241 * right now but it works.
4242 */
4243void http_end_txn_clean_session(struct session *s)
4244{
4245 /* FIXME: We need a more portable way of releasing a backend's and a
4246 * server's connections. We need a safer way to reinitialize buffer
4247 * flags. We also need a more accurate method for computing per-request
4248 * data.
4249 */
4250 http_silent_debug(__LINE__, s);
4251
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004252 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004253 si_shutr(s->req->cons);
4254 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004255
4256 http_silent_debug(__LINE__, s);
4257
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004258 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004260 if (unlikely(s->srv_conn))
4261 sess_change_server(s, NULL);
4262 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263
4264 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4265 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004266 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004267
4268 if (s->txn.status) {
4269 int n;
4270
4271 n = s->txn.status / 100;
4272 if (n < 1 || n > 5)
4273 n = 0;
4274
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004275 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004276 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004277 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004278 s->fe->fe_counters.p.http.comp_rsp++;
4279 }
Willy Tarreau24657792010-02-26 10:30:28 +01004280 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004281 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004282 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004283 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004284 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004285 s->be->be_counters.p.http.comp_rsp++;
4286 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287 }
4288
4289 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004290 s->logs.bytes_in -= s->req->buf->i;
4291 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292
4293 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004294 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004295 !(s->flags & SN_MONITOR) &&
4296 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4297 s->do_log(s);
4298 }
4299
4300 s->logs.accept_date = date; /* user-visible date for logging */
4301 s->logs.tv_accept = now; /* corrected date for internal use */
4302 tv_zero(&s->logs.tv_request);
4303 s->logs.t_queue = -1;
4304 s->logs.t_connect = -1;
4305 s->logs.t_data = -1;
4306 s->logs.t_close = 0;
4307 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4308 s->logs.srv_queue_size = 0; /* we will get this number soon */
4309
Willy Tarreau9b28e032012-10-12 23:49:43 +02004310 s->logs.bytes_in = s->req->total = s->req->buf->i;
4311 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004312
4313 if (s->pend_pos)
4314 pendconn_free(s->pend_pos);
4315
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004316 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317 if (s->flags & SN_CURR_SESS) {
4318 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004319 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004321 if (may_dequeue_tasks(objt_server(s->target), s->be))
4322 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004323 }
4324
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004325 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004326
4327 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004328 si_release_endpoint(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004330 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 s->req->cons->exp = TICK_ETERNITY;
4332 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004333 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4334 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 +02004335 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 +01004336 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004337
4338 if (s->flags & SN_COMP_READY)
4339 s->comp_algo->end(&s->comp_ctx);
4340 s->comp_algo = NULL;
4341 s->flags &= ~SN_COMP_READY;
4342
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343 s->txn.meth = 0;
4344 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004345 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004346 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347 s->req->cons->flags |= SI_FL_INDEP_STR;
4348
Willy Tarreau96e31212011-05-30 18:10:30 +02004349 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004350 s->req->flags |= CF_NEVER_WAIT;
4351 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004352 }
4353
Willy Tarreau610ecce2010-01-04 21:15:02 +01004354 /* if the request buffer is not empty, it means we're
4355 * about to process another request, so send pending
4356 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004357 * Just don't do this if the buffer is close to be full,
4358 * because the request will wait for it to flush a little
4359 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004361 if (s->req->buf->i) {
4362 if (s->rep->buf->o &&
4363 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4364 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004365 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004366 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004367
4368 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004369 channel_auto_read(s->req);
4370 channel_auto_close(s->req);
4371 channel_auto_read(s->rep);
4372 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373
Willy Tarreau342b11c2010-11-24 16:22:09 +01004374 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004375 s->rep->analysers = 0;
4376
4377 http_silent_debug(__LINE__, s);
4378}
4379
4380
4381/* This function updates the request state machine according to the response
4382 * state machine and buffer flags. It returns 1 if it changes anything (flag
4383 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4384 * it is only used to find when a request/response couple is complete. Both
4385 * this function and its equivalent should loop until both return zero. It
4386 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4387 */
4388int http_sync_req_state(struct session *s)
4389{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004390 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004391 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004392 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004393 unsigned int old_state = txn->req.msg_state;
4394
4395 http_silent_debug(__LINE__, s);
4396 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4397 return 0;
4398
4399 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004400 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004401 * We can shut the read side unless we want to abort_on_close,
4402 * or we have a POST request. The issue with POST requests is
4403 * that some browsers still send a CRLF after the request, and
4404 * this CRLF must be read so that it does not remain in the kernel
4405 * buffers, otherwise a close could cause an RST on some systems
4406 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004407 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004408 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004409 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410
Willy Tarreau40f151a2012-12-20 12:10:09 +01004411 /* if the server closes the connection, we want to immediately react
4412 * and close the socket to save packets and syscalls.
4413 */
4414 chn->cons->flags |= SI_FL_NOHALF;
4415
Willy Tarreau610ecce2010-01-04 21:15:02 +01004416 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4417 goto wait_other_side;
4418
4419 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4420 /* The server has not finished to respond, so we
4421 * don't want to move in order not to upset it.
4422 */
4423 goto wait_other_side;
4424 }
4425
4426 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4427 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004428 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004430 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431 goto wait_other_side;
4432 }
4433
4434 /* When we get here, it means that both the request and the
4435 * response have finished receiving. Depending on the connection
4436 * mode, we'll have to wait for the last bytes to leave in either
4437 * direction, and sometimes for a close to be effective.
4438 */
4439
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004440 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4441 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004442 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4443 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004444 }
4445 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4446 /* Option forceclose is set, or either side wants to close,
4447 * let's enforce it now that we're not expecting any new
4448 * data to come. The caller knows the session is complete
4449 * once both states are CLOSED.
4450 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004451 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4452 channel_shutr_now(chn);
4453 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004454 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004455 }
4456 else {
4457 /* The last possible modes are keep-alive and tunnel. Since tunnel
4458 * mode does not set the body analyser, we can't reach this place
4459 * in tunnel mode, so we're left with keep-alive only.
4460 * This mode is currently not implemented, we switch to tunnel mode.
4461 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004462 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004463 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004464 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004465 }
4466
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004467 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004469 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004470
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004471 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004472 txn->req.msg_state = HTTP_MSG_CLOSING;
4473 goto http_msg_closing;
4474 }
4475 else {
4476 txn->req.msg_state = HTTP_MSG_CLOSED;
4477 goto http_msg_closed;
4478 }
4479 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004480 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 }
4482
4483 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4484 http_msg_closing:
4485 /* nothing else to forward, just waiting for the output buffer
4486 * to be empty and for the shutw_now to take effect.
4487 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004488 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004489 txn->req.msg_state = HTTP_MSG_CLOSED;
4490 goto http_msg_closed;
4491 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004492 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 txn->req.msg_state = HTTP_MSG_ERROR;
4494 goto wait_other_side;
4495 }
4496 }
4497
4498 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4499 http_msg_closed:
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004500 if (!(s->be->options & PR_O_ABRT_CLOSE))
4501 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004502 goto wait_other_side;
4503 }
4504
4505 wait_other_side:
4506 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004507 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508}
4509
4510
4511/* This function updates the response state machine according to the request
4512 * state machine and buffer flags. It returns 1 if it changes anything (flag
4513 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4514 * it is only used to find when a request/response couple is complete. Both
4515 * this function and its equivalent should loop until both return zero. It
4516 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4517 */
4518int http_sync_res_state(struct session *s)
4519{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004520 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004521 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004522 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 unsigned int old_state = txn->rsp.msg_state;
4524
4525 http_silent_debug(__LINE__, s);
4526 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4527 return 0;
4528
4529 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4530 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004531 * still monitor the server connection for a possible close
4532 * while the request is being uploaded, so we don't disable
4533 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004534 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004535 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536
4537 if (txn->req.msg_state == HTTP_MSG_ERROR)
4538 goto wait_other_side;
4539
4540 if (txn->req.msg_state < HTTP_MSG_DONE) {
4541 /* The client seems to still be sending data, probably
4542 * because we got an error response during an upload.
4543 * We have the choice of either breaking the connection
4544 * or letting it pass through. Let's do the later.
4545 */
4546 goto wait_other_side;
4547 }
4548
4549 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4550 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004551 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004552 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004553 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554 goto wait_other_side;
4555 }
4556
4557 /* When we get here, it means that both the request and the
4558 * response have finished receiving. Depending on the connection
4559 * mode, we'll have to wait for the last bytes to leave in either
4560 * direction, and sometimes for a close to be effective.
4561 */
4562
4563 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4564 /* Server-close mode : shut read and wait for the request
4565 * side to close its output buffer. The caller will detect
4566 * when we're in DONE and the other is in CLOSED and will
4567 * catch that for the final cleanup.
4568 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004569 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4570 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004572 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4573 /* Option forceclose is set, or either side wants to close,
4574 * let's enforce it now that we're not expecting any new
4575 * data to come. The caller knows the session is complete
4576 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004577 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004578 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4579 channel_shutr_now(chn);
4580 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004581 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004582 }
4583 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004584 /* The last possible modes are keep-alive and tunnel. Since tunnel
4585 * mode does not set the body analyser, we can't reach this place
4586 * in tunnel mode, so we're left with keep-alive only.
4587 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004589 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004591 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 }
4593
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004594 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004596 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004597 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4598 goto http_msg_closing;
4599 }
4600 else {
4601 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4602 goto http_msg_closed;
4603 }
4604 }
4605 goto wait_other_side;
4606 }
4607
4608 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4609 http_msg_closing:
4610 /* nothing else to forward, just waiting for the output buffer
4611 * to be empty and for the shutw_now to take effect.
4612 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004613 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004614 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4615 goto http_msg_closed;
4616 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004617 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004619 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004620 if (objt_server(s->target))
4621 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622 goto wait_other_side;
4623 }
4624 }
4625
4626 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4627 http_msg_closed:
4628 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004629 bi_erase(chn);
4630 channel_auto_close(chn);
4631 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004632 goto wait_other_side;
4633 }
4634
4635 wait_other_side:
4636 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004637 /* We force the response to leave immediately if we're waiting for the
4638 * other side, since there is no pending shutdown to push it out.
4639 */
4640 if (!channel_is_empty(chn))
4641 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004642 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004643}
4644
4645
4646/* Resync the request and response state machines. Return 1 if either state
4647 * changes.
4648 */
4649int http_resync_states(struct session *s)
4650{
4651 struct http_txn *txn = &s->txn;
4652 int old_req_state = txn->req.msg_state;
4653 int old_res_state = txn->rsp.msg_state;
4654
4655 http_silent_debug(__LINE__, s);
4656 http_sync_req_state(s);
4657 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004658 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 if (!http_sync_res_state(s))
4660 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004661 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 if (!http_sync_req_state(s))
4663 break;
4664 }
4665 http_silent_debug(__LINE__, s);
4666 /* OK, both state machines agree on a compatible state.
4667 * There are a few cases we're interested in :
4668 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4669 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4670 * directions, so let's simply disable both analysers.
4671 * - HTTP_MSG_CLOSED on the response only means we must abort the
4672 * request.
4673 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4674 * with server-close mode means we've completed one request and we
4675 * must re-initialize the server connection.
4676 */
4677
4678 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4679 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4680 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4681 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4682 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004683 channel_auto_close(s->req);
4684 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004686 channel_auto_close(s->rep);
4687 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004688 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004689 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4690 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004691 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004692 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004693 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004694 channel_auto_close(s->rep);
4695 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004696 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004697 channel_abort(s->req);
4698 channel_auto_close(s->req);
4699 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004700 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004701 }
4702 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4703 txn->rsp.msg_state == HTTP_MSG_DONE &&
4704 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4705 /* server-close: terminate this server connection and
4706 * reinitialize a fresh-new transaction.
4707 */
4708 http_end_txn_clean_session(s);
4709 }
4710
4711 http_silent_debug(__LINE__, s);
4712 return txn->req.msg_state != old_req_state ||
4713 txn->rsp.msg_state != old_res_state;
4714}
4715
Willy Tarreaud98cf932009-12-27 22:54:55 +01004716/* This function is an analyser which forwards request body (including chunk
4717 * sizes if any). It is called as soon as we must forward, even if we forward
4718 * zero byte. The only situation where it must not be called is when we're in
4719 * tunnel mode and we want to forward till the close. It's used both to forward
4720 * remaining data and to resync after end of body. It expects the msg_state to
4721 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4722 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004723 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004724 * bytes of pending data + the headers if not already done (between sol and sov).
4725 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004726 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004727int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004728{
4729 struct http_txn *txn = &s->txn;
4730 struct http_msg *msg = &s->txn.req;
4731
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004732 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4733 return 0;
4734
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004735 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004736 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004737 /* Output closed while we were sending data. We must abort and
4738 * wake the other side up.
4739 */
4740 msg->msg_state = HTTP_MSG_ERROR;
4741 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004742 return 1;
4743 }
4744
Willy Tarreau4fe41902010-06-07 22:27:41 +02004745 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004746 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004747
4748 /* Note that we don't have to send 100-continue back because we don't
4749 * need the data to complete our job, and it's up to the server to
4750 * decide whether to return 100, 417 or anything else in return of
4751 * an "Expect: 100-continue" header.
4752 */
4753
4754 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004755 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004756 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004757 * is still null. We must save the body in msg->next because it
4758 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004759 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004760 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004761
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004762 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004764 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004765 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004766 }
4767
Willy Tarreaud98cf932009-12-27 22:54:55 +01004768 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004769 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004770
Willy Tarreau610ecce2010-01-04 21:15:02 +01004771 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004772 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004773 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004774 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004775 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004776 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004777 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004778 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004779 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004780
Willy Tarreaucaabe412010-01-03 23:08:28 +01004781 if (msg->msg_state == HTTP_MSG_DATA) {
4782 /* must still forward */
4783 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004784 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004785
4786 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004787 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004788 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004789 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004790 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004791 }
4792 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004793 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004794 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004795 * TRAILERS state.
4796 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004797 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004798
Willy Tarreau54d23df2012-10-25 19:04:45 +02004799 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004800 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004801 else if (ret < 0) {
4802 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004803 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004804 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004805 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004806 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004807 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004808 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004809 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004810 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004811 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812
4813 if (ret == 0)
4814 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004815 else if (ret < 0) {
4816 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004817 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004818 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004819 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004820 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821 /* we're in MSG_CHUNK_SIZE now */
4822 }
4823 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004824 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004825
4826 if (ret == 0)
4827 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004828 else if (ret < 0) {
4829 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004830 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004831 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004832 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004833 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004834 /* we're in HTTP_MSG_DONE now */
4835 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004836 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004837 int old_state = msg->msg_state;
4838
Willy Tarreau610ecce2010-01-04 21:15:02 +01004839 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004840 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004841 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4842 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004843 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004844 if (http_resync_states(s)) {
4845 /* some state changes occurred, maybe the analyser
4846 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004847 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004848 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004849 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004850 /* request errors are most likely due to
4851 * the server aborting the transfer.
4852 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004853 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004854 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004855 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004856 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004857 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004858 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004859 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004860 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004861
4862 /* If "option abortonclose" is set on the backend, we
4863 * want to monitor the client's connection and forward
4864 * any shutdown notification to the server, which will
4865 * decide whether to close or to go on processing the
4866 * request.
4867 */
4868 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004869 channel_auto_read(req);
4870 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004871 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004872 else if (s->txn.meth == HTTP_METH_POST) {
4873 /* POST requests may require to read extra CRLF
4874 * sent by broken browsers and which could cause
4875 * an RST to be sent upon close on some systems
4876 * (eg: Linux).
4877 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004878 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004879 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004880
Willy Tarreau610ecce2010-01-04 21:15:02 +01004881 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004882 }
4883 }
4884
Willy Tarreaud98cf932009-12-27 22:54:55 +01004885 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004886 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004887 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004888 if (!(s->flags & SN_ERR_MASK))
4889 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004890 if (!(s->flags & SN_FINST_MASK)) {
4891 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4892 s->flags |= SN_FINST_H;
4893 else
4894 s->flags |= SN_FINST_D;
4895 }
4896
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004897 s->fe->fe_counters.cli_aborts++;
4898 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004899 if (objt_server(s->target))
4900 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004901
4902 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004903 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004904
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004905 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004906 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004907 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004909 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004910 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004911 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004912 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004913 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004914
Willy Tarreau5c620922011-05-11 19:56:11 +02004915 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004916 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004917 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004918 * modes are already handled by the stream sock layer. We must not do
4919 * this in content-length mode because it could present the MSG_MORE
4920 * flag with the last block of forwarded data, which would cause an
4921 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004922 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004923 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004924 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004925
Willy Tarreau610ecce2010-01-04 21:15:02 +01004926 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004927 return 0;
4928
Willy Tarreaud98cf932009-12-27 22:54:55 +01004929 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004930 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004931 if (s->listener->counters)
4932 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004933 return_bad_req_stats_ok:
4934 txn->req.msg_state = HTTP_MSG_ERROR;
4935 if (txn->status) {
4936 /* Note: we don't send any error if some data were already sent */
4937 stream_int_retnclose(req->prod, NULL);
4938 } else {
4939 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004940 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004941 }
4942 req->analysers = 0;
4943 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004944
4945 if (!(s->flags & SN_ERR_MASK))
4946 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004947 if (!(s->flags & SN_FINST_MASK)) {
4948 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4949 s->flags |= SN_FINST_H;
4950 else
4951 s->flags |= SN_FINST_D;
4952 }
4953 return 0;
4954
4955 aborted_xfer:
4956 txn->req.msg_state = HTTP_MSG_ERROR;
4957 if (txn->status) {
4958 /* Note: we don't send any error if some data were already sent */
4959 stream_int_retnclose(req->prod, NULL);
4960 } else {
4961 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004962 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963 }
4964 req->analysers = 0;
4965 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4966
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004967 s->fe->fe_counters.srv_aborts++;
4968 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004969 if (objt_server(s->target))
4970 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004971
4972 if (!(s->flags & SN_ERR_MASK))
4973 s->flags |= SN_ERR_SRVCL;
4974 if (!(s->flags & SN_FINST_MASK)) {
4975 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4976 s->flags |= SN_FINST_H;
4977 else
4978 s->flags |= SN_FINST_D;
4979 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004980 return 0;
4981}
4982
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004983/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4984 * processing can continue on next analysers, or zero if it either needs more
4985 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4986 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4987 * when it has nothing left to do, and may remove any analyser when it wants to
4988 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004989 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004990int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004991{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004992 struct http_txn *txn = &s->txn;
4993 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004994 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004995 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004996 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004997 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004998
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004999 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 +02005000 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005001 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005002 rep,
5003 rep->rex, rep->wex,
5004 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005005 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005006 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005007
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005008 /*
5009 * Now parse the partial (or complete) lines.
5010 * We will check the response syntax, and also join multi-line
5011 * headers. An index of all the lines will be elaborated while
5012 * parsing.
5013 *
5014 * For the parsing, we use a 28 states FSM.
5015 *
5016 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005017 * rep->buf->p = beginning of response
5018 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5019 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005020 * msg->eol = end of current header or line (LF or CRLF)
5021 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005022 */
5023
Willy Tarreau83e3af02009-12-28 17:39:57 +01005024 /* There's a protected area at the end of the buffer for rewriting
5025 * purposes. We don't want to start to parse the request if the
5026 * protected area is affected, because we may have to move processed
5027 * data later, which is much more complicated.
5028 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005029 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005030 if (unlikely(!channel_reserved(rep))) {
5031 /* some data has still not left the buffer, wake us once that's done */
5032 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5033 goto abort_response;
5034 channel_dont_close(rep);
5035 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5036 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005037 }
5038
Willy Tarreau379357a2013-06-08 12:55:46 +02005039 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5040 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5041 buffer_slow_realign(rep->buf);
5042
Willy Tarreau9b28e032012-10-12 23:49:43 +02005043 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005044 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005045 }
5046
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005047 /* 1: we might have to print this header in debug mode */
5048 if (unlikely((global.mode & MODE_DEBUG) &&
5049 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005050 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005051 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005052
Willy Tarreau9b28e032012-10-12 23:49:43 +02005053 sol = rep->buf->p;
5054 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005055 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005057 sol += hdr_idx_first_pos(&txn->hdr_idx);
5058 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005059
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005060 while (cur_idx) {
5061 eol = sol + txn->hdr_idx.v[cur_idx].len;
5062 debug_hdr("srvhdr", s, sol, eol);
5063 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5064 cur_idx = txn->hdr_idx.v[cur_idx].next;
5065 }
5066 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005068 /*
5069 * Now we quickly check if we have found a full valid response.
5070 * If not so, we check the FD and buffer states before leaving.
5071 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005072 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073 * responses are checked first.
5074 *
5075 * Depending on whether the client is still there or not, we
5076 * may send an error response back or not. Note that normally
5077 * we should only check for HTTP status there, and check I/O
5078 * errors somewhere else.
5079 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005080
Willy Tarreau655dce92009-11-08 13:10:58 +01005081 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082 /* Invalid response */
5083 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5084 /* we detected a parsing error. We want to archive this response
5085 * in the dedicated proxy area for later troubleshooting.
5086 */
5087 hdr_response_bad:
5088 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005089 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005090
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005091 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005092 if (objt_server(s->target)) {
5093 objt_server(s->target)->counters.failed_resp++;
5094 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005095 }
Willy Tarreau64648412010-03-05 10:41:54 +01005096 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005097 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005098 rep->analysers = 0;
5099 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005100 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005101 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005102 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005103
5104 if (!(s->flags & SN_ERR_MASK))
5105 s->flags |= SN_ERR_PRXCOND;
5106 if (!(s->flags & SN_FINST_MASK))
5107 s->flags |= SN_FINST_H;
5108
5109 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005110 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005111
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005112 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005113 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005114 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005115 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005116 goto hdr_response_bad;
5117 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005119 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005120 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005122 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005123
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005124 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005125 if (objt_server(s->target)) {
5126 objt_server(s->target)->counters.failed_resp++;
5127 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005128 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005129
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005130 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005131 rep->analysers = 0;
5132 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005133 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005134 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005135 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005136
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005137 if (!(s->flags & SN_ERR_MASK))
5138 s->flags |= SN_ERR_SRVCL;
5139 if (!(s->flags & SN_FINST_MASK))
5140 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005141 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005142 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005143
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005144 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005145 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005147 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005148
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005149 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005150 if (objt_server(s->target)) {
5151 objt_server(s->target)->counters.failed_resp++;
5152 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005153 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005154
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005155 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005156 rep->analysers = 0;
5157 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005158 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005159 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005160 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005161
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005162 if (!(s->flags & SN_ERR_MASK))
5163 s->flags |= SN_ERR_SRVTO;
5164 if (!(s->flags & SN_FINST_MASK))
5165 s->flags |= SN_FINST_H;
5166 return 0;
5167 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005168
Willy Tarreauf003d372012-11-26 13:35:37 +01005169 /* client abort with an abortonclose */
5170 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5171 s->fe->fe_counters.cli_aborts++;
5172 s->be->be_counters.cli_aborts++;
5173 if (objt_server(s->target))
5174 objt_server(s->target)->counters.cli_aborts++;
5175
5176 rep->analysers = 0;
5177 channel_auto_close(rep);
5178
5179 txn->status = 400;
5180 bi_erase(rep);
5181 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5182
5183 if (!(s->flags & SN_ERR_MASK))
5184 s->flags |= SN_ERR_CLICL;
5185 if (!(s->flags & SN_FINST_MASK))
5186 s->flags |= SN_FINST_H;
5187
5188 /* process_session() will take care of the error */
5189 return 0;
5190 }
5191
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005192 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005193 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005194 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005195 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005196
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005197 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005198 if (objt_server(s->target)) {
5199 objt_server(s->target)->counters.failed_resp++;
5200 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005201 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005202
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005203 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005204 rep->analysers = 0;
5205 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005206 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005207 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005208 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005209
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005210 if (!(s->flags & SN_ERR_MASK))
5211 s->flags |= SN_ERR_SRVCL;
5212 if (!(s->flags & SN_FINST_MASK))
5213 s->flags |= SN_FINST_H;
5214 return 0;
5215 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005216
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005217 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005218 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005220 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005221
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005222 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005223 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005224 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225
5226 if (!(s->flags & SN_ERR_MASK))
5227 s->flags |= SN_ERR_CLICL;
5228 if (!(s->flags & SN_FINST_MASK))
5229 s->flags |= SN_FINST_H;
5230
5231 /* process_session() will take care of the error */
5232 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005233 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005234
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005235 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005236 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005237 return 0;
5238 }
5239
5240 /* More interesting part now : we know that we have a complete
5241 * response which at least looks like HTTP. We have an indicator
5242 * of each header's length, so we can parse them quickly.
5243 */
5244
5245 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005246 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005248 /*
5249 * 1: get the status code
5250 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005251 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005252 if (n < 1 || n > 5)
5253 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005254 /* when the client triggers a 4xx from the server, it's most often due
5255 * to a missing object or permission. These events should be tracked
5256 * because if they happen often, it may indicate a brute force or a
5257 * vulnerability scan.
5258 */
5259 if (n == 4)
5260 session_inc_http_err_ctr(s);
5261
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005262 if (objt_server(s->target))
5263 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264
Willy Tarreau5b154472009-12-21 20:11:07 +01005265 /* check if the response is HTTP/1.1 or above */
5266 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005267 ((rep->buf->p[5] > '1') ||
5268 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005269 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005270
5271 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005272 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 +01005273
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005274 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005275 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005276
Willy Tarreau9b28e032012-10-12 23:49:43 +02005277 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005278
Willy Tarreau39650402010-03-15 19:44:39 +01005279 /* Adjust server's health based on status code. Note: status codes 501
5280 * and 505 are triggered on demand by client request, so we must not
5281 * count them as server failures.
5282 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005283 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005284 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005285 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005286 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005287 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005288 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005289
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290 /*
5291 * 2: check for cacheability.
5292 */
5293
5294 switch (txn->status) {
5295 case 200:
5296 case 203:
5297 case 206:
5298 case 300:
5299 case 301:
5300 case 410:
5301 /* RFC2616 @13.4:
5302 * "A response received with a status code of
5303 * 200, 203, 206, 300, 301 or 410 MAY be stored
5304 * by a cache (...) unless a cache-control
5305 * directive prohibits caching."
5306 *
5307 * RFC2616 @9.5: POST method :
5308 * "Responses to this method are not cacheable,
5309 * unless the response includes appropriate
5310 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005311 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005312 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005313 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005314 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5315 break;
5316 default:
5317 break;
5318 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005320 /*
5321 * 3: we may need to capture headers
5322 */
5323 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005324 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005325 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005326 txn->rsp.cap, s->fe->rsp_cap);
5327
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005328 /* 4: determine the transfer-length.
5329 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5330 * the presence of a message-body in a RESPONSE and its transfer length
5331 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005332 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005333 * All responses to the HEAD request method MUST NOT include a
5334 * message-body, even though the presence of entity-header fields
5335 * might lead one to believe they do. All 1xx (informational), 204
5336 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5337 * message-body. All other responses do include a message-body,
5338 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005339 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005340 * 1. Any response which "MUST NOT" include a message-body (such as the
5341 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5342 * always terminated by the first empty line after the header fields,
5343 * regardless of the entity-header fields present in the message.
5344 *
5345 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5346 * the "chunked" transfer-coding (Section 6.2) is used, the
5347 * transfer-length is defined by the use of this transfer-coding.
5348 * If a Transfer-Encoding header field is present and the "chunked"
5349 * transfer-coding is not present, the transfer-length is defined by
5350 * the sender closing the connection.
5351 *
5352 * 3. If a Content-Length header field is present, its decimal value in
5353 * OCTETs represents both the entity-length and the transfer-length.
5354 * If a message is received with both a Transfer-Encoding header
5355 * field and a Content-Length header field, the latter MUST be ignored.
5356 *
5357 * 4. If the message uses the media type "multipart/byteranges", and
5358 * the transfer-length is not otherwise specified, then this self-
5359 * delimiting media type defines the transfer-length. This media
5360 * type MUST NOT be used unless the sender knows that the recipient
5361 * can parse it; the presence in a request of a Range header with
5362 * multiple byte-range specifiers from a 1.1 client implies that the
5363 * client can parse multipart/byteranges responses.
5364 *
5365 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005366 */
5367
5368 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005369 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005370 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005371 * FIXME: should we parse anyway and return an error on chunked encoding ?
5372 */
5373 if (txn->meth == HTTP_METH_HEAD ||
5374 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005375 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005376 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005377 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005378 goto skip_content_length;
5379 }
5380
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005381 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005382 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005383 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005384 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005385 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005386 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5387 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005388 /* bad transfer-encoding (chunked followed by something else) */
5389 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005390 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005391 break;
5392 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005393 }
5394
5395 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5396 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005397 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005398 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005399 signed long long cl;
5400
Willy Tarreauad14f752011-09-02 20:33:27 +02005401 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005402 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005403 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005404 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005405
Willy Tarreauad14f752011-09-02 20:33:27 +02005406 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005407 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005408 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005409 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005410
Willy Tarreauad14f752011-09-02 20:33:27 +02005411 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005412 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005413 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005414 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005415
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005416 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005417 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005418 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005419 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005420
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005421 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005422 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005423 }
5424
William Lallemand82fe75c2012-10-23 10:25:10 +02005425 if (s->fe->comp || s->be->comp)
5426 select_compression_response_header(s, rep->buf);
5427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005428 /* FIXME: we should also implement the multipart/byterange method.
5429 * For now on, we resort to close mode in this case (unknown length).
5430 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005431skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005432
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433 /* end of job, return OK */
5434 rep->analysers &= ~an_bit;
5435 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005436 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005437 return 1;
5438}
5439
5440/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005441 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5442 * and updates t->rep->analysers. It might make sense to explode it into several
5443 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005444 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005445int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005446{
5447 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005448 struct http_msg *msg = &txn->rsp;
5449 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005450 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005451 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005452
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005453 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 +02005454 now_ms, __FUNCTION__,
5455 t,
5456 rep,
5457 rep->rex, rep->wex,
5458 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005459 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005460 rep->analysers);
5461
Willy Tarreau655dce92009-11-08 13:10:58 +01005462 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 return 0;
5464
5465 rep->analysers &= ~an_bit;
5466 rep->analyse_exp = TICK_ETERNITY;
5467
Willy Tarreau5b154472009-12-21 20:11:07 +01005468 /* Now we have to check if we need to modify the Connection header.
5469 * This is more difficult on the response than it is on the request,
5470 * because we can have two different HTTP versions and we don't know
5471 * how the client will interprete a response. For instance, let's say
5472 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5473 * HTTP/1.1 response without any header. Maybe it will bound itself to
5474 * HTTP/1.0 because it only knows about it, and will consider the lack
5475 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5476 * the lack of header as a keep-alive. Thus we will use two flags
5477 * indicating how a request MAY be understood by the client. In case
5478 * of multiple possibilities, we'll fix the header to be explicit. If
5479 * ambiguous cases such as both close and keepalive are seen, then we
5480 * will fall back to explicit close. Note that we won't take risks with
5481 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005482 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005483 */
5484
Willy Tarreaudc008c52010-02-01 16:20:08 +01005485 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5486 txn->status == 101)) {
5487 /* Either we've established an explicit tunnel, or we're
5488 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005489 * to understand the next protocols. We have to switch to tunnel
5490 * mode, so that we transfer the request and responses then let
5491 * this protocol pass unmodified. When we later implement specific
5492 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005493 * header which contains information about that protocol for
5494 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005495 */
5496 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5497 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005498 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5499 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5500 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005501 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005502
Willy Tarreau60466522010-01-18 19:08:45 +01005503 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005504 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005505 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5506 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005507
Willy Tarreau60466522010-01-18 19:08:45 +01005508 /* now adjust header transformations depending on current state */
5509 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5510 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5511 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005512 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005513 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005514 }
Willy Tarreau60466522010-01-18 19:08:45 +01005515 else { /* SCL / KAL */
5516 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005517 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005518 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005519 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005520
Willy Tarreau60466522010-01-18 19:08:45 +01005521 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005522 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005523
Willy Tarreau60466522010-01-18 19:08:45 +01005524 /* Some keep-alive responses are converted to Server-close if
5525 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005526 */
Willy Tarreau60466522010-01-18 19:08:45 +01005527 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5528 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005529 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005530 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005531 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005532 }
5533
Willy Tarreau7959a552013-09-23 16:44:27 +02005534 /* we want to have the response time before we start processing it */
5535 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5536
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005537 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005538 /*
5539 * 3: we will have to evaluate the filters.
5540 * As opposed to version 1.2, now they will be evaluated in the
5541 * filters order and not in the header order. This means that
5542 * each filter has to be validated among all headers.
5543 *
5544 * Filters are tried with ->be first, then with ->fe if it is
5545 * different from ->be.
5546 */
5547
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005548 cur_proxy = t->be;
5549 while (1) {
5550 struct proxy *rule_set = cur_proxy;
5551
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005552 /* evaluate http-response rules */
5553 if (!http_res_last_rule)
5554 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5555
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005556 /* try headers filters */
5557 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005558 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005559 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005560 if (objt_server(t->target)) {
5561 objt_server(t->target)->counters.failed_resp++;
5562 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005563 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005564 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005565 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005566 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005567 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005568 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005569 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005570 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005571 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005572 if (!(t->flags & SN_ERR_MASK))
5573 t->flags |= SN_ERR_PRXCOND;
5574 if (!(t->flags & SN_FINST_MASK))
5575 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005576 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005577 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005578 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005579
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005580 /* has the response been denied ? */
5581 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005582 if (objt_server(t->target))
5583 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005584
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005585 t->be->be_counters.denied_resp++;
5586 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005587 if (t->listener->counters)
5588 t->listener->counters->denied_resp++;
5589
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005590 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005591 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005592
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005593 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005594 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005595 if (txn->status < 200)
5596 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005597 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005598 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005599 ret = acl_pass(ret);
5600 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5601 ret = !ret;
5602 if (!ret)
5603 continue;
5604 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005605 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005606 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005607 }
5608
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005609 /* check whether we're already working on the frontend */
5610 if (cur_proxy == t->fe)
5611 break;
5612 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005613 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005614
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005615 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005616 * We may be facing a 100-continue response, in which case this
5617 * is not the right response, and we're waiting for the next one.
5618 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005619 * next one.
5620 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005621 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005622 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005623 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005624 msg->msg_state = HTTP_MSG_RPBEFORE;
5625 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005626 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005627 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5628 return 1;
5629 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005630 else if (unlikely(txn->status < 200))
5631 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005632
5633 /* we don't have any 1xx status code now */
5634
5635 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005636 * 4: check for server cookie.
5637 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005638 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5639 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005640 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005641
Willy Tarreaubaaee002006-06-26 02:48:02 +02005642
Willy Tarreaua15645d2007-03-18 16:22:39 +01005643 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005644 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005645 */
Willy Tarreau67402132012-05-31 20:40:20 +02005646 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005647 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005648
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005649 /*
5650 * 6: add server cookie in the response if needed
5651 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005652 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005653 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005654 (!(t->flags & SN_DIRECT) ||
5655 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5656 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5657 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5658 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005659 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005660 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005661 /* the server is known, it's not the one the client requested, or the
5662 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005663 * insert a set-cookie here, except if we want to insert only on POST
5664 * requests and this one isn't. Note that servers which don't have cookies
5665 * (eg: some backup servers) will return a full cookie removal request.
5666 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005667 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005668 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005669 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5670 t->be->cookie_name);
5671 }
5672 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005673 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005674
5675 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5676 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005677 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5678 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5679 trash.len += 5;
5680
Willy Tarreauef4f3912010-10-07 21:00:29 +02005681 if (t->be->cookie_maxlife) {
5682 /* emit first_date, which is either the original one or
5683 * the current date.
5684 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005685 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005686 s30tob64(txn->cookie_first_date ?
5687 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005688 (date.tv_sec+3) >> 2, trash.str + trash.len);
5689 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005690 }
5691 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005692 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005693 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005694
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005695 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005696 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005697
Willy Tarreau4992dd22012-05-31 21:02:17 +02005698 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005699 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005700
5701 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005702 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005703
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005704 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005705 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005706
Willy Tarreauf1348312010-10-07 15:54:11 +02005707 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005708 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005709 /* the server did not change, only the date was updated */
5710 txn->flags |= TX_SCK_UPDATED;
5711 else
5712 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005713
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005714 /* Here, we will tell an eventual cache on the client side that we don't
5715 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5716 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5717 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5718 */
Willy Tarreau67402132012-05-31 20:40:20 +02005719 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005720
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005721 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5722
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005723 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005724 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005725 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005726 }
5727 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005728
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005729 /*
5730 * 7: check if result will be cacheable with a cookie.
5731 * We'll block the response if security checks have caught
5732 * nasty things such as a cacheable cookie.
5733 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005734 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5735 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005736 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005737
5738 /* we're in presence of a cacheable response containing
5739 * a set-cookie header. We'll block it as requested by
5740 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005741 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005742 if (objt_server(t->target))
5743 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005744
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005745 t->be->be_counters.denied_resp++;
5746 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005747 if (t->listener->counters)
5748 t->listener->counters->denied_resp++;
5749
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005750 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005751 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005752 send_log(t->be, LOG_ALERT,
5753 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005754 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005755 goto return_srv_prx_502;
5756 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005757
5758 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005759 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005760 * If an "Upgrade" token is found, the header is left untouched in order
5761 * not to have to deal with some client bugs : some of them fail an upgrade
5762 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005763 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005764 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5765 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5766 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005767 unsigned int want_flags = 0;
5768
5769 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5770 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5771 /* we want a keep-alive response here. Keep-alive header
5772 * required if either side is not 1.1.
5773 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005774 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005775 want_flags |= TX_CON_KAL_SET;
5776 }
5777 else {
5778 /* we want a close response here. Close header required if
5779 * the server is 1.1, regardless of the client.
5780 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005781 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005782 want_flags |= TX_CON_CLO_SET;
5783 }
5784
5785 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005786 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005787 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005788
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005789 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005790 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005791 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005792 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005793
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005794 /*************************************************************
5795 * OK, that's finished for the headers. We have done what we *
5796 * could. Let's switch to the DATA state. *
5797 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005798
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005799 /* if the user wants to log as soon as possible, without counting
5800 * bytes from the server, then this is the right moment. We have
5801 * to temporarily assign bytes_out to log what we currently have.
5802 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005803 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005804 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5805 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005806 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005807 t->logs.bytes_out = 0;
5808 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005809
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005810 /* Note: we must not try to cheat by jumping directly to DATA,
5811 * otherwise we would not let the client side wake up.
5812 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005813
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005814 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005815 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005816 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005817}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005818
Willy Tarreaud98cf932009-12-27 22:54:55 +01005819/* This function is an analyser which forwards response body (including chunk
5820 * sizes if any). It is called as soon as we must forward, even if we forward
5821 * zero byte. The only situation where it must not be called is when we're in
5822 * tunnel mode and we want to forward till the close. It's used both to forward
5823 * remaining data and to resync after end of body. It expects the msg_state to
5824 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5825 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005826 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005827 * bytes of pending data + the headers if not already done (between sol and sov).
5828 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005829 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005830int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005831{
5832 struct http_txn *txn = &s->txn;
5833 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005834 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005835 static struct buffer *tmpbuf = NULL;
5836 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005837 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005838 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005839
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005840 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5841 return 0;
5842
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005843 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005844 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005845 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005846 /* Output closed while we were sending data. We must abort and
5847 * wake the other side up.
5848 */
5849 msg->msg_state = HTTP_MSG_ERROR;
5850 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005851 return 1;
5852 }
5853
Willy Tarreau4fe41902010-06-07 22:27:41 +02005854 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005855 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005856
William Lallemand82fe75c2012-10-23 10:25:10 +02005857 /* this is the first time we need the compression buffer */
5858 if (s->comp_algo != NULL && tmpbuf == NULL) {
5859 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5860 goto aborted_xfer; /* no memory */
5861 }
5862
Willy Tarreaud98cf932009-12-27 22:54:55 +01005863 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005864 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005865 * rep->buf.p still points to the beginning of the message and msg->sol
5866 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005867 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005868 channel_forward(res, msg->sov);
5869 msg->next = 0;
5870 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005871
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005872 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005873 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005874 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005875 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005876 }
5877
William Lallemand82fe75c2012-10-23 10:25:10 +02005878 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005879 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005880 if (ret < 0)
5881 goto missing_data; /* not enough spaces in buffers */
5882 compressing = 1;
5883 }
5884
Willy Tarreaud98cf932009-12-27 22:54:55 +01005885 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005886 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005887 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005888 if (s->comp_algo == NULL) {
5889 bytes = msg->sov - msg->sol;
5890 if (msg->chunk_len || bytes) {
5891 msg->sol = msg->sov;
5892 msg->next -= bytes; /* will be forwarded */
5893 msg->chunk_len += bytes;
5894 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5895 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005896 }
5897
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005898 switch (msg->msg_state - HTTP_MSG_DATA) {
5899 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005900 if (compressing) {
5901 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5902 if (ret < 0)
5903 goto aborted_xfer;
5904 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005905
5906 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005907 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005908
5909 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005910 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005911 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005912 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005913 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005914 if (compressing && consumed_data) {
5915 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5916 compressing = 0;
5917 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005918 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005919 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005920 /* fall through for HTTP_MSG_CHUNK_CRLF */
5921
5922 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5923 /* we want the CRLF after the data */
5924
5925 ret = http_skip_chunk_crlf(msg);
5926 if (ret == 0)
5927 goto missing_data;
5928 else if (ret < 0) {
5929 if (msg->err_pos >= 0)
5930 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5931 goto return_bad_res;
5932 }
5933 /* skipping data in buffer for compression */
5934 if (compressing) {
5935 b_adv(res->buf, msg->next);
5936 msg->next = 0;
5937 msg->sov = 0;
5938 msg->sol = 0;
5939 }
5940 /* we're in MSG_CHUNK_SIZE now, fall through */
5941
5942 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005943 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005944 * set ->sov and ->next to point to the body and switch to DATA or
5945 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005946 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005947
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005948 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005949 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005950 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005951 else if (ret < 0) {
5952 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005953 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005954 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005955 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005956 if (compressing) {
5957 if (likely(msg->chunk_len > 0)) {
5958 /* skipping data if we are in compression mode */
5959 b_adv(res->buf, msg->next);
5960 msg->next = 0;
5961 msg->sov = 0;
5962 msg->sol = 0;
5963 } else {
5964 if (consumed_data) {
5965 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5966 compressing = 0;
5967 }
5968 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005969 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005970 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005971 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005972
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005973 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5974 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005975 if (ret == 0)
5976 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005977 else if (ret < 0) {
5978 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005979 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005980 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005981 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005982 if (s->comp_algo != NULL) {
5983 /* forwarding trailers */
5984 channel_forward(res, msg->next);
5985 msg->next = 0;
5986 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02005987 /* we're in HTTP_MSG_DONE now, but we might still have
5988 * some data pending, so let's loop over once.
5989 */
5990 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005991
5992 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005993 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005994
5995 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005996 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005997 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5998 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005999 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006000 if (http_resync_states(s)) {
6001 http_silent_debug(__LINE__, s);
6002 /* some state changes occurred, maybe the analyser
6003 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006004 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006005 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006006 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006007 /* response errors are most likely due to
6008 * the client aborting the transfer.
6009 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006010 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006011 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006012 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006013 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006014 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006015 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006016 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006017 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006018 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006019 }
6020 }
6021
Willy Tarreaud98cf932009-12-27 22:54:55 +01006022 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006023 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006024 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6025 compressing = 0;
6026 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006027
6028 if (res->flags & CF_SHUTW)
6029 goto aborted_xfer;
6030
6031 /* stop waiting for data if the input is closed before the end. If the
6032 * client side was already closed, it means that the client has aborted,
6033 * so we don't want to count this as a server abort. Otherwise it's a
6034 * server abort.
6035 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006036 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006037 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6038 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006039 if (!(s->flags & SN_ERR_MASK))
6040 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006041 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006042 if (objt_server(s->target))
6043 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006044 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006045 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006046
Willy Tarreau40dba092010-03-04 18:14:51 +01006047 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006048 if (!s->req->analysers)
6049 goto return_bad_res;
6050
Willy Tarreauea953162012-05-18 23:41:28 +02006051 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006052 if (s->comp_algo == NULL) {
6053 bytes = msg->sov - msg->sol;
6054 if (msg->chunk_len || bytes) {
6055 msg->sol = msg->sov;
6056 msg->next -= bytes; /* will be forwarded */
6057 msg->chunk_len += bytes;
6058 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6059 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006060 }
6061
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006062 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006063 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006064 * Similarly, with keep-alive on the client side, we don't want to forward a
6065 * close.
6066 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006067 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006068 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6069 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006070 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006071
Willy Tarreau5c620922011-05-11 19:56:11 +02006072 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006073 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006074 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006075 * modes are already handled by the stream sock layer. We must not do
6076 * this in content-length mode because it could present the MSG_MORE
6077 * flag with the last block of forwarded data, which would cause an
6078 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006079 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006080 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006081 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006082
Willy Tarreaud98cf932009-12-27 22:54:55 +01006083 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006084 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006085 return 0;
6086
Willy Tarreau40dba092010-03-04 18:14:51 +01006087 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006088 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006089 if (objt_server(s->target))
6090 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006091
6092 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006093 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006094 /* don't send any error message as we're in the body */
6095 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006096 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006097 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006098 if (objt_server(s->target))
6099 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006100
6101 if (!(s->flags & SN_ERR_MASK))
6102 s->flags |= SN_ERR_PRXCOND;
6103 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006104 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006105 return 0;
6106
6107 aborted_xfer:
6108 txn->rsp.msg_state = HTTP_MSG_ERROR;
6109 /* don't send any error message as we're in the body */
6110 stream_int_retnclose(res->cons, NULL);
6111 res->analysers = 0;
6112 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6113
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006114 s->fe->fe_counters.cli_aborts++;
6115 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006116 if (objt_server(s->target))
6117 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006118
6119 if (!(s->flags & SN_ERR_MASK))
6120 s->flags |= SN_ERR_CLICL;
6121 if (!(s->flags & SN_FINST_MASK))
6122 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006123 return 0;
6124}
6125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006126/* Iterate the same filter through all request headers.
6127 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006128 * Since it can manage the switch to another backend, it updates the per-proxy
6129 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006131int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006133 char term;
6134 char *cur_ptr, *cur_end, *cur_next;
6135 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006136 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006137 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006138 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006140 last_hdr = 0;
6141
Willy Tarreau9b28e032012-10-12 23:49:43 +02006142 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006143 old_idx = 0;
6144
6145 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006146 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006147 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006148 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006149 (exp->action == ACT_ALLOW ||
6150 exp->action == ACT_DENY ||
6151 exp->action == ACT_TARPIT))
6152 return 0;
6153
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006154 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006155 if (!cur_idx)
6156 break;
6157
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006158 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006159 cur_ptr = cur_next;
6160 cur_end = cur_ptr + cur_hdr->len;
6161 cur_next = cur_end + cur_hdr->cr + 1;
6162
6163 /* Now we have one header between cur_ptr and cur_end,
6164 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006165 */
6166
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006167 /* The annoying part is that pattern matching needs
6168 * that we modify the contents to null-terminate all
6169 * strings before testing them.
6170 */
6171
6172 term = *cur_end;
6173 *cur_end = '\0';
6174
6175 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6176 switch (exp->action) {
6177 case ACT_SETBE:
6178 /* It is not possible to jump a second time.
6179 * FIXME: should we return an HTTP/500 here so that
6180 * the admin knows there's a problem ?
6181 */
6182 if (t->be != t->fe)
6183 break;
6184
6185 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006186 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006187 last_hdr = 1;
6188 break;
6189
6190 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006191 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006192 last_hdr = 1;
6193 break;
6194
6195 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006196 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006197 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006198 break;
6199
6200 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006201 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006202 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006203 break;
6204
6205 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006206 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6207 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006208 /* FIXME: if the user adds a newline in the replacement, the
6209 * index will not be recalculated for now, and the new line
6210 * will not be counted as a new header.
6211 */
6212
6213 cur_end += delta;
6214 cur_next += delta;
6215 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006216 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006217 break;
6218
6219 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006220 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006221 cur_next += delta;
6222
Willy Tarreaufa355d42009-11-29 18:12:29 +01006223 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006224 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6225 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006226 cur_hdr->len = 0;
6227 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006228 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006229 break;
6230
6231 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006232 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006233 if (cur_end)
6234 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006236 /* keep the link from this header to next one in case of later
6237 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006239 old_idx = cur_idx;
6240 }
6241 return 0;
6242}
6243
6244
6245/* Apply the filter to the request line.
6246 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6247 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006248 * Since it can manage the switch to another backend, it updates the per-proxy
6249 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006250 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006251int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006252{
6253 char term;
6254 char *cur_ptr, *cur_end;
6255 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006256 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006257 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreau3d300592007-03-18 18:34:41 +01006259 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006260 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006261 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006262 (exp->action == ACT_ALLOW ||
6263 exp->action == ACT_DENY ||
6264 exp->action == ACT_TARPIT))
6265 return 0;
6266 else if (exp->action == ACT_REMOVE)
6267 return 0;
6268
6269 done = 0;
6270
Willy Tarreau9b28e032012-10-12 23:49:43 +02006271 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006272 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006273
6274 /* Now we have the request line between cur_ptr and cur_end */
6275
6276 /* The annoying part is that pattern matching needs
6277 * that we modify the contents to null-terminate all
6278 * strings before testing them.
6279 */
6280
6281 term = *cur_end;
6282 *cur_end = '\0';
6283
6284 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6285 switch (exp->action) {
6286 case ACT_SETBE:
6287 /* It is not possible to jump a second time.
6288 * FIXME: should we return an HTTP/500 here so that
6289 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006290 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006291 if (t->be != t->fe)
6292 break;
6293
6294 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006295 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006296 done = 1;
6297 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006299 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006300 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006301 done = 1;
6302 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006304 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006305 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006306 done = 1;
6307 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006309 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006310 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006311 done = 1;
6312 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006314 case ACT_REPLACE:
6315 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006316 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6317 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006318 /* FIXME: if the user adds a newline in the replacement, the
6319 * index will not be recalculated for now, and the new line
6320 * will not be counted as a new header.
6321 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006322
Willy Tarreaufa355d42009-11-29 18:12:29 +01006323 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006324 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006325 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006326 HTTP_MSG_RQMETH,
6327 cur_ptr, cur_end + 1,
6328 NULL, NULL);
6329 if (unlikely(!cur_end))
6330 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006331
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006332 /* we have a full request and we know that we have either a CR
6333 * or an LF at <ptr>.
6334 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006335 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6336 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006337 /* there is no point trying this regex on headers */
6338 return 1;
6339 }
6340 }
6341 *cur_end = term; /* restore the string terminator */
6342 return done;
6343}
Willy Tarreau97de6242006-12-27 17:18:38 +01006344
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006348 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006349 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006350 * unparsable request. Since it can manage the switch to another backend, it
6351 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006353int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006354{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006355 struct http_txn *txn = &s->txn;
6356 struct hdr_exp *exp;
6357
6358 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006359 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006361 /*
6362 * The interleaving of transformations and verdicts
6363 * makes it difficult to decide to continue or stop
6364 * the evaluation.
6365 */
6366
Willy Tarreau6c123b12010-01-28 20:22:06 +01006367 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6368 break;
6369
Willy Tarreau3d300592007-03-18 18:34:41 +01006370 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006371 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006372 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006373 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006374
6375 /* if this filter had a condition, evaluate it now and skip to
6376 * next filter if the condition does not match.
6377 */
6378 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006379 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006380 ret = acl_pass(ret);
6381 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6382 ret = !ret;
6383
6384 if (!ret)
6385 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006386 }
6387
6388 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006389 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006390 if (unlikely(ret < 0))
6391 return -1;
6392
6393 if (likely(ret == 0)) {
6394 /* The filter did not match the request, it can be
6395 * iterated through all headers.
6396 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006397 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006399 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006400 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401}
6402
6403
Willy Tarreaua15645d2007-03-18 16:22:39 +01006404
Willy Tarreau58f10d72006-12-04 02:26:12 +01006405/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006406 * Try to retrieve the server associated to the appsession.
6407 * If the server is found, it's assigned to the session.
6408 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006409void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006410 struct http_txn *txn = &t->txn;
6411 appsess *asession = NULL;
6412 char *sessid_temp = NULL;
6413
Cyril Bontéb21570a2009-11-29 20:04:48 +01006414 if (len > t->be->appsession_len) {
6415 len = t->be->appsession_len;
6416 }
6417
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006418 if (t->be->options2 & PR_O2_AS_REQL) {
6419 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006420 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006421 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006422 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006423 }
6424
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006425 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006426 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6427 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6428 return;
6429 }
6430
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006431 memcpy(txn->sessid, buf, len);
6432 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006433 }
6434
6435 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6436 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6437 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6438 return;
6439 }
6440
Cyril Bontéb21570a2009-11-29 20:04:48 +01006441 memcpy(sessid_temp, buf, len);
6442 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006443
6444 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6445 /* free previously allocated memory */
6446 pool_free2(apools.sessid, sessid_temp);
6447
6448 if (asession != NULL) {
6449 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6450 if (!(t->be->options2 & PR_O2_AS_REQL))
6451 asession->request_count++;
6452
6453 if (asession->serverid != NULL) {
6454 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006455
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006456 while (srv) {
6457 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006458 if ((srv->state & SRV_RUNNING) ||
6459 (t->be->options & PR_O_PERSIST) ||
6460 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006461 /* we found the server and it's usable */
6462 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006463 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006464 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006465 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006466
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006467 break;
6468 } else {
6469 txn->flags &= ~TX_CK_MASK;
6470 txn->flags |= TX_CK_DOWN;
6471 }
6472 }
6473 srv = srv->next;
6474 }
6475 }
6476 }
6477}
6478
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006479/* Find the end of a cookie value contained between <s> and <e>. It works the
6480 * same way as with headers above except that the semi-colon also ends a token.
6481 * See RFC2965 for more information. Note that it requires a valid header to
6482 * return a valid result.
6483 */
6484char *find_cookie_value_end(char *s, const char *e)
6485{
6486 int quoted, qdpair;
6487
6488 quoted = qdpair = 0;
6489 for (; s < e; s++) {
6490 if (qdpair) qdpair = 0;
6491 else if (quoted) {
6492 if (*s == '\\') qdpair = 1;
6493 else if (*s == '"') quoted = 0;
6494 }
6495 else if (*s == '"') quoted = 1;
6496 else if (*s == ',' || *s == ';') return s;
6497 }
6498 return s;
6499}
6500
6501/* Delete a value in a header between delimiters <from> and <next> in buffer
6502 * <buf>. The number of characters displaced is returned, and the pointer to
6503 * the first delimiter is updated if required. The function tries as much as
6504 * possible to respect the following principles :
6505 * - replace <from> delimiter by the <next> one unless <from> points to a
6506 * colon, in which case <next> is simply removed
6507 * - set exactly one space character after the new first delimiter, unless
6508 * there are not enough characters in the block being moved to do so.
6509 * - remove unneeded spaces before the previous delimiter and after the new
6510 * one.
6511 *
6512 * It is the caller's responsibility to ensure that :
6513 * - <from> points to a valid delimiter or the colon ;
6514 * - <next> points to a valid delimiter or the final CR/LF ;
6515 * - there are non-space chars before <from> ;
6516 * - there is a CR/LF at or after <next>.
6517 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006518int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006519{
6520 char *prev = *from;
6521
6522 if (*prev == ':') {
6523 /* We're removing the first value, preserve the colon and add a
6524 * space if possible.
6525 */
6526 if (!http_is_crlf[(unsigned char)*next])
6527 next++;
6528 prev++;
6529 if (prev < next)
6530 *prev++ = ' ';
6531
6532 while (http_is_spht[(unsigned char)*next])
6533 next++;
6534 } else {
6535 /* Remove useless spaces before the old delimiter. */
6536 while (http_is_spht[(unsigned char)*(prev-1)])
6537 prev--;
6538 *from = prev;
6539
6540 /* copy the delimiter and if possible a space if we're
6541 * not at the end of the line.
6542 */
6543 if (!http_is_crlf[(unsigned char)*next]) {
6544 *prev++ = *next++;
6545 if (prev + 1 < next)
6546 *prev++ = ' ';
6547 while (http_is_spht[(unsigned char)*next])
6548 next++;
6549 }
6550 }
6551 return buffer_replace2(buf, prev, next, NULL, 0);
6552}
6553
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006554/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006555 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 * desirable to call it only when needed. This code is quite complex because
6557 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6558 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006559 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006560void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006561{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006562 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006563 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006565 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6566 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006568 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006569 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006570 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006572 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006574 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006576 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006577 hdr_beg = hdr_next;
6578 hdr_end = hdr_beg + cur_hdr->len;
6579 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006581 /* We have one full header between hdr_beg and hdr_end, and the
6582 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006583 * "Cookie:" headers.
6584 */
6585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006587 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588 old_idx = cur_idx;
6589 continue;
6590 }
6591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006592 del_from = NULL; /* nothing to be deleted */
6593 preserve_hdr = 0; /* assume we may kill the whole header */
6594
Willy Tarreau58f10d72006-12-04 02:26:12 +01006595 /* Now look for cookies. Conforming to RFC2109, we have to support
6596 * attributes whose name begin with a '$', and associate them with
6597 * the right cookie, if we want to delete this cookie.
6598 * So there are 3 cases for each cookie read :
6599 * 1) it's a special attribute, beginning with a '$' : ignore it.
6600 * 2) it's a server id cookie that we *MAY* want to delete : save
6601 * some pointers on it (last semi-colon, beginning of cookie...)
6602 * 3) it's an application cookie : we *MAY* have to delete a previous
6603 * "special" cookie.
6604 * At the end of loop, if a "special" cookie remains, we may have to
6605 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006606 * *MUST* delete it.
6607 *
6608 * Note: RFC2965 is unclear about the processing of spaces around
6609 * the equal sign in the ATTR=VALUE form. A careful inspection of
6610 * the RFC explicitly allows spaces before it, and not within the
6611 * tokens (attrs or values). An inspection of RFC2109 allows that
6612 * too but section 10.1.3 lets one think that spaces may be allowed
6613 * after the equal sign too, resulting in some (rare) buggy
6614 * implementations trying to do that. So let's do what servers do.
6615 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6616 * allowed quoted strings in values, with any possible character
6617 * after a backslash, including control chars and delimitors, which
6618 * causes parsing to become ambiguous. Browsers also allow spaces
6619 * within values even without quotes.
6620 *
6621 * We have to keep multiple pointers in order to support cookie
6622 * removal at the beginning, middle or end of header without
6623 * corrupting the header. All of these headers are valid :
6624 *
6625 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6626 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6627 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6628 * | | | | | | | | |
6629 * | | | | | | | | hdr_end <--+
6630 * | | | | | | | +--> next
6631 * | | | | | | +----> val_end
6632 * | | | | | +-----------> val_beg
6633 * | | | | +--------------> equal
6634 * | | | +----------------> att_end
6635 * | | +---------------------> att_beg
6636 * | +--------------------------> prev
6637 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006638 */
6639
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006640 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6641 /* Iterate through all cookies on this line */
6642
6643 /* find att_beg */
6644 att_beg = prev + 1;
6645 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6646 att_beg++;
6647
6648 /* find att_end : this is the first character after the last non
6649 * space before the equal. It may be equal to hdr_end.
6650 */
6651 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006652
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006653 while (equal < hdr_end) {
6654 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006655 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006656 if (http_is_spht[(unsigned char)*equal++])
6657 continue;
6658 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006659 }
6660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006661 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6662 * is between <att_beg> and <equal>, both may be identical.
6663 */
6664
6665 /* look for end of cookie if there is an equal sign */
6666 if (equal < hdr_end && *equal == '=') {
6667 /* look for the beginning of the value */
6668 val_beg = equal + 1;
6669 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6670 val_beg++;
6671
6672 /* find the end of the value, respecting quotes */
6673 next = find_cookie_value_end(val_beg, hdr_end);
6674
6675 /* make val_end point to the first white space or delimitor after the value */
6676 val_end = next;
6677 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6678 val_end--;
6679 } else {
6680 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006681 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 /* We have nothing to do with attributes beginning with '$'. However,
6684 * they will automatically be removed if a header before them is removed,
6685 * since they're supposed to be linked together.
6686 */
6687 if (*att_beg == '$')
6688 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006689
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006690 /* Ignore cookies with no equal sign */
6691 if (equal == next) {
6692 /* This is not our cookie, so we must preserve it. But if we already
6693 * scheduled another cookie for removal, we cannot remove the
6694 * complete header, but we can remove the previous block itself.
6695 */
6696 preserve_hdr = 1;
6697 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006698 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006699 val_end += delta;
6700 next += delta;
6701 hdr_end += delta;
6702 hdr_next += delta;
6703 cur_hdr->len += delta;
6704 http_msg_move_end(&txn->req, delta);
6705 prev = del_from;
6706 del_from = NULL;
6707 }
6708 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006711 /* if there are spaces around the equal sign, we need to
6712 * strip them otherwise we'll get trouble for cookie captures,
6713 * or even for rewrites. Since this happens extremely rarely,
6714 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006715 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006716 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6717 int stripped_before = 0;
6718 int stripped_after = 0;
6719
6720 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006721 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006722 equal += stripped_before;
6723 val_beg += stripped_before;
6724 }
6725
6726 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006727 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 val_beg += stripped_after;
6729 stripped_before += stripped_after;
6730 }
6731
6732 val_end += stripped_before;
6733 next += stripped_before;
6734 hdr_end += stripped_before;
6735 hdr_next += stripped_before;
6736 cur_hdr->len += stripped_before;
6737 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006739 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006740
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006741 /* First, let's see if we want to capture this cookie. We check
6742 * that we don't already have a client side cookie, because we
6743 * can only capture one. Also as an optimisation, we ignore
6744 * cookies shorter than the declared name.
6745 */
6746 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6747 (val_end - att_beg >= t->fe->capture_namelen) &&
6748 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6749 int log_len = val_end - att_beg;
6750
6751 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6752 Alert("HTTP logging : out of memory.\n");
6753 } else {
6754 if (log_len > t->fe->capture_len)
6755 log_len = t->fe->capture_len;
6756 memcpy(txn->cli_cookie, att_beg, log_len);
6757 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006759 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760
Willy Tarreaubca99692010-10-06 19:25:55 +02006761 /* Persistence cookies in passive, rewrite or insert mode have the
6762 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006763 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006764 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006765 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006766 * For cookies in prefix mode, the form is :
6767 *
6768 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006769 */
6770 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6771 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6772 struct server *srv = t->be->srv;
6773 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006774
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006775 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6776 * have the server ID between val_beg and delim, and the original cookie between
6777 * delim+1 and val_end. Otherwise, delim==val_end :
6778 *
6779 * Cookie: NAME=SRV; # in all but prefix modes
6780 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6781 * | || || | |+-> next
6782 * | || || | +--> val_end
6783 * | || || +---------> delim
6784 * | || |+------------> val_beg
6785 * | || +-------------> att_end = equal
6786 * | |+-----------------> att_beg
6787 * | +------------------> prev
6788 * +-------------------------> hdr_beg
6789 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006790
Willy Tarreau67402132012-05-31 20:40:20 +02006791 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006792 for (delim = val_beg; delim < val_end; delim++)
6793 if (*delim == COOKIE_DELIM)
6794 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006795 } else {
6796 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006798 /* Now check if the cookie contains a date field, which would
6799 * appear after a vertical bar ('|') just after the server name
6800 * and before the delimiter.
6801 */
6802 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6803 if (vbar1) {
6804 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006805 * right is the last seen date. It is a base64 encoded
6806 * 30-bit value representing the UNIX date since the
6807 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006808 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006809 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006810 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006811 if (val_end - vbar1 >= 5) {
6812 val = b64tos30(vbar1);
6813 if (val > 0)
6814 txn->cookie_last_date = val << 2;
6815 }
6816 /* look for a second vertical bar */
6817 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6818 if (vbar1 && (val_end - vbar1 > 5)) {
6819 val = b64tos30(vbar1 + 1);
6820 if (val > 0)
6821 txn->cookie_first_date = val << 2;
6822 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006823 }
6824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006825
Willy Tarreauf64d1412010-10-07 20:06:11 +02006826 /* if the cookie has an expiration date and the proxy wants to check
6827 * it, then we do that now. We first check if the cookie is too old,
6828 * then only if it has expired. We detect strict overflow because the
6829 * time resolution here is not great (4 seconds). Cookies with dates
6830 * in the future are ignored if their offset is beyond one day. This
6831 * allows an admin to fix timezone issues without expiring everyone
6832 * and at the same time avoids keeping unwanted side effects for too
6833 * long.
6834 */
6835 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006836 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6837 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006838 txn->flags &= ~TX_CK_MASK;
6839 txn->flags |= TX_CK_OLD;
6840 delim = val_beg; // let's pretend we have not found the cookie
6841 txn->cookie_first_date = 0;
6842 txn->cookie_last_date = 0;
6843 }
6844 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006845 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6846 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006847 txn->flags &= ~TX_CK_MASK;
6848 txn->flags |= TX_CK_EXPIRED;
6849 delim = val_beg; // let's pretend we have not found the cookie
6850 txn->cookie_first_date = 0;
6851 txn->cookie_last_date = 0;
6852 }
6853
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006854 /* Here, we'll look for the first running server which supports the cookie.
6855 * This allows to share a same cookie between several servers, for example
6856 * to dedicate backup servers to specific servers only.
6857 * However, to prevent clients from sticking to cookie-less backup server
6858 * when they have incidentely learned an empty cookie, we simply ignore
6859 * empty cookies and mark them as invalid.
6860 * The same behaviour is applied when persistence must be ignored.
6861 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006862 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006863 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 while (srv) {
6866 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6867 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6868 if ((srv->state & SRV_RUNNING) ||
6869 (t->be->options & PR_O_PERSIST) ||
6870 (t->flags & SN_FORCE_PRST)) {
6871 /* we found the server and we can use it */
6872 txn->flags &= ~TX_CK_MASK;
6873 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6874 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006875 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006876 break;
6877 } else {
6878 /* we found a server, but it's down,
6879 * mark it as such and go on in case
6880 * another one is available.
6881 */
6882 txn->flags &= ~TX_CK_MASK;
6883 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006884 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006885 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 srv = srv->next;
6887 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006888
Willy Tarreauf64d1412010-10-07 20:06:11 +02006889 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006890 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006892 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6893 txn->flags |= TX_CK_UNUSED;
6894 else
6895 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 /* depending on the cookie mode, we may have to either :
6899 * - delete the complete cookie if we're in insert+indirect mode, so that
6900 * the server never sees it ;
6901 * - remove the server id from the cookie value, and tag the cookie as an
6902 * application cookie so that it does not get accidentely removed later,
6903 * if we're in cookie prefix mode
6904 */
Willy Tarreau67402132012-05-31 20:40:20 +02006905 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006906 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006907
Willy Tarreau9b28e032012-10-12 23:49:43 +02006908 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 val_end += delta;
6910 next += delta;
6911 hdr_end += delta;
6912 hdr_next += delta;
6913 cur_hdr->len += delta;
6914 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006915
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006916 del_from = NULL;
6917 preserve_hdr = 1; /* we want to keep this cookie */
6918 }
6919 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006920 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006921 del_from = prev;
6922 }
6923 } else {
6924 /* This is not our cookie, so we must preserve it. But if we already
6925 * scheduled another cookie for removal, we cannot remove the
6926 * complete header, but we can remove the previous block itself.
6927 */
6928 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006930 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006931 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006932 if (att_beg >= del_from)
6933 att_beg += delta;
6934 if (att_end >= del_from)
6935 att_end += delta;
6936 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 val_end += delta;
6938 next += delta;
6939 hdr_end += delta;
6940 hdr_next += delta;
6941 cur_hdr->len += delta;
6942 http_msg_move_end(&txn->req, delta);
6943 prev = del_from;
6944 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 /* Look for the appsession cookie unless persistence must be ignored */
6949 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6950 int cmp_len, value_len;
6951 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006952
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 if (t->be->options2 & PR_O2_AS_PFX) {
6954 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6955 value_begin = att_beg + t->be->appsession_name_len;
6956 value_len = val_end - att_beg - t->be->appsession_name_len;
6957 } else {
6958 cmp_len = att_end - att_beg;
6959 value_begin = val_beg;
6960 value_len = val_end - val_beg;
6961 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006962
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006963 /* let's see if the cookie is our appcookie */
6964 if (cmp_len == t->be->appsession_name_len &&
6965 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6966 manage_client_side_appsession(t, value_begin, value_len);
6967 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968 }
6969
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006970 /* continue with next cookie on this header line */
6971 att_beg = next;
6972 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006974 /* There are no more cookies on this line.
6975 * We may still have one (or several) marked for deletion at the
6976 * end of the line. We must do this now in two ways :
6977 * - if some cookies must be preserved, we only delete from the
6978 * mark to the end of line ;
6979 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006980 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006984 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006985 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006986 cur_hdr->len += delta;
6987 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006988 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006989
6990 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006991 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6992 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006993 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006994 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006995 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006996 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006997 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006998 }
6999
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007000 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007001 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007002 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007003}
7004
7005
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006/* Iterate the same filter through all response headers contained in <rtr>.
7007 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7008 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007009int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007010{
7011 char term;
7012 char *cur_ptr, *cur_end, *cur_next;
7013 int cur_idx, old_idx, last_hdr;
7014 struct http_txn *txn = &t->txn;
7015 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007016 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017
7018 last_hdr = 0;
7019
Willy Tarreau9b28e032012-10-12 23:49:43 +02007020 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021 old_idx = 0;
7022
7023 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007024 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007026 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007027 (exp->action == ACT_ALLOW ||
7028 exp->action == ACT_DENY))
7029 return 0;
7030
7031 cur_idx = txn->hdr_idx.v[old_idx].next;
7032 if (!cur_idx)
7033 break;
7034
7035 cur_hdr = &txn->hdr_idx.v[cur_idx];
7036 cur_ptr = cur_next;
7037 cur_end = cur_ptr + cur_hdr->len;
7038 cur_next = cur_end + cur_hdr->cr + 1;
7039
7040 /* Now we have one header between cur_ptr and cur_end,
7041 * and the next header starts at cur_next.
7042 */
7043
7044 /* The annoying part is that pattern matching needs
7045 * that we modify the contents to null-terminate all
7046 * strings before testing them.
7047 */
7048
7049 term = *cur_end;
7050 *cur_end = '\0';
7051
7052 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7053 switch (exp->action) {
7054 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007055 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 last_hdr = 1;
7057 break;
7058
7059 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007060 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061 last_hdr = 1;
7062 break;
7063
7064 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007065 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7066 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067 /* FIXME: if the user adds a newline in the replacement, the
7068 * index will not be recalculated for now, and the new line
7069 * will not be counted as a new header.
7070 */
7071
7072 cur_end += delta;
7073 cur_next += delta;
7074 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007075 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076 break;
7077
7078 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007079 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080 cur_next += delta;
7081
Willy Tarreaufa355d42009-11-29 18:12:29 +01007082 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7084 txn->hdr_idx.used--;
7085 cur_hdr->len = 0;
7086 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007087 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007088 break;
7089
7090 }
7091 }
7092 if (cur_end)
7093 *cur_end = term; /* restore the string terminator */
7094
7095 /* keep the link from this header to next one in case of later
7096 * removal of next header.
7097 */
7098 old_idx = cur_idx;
7099 }
7100 return 0;
7101}
7102
7103
7104/* Apply the filter to the status line in the response buffer <rtr>.
7105 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7106 * or -1 if a replacement resulted in an invalid status line.
7107 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007108int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007109{
7110 char term;
7111 char *cur_ptr, *cur_end;
7112 int done;
7113 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007114 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007115
7116
Willy Tarreau3d300592007-03-18 18:34:41 +01007117 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007118 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007119 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007120 (exp->action == ACT_ALLOW ||
7121 exp->action == ACT_DENY))
7122 return 0;
7123 else if (exp->action == ACT_REMOVE)
7124 return 0;
7125
7126 done = 0;
7127
Willy Tarreau9b28e032012-10-12 23:49:43 +02007128 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007129 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130
7131 /* Now we have the status line between cur_ptr and cur_end */
7132
7133 /* The annoying part is that pattern matching needs
7134 * that we modify the contents to null-terminate all
7135 * strings before testing them.
7136 */
7137
7138 term = *cur_end;
7139 *cur_end = '\0';
7140
7141 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7142 switch (exp->action) {
7143 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007144 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145 done = 1;
7146 break;
7147
7148 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007149 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 done = 1;
7151 break;
7152
7153 case ACT_REPLACE:
7154 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007155 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7156 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157 /* FIXME: if the user adds a newline in the replacement, the
7158 * index will not be recalculated for now, and the new line
7159 * will not be counted as a new header.
7160 */
7161
Willy Tarreaufa355d42009-11-29 18:12:29 +01007162 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007163 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007164 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007165 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166 cur_ptr, cur_end + 1,
7167 NULL, NULL);
7168 if (unlikely(!cur_end))
7169 return -1;
7170
7171 /* we have a full respnse and we know that we have either a CR
7172 * or an LF at <ptr>.
7173 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007174 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007175 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 /* there is no point trying this regex on headers */
7177 return 1;
7178 }
7179 }
7180 *cur_end = term; /* restore the string terminator */
7181 return done;
7182}
7183
7184
7185
7186/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007187 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7189 * unparsable response.
7190 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007191int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007193 struct http_txn *txn = &s->txn;
7194 struct hdr_exp *exp;
7195
7196 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 int ret;
7198
7199 /*
7200 * The interleaving of transformations and verdicts
7201 * makes it difficult to decide to continue or stop
7202 * the evaluation.
7203 */
7204
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007205 if (txn->flags & TX_SVDENY)
7206 break;
7207
Willy Tarreau3d300592007-03-18 18:34:41 +01007208 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7210 exp->action == ACT_PASS)) {
7211 exp = exp->next;
7212 continue;
7213 }
7214
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007215 /* if this filter had a condition, evaluate it now and skip to
7216 * next filter if the condition does not match.
7217 */
7218 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007219 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007220 ret = acl_pass(ret);
7221 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7222 ret = !ret;
7223 if (!ret)
7224 continue;
7225 }
7226
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007228 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 if (unlikely(ret < 0))
7230 return -1;
7231
7232 if (likely(ret == 0)) {
7233 /* The filter did not match the response, it can be
7234 * iterated through all headers.
7235 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007236 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 }
7239 return 0;
7240}
7241
7242
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007244 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007245 * desirable to call it only when needed. This function is also used when we
7246 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007248void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249{
7250 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007251 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007252 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007253 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007254 char *hdr_beg, *hdr_end, *hdr_next;
7255 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256
Willy Tarreaua15645d2007-03-18 16:22:39 +01007257 /* Iterate through the headers.
7258 * we start with the start line.
7259 */
7260 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007261 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262
7263 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7264 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007265 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007266
7267 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007268 hdr_beg = hdr_next;
7269 hdr_end = hdr_beg + cur_hdr->len;
7270 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271
Willy Tarreau24581ba2010-08-31 22:39:35 +02007272 /* We have one full header between hdr_beg and hdr_end, and the
7273 * next header starts at hdr_next. We're only interested in
7274 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 */
7276
Willy Tarreau24581ba2010-08-31 22:39:35 +02007277 is_cookie2 = 0;
7278 prev = hdr_beg + 10;
7279 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007280 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007281 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7282 if (!val) {
7283 old_idx = cur_idx;
7284 continue;
7285 }
7286 is_cookie2 = 1;
7287 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288 }
7289
Willy Tarreau24581ba2010-08-31 22:39:35 +02007290 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7291 * <prev> points to the colon.
7292 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007293 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294
Willy Tarreau24581ba2010-08-31 22:39:35 +02007295 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7296 * check-cache is enabled) and we are not interested in checking
7297 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007298 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007299 if (t->be->cookie_name == NULL &&
7300 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007301 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302 return;
7303
Willy Tarreau24581ba2010-08-31 22:39:35 +02007304 /* OK so now we know we have to process this response cookie.
7305 * The format of the Set-Cookie header is slightly different
7306 * from the format of the Cookie header in that it does not
7307 * support the comma as a cookie delimiter (thus the header
7308 * cannot be folded) because the Expires attribute described in
7309 * the original Netscape's spec may contain an unquoted date
7310 * with a comma inside. We have to live with this because
7311 * many browsers don't support Max-Age and some browsers don't
7312 * support quoted strings. However the Set-Cookie2 header is
7313 * clean.
7314 *
7315 * We have to keep multiple pointers in order to support cookie
7316 * removal at the beginning, middle or end of header without
7317 * corrupting the header (in case of set-cookie2). A special
7318 * pointer, <scav> points to the beginning of the set-cookie-av
7319 * fields after the first semi-colon. The <next> pointer points
7320 * either to the end of line (set-cookie) or next unquoted comma
7321 * (set-cookie2). All of these headers are valid :
7322 *
7323 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7324 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7325 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7326 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7327 * | | | | | | | | | |
7328 * | | | | | | | | +-> next hdr_end <--+
7329 * | | | | | | | +------------> scav
7330 * | | | | | | +--------------> val_end
7331 * | | | | | +--------------------> val_beg
7332 * | | | | +----------------------> equal
7333 * | | | +------------------------> att_end
7334 * | | +----------------------------> att_beg
7335 * | +------------------------------> prev
7336 * +-----------------------------------------> hdr_beg
7337 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007338
Willy Tarreau24581ba2010-08-31 22:39:35 +02007339 for (; prev < hdr_end; prev = next) {
7340 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341
Willy Tarreau24581ba2010-08-31 22:39:35 +02007342 /* find att_beg */
7343 att_beg = prev + 1;
7344 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7345 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007346
Willy Tarreau24581ba2010-08-31 22:39:35 +02007347 /* find att_end : this is the first character after the last non
7348 * space before the equal. It may be equal to hdr_end.
7349 */
7350 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007351
Willy Tarreau24581ba2010-08-31 22:39:35 +02007352 while (equal < hdr_end) {
7353 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7354 break;
7355 if (http_is_spht[(unsigned char)*equal++])
7356 continue;
7357 att_end = equal;
7358 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359
Willy Tarreau24581ba2010-08-31 22:39:35 +02007360 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7361 * is between <att_beg> and <equal>, both may be identical.
7362 */
7363
7364 /* look for end of cookie if there is an equal sign */
7365 if (equal < hdr_end && *equal == '=') {
7366 /* look for the beginning of the value */
7367 val_beg = equal + 1;
7368 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7369 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370
Willy Tarreau24581ba2010-08-31 22:39:35 +02007371 /* find the end of the value, respecting quotes */
7372 next = find_cookie_value_end(val_beg, hdr_end);
7373
7374 /* make val_end point to the first white space or delimitor after the value */
7375 val_end = next;
7376 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7377 val_end--;
7378 } else {
7379 /* <equal> points to next comma, semi-colon or EOL */
7380 val_beg = val_end = next = equal;
7381 }
7382
7383 if (next < hdr_end) {
7384 /* Set-Cookie2 supports multiple cookies, and <next> points to
7385 * a colon or semi-colon before the end. So skip all attr-value
7386 * pairs and look for the next comma. For Set-Cookie, since
7387 * commas are permitted in values, skip to the end.
7388 */
7389 if (is_cookie2)
7390 next = find_hdr_value_end(next, hdr_end);
7391 else
7392 next = hdr_end;
7393 }
7394
7395 /* Now everything is as on the diagram above */
7396
7397 /* Ignore cookies with no equal sign */
7398 if (equal == val_end)
7399 continue;
7400
7401 /* If there are spaces around the equal sign, we need to
7402 * strip them otherwise we'll get trouble for cookie captures,
7403 * or even for rewrites. Since this happens extremely rarely,
7404 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007406 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7407 int stripped_before = 0;
7408 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409
Willy Tarreau24581ba2010-08-31 22:39:35 +02007410 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007411 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007412 equal += stripped_before;
7413 val_beg += stripped_before;
7414 }
7415
7416 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007417 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007418 val_beg += stripped_after;
7419 stripped_before += stripped_after;
7420 }
7421
7422 val_end += stripped_before;
7423 next += stripped_before;
7424 hdr_end += stripped_before;
7425 hdr_next += stripped_before;
7426 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007427 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007428 }
7429
7430 /* First, let's see if we want to capture this cookie. We check
7431 * that we don't already have a server side cookie, because we
7432 * can only capture one. Also as an optimisation, we ignore
7433 * cookies shorter than the declared name.
7434 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007435 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007436 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007437 (val_end - att_beg >= t->fe->capture_namelen) &&
7438 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7439 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007440 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441 Alert("HTTP logging : out of memory.\n");
7442 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007443 else {
7444 if (log_len > t->fe->capture_len)
7445 log_len = t->fe->capture_len;
7446 memcpy(txn->srv_cookie, att_beg, log_len);
7447 txn->srv_cookie[log_len] = 0;
7448 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007449 }
7450
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007451 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007453 if (!(t->flags & SN_IGNORE_PRST) &&
7454 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7455 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007456 /* assume passive cookie by default */
7457 txn->flags &= ~TX_SCK_MASK;
7458 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007459
7460 /* If the cookie is in insert mode on a known server, we'll delete
7461 * this occurrence because we'll insert another one later.
7462 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007463 * a direct access.
7464 */
Willy Tarreau67402132012-05-31 20:40:20 +02007465 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007466 /* The "preserve" flag was set, we don't want to touch the
7467 * server's cookie.
7468 */
7469 }
Willy Tarreau67402132012-05-31 20:40:20 +02007470 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7471 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007472 /* this cookie must be deleted */
7473 if (*prev == ':' && next == hdr_end) {
7474 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007475 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007476 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7477 txn->hdr_idx.used--;
7478 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007479 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007480 hdr_next += delta;
7481 http_msg_move_end(&txn->rsp, delta);
7482 /* note: while both invalid now, <next> and <hdr_end>
7483 * are still equal, so the for() will stop as expected.
7484 */
7485 } else {
7486 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007487 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007488 next = prev;
7489 hdr_end += delta;
7490 hdr_next += delta;
7491 cur_hdr->len += delta;
7492 http_msg_move_end(&txn->rsp, delta);
7493 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007494 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007495 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007497 }
Willy Tarreau67402132012-05-31 20:40:20 +02007498 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007500 * with this server since we know it.
7501 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007502 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007503 next += delta;
7504 hdr_end += delta;
7505 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007507 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007508
Willy Tarreauf1348312010-10-07 15:54:11 +02007509 txn->flags &= ~TX_SCK_MASK;
7510 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007511 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007512 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007513 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007514 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007515 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007516 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007517 next += delta;
7518 hdr_end += delta;
7519 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007521 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007522
Willy Tarreau827aee92011-03-10 16:55:02 +01007523 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007524 txn->flags &= ~TX_SCK_MASK;
7525 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007526 }
7527 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007528 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7529 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007530 int cmp_len, value_len;
7531 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007532
Cyril Bontéb21570a2009-11-29 20:04:48 +01007533 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007534 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7535 value_begin = att_beg + t->be->appsession_name_len;
7536 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007537 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007538 cmp_len = att_end - att_beg;
7539 value_begin = val_beg;
7540 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007541 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007542
Cyril Bonté17530c32010-04-06 21:11:10 +02007543 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007544 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7545 /* free a possibly previously allocated memory */
7546 pool_free2(apools.sessid, txn->sessid);
7547
Cyril Bontéb21570a2009-11-29 20:04:48 +01007548 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007549 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007550 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7551 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7552 return;
7553 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007554 memcpy(txn->sessid, value_begin, value_len);
7555 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 }
7558 /* that's done for this cookie, check the next one on the same
7559 * line when next != hdr_end (only if is_cookie2).
7560 */
7561 }
7562 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007563 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007564 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007565
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007566 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007567 appsess *asession = NULL;
7568 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007569 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007570 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007571 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007572 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7573 Alert("Not enough Memory process_srv():asession:calloc().\n");
7574 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7575 return;
7576 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007577 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7578
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007579 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7580 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7581 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007582 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007583 return;
7584 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007585 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007586 asession->sessid[t->be->appsession_len] = 0;
7587
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007588 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007589 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007590 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007591 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007592 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007593 return;
7594 }
7595 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007596 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007597
7598 asession->request_count = 0;
7599 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7600 }
7601
7602 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7603 asession->request_count++;
7604 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605}
7606
7607
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608/*
7609 * Check if response is cacheable or not. Updates t->flags.
7610 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007611void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007612{
7613 struct http_txn *txn = &t->txn;
7614 char *p1, *p2;
7615
7616 char *cur_ptr, *cur_end, *cur_next;
7617 int cur_idx;
7618
Willy Tarreau5df51872007-11-25 16:20:08 +01007619 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620 return;
7621
7622 /* Iterate through the headers.
7623 * we start with the start line.
7624 */
7625 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007626 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627
7628 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7629 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007630 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631
7632 cur_hdr = &txn->hdr_idx.v[cur_idx];
7633 cur_ptr = cur_next;
7634 cur_end = cur_ptr + cur_hdr->len;
7635 cur_next = cur_end + cur_hdr->cr + 1;
7636
7637 /* We have one full header between cur_ptr and cur_end, and the
7638 * next header starts at cur_next. We're only interested in
7639 * "Cookie:" headers.
7640 */
7641
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007642 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7643 if (val) {
7644 if ((cur_end - (cur_ptr + val) >= 8) &&
7645 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7646 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7647 return;
7648 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 }
7650
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007651 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7652 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007653 continue;
7654
7655 /* OK, right now we know we have a cache-control header at cur_ptr */
7656
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007657 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007658
7659 if (p1 >= cur_end) /* no more info */
7660 continue;
7661
7662 /* p1 is at the beginning of the value */
7663 p2 = p1;
7664
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007665 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007666 p2++;
7667
7668 /* we have a complete value between p1 and p2 */
7669 if (p2 < cur_end && *p2 == '=') {
7670 /* we have something of the form no-cache="set-cookie" */
7671 if ((cur_end - p1 >= 21) &&
7672 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7673 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007674 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007675 continue;
7676 }
7677
7678 /* OK, so we know that either p2 points to the end of string or to a comma */
7679 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007680 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7682 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7683 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007684 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685 return;
7686 }
7687
7688 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007689 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690 continue;
7691 }
7692 }
7693}
7694
7695
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696/*
7697 * Try to retrieve a known appsession in the URI, then the associated server.
7698 * If the server is found, it's assigned to the session.
7699 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007700void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007702 char *end_params, *first_param, *cur_param, *next_param;
7703 char separator;
7704 int value_len;
7705
7706 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007707
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007708 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007709 (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 +01007710 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712
Cyril Bontéb21570a2009-11-29 20:04:48 +01007713 first_param = NULL;
7714 switch (mode) {
7715 case PR_O2_AS_M_PP:
7716 first_param = memchr(begin, ';', len);
7717 break;
7718 case PR_O2_AS_M_QS:
7719 first_param = memchr(begin, '?', len);
7720 break;
7721 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722
Cyril Bontéb21570a2009-11-29 20:04:48 +01007723 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007724 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007726
Cyril Bontéb21570a2009-11-29 20:04:48 +01007727 switch (mode) {
7728 case PR_O2_AS_M_PP:
7729 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7730 end_params = (char *) begin + len;
7731 }
7732 separator = ';';
7733 break;
7734 case PR_O2_AS_M_QS:
7735 end_params = (char *) begin + len;
7736 separator = '&';
7737 break;
7738 default:
7739 /* unknown mode, shouldn't happen */
7740 return;
7741 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007742
Cyril Bontéb21570a2009-11-29 20:04:48 +01007743 cur_param = next_param = end_params;
7744 while (cur_param > first_param) {
7745 cur_param--;
7746 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7747 /* let's see if this is the appsession parameter */
7748 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7749 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7750 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7751 /* Cool... it's the right one */
7752 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7753 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7754 if (value_len > 0) {
7755 manage_client_side_appsession(t, cur_param, value_len);
7756 }
7757 break;
7758 }
7759 next_param = cur_param;
7760 }
7761 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007762#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007763 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007764 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007765#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007766}
7767
Willy Tarreaub2513902006-12-17 14:52:38 +01007768/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007769 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007770 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007771 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007772 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007773 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007774 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007775 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007776 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007777int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007778{
7779 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007780 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007781 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007782
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007783 if (!uri_auth)
7784 return 0;
7785
Cyril Bonté70be45d2010-10-12 00:14:35 +02007786 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007787 return 0;
7788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007789 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007790 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007791 return 0;
7792
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007793 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007794 return 0;
7795
Willy Tarreaub2513902006-12-17 14:52:38 +01007796 return 1;
7797}
7798
Willy Tarreau4076a152009-04-02 15:18:36 +02007799/*
7800 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007801 * By default it tries to report the error position as msg->err_pos. However if
7802 * this one is not set, it will then report msg->next, which is the last known
7803 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007804 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007805 */
7806void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007807 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007808 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007809{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007810 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007811 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007812
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 es->len = MIN(chn->buf->i, sizeof(es->buf));
7814 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007815 len1 = MIN(len1, es->len);
7816 len2 = es->len - len1; /* remaining data if buffer wraps */
7817
Willy Tarreau9b28e032012-10-12 23:49:43 +02007818 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007819 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007820 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007821
Willy Tarreau4076a152009-04-02 15:18:36 +02007822 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007823 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007824 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007825 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007826
Willy Tarreau4076a152009-04-02 15:18:36 +02007827 es->when = date; // user-visible date
7828 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007829 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007830 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007831 if (objt_conn(s->req->prod->end))
7832 es->src = __objt_conn(s->req->prod->end)->addr.from;
7833 else
7834 memset(&es->src, 0, sizeof(es->src));
7835
Willy Tarreau078272e2010-12-12 12:46:33 +01007836 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007837 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007838 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007839 es->s_flags = s->flags;
7840 es->t_flags = s->txn.flags;
7841 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007842 es->b_out = chn->buf->o;
7843 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007844 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007845 es->m_clen = msg->chunk_len;
7846 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007847}
Willy Tarreaub2513902006-12-17 14:52:38 +01007848
Willy Tarreau294c4732011-12-16 21:35:50 +01007849/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7850 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7851 * performed over the whole headers. Otherwise it must contain a valid header
7852 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7853 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7854 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7855 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007856 * -1. The value fetch stops at commas, so this function is suited for use with
7857 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007858 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007859 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007860unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007861 struct hdr_idx *idx, int occ,
7862 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007863{
Willy Tarreau294c4732011-12-16 21:35:50 +01007864 struct hdr_ctx local_ctx;
7865 char *ptr_hist[MAX_HDR_HISTORY];
7866 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007867 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007868 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007869
Willy Tarreau294c4732011-12-16 21:35:50 +01007870 if (!ctx) {
7871 local_ctx.idx = 0;
7872 ctx = &local_ctx;
7873 }
7874
Willy Tarreaubce70882009-09-07 11:51:47 +02007875 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007876 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007877 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007878 occ--;
7879 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007880 *vptr = ctx->line + ctx->val;
7881 *vlen = ctx->vlen;
7882 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007883 }
7884 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007885 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007886 }
7887
7888 /* negative occurrence, we scan all the list then walk back */
7889 if (-occ > MAX_HDR_HISTORY)
7890 return 0;
7891
Willy Tarreau294c4732011-12-16 21:35:50 +01007892 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007893 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007894 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7895 len_hist[hist_ptr] = ctx->vlen;
7896 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007897 hist_ptr = 0;
7898 found++;
7899 }
7900 if (-occ > found)
7901 return 0;
7902 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007903 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7904 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7905 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007906 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007907 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007908 if (hist_ptr >= MAX_HDR_HISTORY)
7909 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007910 *vptr = ptr_hist[hist_ptr];
7911 *vlen = len_hist[hist_ptr];
7912 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007913}
7914
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007915/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7916 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7917 * performed over the whole headers. Otherwise it must contain a valid header
7918 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7919 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7920 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7921 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7922 * -1. This function differs from http_get_hdr() in that it only returns full
7923 * line header values and does not stop at commas.
7924 * The return value is 0 if nothing was found, or non-zero otherwise.
7925 */
7926unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7927 struct hdr_idx *idx, int occ,
7928 struct hdr_ctx *ctx, char **vptr, int *vlen)
7929{
7930 struct hdr_ctx local_ctx;
7931 char *ptr_hist[MAX_HDR_HISTORY];
7932 int len_hist[MAX_HDR_HISTORY];
7933 unsigned int hist_ptr;
7934 int found;
7935
7936 if (!ctx) {
7937 local_ctx.idx = 0;
7938 ctx = &local_ctx;
7939 }
7940
7941 if (occ >= 0) {
7942 /* search from the beginning */
7943 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7944 occ--;
7945 if (occ <= 0) {
7946 *vptr = ctx->line + ctx->val;
7947 *vlen = ctx->vlen;
7948 return 1;
7949 }
7950 }
7951 return 0;
7952 }
7953
7954 /* negative occurrence, we scan all the list then walk back */
7955 if (-occ > MAX_HDR_HISTORY)
7956 return 0;
7957
7958 found = hist_ptr = 0;
7959 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7960 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7961 len_hist[hist_ptr] = ctx->vlen;
7962 if (++hist_ptr >= MAX_HDR_HISTORY)
7963 hist_ptr = 0;
7964 found++;
7965 }
7966 if (-occ > found)
7967 return 0;
7968 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7969 * find occurrence -occ, so we have to check [hist_ptr+occ].
7970 */
7971 hist_ptr += occ;
7972 if (hist_ptr >= MAX_HDR_HISTORY)
7973 hist_ptr -= MAX_HDR_HISTORY;
7974 *vptr = ptr_hist[hist_ptr];
7975 *vlen = len_hist[hist_ptr];
7976 return 1;
7977}
7978
Willy Tarreaubaaee002006-06-26 02:48:02 +02007979/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007980 * Print a debug line with a header. Always stop at the first CR or LF char,
7981 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7982 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007983 */
7984void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7985{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007986 int max;
7987 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007988 dir,
7989 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
7990 objt_conn(t->req->cons->end) ? (unsigned short)objt_conn(t->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007991
7992 for (max = 0; start + max < end; max++)
7993 if (start[max] == '\r' || start[max] == '\n')
7994 break;
7995
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007996 UBOUND(max, trash.size - trash.len - 3);
7997 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7998 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007999 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008000}
8001
Willy Tarreau0937bc42009-12-22 15:03:09 +01008002/*
8003 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8004 * the required fields are properly allocated and that we only need to (re)init
8005 * them. This should be used before processing any new request.
8006 */
8007void http_init_txn(struct session *s)
8008{
8009 struct http_txn *txn = &s->txn;
8010 struct proxy *fe = s->fe;
8011
8012 txn->flags = 0;
8013 txn->status = -1;
8014
Willy Tarreauf64d1412010-10-07 20:06:11 +02008015 txn->cookie_first_date = 0;
8016 txn->cookie_last_date = 0;
8017
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008018 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008019 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008020 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008021 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008022 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008023 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008024 txn->req.chunk_len = 0LL;
8025 txn->req.body_len = 0LL;
8026 txn->rsp.chunk_len = 0LL;
8027 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008028 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8029 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008030 txn->req.chn = s->req;
8031 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008032
8033 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008034
8035 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8036 if (fe->options2 & PR_O2_REQBUG_OK)
8037 txn->req.err_pos = -1; /* let buggy requests pass */
8038
Willy Tarreau46023632010-01-07 22:51:47 +01008039 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008040 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8041
Willy Tarreau46023632010-01-07 22:51:47 +01008042 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008043 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8044
8045 if (txn->hdr_idx.v)
8046 hdr_idx_init(&txn->hdr_idx);
8047}
8048
8049/* to be used at the end of a transaction */
8050void http_end_txn(struct session *s)
8051{
8052 struct http_txn *txn = &s->txn;
8053
8054 /* these ones will have been dynamically allocated */
8055 pool_free2(pool2_requri, txn->uri);
8056 pool_free2(pool2_capture, txn->cli_cookie);
8057 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008058 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008059 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008060
William Lallemanda73203e2012-03-12 12:48:57 +01008061 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008062 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008063 txn->uri = NULL;
8064 txn->srv_cookie = NULL;
8065 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008066
8067 if (txn->req.cap) {
8068 struct cap_hdr *h;
8069 for (h = s->fe->req_cap; h; h = h->next)
8070 pool_free2(h->pool, txn->req.cap[h->index]);
8071 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8072 }
8073
8074 if (txn->rsp.cap) {
8075 struct cap_hdr *h;
8076 for (h = s->fe->rsp_cap; h; h = h->next)
8077 pool_free2(h->pool, txn->rsp.cap[h->index]);
8078 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8079 }
8080
Willy Tarreau0937bc42009-12-22 15:03:09 +01008081}
8082
8083/* to be used at the end of a transaction to prepare a new one */
8084void http_reset_txn(struct session *s)
8085{
8086 http_end_txn(s);
8087 http_init_txn(s);
8088
8089 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008090 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008091 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008092 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008093 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008094 /* re-init store persistence */
8095 s->store_count = 0;
8096
Willy Tarreau0937bc42009-12-22 15:03:09 +01008097 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008098
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008099 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008100
Willy Tarreau739cfba2010-01-25 23:11:14 +01008101 /* We must trim any excess data from the response buffer, because we
8102 * may have blocked an invalid response from a server that we don't
8103 * want to accidentely forward once we disable the analysers, nor do
8104 * we want those data to come along with next response. A typical
8105 * example of such data would be from a buggy server responding to
8106 * a HEAD with some data, or sending more than the advertised
8107 * content-length.
8108 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008109 if (unlikely(s->rep->buf->i))
8110 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008111
Willy Tarreau0937bc42009-12-22 15:03:09 +01008112 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008113 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008114
Willy Tarreaud04e8582010-05-31 12:31:35 +02008115 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008116 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008117
8118 s->req->rex = TICK_ETERNITY;
8119 s->req->wex = TICK_ETERNITY;
8120 s->req->analyse_exp = TICK_ETERNITY;
8121 s->rep->rex = TICK_ETERNITY;
8122 s->rep->wex = TICK_ETERNITY;
8123 s->rep->analyse_exp = TICK_ETERNITY;
8124}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008125
Willy Tarreauff011f22011-01-06 17:51:27 +01008126void free_http_req_rules(struct list *r) {
8127 struct http_req_rule *tr, *pr;
8128
8129 list_for_each_entry_safe(pr, tr, r, list) {
8130 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008131 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008132 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008133
8134 free(pr);
8135 }
8136}
8137
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008138/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008139struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8140{
8141 struct http_req_rule *rule;
8142 int cur_arg;
8143
8144 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8145 if (!rule) {
8146 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008147 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008148 }
8149
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008150 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008151 rule->action = HTTP_REQ_ACT_ALLOW;
8152 cur_arg = 1;
8153 } else if (!strcmp(args[0], "deny")) {
8154 rule->action = HTTP_REQ_ACT_DENY;
8155 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008156 } else if (!strcmp(args[0], "tarpit")) {
8157 rule->action = HTTP_REQ_ACT_TARPIT;
8158 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008159 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008160 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008161 cur_arg = 1;
8162
8163 while(*args[cur_arg]) {
8164 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008165 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008166 cur_arg+=2;
8167 continue;
8168 } else
8169 break;
8170 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008171 } else if (!strcmp(args[0], "set-nice")) {
8172 rule->action = HTTP_REQ_ACT_SET_NICE;
8173 cur_arg = 1;
8174
8175 if (!*args[cur_arg] ||
8176 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8177 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8178 file, linenum, args[0]);
8179 goto out_err;
8180 }
8181 rule->arg.nice = atoi(args[cur_arg]);
8182 if (rule->arg.nice < -1024)
8183 rule->arg.nice = -1024;
8184 else if (rule->arg.nice > 1024)
8185 rule->arg.nice = 1024;
8186 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008187 } else if (!strcmp(args[0], "set-tos")) {
8188#ifdef IP_TOS
8189 char *err;
8190 rule->action = HTTP_REQ_ACT_SET_TOS;
8191 cur_arg = 1;
8192
8193 if (!*args[cur_arg] ||
8194 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8195 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8196 file, linenum, args[0]);
8197 goto out_err;
8198 }
8199
8200 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8201 if (err && *err != '\0') {
8202 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8203 file, linenum, err, args[0]);
8204 goto out_err;
8205 }
8206 cur_arg++;
8207#else
8208 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8209 goto out_err;
8210#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008211 } else if (!strcmp(args[0], "set-mark")) {
8212#ifdef SO_MARK
8213 char *err;
8214 rule->action = HTTP_REQ_ACT_SET_MARK;
8215 cur_arg = 1;
8216
8217 if (!*args[cur_arg] ||
8218 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8219 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8220 file, linenum, args[0]);
8221 goto out_err;
8222 }
8223
8224 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8225 if (err && *err != '\0') {
8226 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8227 file, linenum, err, args[0]);
8228 goto out_err;
8229 }
8230 cur_arg++;
8231 global.last_checks |= LSTCHK_NETADM;
8232#else
8233 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8234 goto out_err;
8235#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008236 } else if (!strcmp(args[0], "set-log-level")) {
8237 rule->action = HTTP_REQ_ACT_SET_LOGL;
8238 cur_arg = 1;
8239
8240 if (!*args[cur_arg] ||
8241 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8242 bad_log_level:
8243 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8244 file, linenum, args[0]);
8245 goto out_err;
8246 }
8247 if (strcmp(args[cur_arg], "silent") == 0)
8248 rule->arg.loglevel = -1;
8249 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8250 goto bad_log_level;
8251 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008252 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8253 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8254 cur_arg = 1;
8255
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008256 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8257 (*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 +01008258 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8259 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008260 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008261 }
8262
8263 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8264 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8265 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008266
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008267 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008268 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8269 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008270 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008271 } else if (strcmp(args[0], "redirect") == 0) {
8272 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008273 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008274
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008275 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008276 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8277 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8278 goto out_err;
8279 }
8280
8281 /* this redirect rule might already contain a parsed condition which
8282 * we'll pass to the http-request rule.
8283 */
8284 rule->action = HTTP_REQ_ACT_REDIR;
8285 rule->arg.redir = redir;
8286 rule->cond = redir->cond;
8287 redir->cond = NULL;
8288 cur_arg = 2;
8289 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008290 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008291 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 +01008292 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008293 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008294 }
8295
8296 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8297 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008298 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008299
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008300 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8301 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8302 file, linenum, args[0], errmsg);
8303 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008304 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008305 }
8306 rule->cond = cond;
8307 }
8308 else if (*args[cur_arg]) {
8309 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8310 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8311 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008312 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008313 }
8314
8315 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008316 out_err:
8317 free(rule);
8318 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008319}
8320
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008321/* parse an "http-respose" rule */
8322struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8323{
8324 struct http_res_rule *rule;
8325 int cur_arg;
8326
8327 rule = calloc(1, sizeof(*rule));
8328 if (!rule) {
8329 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8330 goto out_err;
8331 }
8332
8333 if (!strcmp(args[0], "allow")) {
8334 rule->action = HTTP_RES_ACT_ALLOW;
8335 cur_arg = 1;
8336 } else if (!strcmp(args[0], "deny")) {
8337 rule->action = HTTP_RES_ACT_DENY;
8338 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008339 } else if (!strcmp(args[0], "set-nice")) {
8340 rule->action = HTTP_RES_ACT_SET_NICE;
8341 cur_arg = 1;
8342
8343 if (!*args[cur_arg] ||
8344 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8345 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8346 file, linenum, args[0]);
8347 goto out_err;
8348 }
8349 rule->arg.nice = atoi(args[cur_arg]);
8350 if (rule->arg.nice < -1024)
8351 rule->arg.nice = -1024;
8352 else if (rule->arg.nice > 1024)
8353 rule->arg.nice = 1024;
8354 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008355 } else if (!strcmp(args[0], "set-tos")) {
8356#ifdef IP_TOS
8357 char *err;
8358 rule->action = HTTP_RES_ACT_SET_TOS;
8359 cur_arg = 1;
8360
8361 if (!*args[cur_arg] ||
8362 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8363 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8364 file, linenum, args[0]);
8365 goto out_err;
8366 }
8367
8368 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8369 if (err && *err != '\0') {
8370 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8371 file, linenum, err, args[0]);
8372 goto out_err;
8373 }
8374 cur_arg++;
8375#else
8376 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8377 goto out_err;
8378#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008379 } else if (!strcmp(args[0], "set-mark")) {
8380#ifdef SO_MARK
8381 char *err;
8382 rule->action = HTTP_RES_ACT_SET_MARK;
8383 cur_arg = 1;
8384
8385 if (!*args[cur_arg] ||
8386 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8387 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8388 file, linenum, args[0]);
8389 goto out_err;
8390 }
8391
8392 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8393 if (err && *err != '\0') {
8394 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8395 file, linenum, err, args[0]);
8396 goto out_err;
8397 }
8398 cur_arg++;
8399 global.last_checks |= LSTCHK_NETADM;
8400#else
8401 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8402 goto out_err;
8403#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008404 } else if (!strcmp(args[0], "set-log-level")) {
8405 rule->action = HTTP_RES_ACT_SET_LOGL;
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 bad_log_level:
8411 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8412 file, linenum, args[0]);
8413 goto out_err;
8414 }
8415 if (strcmp(args[cur_arg], "silent") == 0)
8416 rule->arg.loglevel = -1;
8417 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8418 goto bad_log_level;
8419 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008420 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8421 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8422 cur_arg = 1;
8423
8424 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8425 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8426 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8427 file, linenum, args[0]);
8428 goto out_err;
8429 }
8430
8431 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8432 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8433 LIST_INIT(&rule->arg.hdr_add.fmt);
8434
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008435 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008436 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8437 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8438 cur_arg += 2;
8439 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008440 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 +02008441 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8442 goto out_err;
8443 }
8444
8445 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8446 struct acl_cond *cond;
8447 char *errmsg = NULL;
8448
8449 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8450 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8451 file, linenum, args[0], errmsg);
8452 free(errmsg);
8453 goto out_err;
8454 }
8455 rule->cond = cond;
8456 }
8457 else if (*args[cur_arg]) {
8458 Alert("parsing [%s:%d]: 'http-response %s' expects"
8459 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8460 file, linenum, args[0], args[cur_arg]);
8461 goto out_err;
8462 }
8463
8464 return rule;
8465 out_err:
8466 free(rule);
8467 return NULL;
8468}
8469
Willy Tarreau4baae242012-12-27 12:00:31 +01008470/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008471 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8472 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008473 */
8474struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008475 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008476{
8477 struct redirect_rule *rule;
8478 int cur_arg;
8479 int type = REDIRECT_TYPE_NONE;
8480 int code = 302;
8481 const char *destination = NULL;
8482 const char *cookie = NULL;
8483 int cookie_set = 0;
8484 unsigned int flags = REDIRECT_FLAG_NONE;
8485 struct acl_cond *cond = NULL;
8486
8487 cur_arg = 0;
8488 while (*(args[cur_arg])) {
8489 if (strcmp(args[cur_arg], "location") == 0) {
8490 if (!*args[cur_arg + 1])
8491 goto missing_arg;
8492
8493 type = REDIRECT_TYPE_LOCATION;
8494 cur_arg++;
8495 destination = args[cur_arg];
8496 }
8497 else if (strcmp(args[cur_arg], "prefix") == 0) {
8498 if (!*args[cur_arg + 1])
8499 goto missing_arg;
8500
8501 type = REDIRECT_TYPE_PREFIX;
8502 cur_arg++;
8503 destination = args[cur_arg];
8504 }
8505 else if (strcmp(args[cur_arg], "scheme") == 0) {
8506 if (!*args[cur_arg + 1])
8507 goto missing_arg;
8508
8509 type = REDIRECT_TYPE_SCHEME;
8510 cur_arg++;
8511 destination = args[cur_arg];
8512 }
8513 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8514 if (!*args[cur_arg + 1])
8515 goto missing_arg;
8516
8517 cur_arg++;
8518 cookie = args[cur_arg];
8519 cookie_set = 1;
8520 }
8521 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8522 if (!*args[cur_arg + 1])
8523 goto missing_arg;
8524
8525 cur_arg++;
8526 cookie = args[cur_arg];
8527 cookie_set = 0;
8528 }
8529 else if (strcmp(args[cur_arg], "code") == 0) {
8530 if (!*args[cur_arg + 1])
8531 goto missing_arg;
8532
8533 cur_arg++;
8534 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008535 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008536 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008537 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008538 args[cur_arg - 1], args[cur_arg]);
8539 return NULL;
8540 }
8541 }
8542 else if (!strcmp(args[cur_arg],"drop-query")) {
8543 flags |= REDIRECT_FLAG_DROP_QS;
8544 }
8545 else if (!strcmp(args[cur_arg],"append-slash")) {
8546 flags |= REDIRECT_FLAG_APPEND_SLASH;
8547 }
8548 else if (strcmp(args[cur_arg], "if") == 0 ||
8549 strcmp(args[cur_arg], "unless") == 0) {
8550 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8551 if (!cond) {
8552 memprintf(errmsg, "error in condition: %s", *errmsg);
8553 return NULL;
8554 }
8555 break;
8556 }
8557 else {
8558 memprintf(errmsg,
8559 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8560 args[cur_arg]);
8561 return NULL;
8562 }
8563 cur_arg++;
8564 }
8565
8566 if (type == REDIRECT_TYPE_NONE) {
8567 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8568 return NULL;
8569 }
8570
8571 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8572 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008573 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008574
8575 if (!use_fmt) {
8576 /* old-style static redirect rule */
8577 rule->rdr_str = strdup(destination);
8578 rule->rdr_len = strlen(destination);
8579 }
8580 else {
8581 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008582
8583 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8584 * if prefix == "/", we don't want to add anything, otherwise it
8585 * makes it hard for the user to configure a self-redirection.
8586 */
8587 proxy->conf.args.ctx = ARGC_RDR;
8588 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8589 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8590 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8591 }
8592 }
8593
Willy Tarreau4baae242012-12-27 12:00:31 +01008594 if (cookie) {
8595 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8596 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8597 */
8598 rule->cookie_len = strlen(cookie);
8599 if (cookie_set) {
8600 rule->cookie_str = malloc(rule->cookie_len + 10);
8601 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8602 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8603 rule->cookie_len += 9;
8604 } else {
8605 rule->cookie_str = malloc(rule->cookie_len + 21);
8606 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8607 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8608 rule->cookie_len += 20;
8609 }
8610 }
8611 rule->type = type;
8612 rule->code = code;
8613 rule->flags = flags;
8614 LIST_INIT(&rule->list);
8615 return rule;
8616
8617 missing_arg:
8618 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8619 return NULL;
8620}
8621
Willy Tarreau8797c062007-05-07 00:55:35 +02008622/************************************************************************/
8623/* The code below is dedicated to ACL parsing and matching */
8624/************************************************************************/
8625
8626
Willy Tarreau14174bc2012-04-16 14:34:04 +02008627/* This function ensures that the prerequisites for an L7 fetch are ready,
8628 * which means that a request or response is ready. If some data is missing,
8629 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008630 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8631 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008632 *
8633 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008634 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8635 * decide whether or not an HTTP message is present ;
8636 * 0 if the requested data cannot be fetched or if it is certain that
8637 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008638 * 1 if an HTTP message is ready
8639 */
8640static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008641smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008642 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008643{
8644 struct http_txn *txn = l7;
8645 struct http_msg *msg = &txn->req;
8646
8647 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8648 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8649 */
8650
8651 if (unlikely(!s || !txn))
8652 return 0;
8653
8654 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008655 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008656
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008657 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008658 if (unlikely(!s->req))
8659 return 0;
8660
Willy Tarreauaae75e32013-03-29 12:31:49 +01008661 /* If the buffer does not leave enough free space at the end,
8662 * we must first realign it.
8663 */
8664 if (s->req->buf->p > s->req->buf->data &&
8665 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8666 buffer_slow_realign(s->req->buf);
8667
Willy Tarreau14174bc2012-04-16 14:34:04 +02008668 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008669 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008670 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008671
8672 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008673 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008674 http_msg_analyzer(msg, &txn->hdr_idx);
8675
8676 /* Still no valid request ? */
8677 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008678 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008679 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008680 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008681 }
8682 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008683 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008684 return 0;
8685 }
8686
8687 /* OK we just got a valid HTTP request. We have some minor
8688 * preparation to perform so that further checks can rely
8689 * on HTTP tests.
8690 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008691
8692 /* If the request was parsed but was too large, we must absolutely
8693 * return an error so that it is not processed. At the moment this
8694 * cannot happen, but if the parsers are to change in the future,
8695 * we want this check to be maintained.
8696 */
8697 if (unlikely(s->req->buf->i + s->req->buf->p >
8698 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8699 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008700 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008701 return 1;
8702 }
8703
Willy Tarreau9b28e032012-10-12 23:49:43 +02008704 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008705 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8706 s->flags |= SN_REDIRECTABLE;
8707
Willy Tarreau506d0502013-07-06 13:29:24 +02008708 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8709 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008710 }
8711
Willy Tarreau506d0502013-07-06 13:29:24 +02008712 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008713 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008714 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008715
8716 /* otherwise everything's ready for the request */
8717 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008718 else {
8719 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008720 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8721 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008722 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008723 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008724 }
8725
8726 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008727 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008728 return 1;
8729}
Willy Tarreau8797c062007-05-07 00:55:35 +02008730
Willy Tarreauc0239e02012-04-16 14:42:55 +02008731#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008732 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 +02008733
Willy Tarreau24e32d82012-04-23 23:55:44 +02008734#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008735 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 +02008736
Willy Tarreau8797c062007-05-07 00:55:35 +02008737
8738/* 1. Check on METHOD
8739 * We use the pre-parsed method if it is known, and store its number as an
8740 * integer. If it is unknown, we use the pointer and the length.
8741 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008742static int pat_parse_meth(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008743{
8744 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008745 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008746
Willy Tarreauae8b7962007-06-09 23:10:04 +02008747 len = strlen(*text);
8748 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008749
8750 pattern->val.i = meth;
8751 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008752 if (usage == PAT_U_COMPILE) {
8753 pattern->ptr.str = strdup(*text);
8754 if (!pattern->ptr.str) {
8755 memprintf(err, "out of memory while loading pattern");
8756 return 0;
8757 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008758 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008759 else {
8760 trash = get_trash_chunk();
8761 if (trash->size < len) {
8762 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8763 len, trash->size);
8764 return 0;
8765 }
8766 pattern->ptr.str = trash->str;
8767 }
8768 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008769 pattern->len = len;
8770 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008771 else
8772 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008773 return 1;
8774}
8775
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008776/* This function fetches the method of current HTTP request and stores
8777 * it in the global pattern struct as a chunk. There are two possibilities :
8778 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8779 * in <len> and <ptr> is NULL ;
8780 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8781 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008782 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008783 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008784static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008785smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008786 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008787{
8788 int meth;
8789 struct http_txn *txn = l7;
8790
Willy Tarreau24e32d82012-04-23 23:55:44 +02008791 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008792
Willy Tarreau8797c062007-05-07 00:55:35 +02008793 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008794 smp->type = SMP_T_UINT;
8795 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008796 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008797 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8798 /* ensure the indexes are not affected */
8799 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008800 smp->type = SMP_T_CSTR;
8801 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008802 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008803 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008804 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008805 return 1;
8806}
8807
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008808/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008809static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008810{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008811 int icase;
8812
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008813
Willy Tarreauf853c462012-04-23 18:53:56 +02008814 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008815 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008816 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008817 return PAT_MATCH;
8818 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008819 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008820
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008821 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8822 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008823 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008824
8825 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008826 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008827 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008828
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008829 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008830 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8831 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008832 return PAT_NOMATCH;
8833 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008834}
8835
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008836static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008837smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008838 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008839{
8840 struct http_txn *txn = l7;
8841 char *ptr;
8842 int len;
8843
Willy Tarreauc0239e02012-04-16 14:42:55 +02008844 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008845
Willy Tarreau8797c062007-05-07 00:55:35 +02008846 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008847 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008848
8849 while ((len-- > 0) && (*ptr++ != '/'));
8850 if (len <= 0)
8851 return 0;
8852
Willy Tarreauf853c462012-04-23 18:53:56 +02008853 smp->type = SMP_T_CSTR;
8854 smp->data.str.str = ptr;
8855 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008856
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008857 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008858 return 1;
8859}
8860
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008861static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008862smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008863 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008864{
8865 struct http_txn *txn = l7;
8866 char *ptr;
8867 int len;
8868
Willy Tarreauc0239e02012-04-16 14:42:55 +02008869 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008870
Willy Tarreauf26b2522012-12-14 08:33:14 +01008871 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8872 return 0;
8873
Willy Tarreau8797c062007-05-07 00:55:35 +02008874 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008875 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008876
8877 while ((len-- > 0) && (*ptr++ != '/'));
8878 if (len <= 0)
8879 return 0;
8880
Willy Tarreauf853c462012-04-23 18:53:56 +02008881 smp->type = SMP_T_CSTR;
8882 smp->data.str.str = ptr;
8883 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008884
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008885 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008886 return 1;
8887}
8888
8889/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008890static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008891smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008892 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008893{
8894 struct http_txn *txn = l7;
8895 char *ptr;
8896 int len;
8897
Willy Tarreauc0239e02012-04-16 14:42:55 +02008898 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008899
Willy Tarreauf26b2522012-12-14 08:33:14 +01008900 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8901 return 0;
8902
Willy Tarreau8797c062007-05-07 00:55:35 +02008903 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008904 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008905
Willy Tarreauf853c462012-04-23 18:53:56 +02008906 smp->type = SMP_T_UINT;
8907 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008908 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008909 return 1;
8910}
8911
8912/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008913static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008914smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008915 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008916{
8917 struct http_txn *txn = l7;
8918
Willy Tarreauc0239e02012-04-16 14:42:55 +02008919 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008920
Willy Tarreauf853c462012-04-23 18:53:56 +02008921 smp->type = SMP_T_CSTR;
8922 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008923 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008924 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008925 return 1;
8926}
8927
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008928static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008929smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008930 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008931{
8932 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008933 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008934
Willy Tarreauc0239e02012-04-16 14:42:55 +02008935 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008936
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008937 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8938 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008939 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008940
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008941 smp->type = SMP_T_IPV4;
8942 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02008943 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008944 return 1;
8945}
8946
8947static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008948smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008949 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008950{
8951 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008952 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008953
Willy Tarreauc0239e02012-04-16 14:42:55 +02008954 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008955
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008956 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8957 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
8958 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008959
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008960 smp->type = SMP_T_UINT;
8961 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008962 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008963 return 1;
8964}
8965
Willy Tarreau185b5c42012-04-26 15:11:51 +02008966/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8967 * Accepts an optional argument of type string containing the header field name,
8968 * and an optional argument of type signed or unsigned integer to request an
8969 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008970 * headers are considered from the first one. It does not stop on commas and
8971 * returns full lines instead (useful for User-Agent or Date for example).
8972 */
8973static int
8974smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008975 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008976{
8977 struct http_txn *txn = l7;
8978 struct hdr_idx *idx = &txn->hdr_idx;
8979 struct hdr_ctx *ctx = smp->ctx.a[0];
8980 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
8981 int occ = 0;
8982 const char *name_str = NULL;
8983 int name_len = 0;
8984
8985 if (!ctx) {
8986 /* first call */
8987 ctx = &static_hdr_ctx;
8988 ctx->idx = 0;
8989 smp->ctx.a[0] = ctx;
8990 }
8991
8992 if (args) {
8993 if (args[0].type != ARGT_STR)
8994 return 0;
8995 name_str = args[0].data.str.str;
8996 name_len = args[0].data.str.len;
8997
8998 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8999 occ = args[1].data.uint;
9000 }
9001
9002 CHECK_HTTP_MESSAGE_FIRST();
9003
9004 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9005 /* search for header from the beginning */
9006 ctx->idx = 0;
9007
9008 if (!occ && !(opt & SMP_OPT_ITERATE))
9009 /* no explicit occurrence and single fetch => last header by default */
9010 occ = -1;
9011
9012 if (!occ)
9013 /* prepare to report multiple occurrences for ACL fetches */
9014 smp->flags |= SMP_F_NOT_LAST;
9015
9016 smp->type = SMP_T_CSTR;
9017 smp->flags |= SMP_F_VOL_HDR;
9018 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9019 return 1;
9020
9021 smp->flags &= ~SMP_F_NOT_LAST;
9022 return 0;
9023}
9024
9025/* 6. Check on HTTP header count. The number of occurrences is returned.
9026 * Accepts exactly 1 argument of type string. It does not stop on commas and
9027 * returns full lines instead (useful for User-Agent or Date for example).
9028 */
9029static int
9030smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009031 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009032{
9033 struct http_txn *txn = l7;
9034 struct hdr_idx *idx = &txn->hdr_idx;
9035 struct hdr_ctx ctx;
9036 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9037 int cnt;
9038
9039 if (!args || args->type != ARGT_STR)
9040 return 0;
9041
9042 CHECK_HTTP_MESSAGE_FIRST();
9043
9044 ctx.idx = 0;
9045 cnt = 0;
9046 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9047 cnt++;
9048
9049 smp->type = SMP_T_UINT;
9050 smp->data.uint = cnt;
9051 smp->flags = SMP_F_VOL_HDR;
9052 return 1;
9053}
9054
9055/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9056 * Accepts an optional argument of type string containing the header field name,
9057 * and an optional argument of type signed or unsigned integer to request an
9058 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009059 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009060 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009061static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009062smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009063 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009064{
9065 struct http_txn *txn = l7;
9066 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009067 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009068 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009069 int occ = 0;
9070 const char *name_str = NULL;
9071 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009072
Willy Tarreaua890d072013-04-02 12:01:06 +02009073 if (!ctx) {
9074 /* first call */
9075 ctx = &static_hdr_ctx;
9076 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009077 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009078 }
9079
Willy Tarreau185b5c42012-04-26 15:11:51 +02009080 if (args) {
9081 if (args[0].type != ARGT_STR)
9082 return 0;
9083 name_str = args[0].data.str.str;
9084 name_len = args[0].data.str.len;
9085
9086 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9087 occ = args[1].data.uint;
9088 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009089
Willy Tarreaue333ec92012-04-16 16:26:40 +02009090 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009091
Willy Tarreau185b5c42012-04-26 15:11:51 +02009092 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009093 /* search for header from the beginning */
9094 ctx->idx = 0;
9095
Willy Tarreau185b5c42012-04-26 15:11:51 +02009096 if (!occ && !(opt & SMP_OPT_ITERATE))
9097 /* no explicit occurrence and single fetch => last header by default */
9098 occ = -1;
9099
9100 if (!occ)
9101 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009102 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009103
Willy Tarreau185b5c42012-04-26 15:11:51 +02009104 smp->type = SMP_T_CSTR;
9105 smp->flags |= SMP_F_VOL_HDR;
9106 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 +02009107 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009108
Willy Tarreau37406352012-04-23 16:16:37 +02009109 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009110 return 0;
9111}
9112
Willy Tarreauc11416f2007-06-17 16:58:38 +02009113/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009114 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009115 */
9116static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009117smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009118 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009119{
9120 struct http_txn *txn = l7;
9121 struct hdr_idx *idx = &txn->hdr_idx;
9122 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009123 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009124 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009125
Willy Tarreau24e32d82012-04-23 23:55:44 +02009126 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009127 return 0;
9128
Willy Tarreaue333ec92012-04-16 16:26:40 +02009129 CHECK_HTTP_MESSAGE_FIRST();
9130
Willy Tarreau33a7e692007-06-10 19:45:56 +02009131 ctx.idx = 0;
9132 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009133 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 +02009134 cnt++;
9135
Willy Tarreauf853c462012-04-23 18:53:56 +02009136 smp->type = SMP_T_UINT;
9137 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009138 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009139 return 1;
9140}
9141
Willy Tarreau185b5c42012-04-26 15:11:51 +02009142/* Fetch an HTTP header's integer value. The integer value is returned. It
9143 * takes a mandatory argument of type string and an optional one of type int
9144 * to designate a specific occurrence. It returns an unsigned integer, which
9145 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009146 */
9147static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009148smp_fetch_hdr_val(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)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009150{
Willy Tarreauef38c392013-07-22 16:29:32 +02009151 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009152
Willy Tarreauf853c462012-04-23 18:53:56 +02009153 if (ret > 0) {
9154 smp->type = SMP_T_UINT;
9155 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9156 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009157
Willy Tarreaud53e2422012-04-16 17:21:11 +02009158 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009159}
9160
Cyril Bonté69fa9922012-10-25 00:01:06 +02009161/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9162 * and an optional one of type int to designate a specific occurrence.
9163 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009164 */
9165static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009166smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009167 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009168{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009169 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009170
Willy Tarreauef38c392013-07-22 16:29:32 +02009171 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009172 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9173 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009174 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009175 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009176 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009177 if (smp->data.str.len < temp->size - 1) {
9178 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9179 temp->str[smp->data.str.len] = '\0';
9180 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9181 smp->type = SMP_T_IPV6;
9182 break;
9183 }
9184 }
9185 }
9186
Willy Tarreaud53e2422012-04-16 17:21:11 +02009187 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009188 if (!(smp->flags & SMP_F_NOT_LAST))
9189 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009190 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009191 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009192}
9193
Willy Tarreau737b0c12007-06-10 21:28:46 +02009194/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9195 * the first '/' after the possible hostname, and ends before the possible '?'.
9196 */
9197static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009198smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009199 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009200{
9201 struct http_txn *txn = l7;
9202 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009203
Willy Tarreauc0239e02012-04-16 14:42:55 +02009204 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009205
Willy Tarreau9b28e032012-10-12 23:49:43 +02009206 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009207 ptr = http_get_path(txn);
9208 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009209 return 0;
9210
9211 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009212 smp->type = SMP_T_CSTR;
9213 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009214
9215 while (ptr < end && *ptr != '?')
9216 ptr++;
9217
Willy Tarreauf853c462012-04-23 18:53:56 +02009218 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009219 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009220 return 1;
9221}
9222
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009223/* This produces a concatenation of the first occurrence of the Host header
9224 * followed by the path component if it begins with a slash ('/'). This means
9225 * that '*' will not be added, resulting in exactly the first Host entry.
9226 * If no Host header is found, then the path is returned as-is. The returned
9227 * value is stored in the trash so it does not need to be marked constant.
9228 */
9229static int
9230smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009231 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009232{
9233 struct http_txn *txn = l7;
9234 char *ptr, *end, *beg;
9235 struct hdr_ctx ctx;
9236
9237 CHECK_HTTP_MESSAGE_FIRST();
9238
9239 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009240 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 +02009241 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009242 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009243
9244 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009245 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009246 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009247 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009248 smp->data.str.len = ctx.vlen;
9249
9250 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009251 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 +02009252 beg = http_get_path(txn);
9253 if (!beg)
9254 beg = end;
9255
9256 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9257
9258 if (beg < ptr && *beg == '/') {
9259 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9260 smp->data.str.len += ptr - beg;
9261 }
9262
9263 smp->flags = SMP_F_VOL_1ST;
9264 return 1;
9265}
9266
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009267/* This produces a 32-bit hash of the concatenation of the first occurrence of
9268 * the Host header followed by the path component if it begins with a slash ('/').
9269 * This means that '*' will not be added, resulting in exactly the first Host
9270 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009271 * is hashed using the path hash followed by a full avalanche hash and provides a
9272 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009273 * high-traffic sites without having to store whole paths.
9274 */
9275static int
9276smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009277 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009278{
9279 struct http_txn *txn = l7;
9280 struct hdr_ctx ctx;
9281 unsigned int hash = 0;
9282 char *ptr, *beg, *end;
9283 int len;
9284
9285 CHECK_HTTP_MESSAGE_FIRST();
9286
9287 ctx.idx = 0;
9288 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9289 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9290 ptr = ctx.line + ctx.val;
9291 len = ctx.vlen;
9292 while (len--)
9293 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9294 }
9295
9296 /* now retrieve the path */
9297 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9298 beg = http_get_path(txn);
9299 if (!beg)
9300 beg = end;
9301
9302 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9303
9304 if (beg < ptr && *beg == '/') {
9305 while (beg < ptr)
9306 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9307 }
9308 hash = full_hash(hash);
9309
9310 smp->type = SMP_T_UINT;
9311 smp->data.uint = hash;
9312 smp->flags = SMP_F_VOL_1ST;
9313 return 1;
9314}
9315
Willy Tarreau4a550602012-12-09 14:53:32 +01009316/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009317 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9318 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9319 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009320 * that in environments where IPv6 is insignificant, truncating the output to
9321 * 8 bytes would still work.
9322 */
9323static int
9324smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009325 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009326{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009327 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009328 struct connection *cli_conn = objt_conn(l4->si[0].end);
9329
9330 if (!cli_conn)
9331 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009332
Willy Tarreauef38c392013-07-22 16:29:32 +02009333 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009334 return 0;
9335
Willy Tarreau47ca5452012-12-23 20:22:19 +01009336 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009337 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9338 temp->len += sizeof(smp->data.uint);
9339
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009340 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009341 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009342 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009343 temp->len += 4;
9344 break;
9345 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009346 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009347 temp->len += 16;
9348 break;
9349 default:
9350 return 0;
9351 }
9352
9353 smp->data.str = *temp;
9354 smp->type = SMP_T_BIN;
9355 return 1;
9356}
9357
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009358static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009359smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009360 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009361{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009362 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9363 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9364 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009365
Willy Tarreau24e32d82012-04-23 23:55:44 +02009366 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009367
Willy Tarreauf853c462012-04-23 18:53:56 +02009368 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009369 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009370 return 1;
9371}
9372
Willy Tarreau7f18e522010-10-22 20:04:13 +02009373/* return a valid test if the current request is the first one on the connection */
9374static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009375smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009376 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009377{
9378 if (!s)
9379 return 0;
9380
Willy Tarreauf853c462012-04-23 18:53:56 +02009381 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009382 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009383 return 1;
9384}
9385
Willy Tarreau34db1082012-04-19 17:16:54 +02009386/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009387static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009388smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009389 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009390{
9391
Willy Tarreau24e32d82012-04-23 23:55:44 +02009392 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009393 return 0;
9394
Willy Tarreauc0239e02012-04-16 14:42:55 +02009395 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009396
Willy Tarreauc0239e02012-04-16 14:42:55 +02009397 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009398 return 0;
9399
Willy Tarreauf853c462012-04-23 18:53:56 +02009400 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009401 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 +01009402 return 1;
9403}
Willy Tarreau8797c062007-05-07 00:55:35 +02009404
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009405/* Accepts exactly 1 argument of type userlist */
9406static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009407smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009408 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009409{
9410
9411 if (!args || args->type != ARGT_USR)
9412 return 0;
9413
9414 CHECK_HTTP_MESSAGE_FIRST();
9415
9416 if (!get_http_auth(l4))
9417 return 0;
9418
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009419 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009420 smp->ctx.a[0] = args->data.usr; /* user list */
9421 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9422 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9423
9424 /* if the user does not belong to the userlist or has a wrong password,
9425 * report that it unconditionally does not match. Otherwise we return
9426 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009427 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009428 */
9429 smp->type = SMP_T_BOOL;
9430 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9431 if (smp->data.uint)
9432 smp->type = SMP_T_UINT;
9433
9434 return 1;
9435}
9436
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009437/* Try to find the next occurrence of a cookie name in a cookie header value.
9438 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9439 * the cookie value is returned into *value and *value_l, and the function
9440 * returns a pointer to the next pointer to search from if the value was found.
9441 * Otherwise if the cookie was not found, NULL is returned and neither value
9442 * nor value_l are touched. The input <hdr> string should first point to the
9443 * header's value, and the <hdr_end> pointer must point to the first character
9444 * not part of the value. <list> must be non-zero if value may represent a list
9445 * of values (cookie headers). This makes it faster to abort parsing when no
9446 * list is expected.
9447 */
9448static char *
9449extract_cookie_value(char *hdr, const char *hdr_end,
9450 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009451 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009452{
9453 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9454 char *next;
9455
9456 /* we search at least a cookie name followed by an equal, and more
9457 * generally something like this :
9458 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9459 */
9460 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9461 /* Iterate through all cookies on this line */
9462
9463 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9464 att_beg++;
9465
9466 /* find att_end : this is the first character after the last non
9467 * space before the equal. It may be equal to hdr_end.
9468 */
9469 equal = att_end = att_beg;
9470
9471 while (equal < hdr_end) {
9472 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9473 break;
9474 if (http_is_spht[(unsigned char)*equal++])
9475 continue;
9476 att_end = equal;
9477 }
9478
9479 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9480 * is between <att_beg> and <equal>, both may be identical.
9481 */
9482
9483 /* look for end of cookie if there is an equal sign */
9484 if (equal < hdr_end && *equal == '=') {
9485 /* look for the beginning of the value */
9486 val_beg = equal + 1;
9487 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9488 val_beg++;
9489
9490 /* find the end of the value, respecting quotes */
9491 next = find_cookie_value_end(val_beg, hdr_end);
9492
9493 /* make val_end point to the first white space or delimitor after the value */
9494 val_end = next;
9495 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9496 val_end--;
9497 } else {
9498 val_beg = val_end = next = equal;
9499 }
9500
9501 /* We have nothing to do with attributes beginning with '$'. However,
9502 * they will automatically be removed if a header before them is removed,
9503 * since they're supposed to be linked together.
9504 */
9505 if (*att_beg == '$')
9506 continue;
9507
9508 /* Ignore cookies with no equal sign */
9509 if (equal == next)
9510 continue;
9511
9512 /* Now we have the cookie name between att_beg and att_end, and
9513 * its value between val_beg and val_end.
9514 */
9515
9516 if (att_end - att_beg == cookie_name_l &&
9517 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9518 /* let's return this value and indicate where to go on from */
9519 *value = val_beg;
9520 *value_l = val_end - val_beg;
9521 return next + 1;
9522 }
9523
9524 /* Set-Cookie headers only have the name in the first attr=value part */
9525 if (!list)
9526 break;
9527 }
9528
9529 return NULL;
9530}
9531
Willy Tarreaue333ec92012-04-16 16:26:40 +02009532/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009533 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009534 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009535 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009536 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009537 * Accepts exactly 1 argument of type string. If the input options indicate
9538 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009539 */
9540static int
Willy Tarreau51539362012-05-08 12:46:28 +02009541smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009542 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009543{
9544 struct http_txn *txn = l7;
9545 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009546 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009547 const struct http_msg *msg;
9548 const char *hdr_name;
9549 int hdr_name_len;
9550 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009551 int occ = 0;
9552 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009553
Willy Tarreau24e32d82012-04-23 23:55:44 +02009554 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009555 return 0;
9556
Willy Tarreaua890d072013-04-02 12:01:06 +02009557 if (!ctx) {
9558 /* first call */
9559 ctx = &static_hdr_ctx;
9560 ctx->idx = 0;
9561 smp->ctx.a[2] = ctx;
9562 }
9563
Willy Tarreaue333ec92012-04-16 16:26:40 +02009564 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009565
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009566 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009567 msg = &txn->req;
9568 hdr_name = "Cookie";
9569 hdr_name_len = 6;
9570 } else {
9571 msg = &txn->rsp;
9572 hdr_name = "Set-Cookie";
9573 hdr_name_len = 10;
9574 }
9575
Willy Tarreau28376d62012-04-26 21:26:10 +02009576 if (!occ && !(opt & SMP_OPT_ITERATE))
9577 /* no explicit occurrence and single fetch => last cookie by default */
9578 occ = -1;
9579
9580 /* OK so basically here, either we want only one value and it's the
9581 * last one, or we want to iterate over all of them and we fetch the
9582 * next one.
9583 */
9584
Willy Tarreau9b28e032012-10-12 23:49:43 +02009585 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009586 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009587 /* search for the header from the beginning, we must first initialize
9588 * the search parameters.
9589 */
Willy Tarreau37406352012-04-23 16:16:37 +02009590 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009591 ctx->idx = 0;
9592 }
9593
Willy Tarreau28376d62012-04-26 21:26:10 +02009594 smp->flags |= SMP_F_VOL_HDR;
9595
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009596 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009597 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9598 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009599 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9600 goto out;
9601
Willy Tarreau24e32d82012-04-23 23:55:44 +02009602 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009603 continue;
9604
Willy Tarreau37406352012-04-23 16:16:37 +02009605 smp->ctx.a[0] = ctx->line + ctx->val;
9606 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009607 }
9608
Willy Tarreauf853c462012-04-23 18:53:56 +02009609 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009610 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009611 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009612 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009613 &smp->data.str.str,
9614 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009615 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009616 found = 1;
9617 if (occ >= 0) {
9618 /* one value was returned into smp->data.str.{str,len} */
9619 smp->flags |= SMP_F_NOT_LAST;
9620 return 1;
9621 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009622 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009623 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009624 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009625 /* all cookie headers and values were scanned. If we're looking for the
9626 * last occurrence, we may return it now.
9627 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009628 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009629 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009630 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009631}
9632
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009633/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009634 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009635 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009636 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009637 */
9638static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009639smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009640 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009641{
9642 struct http_txn *txn = l7;
9643 struct hdr_idx *idx = &txn->hdr_idx;
9644 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009645 const struct http_msg *msg;
9646 const char *hdr_name;
9647 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009648 int cnt;
9649 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009650 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009651
Willy Tarreau24e32d82012-04-23 23:55:44 +02009652 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009653 return 0;
9654
Willy Tarreaue333ec92012-04-16 16:26:40 +02009655 CHECK_HTTP_MESSAGE_FIRST();
9656
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009657 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009658 msg = &txn->req;
9659 hdr_name = "Cookie";
9660 hdr_name_len = 6;
9661 } else {
9662 msg = &txn->rsp;
9663 hdr_name = "Set-Cookie";
9664 hdr_name_len = 10;
9665 }
9666
Willy Tarreau9b28e032012-10-12 23:49:43 +02009667 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009668 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009669 ctx.idx = 0;
9670 cnt = 0;
9671
9672 while (1) {
9673 /* Note: val_beg == NULL every time we need to fetch a new header */
9674 if (!val_beg) {
9675 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9676 break;
9677
Willy Tarreau24e32d82012-04-23 23:55:44 +02009678 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009679 continue;
9680
9681 val_beg = ctx.line + ctx.val;
9682 val_end = val_beg + ctx.vlen;
9683 }
9684
Willy Tarreauf853c462012-04-23 18:53:56 +02009685 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009686 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009687 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009688 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009689 &smp->data.str.str,
9690 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009691 cnt++;
9692 }
9693 }
9694
Willy Tarreauf853c462012-04-23 18:53:56 +02009695 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009696 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009697 return 1;
9698}
9699
Willy Tarreau51539362012-05-08 12:46:28 +02009700/* Fetch an cookie's integer value. The integer value is returned. It
9701 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9702 */
9703static int
9704smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009705 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009706{
Willy Tarreauef38c392013-07-22 16:29:32 +02009707 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009708
9709 if (ret > 0) {
9710 smp->type = SMP_T_UINT;
9711 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9712 }
9713
9714 return ret;
9715}
9716
Willy Tarreau8797c062007-05-07 00:55:35 +02009717/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009718/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009719/************************************************************************/
9720
David Cournapeau16023ee2010-12-23 20:55:41 +09009721/*
9722 * Given a path string and its length, find the position of beginning of the
9723 * query string. Returns NULL if no query string is found in the path.
9724 *
9725 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9726 *
9727 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9728 */
bedis4c75cca2012-10-05 08:38:24 +02009729static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009730{
9731 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009732
bedis4c75cca2012-10-05 08:38:24 +02009733 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009734 return p ? p + 1 : NULL;
9735}
9736
bedis4c75cca2012-10-05 08:38:24 +02009737static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009738{
bedis4c75cca2012-10-05 08:38:24 +02009739 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009740}
9741
9742/*
9743 * Given a url parameter, find the starting position of the first occurence,
9744 * or NULL if the parameter is not found.
9745 *
9746 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9747 * the function will return query_string+8.
9748 */
9749static char*
9750find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009751 char* url_param_name, size_t url_param_name_l,
9752 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009753{
9754 char *pos, *last;
9755
9756 pos = query_string;
9757 last = query_string + query_string_l - url_param_name_l - 1;
9758
9759 while (pos <= last) {
9760 if (pos[url_param_name_l] == '=') {
9761 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9762 return pos;
9763 pos += url_param_name_l + 1;
9764 }
bedis4c75cca2012-10-05 08:38:24 +02009765 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009766 pos++;
9767 pos++;
9768 }
9769 return NULL;
9770}
9771
9772/*
9773 * Given a url parameter name, returns its value and size into *value and
9774 * *value_l respectively, and returns non-zero. If the parameter is not found,
9775 * zero is returned and value/value_l are not touched.
9776 */
9777static int
9778find_url_param_value(char* path, size_t path_l,
9779 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009780 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009781{
9782 char *query_string, *qs_end;
9783 char *arg_start;
9784 char *value_start, *value_end;
9785
bedis4c75cca2012-10-05 08:38:24 +02009786 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009787 if (!query_string)
9788 return 0;
9789
9790 qs_end = path + path_l;
9791 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009792 url_param_name, url_param_name_l,
9793 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009794 if (!arg_start)
9795 return 0;
9796
9797 value_start = arg_start + url_param_name_l + 1;
9798 value_end = value_start;
9799
bedis4c75cca2012-10-05 08:38:24 +02009800 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009801 value_end++;
9802
9803 *value = value_start;
9804 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009805 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009806}
9807
9808static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009809smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009810 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009811{
bedis4c75cca2012-10-05 08:38:24 +02009812 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009813 struct http_txn *txn = l7;
9814 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009815
bedis4c75cca2012-10-05 08:38:24 +02009816 if (!args || args[0].type != ARGT_STR ||
9817 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009818 return 0;
9819
9820 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009821
bedis4c75cca2012-10-05 08:38:24 +02009822 if (args[1].type)
9823 delim = *args[1].data.str.str;
9824
Willy Tarreau9b28e032012-10-12 23:49:43 +02009825 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009826 args->data.str.str, args->data.str.len,
9827 &smp->data.str.str, &smp->data.str.len,
9828 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009829 return 0;
9830
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009831 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009832 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009833 return 1;
9834}
9835
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009836/* Return the signed integer value for the specified url parameter (see url_param
9837 * above).
9838 */
9839static int
9840smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009841 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009842{
Willy Tarreauef38c392013-07-22 16:29:32 +02009843 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009844
9845 if (ret > 0) {
9846 smp->type = SMP_T_UINT;
9847 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9848 }
9849
9850 return ret;
9851}
9852
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009853/* This produces a 32-bit hash of the concatenation of the first occurrence of
9854 * the Host header followed by the path component if it begins with a slash ('/').
9855 * This means that '*' will not be added, resulting in exactly the first Host
9856 * entry. If no Host header is found, then the path is used. The resulting value
9857 * is hashed using the url hash followed by a full avalanche hash and provides a
9858 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
9859 * high-traffic sites without having to store whole paths.
9860 * this differs from the base32 functions in that it includes the url parameters
9861 * as well as the path
9862 */
9863static int
9864smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009865 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009866{
9867 struct http_txn *txn = l7;
9868 struct hdr_ctx ctx;
9869 unsigned int hash = 0;
9870 char *ptr, *beg, *end;
9871 int len;
9872
9873 CHECK_HTTP_MESSAGE_FIRST();
9874
9875 ctx.idx = 0;
9876 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9877 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9878 ptr = ctx.line + ctx.val;
9879 len = ctx.vlen;
9880 while (len--)
9881 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9882 }
9883
9884 /* now retrieve the path */
9885 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9886 beg = http_get_path(txn);
9887 if (!beg)
9888 beg = end;
9889
9890 for (ptr = beg; ptr < end ; ptr++);
9891
9892 if (beg < ptr && *beg == '/') {
9893 while (beg < ptr)
9894 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9895 }
9896 hash = full_hash(hash);
9897
9898 smp->type = SMP_T_UINT;
9899 smp->data.uint = hash;
9900 smp->flags = SMP_F_VOL_1ST;
9901 return 1;
9902}
9903
9904/* This concatenates the source address with the 32-bit hash of the Host and
9905 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
9906 * per-url counters. The result is a binary block from 8 to 20 bytes depending
9907 * on the source address length. The URL hash is stored before the address so
9908 * that in environments where IPv6 is insignificant, truncating the output to
9909 * 8 bytes would still work.
9910 */
9911static int
9912smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009913 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009914{
9915 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009916 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009917
Willy Tarreaue155ec22013-11-18 18:33:22 +01009918 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009919 return 0;
9920
9921 temp = get_trash_chunk();
9922 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9923 temp->len += sizeof(smp->data.uint);
9924
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009925 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009926 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009927 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009928 temp->len += 4;
9929 break;
9930 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009931 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009932 temp->len += 16;
9933 break;
9934 default:
9935 return 0;
9936 }
9937
9938 smp->data.str = *temp;
9939 smp->type = SMP_T_BIN;
9940 return 1;
9941}
9942
Willy Tarreau185b5c42012-04-26 15:11:51 +02009943/* This function is used to validate the arguments passed to any "hdr" fetch
9944 * keyword. These keywords support an optional positive or negative occurrence
9945 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9946 * is assumed that the types are already the correct ones. Returns 0 on error,
9947 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9948 * error message in case of error, that the caller is responsible for freeing.
9949 * The initial location must either be freeable or NULL.
9950 */
9951static int val_hdr(struct arg *arg, char **err_msg)
9952{
9953 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009954 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009955 return 0;
9956 }
9957 return 1;
9958}
9959
Willy Tarreau276fae92013-07-25 14:36:01 +02009960/* takes an UINT value on input supposed to represent the time since EPOCH,
9961 * adds an optional offset found in args[0] and emits a string representing
9962 * the date in RFC-1123/5322 format.
9963 */
9964static int sample_conv_http_date(const struct arg *args, struct sample *smp)
9965{
9966 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
9967 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
9968 struct chunk *temp;
9969 struct tm *tm;
9970 time_t curr_date = smp->data.uint;
9971
9972 /* add offset */
9973 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
9974 curr_date += args[0].data.sint;
9975
9976 tm = gmtime(&curr_date);
9977
9978 temp = get_trash_chunk();
9979 temp->len = snprintf(temp->str, temp->size - temp->len,
9980 "%s, %02d %s %04d %02d:%02d:%02d GMT",
9981 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
9982 tm->tm_hour, tm->tm_min, tm->tm_sec);
9983
9984 smp->data.str = *temp;
9985 smp->type = SMP_T_STR;
9986 return 1;
9987}
9988
Willy Tarreau4a568972010-05-12 08:08:50 +02009989/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009990/* All supported ACL keywords must be declared here. */
9991/************************************************************************/
9992
9993/* Note: must not be declared <const> as its list will be overwritten.
9994 * Please take care of keeping this list alphabetically sorted.
9995 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009996static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009997 { "base", "base", pat_parse_str, pat_match_str },
9998 { "base_beg", "base", pat_parse_str, pat_match_beg },
9999 { "base_dir", "base", pat_parse_str, pat_match_dir },
10000 { "base_dom", "base", pat_parse_str, pat_match_dom },
10001 { "base_end", "base", pat_parse_str, pat_match_end },
10002 { "base_len", "base", pat_parse_int, pat_match_len },
10003 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10004 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010005
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010006 { "cook", "req.cook", pat_parse_str, pat_match_str },
10007 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10008 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10009 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10010 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10011 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10012 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10013 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010014
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010015 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10016 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10017 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10018 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10019 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10020 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10021 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10022 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010023
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010024 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010025
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010026 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010027
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010028 { "path", "path", pat_parse_str, pat_match_str },
10029 { "path_beg", "path", pat_parse_str, pat_match_beg },
10030 { "path_dir", "path", pat_parse_str, pat_match_dir },
10031 { "path_dom", "path", pat_parse_str, pat_match_dom },
10032 { "path_end", "path", pat_parse_str, pat_match_end },
10033 { "path_len", "path", pat_parse_int, pat_match_len },
10034 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10035 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010036
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010037 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10038 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010039
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010040 { "scook", "res.cook", pat_parse_str, pat_match_str },
10041 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10042 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10043 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10044 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10045 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10046 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10047 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010048
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010049 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10050 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10051 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10052 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10053 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10054 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10055 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10056 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010057
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010058 { "url", "url", pat_parse_str, pat_match_str },
10059 { "url_beg", "url", pat_parse_str, pat_match_beg },
10060 { "url_dir", "url", pat_parse_str, pat_match_dir },
10061 { "url_dom", "url", pat_parse_str, pat_match_dom },
10062 { "url_end", "url", pat_parse_str, pat_match_end },
10063 { "url_len", "url", pat_parse_int, pat_match_len },
10064 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10065 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010066
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010067 { "urlp", "urlp", pat_parse_str, pat_match_str },
10068 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10069 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10070 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10071 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10072 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10073 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10074 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010075
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010076 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010077}};
10078
10079/************************************************************************/
10080/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010081/************************************************************************/
10082/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010083static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010084 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10085 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10086 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10087
10088 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10089 * are only here to match the ACL's name, are request-only and are used
10090 * for ACL compatibility only.
10091 */
10092 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10093 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10094 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10095 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10096
10097 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10098 * only here to match the ACL's name, are request-only and are used for
10099 * ACL compatibility only.
10100 */
10101 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10102 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10103 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10104 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10105
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010106 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10107 { "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 +010010108 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10109 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10110 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010111
10112 /* HTTP protocol on the request path */
10113 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010114 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010115
10116 /* HTTP version on the request path */
10117 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010118 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010119
10120 /* HTTP version on the response path */
10121 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010122 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10123
Willy Tarreau18ed2562013-01-14 15:56:36 +010010124 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10125 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10126 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10127 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10128
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010129 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10130 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010131 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10132 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10133 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10134 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10135
10136 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10137 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10138 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10139 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10140
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010141 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10142 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010143 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10144 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10145 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10146 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10147
Willy Tarreau409bcde2013-01-08 00:31:00 +010010148 /* scook is valid only on the response and is used for ACL compatibility */
10149 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10150 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10151 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10152 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10153
10154 /* shdr is valid only on the response and is used for ACL compatibility */
10155 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10156 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10157 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10158 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10159
10160 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10161 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010162 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10163 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010164 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10165 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10166 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10167 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10168 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10169 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010170}};
10171
Willy Tarreau8797c062007-05-07 00:55:35 +020010172
Willy Tarreau276fae92013-07-25 14:36:01 +020010173/* Note: must not be declared <const> as its list will be overwritten */
10174static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10175 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10176 { NULL, NULL, 0, 0, 0 },
10177}};
10178
Willy Tarreau8797c062007-05-07 00:55:35 +020010179__attribute__((constructor))
10180static void __http_protocol_init(void)
10181{
10182 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010183 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010184 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010185}
10186
10187
Willy Tarreau58f10d72006-12-04 02:26:12 +010010188/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010189 * Local variables:
10190 * c-indent-level: 8
10191 * c-basic-offset: 8
10192 * End:
10193 */