blob: a459c53bca9a8998e9ca12e1fb4f2b9795156852 [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
3745 if (unlikely((conn = si_alloc_conn(req->cons)) == NULL)) {
3746 txn->req.msg_state = HTTP_MSG_ERROR;
3747 txn->status = 500;
3748 req->analysers = 0;
3749 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3750
3751 if (!(s->flags & SN_ERR_MASK))
3752 s->flags |= SN_ERR_RESOURCE;
3753 if (!(s->flags & SN_FINST_MASK))
3754 s->flags |= SN_FINST_R;
3755
3756 return 0;
3757 }
3758 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003759 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003760
Willy Tarreau59234e92008-11-30 23:51:27 +01003761 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003762 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003763 * Note that doing so might move headers in the request, but
3764 * the fields will stay coherent and the URI will not move.
3765 * This should only be performed in the backend.
3766 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003767 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003768 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3769 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003770
Willy Tarreau59234e92008-11-30 23:51:27 +01003771 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003772 * 8: the appsession cookie was looked up very early in 1.2,
3773 * so let's do the same now.
3774 */
3775
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003776 /* It needs to look into the URI unless persistence must be ignored */
3777 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003778 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 +01003779 }
3780
William Lallemanda73203e2012-03-12 12:48:57 +01003781 /* add unique-id if "header-unique-id" is specified */
3782
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003783 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3784 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3785 goto return_bad_req;
3786 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003787 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003788 }
William Lallemanda73203e2012-03-12 12:48:57 +01003789
3790 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003791 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3792 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003793 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003794 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003795 goto return_bad_req;
3796 }
3797
Cyril Bontéb21570a2009-11-29 20:04:48 +01003798 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003799 * 9: add X-Forwarded-For if either the frontend or the backend
3800 * asks for it.
3801 */
3802 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003803 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003804 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003805 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3806 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003807 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003808 /* The header is set to be added only if none is present
3809 * and we found it, so don't do anything.
3810 */
3811 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003812 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003813 /* Add an X-Forwarded-For header unless the source IP is
3814 * in the 'except' network range.
3815 */
3816 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003817 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003818 != s->fe->except_net.s_addr) &&
3819 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003820 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003821 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003822 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003823 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003824 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003825
3826 /* Note: we rely on the backend to get the header name to be used for
3827 * x-forwarded-for, because the header is really meant for the backends.
3828 * However, if the backend did not specify any option, we have to rely
3829 * on the frontend's header name.
3830 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003831 if (s->be->fwdfor_hdr_len) {
3832 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003833 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003834 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003835 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003836 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003837 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003838 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003839
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003840 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003841 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003842 }
3843 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003844 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003845 /* FIXME: for the sake of completeness, we should also support
3846 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003847 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003848 int len;
3849 char pn[INET6_ADDRSTRLEN];
3850 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003851 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003852 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003853
Willy Tarreau59234e92008-11-30 23:51:27 +01003854 /* Note: we rely on the backend to get the header name to be used for
3855 * x-forwarded-for, because the header is really meant for the backends.
3856 * However, if the backend did not specify any option, we have to rely
3857 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003858 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003859 if (s->be->fwdfor_hdr_len) {
3860 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003861 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003862 } else {
3863 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003864 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003865 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003866 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003867
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003868 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003869 goto return_bad_req;
3870 }
3871 }
3872
3873 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003874 * 10: add X-Original-To if either the frontend or the backend
3875 * asks for it.
3876 */
3877 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3878
3879 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003880 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003881 /* Add an X-Original-To header unless the destination IP is
3882 * in the 'except' network range.
3883 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003884 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003885
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003886 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003887 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003888 (((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 +02003889 != s->fe->except_to.s_addr) &&
3890 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003891 (((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 +02003892 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003893 int len;
3894 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003895 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003896
3897 /* Note: we rely on the backend to get the header name to be used for
3898 * x-original-to, because the header is really meant for the backends.
3899 * However, if the backend did not specify any option, we have to rely
3900 * on the frontend's header name.
3901 */
3902 if (s->be->orgto_hdr_len) {
3903 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003904 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003905 } else {
3906 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003907 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003908 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003909 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003910
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003912 goto return_bad_req;
3913 }
3914 }
3915 }
3916
Willy Tarreau50fc7772012-11-11 22:19:57 +01003917 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3918 * If an "Upgrade" token is found, the header is left untouched in order not to have
3919 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3920 * "Upgrade" is present in the Connection header.
3921 */
3922 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3923 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3924 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003925 unsigned int want_flags = 0;
3926
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003927 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003928 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3929 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3930 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003931 want_flags |= TX_CON_CLO_SET;
3932 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003933 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3934 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3935 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003936 want_flags |= TX_CON_KAL_SET;
3937 }
3938
3939 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003940 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003941 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003942
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003943
Willy Tarreau522d6c02009-12-06 18:49:18 +01003944 /* If we have no server assigned yet and we're balancing on url_param
3945 * with a POST request, we may be interested in checking the body for
3946 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003947 */
3948 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3949 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003950 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003951 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003952 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003953 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003954 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003955
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003956 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003957 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003958#ifdef TCP_QUICKACK
3959 /* We expect some data from the client. Unless we know for sure
3960 * we already have a full request, we have to re-enable quick-ack
3961 * in case we previously disabled it, otherwise we might cause
3962 * the client to delay further data.
3963 */
3964 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreauf79c8172013-10-21 16:30:56 +02003965 cli_conn && (cli_conn->flags & CO_FL_CTRL_READY) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003966 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003967 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003968 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003969#endif
3970 }
Willy Tarreau03945942009-12-22 16:50:27 +01003971
Willy Tarreau59234e92008-11-30 23:51:27 +01003972 /*************************************************************
3973 * OK, that's finished for the headers. We have done what we *
3974 * could. Let's switch to the DATA state. *
3975 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003976 req->analyse_exp = TICK_ETERNITY;
3977 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003978
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003979 /* if the server closes the connection, we want to immediately react
3980 * and close the socket to save packets and syscalls.
3981 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003982 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3983 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003984
Willy Tarreau59234e92008-11-30 23:51:27 +01003985 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 /* OK let's go on with the BODY now */
3987 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003988
Willy Tarreau59234e92008-11-30 23:51:27 +01003989 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003990 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003991 /* we detected a parsing error. We want to archive this request
3992 * in the dedicated proxy area for later troubleshooting.
3993 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003994 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003995 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003996
Willy Tarreau59234e92008-11-30 23:51:27 +01003997 txn->req.msg_state = HTTP_MSG_ERROR;
3998 txn->status = 400;
3999 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004000 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004001
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004002 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004003 if (s->listener->counters)
4004 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004005
Willy Tarreau59234e92008-11-30 23:51:27 +01004006 if (!(s->flags & SN_ERR_MASK))
4007 s->flags |= SN_ERR_PRXCOND;
4008 if (!(s->flags & SN_FINST_MASK))
4009 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004010 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004011}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004012
Willy Tarreau60b85b02008-11-30 23:28:40 +01004013/* This function is an analyser which processes the HTTP tarpit. It always
4014 * returns zero, at the beginning because it prevents any other processing
4015 * from occurring, and at the end because it terminates the request.
4016 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004017int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004018{
4019 struct http_txn *txn = &s->txn;
4020
4021 /* This connection is being tarpitted. The CLIENT side has
4022 * already set the connect expiration date to the right
4023 * timeout. We just have to check that the client is still
4024 * there and that the timeout has not expired.
4025 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004026 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004027 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004028 !tick_is_expired(req->analyse_exp, now_ms))
4029 return 0;
4030
4031 /* We will set the queue timer to the time spent, just for
4032 * logging purposes. We fake a 500 server error, so that the
4033 * attacker will not suspect his connection has been tarpitted.
4034 * It will not cause trouble to the logs because we can exclude
4035 * the tarpitted connections by filtering on the 'PT' status flags.
4036 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004037 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4038
4039 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004040 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004041 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004042
4043 req->analysers = 0;
4044 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004045
Willy Tarreau60b85b02008-11-30 23:28:40 +01004046 if (!(s->flags & SN_ERR_MASK))
4047 s->flags |= SN_ERR_PRXCOND;
4048 if (!(s->flags & SN_FINST_MASK))
4049 s->flags |= SN_FINST_T;
4050 return 0;
4051}
4052
Willy Tarreaud34af782008-11-30 23:36:37 +01004053/* This function is an analyser which processes the HTTP request body. It looks
4054 * for parameters to be used for the load balancing algorithm (url_param). It
4055 * must only be called after the standard HTTP request processing has occurred,
4056 * because it expects the request to be parsed. It returns zero if it needs to
4057 * read more data, or 1 once it has completed its analysis.
4058 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004059int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004060{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004061 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004062 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004063 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004064
4065 /* We have to parse the HTTP request body to find any required data.
4066 * "balance url_param check_post" should have been the only way to get
4067 * into this. We were brought here after HTTP header analysis, so all
4068 * related structures are ready.
4069 */
4070
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4072 goto missing_data;
4073
4074 if (msg->msg_state < HTTP_MSG_100_SENT) {
4075 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4076 * send an HTTP/1.1 100 Continue intermediate response.
4077 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004078 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004079 struct hdr_ctx ctx;
4080 ctx.idx = 0;
4081 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004082 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004083 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004084 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004085 }
4086 }
4087 msg->msg_state = HTTP_MSG_100_SENT;
4088 }
4089
4090 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004091 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004092 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004093 * is still null. We must save the body in msg->next because it
4094 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004095 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004096 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004097
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004098 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004099 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4100 else
4101 msg->msg_state = HTTP_MSG_DATA;
4102 }
4103
4104 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004105 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004106 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004107 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004108 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004109 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004110
Willy Tarreau115acb92009-12-26 13:56:06 +01004111 if (!ret)
4112 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004113 else if (ret < 0) {
4114 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004115 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004116 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004117 }
4118
Willy Tarreaud98cf932009-12-27 22:54:55 +01004119 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004120 * We have the first data byte is in msg->sov. We're waiting for at
4121 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004122 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004123
Willy Tarreau124d9912011-03-01 20:30:48 +01004124 if (msg->body_len < limit)
4125 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004126
Willy Tarreau9b28e032012-10-12 23:49:43 +02004127 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004128 goto http_end;
4129
4130 missing_data:
4131 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004132 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004133 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004134 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004135 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004136
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004137 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004138 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004139 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004140
4141 if (!(s->flags & SN_ERR_MASK))
4142 s->flags |= SN_ERR_CLITO;
4143 if (!(s->flags & SN_FINST_MASK))
4144 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004145 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004146 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004147
4148 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004149 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004150 /* Not enough data. We'll re-use the http-request
4151 * timeout here. Ideally, we should set the timeout
4152 * relative to the accept() date. We just set the
4153 * request timeout once at the beginning of the
4154 * request.
4155 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004156 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004157 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004158 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004159 return 0;
4160 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004161
4162 http_end:
4163 /* The situation will not evolve, so let's give up on the analysis. */
4164 s->logs.tv_request = now; /* update the request timer to reflect full request */
4165 req->analysers &= ~an_bit;
4166 req->analyse_exp = TICK_ETERNITY;
4167 return 1;
4168
4169 return_bad_req: /* let's centralize all bad requests */
4170 txn->req.msg_state = HTTP_MSG_ERROR;
4171 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004172 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004173
Willy Tarreau79ebac62010-06-07 13:47:49 +02004174 if (!(s->flags & SN_ERR_MASK))
4175 s->flags |= SN_ERR_PRXCOND;
4176 if (!(s->flags & SN_FINST_MASK))
4177 s->flags |= SN_FINST_R;
4178
Willy Tarreau522d6c02009-12-06 18:49:18 +01004179 return_err_msg:
4180 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004181 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004182 if (s->listener->counters)
4183 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004185}
4186
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004187/* send a server's name with an outgoing request over an established connection.
4188 * Note: this function is designed to be called once the request has been scheduled
4189 * for being forwarded. This is the reason why it rewinds the buffer before
4190 * proceeding.
4191 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004192int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004193
4194 struct hdr_ctx ctx;
4195
Mark Lamourinec2247f02012-01-04 13:02:01 -05004196 char *hdr_name = be->server_id_hdr_name;
4197 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004198 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004199 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004200 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004201
William Lallemandd9e90662012-01-30 17:27:17 +01004202 ctx.idx = 0;
4203
Willy Tarreau9b28e032012-10-12 23:49:43 +02004204 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004205 if (old_o) {
4206 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004207 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004208 }
4209
Willy Tarreau9b28e032012-10-12 23:49:43 +02004210 old_i = chn->buf->i;
4211 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 -05004212 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004213 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004214 }
4215
4216 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004217 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004218 memcpy(hdr_val, hdr_name, hdr_name_len);
4219 hdr_val += hdr_name_len;
4220 *hdr_val++ = ':';
4221 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004222 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4223 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004224
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004225 if (old_o) {
4226 /* If this was a forwarded request, we must readjust the amount of
4227 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004228 * variations. Note that if the request was already scheduled for
4229 * forwarding, it had its req->sol pointing to the body, which
4230 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004231 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004232 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004233 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004234 }
4235
Mark Lamourinec2247f02012-01-04 13:02:01 -05004236 return 0;
4237}
4238
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239/* Terminate current transaction and prepare a new one. This is very tricky
4240 * right now but it works.
4241 */
4242void http_end_txn_clean_session(struct session *s)
4243{
4244 /* FIXME: We need a more portable way of releasing a backend's and a
4245 * server's connections. We need a safer way to reinitialize buffer
4246 * flags. We also need a more accurate method for computing per-request
4247 * data.
4248 */
4249 http_silent_debug(__LINE__, s);
4250
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004251 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004252 si_shutr(s->req->cons);
4253 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004254
4255 http_silent_debug(__LINE__, s);
4256
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004257 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004259 if (unlikely(s->srv_conn))
4260 sess_change_server(s, NULL);
4261 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262
4263 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4264 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004265 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266
4267 if (s->txn.status) {
4268 int n;
4269
4270 n = s->txn.status / 100;
4271 if (n < 1 || n > 5)
4272 n = 0;
4273
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004274 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004275 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004276 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004277 s->fe->fe_counters.p.http.comp_rsp++;
4278 }
Willy Tarreau24657792010-02-26 10:30:28 +01004279 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004280 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004281 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004282 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004283 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004284 s->be->be_counters.p.http.comp_rsp++;
4285 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 }
4287
4288 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004289 s->logs.bytes_in -= s->req->buf->i;
4290 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291
4292 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004293 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 !(s->flags & SN_MONITOR) &&
4295 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4296 s->do_log(s);
4297 }
4298
4299 s->logs.accept_date = date; /* user-visible date for logging */
4300 s->logs.tv_accept = now; /* corrected date for internal use */
4301 tv_zero(&s->logs.tv_request);
4302 s->logs.t_queue = -1;
4303 s->logs.t_connect = -1;
4304 s->logs.t_data = -1;
4305 s->logs.t_close = 0;
4306 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4307 s->logs.srv_queue_size = 0; /* we will get this number soon */
4308
Willy Tarreau9b28e032012-10-12 23:49:43 +02004309 s->logs.bytes_in = s->req->total = s->req->buf->i;
4310 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004311
4312 if (s->pend_pos)
4313 pendconn_free(s->pend_pos);
4314
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004315 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004316 if (s->flags & SN_CURR_SESS) {
4317 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004318 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004320 if (may_dequeue_tasks(objt_server(s->target), s->be))
4321 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004322 }
4323
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004324 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325
4326 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004327 si_release_endpoint(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004328 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004329 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004330 s->req->cons->exp = TICK_ETERNITY;
4331 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004332 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4333 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 +02004334 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 +01004335 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004336
4337 if (s->flags & SN_COMP_READY)
4338 s->comp_algo->end(&s->comp_ctx);
4339 s->comp_algo = NULL;
4340 s->flags &= ~SN_COMP_READY;
4341
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 s->txn.meth = 0;
4343 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004344 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004345 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004346 s->req->cons->flags |= SI_FL_INDEP_STR;
4347
Willy Tarreau96e31212011-05-30 18:10:30 +02004348 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004349 s->req->flags |= CF_NEVER_WAIT;
4350 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004351 }
4352
Willy Tarreau610ecce2010-01-04 21:15:02 +01004353 /* if the request buffer is not empty, it means we're
4354 * about to process another request, so send pending
4355 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004356 * Just don't do this if the buffer is close to be full,
4357 * because the request will wait for it to flush a little
4358 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004359 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004360 if (s->req->buf->i) {
4361 if (s->rep->buf->o &&
4362 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4363 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004364 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004365 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004366
4367 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004368 channel_auto_read(s->req);
4369 channel_auto_close(s->req);
4370 channel_auto_read(s->rep);
4371 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372
Willy Tarreau342b11c2010-11-24 16:22:09 +01004373 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004374 s->rep->analysers = 0;
4375
4376 http_silent_debug(__LINE__, s);
4377}
4378
4379
4380/* This function updates the request state machine according to the response
4381 * state machine and buffer flags. It returns 1 if it changes anything (flag
4382 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4383 * it is only used to find when a request/response couple is complete. Both
4384 * this function and its equivalent should loop until both return zero. It
4385 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4386 */
4387int http_sync_req_state(struct session *s)
4388{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004389 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004390 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004391 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 unsigned int old_state = txn->req.msg_state;
4393
4394 http_silent_debug(__LINE__, s);
4395 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4396 return 0;
4397
4398 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004399 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004400 * We can shut the read side unless we want to abort_on_close,
4401 * or we have a POST request. The issue with POST requests is
4402 * that some browsers still send a CRLF after the request, and
4403 * this CRLF must be read so that it does not remain in the kernel
4404 * buffers, otherwise a close could cause an RST on some systems
4405 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004406 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004407 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004408 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409
Willy Tarreau40f151a2012-12-20 12:10:09 +01004410 /* if the server closes the connection, we want to immediately react
4411 * and close the socket to save packets and syscalls.
4412 */
4413 chn->cons->flags |= SI_FL_NOHALF;
4414
Willy Tarreau610ecce2010-01-04 21:15:02 +01004415 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4416 goto wait_other_side;
4417
4418 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4419 /* The server has not finished to respond, so we
4420 * don't want to move in order not to upset it.
4421 */
4422 goto wait_other_side;
4423 }
4424
4425 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4426 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004427 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004429 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430 goto wait_other_side;
4431 }
4432
4433 /* When we get here, it means that both the request and the
4434 * response have finished receiving. Depending on the connection
4435 * mode, we'll have to wait for the last bytes to leave in either
4436 * direction, and sometimes for a close to be effective.
4437 */
4438
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004439 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4440 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004441 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4442 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004443 }
4444 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4445 /* Option forceclose is set, or either side wants to close,
4446 * let's enforce it now that we're not expecting any new
4447 * data to come. The caller knows the session is complete
4448 * once both states are CLOSED.
4449 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004450 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4451 channel_shutr_now(chn);
4452 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004453 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004454 }
4455 else {
4456 /* The last possible modes are keep-alive and tunnel. Since tunnel
4457 * mode does not set the body analyser, we can't reach this place
4458 * in tunnel mode, so we're left with keep-alive only.
4459 * This mode is currently not implemented, we switch to tunnel mode.
4460 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004461 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004462 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004463 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 }
4465
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004466 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004468 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004469
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004470 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004471 txn->req.msg_state = HTTP_MSG_CLOSING;
4472 goto http_msg_closing;
4473 }
4474 else {
4475 txn->req.msg_state = HTTP_MSG_CLOSED;
4476 goto http_msg_closed;
4477 }
4478 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004479 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 }
4481
4482 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4483 http_msg_closing:
4484 /* nothing else to forward, just waiting for the output buffer
4485 * to be empty and for the shutw_now to take effect.
4486 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004487 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004488 txn->req.msg_state = HTTP_MSG_CLOSED;
4489 goto http_msg_closed;
4490 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004491 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492 txn->req.msg_state = HTTP_MSG_ERROR;
4493 goto wait_other_side;
4494 }
4495 }
4496
4497 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4498 http_msg_closed:
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004499 if (!(s->be->options & PR_O_ABRT_CLOSE))
4500 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004501 goto wait_other_side;
4502 }
4503
4504 wait_other_side:
4505 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004506 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004507}
4508
4509
4510/* This function updates the response state machine according to the request
4511 * state machine and buffer flags. It returns 1 if it changes anything (flag
4512 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4513 * it is only used to find when a request/response couple is complete. Both
4514 * this function and its equivalent should loop until both return zero. It
4515 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4516 */
4517int http_sync_res_state(struct session *s)
4518{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004519 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004521 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 unsigned int old_state = txn->rsp.msg_state;
4523
4524 http_silent_debug(__LINE__, s);
4525 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4526 return 0;
4527
4528 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4529 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004530 * still monitor the server connection for a possible close
4531 * while the request is being uploaded, so we don't disable
4532 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004534 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004535
4536 if (txn->req.msg_state == HTTP_MSG_ERROR)
4537 goto wait_other_side;
4538
4539 if (txn->req.msg_state < HTTP_MSG_DONE) {
4540 /* The client seems to still be sending data, probably
4541 * because we got an error response during an upload.
4542 * We have the choice of either breaking the connection
4543 * or letting it pass through. Let's do the later.
4544 */
4545 goto wait_other_side;
4546 }
4547
4548 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4549 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004550 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004552 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553 goto wait_other_side;
4554 }
4555
4556 /* When we get here, it means that both the request and the
4557 * response have finished receiving. Depending on the connection
4558 * mode, we'll have to wait for the last bytes to leave in either
4559 * direction, and sometimes for a close to be effective.
4560 */
4561
4562 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4563 /* Server-close mode : shut read and wait for the request
4564 * side to close its output buffer. The caller will detect
4565 * when we're in DONE and the other is in CLOSED and will
4566 * catch that for the final cleanup.
4567 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004568 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4569 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004571 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4572 /* Option forceclose is set, or either side wants to close,
4573 * let's enforce it now that we're not expecting any new
4574 * data to come. The caller knows the session is complete
4575 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004577 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4578 channel_shutr_now(chn);
4579 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004580 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004581 }
4582 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004583 /* The last possible modes are keep-alive and tunnel. Since tunnel
4584 * mode does not set the body analyser, we can't reach this place
4585 * in tunnel mode, so we're left with keep-alive only.
4586 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004588 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004590 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004591 }
4592
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004593 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004594 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004595 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4597 goto http_msg_closing;
4598 }
4599 else {
4600 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4601 goto http_msg_closed;
4602 }
4603 }
4604 goto wait_other_side;
4605 }
4606
4607 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4608 http_msg_closing:
4609 /* nothing else to forward, just waiting for the output buffer
4610 * to be empty and for the shutw_now to take effect.
4611 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004612 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004613 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4614 goto http_msg_closed;
4615 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004616 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004618 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004619 if (objt_server(s->target))
4620 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621 goto wait_other_side;
4622 }
4623 }
4624
4625 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4626 http_msg_closed:
4627 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004628 bi_erase(chn);
4629 channel_auto_close(chn);
4630 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004631 goto wait_other_side;
4632 }
4633
4634 wait_other_side:
4635 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004636 /* We force the response to leave immediately if we're waiting for the
4637 * other side, since there is no pending shutdown to push it out.
4638 */
4639 if (!channel_is_empty(chn))
4640 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004641 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004642}
4643
4644
4645/* Resync the request and response state machines. Return 1 if either state
4646 * changes.
4647 */
4648int http_resync_states(struct session *s)
4649{
4650 struct http_txn *txn = &s->txn;
4651 int old_req_state = txn->req.msg_state;
4652 int old_res_state = txn->rsp.msg_state;
4653
4654 http_silent_debug(__LINE__, s);
4655 http_sync_req_state(s);
4656 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004657 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658 if (!http_sync_res_state(s))
4659 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004660 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004661 if (!http_sync_req_state(s))
4662 break;
4663 }
4664 http_silent_debug(__LINE__, s);
4665 /* OK, both state machines agree on a compatible state.
4666 * There are a few cases we're interested in :
4667 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4668 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4669 * directions, so let's simply disable both analysers.
4670 * - HTTP_MSG_CLOSED on the response only means we must abort the
4671 * request.
4672 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4673 * with server-close mode means we've completed one request and we
4674 * must re-initialize the server connection.
4675 */
4676
4677 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4678 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4679 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4680 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4681 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004682 channel_auto_close(s->req);
4683 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004685 channel_auto_close(s->rep);
4686 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004688 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4689 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004690 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004691 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004692 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004693 channel_auto_close(s->rep);
4694 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004695 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004696 channel_abort(s->req);
4697 channel_auto_close(s->req);
4698 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004699 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004700 }
4701 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4702 txn->rsp.msg_state == HTTP_MSG_DONE &&
4703 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4704 /* server-close: terminate this server connection and
4705 * reinitialize a fresh-new transaction.
4706 */
4707 http_end_txn_clean_session(s);
4708 }
4709
4710 http_silent_debug(__LINE__, s);
4711 return txn->req.msg_state != old_req_state ||
4712 txn->rsp.msg_state != old_res_state;
4713}
4714
Willy Tarreaud98cf932009-12-27 22:54:55 +01004715/* This function is an analyser which forwards request body (including chunk
4716 * sizes if any). It is called as soon as we must forward, even if we forward
4717 * zero byte. The only situation where it must not be called is when we're in
4718 * tunnel mode and we want to forward till the close. It's used both to forward
4719 * remaining data and to resync after end of body. It expects the msg_state to
4720 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4721 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004722 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004723 * bytes of pending data + the headers if not already done (between sol and sov).
4724 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004725 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004726int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004727{
4728 struct http_txn *txn = &s->txn;
4729 struct http_msg *msg = &s->txn.req;
4730
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004731 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4732 return 0;
4733
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004734 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004735 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004736 /* Output closed while we were sending data. We must abort and
4737 * wake the other side up.
4738 */
4739 msg->msg_state = HTTP_MSG_ERROR;
4740 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004741 return 1;
4742 }
4743
Willy Tarreau4fe41902010-06-07 22:27:41 +02004744 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004745 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004746
4747 /* Note that we don't have to send 100-continue back because we don't
4748 * need the data to complete our job, and it's up to the server to
4749 * decide whether to return 100, 417 or anything else in return of
4750 * an "Expect: 100-continue" header.
4751 */
4752
4753 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004754 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004755 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004756 * is still null. We must save the body in msg->next because it
4757 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004758 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004759 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004760
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004761 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004762 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004763 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004765 }
4766
Willy Tarreaud98cf932009-12-27 22:54:55 +01004767 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004768 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004769
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004771 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004772 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004773 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004774 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004775 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004776 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004777 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004778 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004779
Willy Tarreaucaabe412010-01-03 23:08:28 +01004780 if (msg->msg_state == HTTP_MSG_DATA) {
4781 /* must still forward */
4782 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004783 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004784
4785 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004786 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004787 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004788 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004789 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004790 }
4791 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004792 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004793 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004794 * TRAILERS state.
4795 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004796 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004797
Willy Tarreau54d23df2012-10-25 19:04:45 +02004798 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004799 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004800 else if (ret < 0) {
4801 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004802 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004803 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004804 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004805 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004807 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004808 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004809 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004810 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004811
4812 if (ret == 0)
4813 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004814 else if (ret < 0) {
4815 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004816 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004817 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004818 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004819 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004820 /* we're in MSG_CHUNK_SIZE now */
4821 }
4822 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004823 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004824
4825 if (ret == 0)
4826 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004827 else if (ret < 0) {
4828 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004829 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004830 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004831 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004832 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004833 /* we're in HTTP_MSG_DONE now */
4834 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004835 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004836 int old_state = msg->msg_state;
4837
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004839 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004840 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4841 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004842 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004843 if (http_resync_states(s)) {
4844 /* some state changes occurred, maybe the analyser
4845 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004846 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004847 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004848 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004849 /* request errors are most likely due to
4850 * the server aborting the transfer.
4851 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004852 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004853 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004854 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004855 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004856 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004857 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004858 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004859 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004860
4861 /* If "option abortonclose" is set on the backend, we
4862 * want to monitor the client's connection and forward
4863 * any shutdown notification to the server, which will
4864 * decide whether to close or to go on processing the
4865 * request.
4866 */
4867 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004868 channel_auto_read(req);
4869 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004870 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004871 else if (s->txn.meth == HTTP_METH_POST) {
4872 /* POST requests may require to read extra CRLF
4873 * sent by broken browsers and which could cause
4874 * an RST to be sent upon close on some systems
4875 * (eg: Linux).
4876 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004877 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004878 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004879
Willy Tarreau610ecce2010-01-04 21:15:02 +01004880 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004881 }
4882 }
4883
Willy Tarreaud98cf932009-12-27 22:54:55 +01004884 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004885 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004886 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004887 if (!(s->flags & SN_ERR_MASK))
4888 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004889 if (!(s->flags & SN_FINST_MASK)) {
4890 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4891 s->flags |= SN_FINST_H;
4892 else
4893 s->flags |= SN_FINST_D;
4894 }
4895
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004896 s->fe->fe_counters.cli_aborts++;
4897 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004898 if (objt_server(s->target))
4899 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004900
4901 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004902 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004903
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004904 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004905 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004906 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004907
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004908 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004909 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004910 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004911 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004912 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004913
Willy Tarreau5c620922011-05-11 19:56:11 +02004914 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004915 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004916 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004917 * modes are already handled by the stream sock layer. We must not do
4918 * this in content-length mode because it could present the MSG_MORE
4919 * flag with the last block of forwarded data, which would cause an
4920 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004921 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004922 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004923 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004924
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004926 return 0;
4927
Willy Tarreaud98cf932009-12-27 22:54:55 +01004928 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004929 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004930 if (s->listener->counters)
4931 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004932 return_bad_req_stats_ok:
4933 txn->req.msg_state = HTTP_MSG_ERROR;
4934 if (txn->status) {
4935 /* Note: we don't send any error if some data were already sent */
4936 stream_int_retnclose(req->prod, NULL);
4937 } else {
4938 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004939 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004940 }
4941 req->analysers = 0;
4942 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004943
4944 if (!(s->flags & SN_ERR_MASK))
4945 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004946 if (!(s->flags & SN_FINST_MASK)) {
4947 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4948 s->flags |= SN_FINST_H;
4949 else
4950 s->flags |= SN_FINST_D;
4951 }
4952 return 0;
4953
4954 aborted_xfer:
4955 txn->req.msg_state = HTTP_MSG_ERROR;
4956 if (txn->status) {
4957 /* Note: we don't send any error if some data were already sent */
4958 stream_int_retnclose(req->prod, NULL);
4959 } else {
4960 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004961 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004962 }
4963 req->analysers = 0;
4964 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4965
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004966 s->fe->fe_counters.srv_aborts++;
4967 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004968 if (objt_server(s->target))
4969 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004970
4971 if (!(s->flags & SN_ERR_MASK))
4972 s->flags |= SN_ERR_SRVCL;
4973 if (!(s->flags & SN_FINST_MASK)) {
4974 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4975 s->flags |= SN_FINST_H;
4976 else
4977 s->flags |= SN_FINST_D;
4978 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004979 return 0;
4980}
4981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004982/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4983 * processing can continue on next analysers, or zero if it either needs more
4984 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4985 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4986 * when it has nothing left to do, and may remove any analyser when it wants to
4987 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004988 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004989int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004990{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004991 struct http_txn *txn = &s->txn;
4992 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004993 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004994 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004995 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004996 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004997
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004998 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 +02004999 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005000 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005001 rep,
5002 rep->rex, rep->wex,
5003 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005004 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005005 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005006
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005007 /*
5008 * Now parse the partial (or complete) lines.
5009 * We will check the response syntax, and also join multi-line
5010 * headers. An index of all the lines will be elaborated while
5011 * parsing.
5012 *
5013 * For the parsing, we use a 28 states FSM.
5014 *
5015 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005016 * rep->buf->p = beginning of response
5017 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5018 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005019 * msg->eol = end of current header or line (LF or CRLF)
5020 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005021 */
5022
Willy Tarreau83e3af02009-12-28 17:39:57 +01005023 /* There's a protected area at the end of the buffer for rewriting
5024 * purposes. We don't want to start to parse the request if the
5025 * protected area is affected, because we may have to move processed
5026 * data later, which is much more complicated.
5027 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005028 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005029 if (unlikely(!channel_reserved(rep))) {
5030 /* some data has still not left the buffer, wake us once that's done */
5031 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5032 goto abort_response;
5033 channel_dont_close(rep);
5034 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5035 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005036 }
5037
Willy Tarreau379357a2013-06-08 12:55:46 +02005038 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5039 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5040 buffer_slow_realign(rep->buf);
5041
Willy Tarreau9b28e032012-10-12 23:49:43 +02005042 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005043 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005044 }
5045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005046 /* 1: we might have to print this header in debug mode */
5047 if (unlikely((global.mode & MODE_DEBUG) &&
5048 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005049 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005050 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005051
Willy Tarreau9b28e032012-10-12 23:49:43 +02005052 sol = rep->buf->p;
5053 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005056 sol += hdr_idx_first_pos(&txn->hdr_idx);
5057 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005058
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005059 while (cur_idx) {
5060 eol = sol + txn->hdr_idx.v[cur_idx].len;
5061 debug_hdr("srvhdr", s, sol, eol);
5062 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5063 cur_idx = txn->hdr_idx.v[cur_idx].next;
5064 }
5065 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005066
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005067 /*
5068 * Now we quickly check if we have found a full valid response.
5069 * If not so, we check the FD and buffer states before leaving.
5070 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005071 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 * responses are checked first.
5073 *
5074 * Depending on whether the client is still there or not, we
5075 * may send an error response back or not. Note that normally
5076 * we should only check for HTTP status there, and check I/O
5077 * errors somewhere else.
5078 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079
Willy Tarreau655dce92009-11-08 13:10:58 +01005080 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005081 /* Invalid response */
5082 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5083 /* we detected a parsing error. We want to archive this response
5084 * in the dedicated proxy area for later troubleshooting.
5085 */
5086 hdr_response_bad:
5087 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005088 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005090 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005091 if (objt_server(s->target)) {
5092 objt_server(s->target)->counters.failed_resp++;
5093 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005094 }
Willy Tarreau64648412010-03-05 10:41:54 +01005095 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005096 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005097 rep->analysers = 0;
5098 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005099 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005100 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005101 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005102
5103 if (!(s->flags & SN_ERR_MASK))
5104 s->flags |= SN_ERR_PRXCOND;
5105 if (!(s->flags & SN_FINST_MASK))
5106 s->flags |= SN_FINST_H;
5107
5108 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005110
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005111 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005112 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005113 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005114 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005115 goto hdr_response_bad;
5116 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005117
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005118 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005119 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005121 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005122
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005123 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005124 if (objt_server(s->target)) {
5125 objt_server(s->target)->counters.failed_resp++;
5126 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005127 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005128
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005129 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005130 rep->analysers = 0;
5131 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005132 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005133 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005134 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005135
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005136 if (!(s->flags & SN_ERR_MASK))
5137 s->flags |= SN_ERR_SRVCL;
5138 if (!(s->flags & SN_FINST_MASK))
5139 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005140 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005141 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005142
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005144 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005145 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005146 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005147
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005148 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005149 if (objt_server(s->target)) {
5150 objt_server(s->target)->counters.failed_resp++;
5151 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005152 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005153
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005154 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005155 rep->analysers = 0;
5156 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005157 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005158 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005159 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005160
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005161 if (!(s->flags & SN_ERR_MASK))
5162 s->flags |= SN_ERR_SRVTO;
5163 if (!(s->flags & SN_FINST_MASK))
5164 s->flags |= SN_FINST_H;
5165 return 0;
5166 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005167
Willy Tarreauf003d372012-11-26 13:35:37 +01005168 /* client abort with an abortonclose */
5169 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5170 s->fe->fe_counters.cli_aborts++;
5171 s->be->be_counters.cli_aborts++;
5172 if (objt_server(s->target))
5173 objt_server(s->target)->counters.cli_aborts++;
5174
5175 rep->analysers = 0;
5176 channel_auto_close(rep);
5177
5178 txn->status = 400;
5179 bi_erase(rep);
5180 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5181
5182 if (!(s->flags & SN_ERR_MASK))
5183 s->flags |= SN_ERR_CLICL;
5184 if (!(s->flags & SN_FINST_MASK))
5185 s->flags |= SN_FINST_H;
5186
5187 /* process_session() will take care of the error */
5188 return 0;
5189 }
5190
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005191 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005192 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005193 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005194 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005195
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005196 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005197 if (objt_server(s->target)) {
5198 objt_server(s->target)->counters.failed_resp++;
5199 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005200 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005201
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005202 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005203 rep->analysers = 0;
5204 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005205 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005206 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005207 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005208
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209 if (!(s->flags & SN_ERR_MASK))
5210 s->flags |= SN_ERR_SRVCL;
5211 if (!(s->flags & SN_FINST_MASK))
5212 s->flags |= SN_FINST_H;
5213 return 0;
5214 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005215
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005216 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005217 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005218 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005219 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005220
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005221 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005222 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005223 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005224
5225 if (!(s->flags & SN_ERR_MASK))
5226 s->flags |= SN_ERR_CLICL;
5227 if (!(s->flags & SN_FINST_MASK))
5228 s->flags |= SN_FINST_H;
5229
5230 /* process_session() will take care of the error */
5231 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005233
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005234 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005235 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005236 return 0;
5237 }
5238
5239 /* More interesting part now : we know that we have a complete
5240 * response which at least looks like HTTP. We have an indicator
5241 * of each header's length, so we can parse them quickly.
5242 */
5243
5244 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005245 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005246
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 /*
5248 * 1: get the status code
5249 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005250 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005251 if (n < 1 || n > 5)
5252 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005253 /* when the client triggers a 4xx from the server, it's most often due
5254 * to a missing object or permission. These events should be tracked
5255 * because if they happen often, it may indicate a brute force or a
5256 * vulnerability scan.
5257 */
5258 if (n == 4)
5259 session_inc_http_err_ctr(s);
5260
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005261 if (objt_server(s->target))
5262 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263
Willy Tarreau5b154472009-12-21 20:11:07 +01005264 /* check if the response is HTTP/1.1 or above */
5265 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005266 ((rep->buf->p[5] > '1') ||
5267 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005268 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005269
5270 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005271 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 +01005272
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005273 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005274 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005275
Willy Tarreau9b28e032012-10-12 23:49:43 +02005276 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005277
Willy Tarreau39650402010-03-15 19:44:39 +01005278 /* Adjust server's health based on status code. Note: status codes 501
5279 * and 505 are triggered on demand by client request, so we must not
5280 * count them as server failures.
5281 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005282 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005283 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005284 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005285 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005286 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005287 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005288
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005289 /*
5290 * 2: check for cacheability.
5291 */
5292
5293 switch (txn->status) {
5294 case 200:
5295 case 203:
5296 case 206:
5297 case 300:
5298 case 301:
5299 case 410:
5300 /* RFC2616 @13.4:
5301 * "A response received with a status code of
5302 * 200, 203, 206, 300, 301 or 410 MAY be stored
5303 * by a cache (...) unless a cache-control
5304 * directive prohibits caching."
5305 *
5306 * RFC2616 @9.5: POST method :
5307 * "Responses to this method are not cacheable,
5308 * unless the response includes appropriate
5309 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005310 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005311 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005312 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5314 break;
5315 default:
5316 break;
5317 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005318
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005319 /*
5320 * 3: we may need to capture headers
5321 */
5322 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005323 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005324 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005325 txn->rsp.cap, s->fe->rsp_cap);
5326
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005327 /* 4: determine the transfer-length.
5328 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5329 * the presence of a message-body in a RESPONSE and its transfer length
5330 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005331 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005332 * All responses to the HEAD request method MUST NOT include a
5333 * message-body, even though the presence of entity-header fields
5334 * might lead one to believe they do. All 1xx (informational), 204
5335 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5336 * message-body. All other responses do include a message-body,
5337 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005338 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005339 * 1. Any response which "MUST NOT" include a message-body (such as the
5340 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5341 * always terminated by the first empty line after the header fields,
5342 * regardless of the entity-header fields present in the message.
5343 *
5344 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5345 * the "chunked" transfer-coding (Section 6.2) is used, the
5346 * transfer-length is defined by the use of this transfer-coding.
5347 * If a Transfer-Encoding header field is present and the "chunked"
5348 * transfer-coding is not present, the transfer-length is defined by
5349 * the sender closing the connection.
5350 *
5351 * 3. If a Content-Length header field is present, its decimal value in
5352 * OCTETs represents both the entity-length and the transfer-length.
5353 * If a message is received with both a Transfer-Encoding header
5354 * field and a Content-Length header field, the latter MUST be ignored.
5355 *
5356 * 4. If the message uses the media type "multipart/byteranges", and
5357 * the transfer-length is not otherwise specified, then this self-
5358 * delimiting media type defines the transfer-length. This media
5359 * type MUST NOT be used unless the sender knows that the recipient
5360 * can parse it; the presence in a request of a Range header with
5361 * multiple byte-range specifiers from a 1.1 client implies that the
5362 * client can parse multipart/byteranges responses.
5363 *
5364 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005365 */
5366
5367 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005368 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005369 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005370 * FIXME: should we parse anyway and return an error on chunked encoding ?
5371 */
5372 if (txn->meth == HTTP_METH_HEAD ||
5373 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005374 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005375 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005376 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005377 goto skip_content_length;
5378 }
5379
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005380 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005381 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005382 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005383 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005384 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005385 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5386 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005387 /* bad transfer-encoding (chunked followed by something else) */
5388 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005389 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005390 break;
5391 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005392 }
5393
5394 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5395 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005396 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005397 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005398 signed long long cl;
5399
Willy Tarreauad14f752011-09-02 20:33:27 +02005400 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005401 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005402 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005403 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005404
Willy Tarreauad14f752011-09-02 20:33:27 +02005405 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005406 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005407 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005408 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005409
Willy Tarreauad14f752011-09-02 20:33:27 +02005410 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005411 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005412 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005413 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005414
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005415 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005416 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005417 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005418 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005419
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005420 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005421 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005422 }
5423
William Lallemand82fe75c2012-10-23 10:25:10 +02005424 if (s->fe->comp || s->be->comp)
5425 select_compression_response_header(s, rep->buf);
5426
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005427 /* FIXME: we should also implement the multipart/byterange method.
5428 * For now on, we resort to close mode in this case (unknown length).
5429 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005430skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005431
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005432 /* end of job, return OK */
5433 rep->analysers &= ~an_bit;
5434 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005435 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005436 return 1;
5437}
5438
5439/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005440 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5441 * and updates t->rep->analysers. It might make sense to explode it into several
5442 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005443 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005444int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005445{
5446 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005447 struct http_msg *msg = &txn->rsp;
5448 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005449 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005450 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005451
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005452 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 +02005453 now_ms, __FUNCTION__,
5454 t,
5455 rep,
5456 rep->rex, rep->wex,
5457 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005458 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005459 rep->analysers);
5460
Willy Tarreau655dce92009-11-08 13:10:58 +01005461 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005462 return 0;
5463
5464 rep->analysers &= ~an_bit;
5465 rep->analyse_exp = TICK_ETERNITY;
5466
Willy Tarreau5b154472009-12-21 20:11:07 +01005467 /* Now we have to check if we need to modify the Connection header.
5468 * This is more difficult on the response than it is on the request,
5469 * because we can have two different HTTP versions and we don't know
5470 * how the client will interprete a response. For instance, let's say
5471 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5472 * HTTP/1.1 response without any header. Maybe it will bound itself to
5473 * HTTP/1.0 because it only knows about it, and will consider the lack
5474 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5475 * the lack of header as a keep-alive. Thus we will use two flags
5476 * indicating how a request MAY be understood by the client. In case
5477 * of multiple possibilities, we'll fix the header to be explicit. If
5478 * ambiguous cases such as both close and keepalive are seen, then we
5479 * will fall back to explicit close. Note that we won't take risks with
5480 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005481 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005482 */
5483
Willy Tarreaudc008c52010-02-01 16:20:08 +01005484 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5485 txn->status == 101)) {
5486 /* Either we've established an explicit tunnel, or we're
5487 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005488 * to understand the next protocols. We have to switch to tunnel
5489 * mode, so that we transfer the request and responses then let
5490 * this protocol pass unmodified. When we later implement specific
5491 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005492 * header which contains information about that protocol for
5493 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005494 */
5495 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5496 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005497 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5498 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5499 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005500 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005501
Willy Tarreau60466522010-01-18 19:08:45 +01005502 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005503 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005504 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5505 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005506
Willy Tarreau60466522010-01-18 19:08:45 +01005507 /* now adjust header transformations depending on current state */
5508 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5509 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5510 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005511 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005512 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005513 }
Willy Tarreau60466522010-01-18 19:08:45 +01005514 else { /* SCL / KAL */
5515 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005516 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005517 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005518 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005519
Willy Tarreau60466522010-01-18 19:08:45 +01005520 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005521 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005522
Willy Tarreau60466522010-01-18 19:08:45 +01005523 /* Some keep-alive responses are converted to Server-close if
5524 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005525 */
Willy Tarreau60466522010-01-18 19:08:45 +01005526 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5527 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005528 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005529 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005530 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005531 }
5532
Willy Tarreau7959a552013-09-23 16:44:27 +02005533 /* we want to have the response time before we start processing it */
5534 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5535
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005536 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005537 /*
5538 * 3: we will have to evaluate the filters.
5539 * As opposed to version 1.2, now they will be evaluated in the
5540 * filters order and not in the header order. This means that
5541 * each filter has to be validated among all headers.
5542 *
5543 * Filters are tried with ->be first, then with ->fe if it is
5544 * different from ->be.
5545 */
5546
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005547 cur_proxy = t->be;
5548 while (1) {
5549 struct proxy *rule_set = cur_proxy;
5550
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005551 /* evaluate http-response rules */
5552 if (!http_res_last_rule)
5553 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5554
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005555 /* try headers filters */
5556 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005557 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005558 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005559 if (objt_server(t->target)) {
5560 objt_server(t->target)->counters.failed_resp++;
5561 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005562 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005563 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005564 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005565 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005566 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005567 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005568 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005569 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005570 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005571 if (!(t->flags & SN_ERR_MASK))
5572 t->flags |= SN_ERR_PRXCOND;
5573 if (!(t->flags & SN_FINST_MASK))
5574 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005575 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005576 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005577 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005578
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005579 /* has the response been denied ? */
5580 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005581 if (objt_server(t->target))
5582 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005583
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005584 t->be->be_counters.denied_resp++;
5585 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005586 if (t->listener->counters)
5587 t->listener->counters->denied_resp++;
5588
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005589 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005590 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005591
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005592 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005593 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005594 if (txn->status < 200)
5595 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005596 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005597 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005598 ret = acl_pass(ret);
5599 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5600 ret = !ret;
5601 if (!ret)
5602 continue;
5603 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005604 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005605 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005606 }
5607
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005608 /* check whether we're already working on the frontend */
5609 if (cur_proxy == t->fe)
5610 break;
5611 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005612 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005613
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005614 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005615 * We may be facing a 100-continue response, in which case this
5616 * is not the right response, and we're waiting for the next one.
5617 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005618 * next one.
5619 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005620 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005621 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005622 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005623 msg->msg_state = HTTP_MSG_RPBEFORE;
5624 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005625 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005626 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5627 return 1;
5628 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005629 else if (unlikely(txn->status < 200))
5630 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005631
5632 /* we don't have any 1xx status code now */
5633
5634 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005635 * 4: check for server cookie.
5636 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005637 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5638 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005639 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005640
Willy Tarreaubaaee002006-06-26 02:48:02 +02005641
Willy Tarreaua15645d2007-03-18 16:22:39 +01005642 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005643 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005644 */
Willy Tarreau67402132012-05-31 20:40:20 +02005645 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005646 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005647
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005648 /*
5649 * 6: add server cookie in the response if needed
5650 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005651 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005652 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005653 (!(t->flags & SN_DIRECT) ||
5654 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5655 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5656 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5657 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005658 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005659 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005660 /* the server is known, it's not the one the client requested, or the
5661 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005662 * insert a set-cookie here, except if we want to insert only on POST
5663 * requests and this one isn't. Note that servers which don't have cookies
5664 * (eg: some backup servers) will return a full cookie removal request.
5665 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005666 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005667 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005668 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5669 t->be->cookie_name);
5670 }
5671 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005672 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005673
5674 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5675 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005676 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5677 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5678 trash.len += 5;
5679
Willy Tarreauef4f3912010-10-07 21:00:29 +02005680 if (t->be->cookie_maxlife) {
5681 /* emit first_date, which is either the original one or
5682 * the current date.
5683 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005684 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005685 s30tob64(txn->cookie_first_date ?
5686 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005687 (date.tv_sec+3) >> 2, trash.str + trash.len);
5688 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005689 }
5690 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005691 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005692 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005693
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005694 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005695 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005696
Willy Tarreau4992dd22012-05-31 21:02:17 +02005697 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005698 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005699
5700 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005701 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005702
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005703 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005704 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005705
Willy Tarreauf1348312010-10-07 15:54:11 +02005706 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005707 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005708 /* the server did not change, only the date was updated */
5709 txn->flags |= TX_SCK_UPDATED;
5710 else
5711 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005712
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005713 /* Here, we will tell an eventual cache on the client side that we don't
5714 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5715 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5716 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5717 */
Willy Tarreau67402132012-05-31 20:40:20 +02005718 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005719
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005720 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5721
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005722 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005723 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005724 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005725 }
5726 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005727
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005728 /*
5729 * 7: check if result will be cacheable with a cookie.
5730 * We'll block the response if security checks have caught
5731 * nasty things such as a cacheable cookie.
5732 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005733 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5734 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005735 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005736
5737 /* we're in presence of a cacheable response containing
5738 * a set-cookie header. We'll block it as requested by
5739 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005740 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005741 if (objt_server(t->target))
5742 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005743
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005744 t->be->be_counters.denied_resp++;
5745 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005746 if (t->listener->counters)
5747 t->listener->counters->denied_resp++;
5748
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005749 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005750 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005751 send_log(t->be, LOG_ALERT,
5752 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005753 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005754 goto return_srv_prx_502;
5755 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005756
5757 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005758 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005759 * If an "Upgrade" token is found, the header is left untouched in order
5760 * not to have to deal with some client bugs : some of them fail an upgrade
5761 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005762 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005763 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5764 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5765 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005766 unsigned int want_flags = 0;
5767
5768 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5769 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5770 /* we want a keep-alive response here. Keep-alive header
5771 * required if either side is not 1.1.
5772 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005773 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005774 want_flags |= TX_CON_KAL_SET;
5775 }
5776 else {
5777 /* we want a close response here. Close header required if
5778 * the server is 1.1, regardless of the client.
5779 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005780 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005781 want_flags |= TX_CON_CLO_SET;
5782 }
5783
5784 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005785 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005786 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005787
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005788 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005789 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005790 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005791 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005792
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005793 /*************************************************************
5794 * OK, that's finished for the headers. We have done what we *
5795 * could. Let's switch to the DATA state. *
5796 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005797
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005798 /* if the user wants to log as soon as possible, without counting
5799 * bytes from the server, then this is the right moment. We have
5800 * to temporarily assign bytes_out to log what we currently have.
5801 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005802 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005803 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5804 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005805 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005806 t->logs.bytes_out = 0;
5807 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005808
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005809 /* Note: we must not try to cheat by jumping directly to DATA,
5810 * otherwise we would not let the client side wake up.
5811 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005812
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005813 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005814 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005815 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005816}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005817
Willy Tarreaud98cf932009-12-27 22:54:55 +01005818/* This function is an analyser which forwards response body (including chunk
5819 * sizes if any). It is called as soon as we must forward, even if we forward
5820 * zero byte. The only situation where it must not be called is when we're in
5821 * tunnel mode and we want to forward till the close. It's used both to forward
5822 * remaining data and to resync after end of body. It expects the msg_state to
5823 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5824 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005825 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005826 * bytes of pending data + the headers if not already done (between sol and sov).
5827 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005828 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005829int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005830{
5831 struct http_txn *txn = &s->txn;
5832 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005833 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005834 static struct buffer *tmpbuf = NULL;
5835 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005836 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005837 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005838
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005839 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5840 return 0;
5841
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005842 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005843 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005844 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005845 /* Output closed while we were sending data. We must abort and
5846 * wake the other side up.
5847 */
5848 msg->msg_state = HTTP_MSG_ERROR;
5849 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005850 return 1;
5851 }
5852
Willy Tarreau4fe41902010-06-07 22:27:41 +02005853 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005854 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005855
William Lallemand82fe75c2012-10-23 10:25:10 +02005856 /* this is the first time we need the compression buffer */
5857 if (s->comp_algo != NULL && tmpbuf == NULL) {
5858 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5859 goto aborted_xfer; /* no memory */
5860 }
5861
Willy Tarreaud98cf932009-12-27 22:54:55 +01005862 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005863 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005864 * rep->buf.p still points to the beginning of the message and msg->sol
5865 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005866 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005867 channel_forward(res, msg->sov);
5868 msg->next = 0;
5869 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005870
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005871 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005872 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005873 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005874 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005875 }
5876
William Lallemand82fe75c2012-10-23 10:25:10 +02005877 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005878 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005879 if (ret < 0)
5880 goto missing_data; /* not enough spaces in buffers */
5881 compressing = 1;
5882 }
5883
Willy Tarreaud98cf932009-12-27 22:54:55 +01005884 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005885 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005886 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005887 if (s->comp_algo == NULL) {
5888 bytes = msg->sov - msg->sol;
5889 if (msg->chunk_len || bytes) {
5890 msg->sol = msg->sov;
5891 msg->next -= bytes; /* will be forwarded */
5892 msg->chunk_len += bytes;
5893 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5894 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005895 }
5896
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005897 switch (msg->msg_state - HTTP_MSG_DATA) {
5898 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005899 if (compressing) {
5900 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5901 if (ret < 0)
5902 goto aborted_xfer;
5903 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005904
5905 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005906 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005907
5908 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005909 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005910 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005911 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005912 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005913 if (compressing && consumed_data) {
5914 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5915 compressing = 0;
5916 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005917 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005918 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005919 /* fall through for HTTP_MSG_CHUNK_CRLF */
5920
5921 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5922 /* we want the CRLF after the data */
5923
5924 ret = http_skip_chunk_crlf(msg);
5925 if (ret == 0)
5926 goto missing_data;
5927 else if (ret < 0) {
5928 if (msg->err_pos >= 0)
5929 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5930 goto return_bad_res;
5931 }
5932 /* skipping data in buffer for compression */
5933 if (compressing) {
5934 b_adv(res->buf, msg->next);
5935 msg->next = 0;
5936 msg->sov = 0;
5937 msg->sol = 0;
5938 }
5939 /* we're in MSG_CHUNK_SIZE now, fall through */
5940
5941 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005942 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005943 * set ->sov and ->next to point to the body and switch to DATA or
5944 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005945 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005946
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005947 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005948 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005949 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005950 else if (ret < 0) {
5951 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005952 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005953 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005954 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005955 if (compressing) {
5956 if (likely(msg->chunk_len > 0)) {
5957 /* skipping data if we are in compression mode */
5958 b_adv(res->buf, msg->next);
5959 msg->next = 0;
5960 msg->sov = 0;
5961 msg->sol = 0;
5962 } else {
5963 if (consumed_data) {
5964 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5965 compressing = 0;
5966 }
5967 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005968 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005969 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005970 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005971
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005972 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5973 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005974 if (ret == 0)
5975 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005976 else if (ret < 0) {
5977 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005978 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005979 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005980 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005981 if (s->comp_algo != NULL) {
5982 /* forwarding trailers */
5983 channel_forward(res, msg->next);
5984 msg->next = 0;
5985 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02005986 /* we're in HTTP_MSG_DONE now, but we might still have
5987 * some data pending, so let's loop over once.
5988 */
5989 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005990
5991 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005992 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005993
5994 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005995 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005996 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5997 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005998 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005999 if (http_resync_states(s)) {
6000 http_silent_debug(__LINE__, s);
6001 /* some state changes occurred, maybe the analyser
6002 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006003 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006004 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006005 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006006 /* response errors are most likely due to
6007 * the client aborting the transfer.
6008 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006009 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006010 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006011 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006012 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006013 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006014 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006015 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006016 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006017 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006018 }
6019 }
6020
Willy Tarreaud98cf932009-12-27 22:54:55 +01006021 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006022 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006023 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6024 compressing = 0;
6025 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006026
6027 if (res->flags & CF_SHUTW)
6028 goto aborted_xfer;
6029
6030 /* stop waiting for data if the input is closed before the end. If the
6031 * client side was already closed, it means that the client has aborted,
6032 * so we don't want to count this as a server abort. Otherwise it's a
6033 * server abort.
6034 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006035 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006036 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6037 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006038 if (!(s->flags & SN_ERR_MASK))
6039 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006040 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006041 if (objt_server(s->target))
6042 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006043 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006044 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006045
Willy Tarreau40dba092010-03-04 18:14:51 +01006046 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006047 if (!s->req->analysers)
6048 goto return_bad_res;
6049
Willy Tarreauea953162012-05-18 23:41:28 +02006050 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006051 if (s->comp_algo == NULL) {
6052 bytes = msg->sov - msg->sol;
6053 if (msg->chunk_len || bytes) {
6054 msg->sol = msg->sov;
6055 msg->next -= bytes; /* will be forwarded */
6056 msg->chunk_len += bytes;
6057 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6058 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006059 }
6060
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006061 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006062 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006063 * Similarly, with keep-alive on the client side, we don't want to forward a
6064 * close.
6065 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006066 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006067 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6068 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006069 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006070
Willy Tarreau5c620922011-05-11 19:56:11 +02006071 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006072 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006073 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006074 * modes are already handled by the stream sock layer. We must not do
6075 * this in content-length mode because it could present the MSG_MORE
6076 * flag with the last block of forwarded data, which would cause an
6077 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006078 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006079 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006080 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006081
Willy Tarreaud98cf932009-12-27 22:54:55 +01006082 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006083 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006084 return 0;
6085
Willy Tarreau40dba092010-03-04 18:14:51 +01006086 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006087 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006088 if (objt_server(s->target))
6089 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006090
6091 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006092 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006093 /* don't send any error message as we're in the body */
6094 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006095 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006096 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006097 if (objt_server(s->target))
6098 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006099
6100 if (!(s->flags & SN_ERR_MASK))
6101 s->flags |= SN_ERR_PRXCOND;
6102 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006103 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006104 return 0;
6105
6106 aborted_xfer:
6107 txn->rsp.msg_state = HTTP_MSG_ERROR;
6108 /* don't send any error message as we're in the body */
6109 stream_int_retnclose(res->cons, NULL);
6110 res->analysers = 0;
6111 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6112
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006113 s->fe->fe_counters.cli_aborts++;
6114 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006115 if (objt_server(s->target))
6116 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006117
6118 if (!(s->flags & SN_ERR_MASK))
6119 s->flags |= SN_ERR_CLICL;
6120 if (!(s->flags & SN_FINST_MASK))
6121 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006122 return 0;
6123}
6124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006125/* Iterate the same filter through all request headers.
6126 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006127 * Since it can manage the switch to another backend, it updates the per-proxy
6128 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006129 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006130int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006131{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006132 char term;
6133 char *cur_ptr, *cur_end, *cur_next;
6134 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006135 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006136 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006137 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 last_hdr = 0;
6140
Willy Tarreau9b28e032012-10-12 23:49:43 +02006141 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006142 old_idx = 0;
6143
6144 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006145 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006146 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006147 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006148 (exp->action == ACT_ALLOW ||
6149 exp->action == ACT_DENY ||
6150 exp->action == ACT_TARPIT))
6151 return 0;
6152
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006153 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006154 if (!cur_idx)
6155 break;
6156
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006157 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006158 cur_ptr = cur_next;
6159 cur_end = cur_ptr + cur_hdr->len;
6160 cur_next = cur_end + cur_hdr->cr + 1;
6161
6162 /* Now we have one header between cur_ptr and cur_end,
6163 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006164 */
6165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006166 /* The annoying part is that pattern matching needs
6167 * that we modify the contents to null-terminate all
6168 * strings before testing them.
6169 */
6170
6171 term = *cur_end;
6172 *cur_end = '\0';
6173
6174 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6175 switch (exp->action) {
6176 case ACT_SETBE:
6177 /* It is not possible to jump a second time.
6178 * FIXME: should we return an HTTP/500 here so that
6179 * the admin knows there's a problem ?
6180 */
6181 if (t->be != t->fe)
6182 break;
6183
6184 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006185 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006186 last_hdr = 1;
6187 break;
6188
6189 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006190 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006191 last_hdr = 1;
6192 break;
6193
6194 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006195 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006196 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006197 break;
6198
6199 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006200 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006201 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006202 break;
6203
6204 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006205 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6206 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006207 /* FIXME: if the user adds a newline in the replacement, the
6208 * index will not be recalculated for now, and the new line
6209 * will not be counted as a new header.
6210 */
6211
6212 cur_end += delta;
6213 cur_next += delta;
6214 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006215 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006216 break;
6217
6218 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006219 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006220 cur_next += delta;
6221
Willy Tarreaufa355d42009-11-29 18:12:29 +01006222 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006223 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6224 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006225 cur_hdr->len = 0;
6226 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006227 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006228 break;
6229
6230 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006231 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006232 if (cur_end)
6233 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006235 /* keep the link from this header to next one in case of later
6236 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006238 old_idx = cur_idx;
6239 }
6240 return 0;
6241}
6242
6243
6244/* Apply the filter to the request line.
6245 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6246 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006247 * Since it can manage the switch to another backend, it updates the per-proxy
6248 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006249 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006250int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006251{
6252 char term;
6253 char *cur_ptr, *cur_end;
6254 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006255 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006256 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreau3d300592007-03-18 18:34:41 +01006258 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006259 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006260 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006261 (exp->action == ACT_ALLOW ||
6262 exp->action == ACT_DENY ||
6263 exp->action == ACT_TARPIT))
6264 return 0;
6265 else if (exp->action == ACT_REMOVE)
6266 return 0;
6267
6268 done = 0;
6269
Willy Tarreau9b28e032012-10-12 23:49:43 +02006270 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006271 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006272
6273 /* Now we have the request line between cur_ptr and cur_end */
6274
6275 /* The annoying part is that pattern matching needs
6276 * that we modify the contents to null-terminate all
6277 * strings before testing them.
6278 */
6279
6280 term = *cur_end;
6281 *cur_end = '\0';
6282
6283 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6284 switch (exp->action) {
6285 case ACT_SETBE:
6286 /* It is not possible to jump a second time.
6287 * FIXME: should we return an HTTP/500 here so that
6288 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006290 if (t->be != t->fe)
6291 break;
6292
6293 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006294 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006295 done = 1;
6296 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006298 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006299 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 done = 1;
6301 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006303 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006304 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006305 done = 1;
6306 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006307
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006308 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006309 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006310 done = 1;
6311 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006313 case ACT_REPLACE:
6314 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006315 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6316 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 /* FIXME: if the user adds a newline in the replacement, the
6318 * index will not be recalculated for now, and the new line
6319 * will not be counted as a new header.
6320 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006321
Willy Tarreaufa355d42009-11-29 18:12:29 +01006322 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006323 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006324 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006325 HTTP_MSG_RQMETH,
6326 cur_ptr, cur_end + 1,
6327 NULL, NULL);
6328 if (unlikely(!cur_end))
6329 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006331 /* we have a full request and we know that we have either a CR
6332 * or an LF at <ptr>.
6333 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006334 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6335 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006336 /* there is no point trying this regex on headers */
6337 return 1;
6338 }
6339 }
6340 *cur_end = term; /* restore the string terminator */
6341 return done;
6342}
Willy Tarreau97de6242006-12-27 17:18:38 +01006343
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006346/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006347 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006349 * unparsable request. Since it can manage the switch to another backend, it
6350 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006351 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006352int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006354 struct http_txn *txn = &s->txn;
6355 struct hdr_exp *exp;
6356
6357 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006358 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006360 /*
6361 * The interleaving of transformations and verdicts
6362 * makes it difficult to decide to continue or stop
6363 * the evaluation.
6364 */
6365
Willy Tarreau6c123b12010-01-28 20:22:06 +01006366 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6367 break;
6368
Willy Tarreau3d300592007-03-18 18:34:41 +01006369 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006371 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006372 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006373
6374 /* if this filter had a condition, evaluate it now and skip to
6375 * next filter if the condition does not match.
6376 */
6377 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006378 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006379 ret = acl_pass(ret);
6380 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6381 ret = !ret;
6382
6383 if (!ret)
6384 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006385 }
6386
6387 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006388 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 if (unlikely(ret < 0))
6390 return -1;
6391
6392 if (likely(ret == 0)) {
6393 /* The filter did not match the request, it can be
6394 * iterated through all headers.
6395 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006396 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006399 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006400}
6401
6402
Willy Tarreaua15645d2007-03-18 16:22:39 +01006403
Willy Tarreau58f10d72006-12-04 02:26:12 +01006404/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006405 * Try to retrieve the server associated to the appsession.
6406 * If the server is found, it's assigned to the session.
6407 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006408void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006409 struct http_txn *txn = &t->txn;
6410 appsess *asession = NULL;
6411 char *sessid_temp = NULL;
6412
Cyril Bontéb21570a2009-11-29 20:04:48 +01006413 if (len > t->be->appsession_len) {
6414 len = t->be->appsession_len;
6415 }
6416
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006417 if (t->be->options2 & PR_O2_AS_REQL) {
6418 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006419 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006420 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006421 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006422 }
6423
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006424 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006425 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6426 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6427 return;
6428 }
6429
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006430 memcpy(txn->sessid, buf, len);
6431 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006432 }
6433
6434 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6435 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6436 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6437 return;
6438 }
6439
Cyril Bontéb21570a2009-11-29 20:04:48 +01006440 memcpy(sessid_temp, buf, len);
6441 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006442
6443 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6444 /* free previously allocated memory */
6445 pool_free2(apools.sessid, sessid_temp);
6446
6447 if (asession != NULL) {
6448 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6449 if (!(t->be->options2 & PR_O2_AS_REQL))
6450 asession->request_count++;
6451
6452 if (asession->serverid != NULL) {
6453 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006454
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006455 while (srv) {
6456 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006457 if ((srv->state & SRV_RUNNING) ||
6458 (t->be->options & PR_O_PERSIST) ||
6459 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006460 /* we found the server and it's usable */
6461 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006462 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006463 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006464 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006465
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006466 break;
6467 } else {
6468 txn->flags &= ~TX_CK_MASK;
6469 txn->flags |= TX_CK_DOWN;
6470 }
6471 }
6472 srv = srv->next;
6473 }
6474 }
6475 }
6476}
6477
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006478/* Find the end of a cookie value contained between <s> and <e>. It works the
6479 * same way as with headers above except that the semi-colon also ends a token.
6480 * See RFC2965 for more information. Note that it requires a valid header to
6481 * return a valid result.
6482 */
6483char *find_cookie_value_end(char *s, const char *e)
6484{
6485 int quoted, qdpair;
6486
6487 quoted = qdpair = 0;
6488 for (; s < e; s++) {
6489 if (qdpair) qdpair = 0;
6490 else if (quoted) {
6491 if (*s == '\\') qdpair = 1;
6492 else if (*s == '"') quoted = 0;
6493 }
6494 else if (*s == '"') quoted = 1;
6495 else if (*s == ',' || *s == ';') return s;
6496 }
6497 return s;
6498}
6499
6500/* Delete a value in a header between delimiters <from> and <next> in buffer
6501 * <buf>. The number of characters displaced is returned, and the pointer to
6502 * the first delimiter is updated if required. The function tries as much as
6503 * possible to respect the following principles :
6504 * - replace <from> delimiter by the <next> one unless <from> points to a
6505 * colon, in which case <next> is simply removed
6506 * - set exactly one space character after the new first delimiter, unless
6507 * there are not enough characters in the block being moved to do so.
6508 * - remove unneeded spaces before the previous delimiter and after the new
6509 * one.
6510 *
6511 * It is the caller's responsibility to ensure that :
6512 * - <from> points to a valid delimiter or the colon ;
6513 * - <next> points to a valid delimiter or the final CR/LF ;
6514 * - there are non-space chars before <from> ;
6515 * - there is a CR/LF at or after <next>.
6516 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006517int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006518{
6519 char *prev = *from;
6520
6521 if (*prev == ':') {
6522 /* We're removing the first value, preserve the colon and add a
6523 * space if possible.
6524 */
6525 if (!http_is_crlf[(unsigned char)*next])
6526 next++;
6527 prev++;
6528 if (prev < next)
6529 *prev++ = ' ';
6530
6531 while (http_is_spht[(unsigned char)*next])
6532 next++;
6533 } else {
6534 /* Remove useless spaces before the old delimiter. */
6535 while (http_is_spht[(unsigned char)*(prev-1)])
6536 prev--;
6537 *from = prev;
6538
6539 /* copy the delimiter and if possible a space if we're
6540 * not at the end of the line.
6541 */
6542 if (!http_is_crlf[(unsigned char)*next]) {
6543 *prev++ = *next++;
6544 if (prev + 1 < next)
6545 *prev++ = ' ';
6546 while (http_is_spht[(unsigned char)*next])
6547 next++;
6548 }
6549 }
6550 return buffer_replace2(buf, prev, next, NULL, 0);
6551}
6552
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006553/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006554 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006555 * desirable to call it only when needed. This code is quite complex because
6556 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6557 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006558 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006559void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006561 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006562 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006564 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6565 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006566
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006567 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006568 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006569 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006570
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006571 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006572 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006573 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006574
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006575 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006576 hdr_beg = hdr_next;
6577 hdr_end = hdr_beg + cur_hdr->len;
6578 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006580 /* We have one full header between hdr_beg and hdr_end, and the
6581 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006582 * "Cookie:" headers.
6583 */
6584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006586 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006587 old_idx = cur_idx;
6588 continue;
6589 }
6590
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 del_from = NULL; /* nothing to be deleted */
6592 preserve_hdr = 0; /* assume we may kill the whole header */
6593
Willy Tarreau58f10d72006-12-04 02:26:12 +01006594 /* Now look for cookies. Conforming to RFC2109, we have to support
6595 * attributes whose name begin with a '$', and associate them with
6596 * the right cookie, if we want to delete this cookie.
6597 * So there are 3 cases for each cookie read :
6598 * 1) it's a special attribute, beginning with a '$' : ignore it.
6599 * 2) it's a server id cookie that we *MAY* want to delete : save
6600 * some pointers on it (last semi-colon, beginning of cookie...)
6601 * 3) it's an application cookie : we *MAY* have to delete a previous
6602 * "special" cookie.
6603 * At the end of loop, if a "special" cookie remains, we may have to
6604 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006605 * *MUST* delete it.
6606 *
6607 * Note: RFC2965 is unclear about the processing of spaces around
6608 * the equal sign in the ATTR=VALUE form. A careful inspection of
6609 * the RFC explicitly allows spaces before it, and not within the
6610 * tokens (attrs or values). An inspection of RFC2109 allows that
6611 * too but section 10.1.3 lets one think that spaces may be allowed
6612 * after the equal sign too, resulting in some (rare) buggy
6613 * implementations trying to do that. So let's do what servers do.
6614 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6615 * allowed quoted strings in values, with any possible character
6616 * after a backslash, including control chars and delimitors, which
6617 * causes parsing to become ambiguous. Browsers also allow spaces
6618 * within values even without quotes.
6619 *
6620 * We have to keep multiple pointers in order to support cookie
6621 * removal at the beginning, middle or end of header without
6622 * corrupting the header. All of these headers are valid :
6623 *
6624 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6625 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6626 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6627 * | | | | | | | | |
6628 * | | | | | | | | hdr_end <--+
6629 * | | | | | | | +--> next
6630 * | | | | | | +----> val_end
6631 * | | | | | +-----------> val_beg
6632 * | | | | +--------------> equal
6633 * | | | +----------------> att_end
6634 * | | +---------------------> att_beg
6635 * | +--------------------------> prev
6636 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006637 */
6638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006639 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6640 /* Iterate through all cookies on this line */
6641
6642 /* find att_beg */
6643 att_beg = prev + 1;
6644 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6645 att_beg++;
6646
6647 /* find att_end : this is the first character after the last non
6648 * space before the equal. It may be equal to hdr_end.
6649 */
6650 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006651
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006652 while (equal < hdr_end) {
6653 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006655 if (http_is_spht[(unsigned char)*equal++])
6656 continue;
6657 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006658 }
6659
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6661 * is between <att_beg> and <equal>, both may be identical.
6662 */
6663
6664 /* look for end of cookie if there is an equal sign */
6665 if (equal < hdr_end && *equal == '=') {
6666 /* look for the beginning of the value */
6667 val_beg = equal + 1;
6668 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6669 val_beg++;
6670
6671 /* find the end of the value, respecting quotes */
6672 next = find_cookie_value_end(val_beg, hdr_end);
6673
6674 /* make val_end point to the first white space or delimitor after the value */
6675 val_end = next;
6676 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6677 val_end--;
6678 } else {
6679 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006680 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006681
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006682 /* We have nothing to do with attributes beginning with '$'. However,
6683 * they will automatically be removed if a header before them is removed,
6684 * since they're supposed to be linked together.
6685 */
6686 if (*att_beg == '$')
6687 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006688
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 /* Ignore cookies with no equal sign */
6690 if (equal == next) {
6691 /* This is not our cookie, so we must preserve it. But if we already
6692 * scheduled another cookie for removal, we cannot remove the
6693 * complete header, but we can remove the previous block itself.
6694 */
6695 preserve_hdr = 1;
6696 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006697 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006698 val_end += delta;
6699 next += delta;
6700 hdr_end += delta;
6701 hdr_next += delta;
6702 cur_hdr->len += delta;
6703 http_msg_move_end(&txn->req, delta);
6704 prev = del_from;
6705 del_from = NULL;
6706 }
6707 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006709
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006710 /* if there are spaces around the equal sign, we need to
6711 * strip them otherwise we'll get trouble for cookie captures,
6712 * or even for rewrites. Since this happens extremely rarely,
6713 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006714 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6716 int stripped_before = 0;
6717 int stripped_after = 0;
6718
6719 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006720 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 equal += stripped_before;
6722 val_beg += stripped_before;
6723 }
6724
6725 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006726 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006727 val_beg += stripped_after;
6728 stripped_before += stripped_after;
6729 }
6730
6731 val_end += stripped_before;
6732 next += stripped_before;
6733 hdr_end += stripped_before;
6734 hdr_next += stripped_before;
6735 cur_hdr->len += stripped_before;
6736 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006737 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006738 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006739
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006740 /* First, let's see if we want to capture this cookie. We check
6741 * that we don't already have a client side cookie, because we
6742 * can only capture one. Also as an optimisation, we ignore
6743 * cookies shorter than the declared name.
6744 */
6745 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6746 (val_end - att_beg >= t->fe->capture_namelen) &&
6747 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6748 int log_len = val_end - att_beg;
6749
6750 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6751 Alert("HTTP logging : out of memory.\n");
6752 } else {
6753 if (log_len > t->fe->capture_len)
6754 log_len = t->fe->capture_len;
6755 memcpy(txn->cli_cookie, att_beg, log_len);
6756 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006757 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006758 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006759
Willy Tarreaubca99692010-10-06 19:25:55 +02006760 /* Persistence cookies in passive, rewrite or insert mode have the
6761 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006762 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006763 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006765 * For cookies in prefix mode, the form is :
6766 *
6767 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 */
6769 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6770 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6771 struct server *srv = t->be->srv;
6772 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006773
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006774 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6775 * have the server ID between val_beg and delim, and the original cookie between
6776 * delim+1 and val_end. Otherwise, delim==val_end :
6777 *
6778 * Cookie: NAME=SRV; # in all but prefix modes
6779 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6780 * | || || | |+-> next
6781 * | || || | +--> val_end
6782 * | || || +---------> delim
6783 * | || |+------------> val_beg
6784 * | || +-------------> att_end = equal
6785 * | |+-----------------> att_beg
6786 * | +------------------> prev
6787 * +-------------------------> hdr_beg
6788 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006789
Willy Tarreau67402132012-05-31 20:40:20 +02006790 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 for (delim = val_beg; delim < val_end; delim++)
6792 if (*delim == COOKIE_DELIM)
6793 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006794 } else {
6795 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006796 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006797 /* Now check if the cookie contains a date field, which would
6798 * appear after a vertical bar ('|') just after the server name
6799 * and before the delimiter.
6800 */
6801 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6802 if (vbar1) {
6803 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006804 * right is the last seen date. It is a base64 encoded
6805 * 30-bit value representing the UNIX date since the
6806 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006807 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006808 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006809 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006810 if (val_end - vbar1 >= 5) {
6811 val = b64tos30(vbar1);
6812 if (val > 0)
6813 txn->cookie_last_date = val << 2;
6814 }
6815 /* look for a second vertical bar */
6816 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6817 if (vbar1 && (val_end - vbar1 > 5)) {
6818 val = b64tos30(vbar1 + 1);
6819 if (val > 0)
6820 txn->cookie_first_date = val << 2;
6821 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006822 }
6823 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006824
Willy Tarreauf64d1412010-10-07 20:06:11 +02006825 /* if the cookie has an expiration date and the proxy wants to check
6826 * it, then we do that now. We first check if the cookie is too old,
6827 * then only if it has expired. We detect strict overflow because the
6828 * time resolution here is not great (4 seconds). Cookies with dates
6829 * in the future are ignored if their offset is beyond one day. This
6830 * allows an admin to fix timezone issues without expiring everyone
6831 * and at the same time avoids keeping unwanted side effects for too
6832 * long.
6833 */
6834 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006835 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6836 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006837 txn->flags &= ~TX_CK_MASK;
6838 txn->flags |= TX_CK_OLD;
6839 delim = val_beg; // let's pretend we have not found the cookie
6840 txn->cookie_first_date = 0;
6841 txn->cookie_last_date = 0;
6842 }
6843 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006844 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6845 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006846 txn->flags &= ~TX_CK_MASK;
6847 txn->flags |= TX_CK_EXPIRED;
6848 delim = val_beg; // let's pretend we have not found the cookie
6849 txn->cookie_first_date = 0;
6850 txn->cookie_last_date = 0;
6851 }
6852
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006853 /* Here, we'll look for the first running server which supports the cookie.
6854 * This allows to share a same cookie between several servers, for example
6855 * to dedicate backup servers to specific servers only.
6856 * However, to prevent clients from sticking to cookie-less backup server
6857 * when they have incidentely learned an empty cookie, we simply ignore
6858 * empty cookies and mark them as invalid.
6859 * The same behaviour is applied when persistence must be ignored.
6860 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006861 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006863
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006864 while (srv) {
6865 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6866 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6867 if ((srv->state & SRV_RUNNING) ||
6868 (t->be->options & PR_O_PERSIST) ||
6869 (t->flags & SN_FORCE_PRST)) {
6870 /* we found the server and we can use it */
6871 txn->flags &= ~TX_CK_MASK;
6872 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6873 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006874 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006875 break;
6876 } else {
6877 /* we found a server, but it's down,
6878 * mark it as such and go on in case
6879 * another one is available.
6880 */
6881 txn->flags &= ~TX_CK_MASK;
6882 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006883 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006884 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006885 srv = srv->next;
6886 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006887
Willy Tarreauf64d1412010-10-07 20:06:11 +02006888 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006889 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006891 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6892 txn->flags |= TX_CK_UNUSED;
6893 else
6894 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006896
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006897 /* depending on the cookie mode, we may have to either :
6898 * - delete the complete cookie if we're in insert+indirect mode, so that
6899 * the server never sees it ;
6900 * - remove the server id from the cookie value, and tag the cookie as an
6901 * application cookie so that it does not get accidentely removed later,
6902 * if we're in cookie prefix mode
6903 */
Willy Tarreau67402132012-05-31 20:40:20 +02006904 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906
Willy Tarreau9b28e032012-10-12 23:49:43 +02006907 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006908 val_end += delta;
6909 next += delta;
6910 hdr_end += delta;
6911 hdr_next += delta;
6912 cur_hdr->len += delta;
6913 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 del_from = NULL;
6916 preserve_hdr = 1; /* we want to keep this cookie */
6917 }
6918 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006919 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 del_from = prev;
6921 }
6922 } else {
6923 /* This is not our cookie, so we must preserve it. But if we already
6924 * scheduled another cookie for removal, we cannot remove the
6925 * complete header, but we can remove the previous block itself.
6926 */
6927 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006928
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006929 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006930 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006931 if (att_beg >= del_from)
6932 att_beg += delta;
6933 if (att_end >= del_from)
6934 att_end += delta;
6935 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006936 val_end += delta;
6937 next += delta;
6938 hdr_end += delta;
6939 hdr_next += delta;
6940 cur_hdr->len += delta;
6941 http_msg_move_end(&txn->req, delta);
6942 prev = del_from;
6943 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006944 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006945 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006946
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006947 /* Look for the appsession cookie unless persistence must be ignored */
6948 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6949 int cmp_len, value_len;
6950 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006951
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 if (t->be->options2 & PR_O2_AS_PFX) {
6953 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6954 value_begin = att_beg + t->be->appsession_name_len;
6955 value_len = val_end - att_beg - t->be->appsession_name_len;
6956 } else {
6957 cmp_len = att_end - att_beg;
6958 value_begin = val_beg;
6959 value_len = val_end - val_beg;
6960 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 /* let's see if the cookie is our appcookie */
6963 if (cmp_len == t->be->appsession_name_len &&
6964 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6965 manage_client_side_appsession(t, value_begin, value_len);
6966 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006967 }
6968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 /* continue with next cookie on this header line */
6970 att_beg = next;
6971 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006973 /* There are no more cookies on this line.
6974 * We may still have one (or several) marked for deletion at the
6975 * end of the line. We must do this now in two ways :
6976 * - if some cookies must be preserved, we only delete from the
6977 * mark to the end of line ;
6978 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006979 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006981 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006982 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006983 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006984 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985 cur_hdr->len += delta;
6986 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006987 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006988
6989 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006990 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6991 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006992 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006993 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006995 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006996 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006997 }
6998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007000 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007001 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007002}
7003
7004
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005/* Iterate the same filter through all response headers contained in <rtr>.
7006 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7007 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007008int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009{
7010 char term;
7011 char *cur_ptr, *cur_end, *cur_next;
7012 int cur_idx, old_idx, last_hdr;
7013 struct http_txn *txn = &t->txn;
7014 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007015 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007016
7017 last_hdr = 0;
7018
Willy Tarreau9b28e032012-10-12 23:49:43 +02007019 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 old_idx = 0;
7021
7022 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007023 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007025 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026 (exp->action == ACT_ALLOW ||
7027 exp->action == ACT_DENY))
7028 return 0;
7029
7030 cur_idx = txn->hdr_idx.v[old_idx].next;
7031 if (!cur_idx)
7032 break;
7033
7034 cur_hdr = &txn->hdr_idx.v[cur_idx];
7035 cur_ptr = cur_next;
7036 cur_end = cur_ptr + cur_hdr->len;
7037 cur_next = cur_end + cur_hdr->cr + 1;
7038
7039 /* Now we have one header between cur_ptr and cur_end,
7040 * and the next header starts at cur_next.
7041 */
7042
7043 /* The annoying part is that pattern matching needs
7044 * that we modify the contents to null-terminate all
7045 * strings before testing them.
7046 */
7047
7048 term = *cur_end;
7049 *cur_end = '\0';
7050
7051 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7052 switch (exp->action) {
7053 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007054 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 last_hdr = 1;
7056 break;
7057
7058 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007059 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007060 last_hdr = 1;
7061 break;
7062
7063 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007064 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7065 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007066 /* FIXME: if the user adds a newline in the replacement, the
7067 * index will not be recalculated for now, and the new line
7068 * will not be counted as a new header.
7069 */
7070
7071 cur_end += delta;
7072 cur_next += delta;
7073 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007074 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007075 break;
7076
7077 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007078 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007079 cur_next += delta;
7080
Willy Tarreaufa355d42009-11-29 18:12:29 +01007081 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7083 txn->hdr_idx.used--;
7084 cur_hdr->len = 0;
7085 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007086 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007087 break;
7088
7089 }
7090 }
7091 if (cur_end)
7092 *cur_end = term; /* restore the string terminator */
7093
7094 /* keep the link from this header to next one in case of later
7095 * removal of next header.
7096 */
7097 old_idx = cur_idx;
7098 }
7099 return 0;
7100}
7101
7102
7103/* Apply the filter to the status line in the response buffer <rtr>.
7104 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7105 * or -1 if a replacement resulted in an invalid status line.
7106 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007107int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007108{
7109 char term;
7110 char *cur_ptr, *cur_end;
7111 int done;
7112 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007113 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007114
7115
Willy Tarreau3d300592007-03-18 18:34:41 +01007116 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007118 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 (exp->action == ACT_ALLOW ||
7120 exp->action == ACT_DENY))
7121 return 0;
7122 else if (exp->action == ACT_REMOVE)
7123 return 0;
7124
7125 done = 0;
7126
Willy Tarreau9b28e032012-10-12 23:49:43 +02007127 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007128 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007129
7130 /* Now we have the status line between cur_ptr and cur_end */
7131
7132 /* The annoying part is that pattern matching needs
7133 * that we modify the contents to null-terminate all
7134 * strings before testing them.
7135 */
7136
7137 term = *cur_end;
7138 *cur_end = '\0';
7139
7140 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7141 switch (exp->action) {
7142 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007143 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 done = 1;
7145 break;
7146
7147 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007148 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 done = 1;
7150 break;
7151
7152 case ACT_REPLACE:
7153 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007154 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7155 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 /* FIXME: if the user adds a newline in the replacement, the
7157 * index will not be recalculated for now, and the new line
7158 * will not be counted as a new header.
7159 */
7160
Willy Tarreaufa355d42009-11-29 18:12:29 +01007161 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007163 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007164 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 cur_ptr, cur_end + 1,
7166 NULL, NULL);
7167 if (unlikely(!cur_end))
7168 return -1;
7169
7170 /* we have a full respnse and we know that we have either a CR
7171 * or an LF at <ptr>.
7172 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007173 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007174 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175 /* there is no point trying this regex on headers */
7176 return 1;
7177 }
7178 }
7179 *cur_end = term; /* restore the string terminator */
7180 return done;
7181}
7182
7183
7184
7185/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007186 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7188 * unparsable response.
7189 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007190int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007191{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007192 struct http_txn *txn = &s->txn;
7193 struct hdr_exp *exp;
7194
7195 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196 int ret;
7197
7198 /*
7199 * The interleaving of transformations and verdicts
7200 * makes it difficult to decide to continue or stop
7201 * the evaluation.
7202 */
7203
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007204 if (txn->flags & TX_SVDENY)
7205 break;
7206
Willy Tarreau3d300592007-03-18 18:34:41 +01007207 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007208 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7209 exp->action == ACT_PASS)) {
7210 exp = exp->next;
7211 continue;
7212 }
7213
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007214 /* if this filter had a condition, evaluate it now and skip to
7215 * next filter if the condition does not match.
7216 */
7217 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007218 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007219 ret = acl_pass(ret);
7220 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7221 ret = !ret;
7222 if (!ret)
7223 continue;
7224 }
7225
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007227 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228 if (unlikely(ret < 0))
7229 return -1;
7230
7231 if (likely(ret == 0)) {
7232 /* The filter did not match the response, it can be
7233 * iterated through all headers.
7234 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007235 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 }
7238 return 0;
7239}
7240
7241
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007243 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007244 * desirable to call it only when needed. This function is also used when we
7245 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007247void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248{
7249 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007250 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007251 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007252 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007253 char *hdr_beg, *hdr_end, *hdr_next;
7254 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256 /* Iterate through the headers.
7257 * we start with the start line.
7258 */
7259 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007260 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007261
7262 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7263 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007264 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265
7266 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007267 hdr_beg = hdr_next;
7268 hdr_end = hdr_beg + cur_hdr->len;
7269 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270
Willy Tarreau24581ba2010-08-31 22:39:35 +02007271 /* We have one full header between hdr_beg and hdr_end, and the
7272 * next header starts at hdr_next. We're only interested in
7273 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 */
7275
Willy Tarreau24581ba2010-08-31 22:39:35 +02007276 is_cookie2 = 0;
7277 prev = hdr_beg + 10;
7278 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007279 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007280 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7281 if (!val) {
7282 old_idx = cur_idx;
7283 continue;
7284 }
7285 is_cookie2 = 1;
7286 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287 }
7288
Willy Tarreau24581ba2010-08-31 22:39:35 +02007289 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7290 * <prev> points to the colon.
7291 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007292 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293
Willy Tarreau24581ba2010-08-31 22:39:35 +02007294 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7295 * check-cache is enabled) and we are not interested in checking
7296 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007297 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007298 if (t->be->cookie_name == NULL &&
7299 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007300 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 return;
7302
Willy Tarreau24581ba2010-08-31 22:39:35 +02007303 /* OK so now we know we have to process this response cookie.
7304 * The format of the Set-Cookie header is slightly different
7305 * from the format of the Cookie header in that it does not
7306 * support the comma as a cookie delimiter (thus the header
7307 * cannot be folded) because the Expires attribute described in
7308 * the original Netscape's spec may contain an unquoted date
7309 * with a comma inside. We have to live with this because
7310 * many browsers don't support Max-Age and some browsers don't
7311 * support quoted strings. However the Set-Cookie2 header is
7312 * clean.
7313 *
7314 * We have to keep multiple pointers in order to support cookie
7315 * removal at the beginning, middle or end of header without
7316 * corrupting the header (in case of set-cookie2). A special
7317 * pointer, <scav> points to the beginning of the set-cookie-av
7318 * fields after the first semi-colon. The <next> pointer points
7319 * either to the end of line (set-cookie) or next unquoted comma
7320 * (set-cookie2). All of these headers are valid :
7321 *
7322 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7323 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7324 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7325 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7326 * | | | | | | | | | |
7327 * | | | | | | | | +-> next hdr_end <--+
7328 * | | | | | | | +------------> scav
7329 * | | | | | | +--------------> val_end
7330 * | | | | | +--------------------> val_beg
7331 * | | | | +----------------------> equal
7332 * | | | +------------------------> att_end
7333 * | | +----------------------------> att_beg
7334 * | +------------------------------> prev
7335 * +-----------------------------------------> hdr_beg
7336 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337
Willy Tarreau24581ba2010-08-31 22:39:35 +02007338 for (; prev < hdr_end; prev = next) {
7339 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340
Willy Tarreau24581ba2010-08-31 22:39:35 +02007341 /* find att_beg */
7342 att_beg = prev + 1;
7343 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7344 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345
Willy Tarreau24581ba2010-08-31 22:39:35 +02007346 /* find att_end : this is the first character after the last non
7347 * space before the equal. It may be equal to hdr_end.
7348 */
7349 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350
Willy Tarreau24581ba2010-08-31 22:39:35 +02007351 while (equal < hdr_end) {
7352 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7353 break;
7354 if (http_is_spht[(unsigned char)*equal++])
7355 continue;
7356 att_end = equal;
7357 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007358
Willy Tarreau24581ba2010-08-31 22:39:35 +02007359 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7360 * is between <att_beg> and <equal>, both may be identical.
7361 */
7362
7363 /* look for end of cookie if there is an equal sign */
7364 if (equal < hdr_end && *equal == '=') {
7365 /* look for the beginning of the value */
7366 val_beg = equal + 1;
7367 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7368 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007369
Willy Tarreau24581ba2010-08-31 22:39:35 +02007370 /* find the end of the value, respecting quotes */
7371 next = find_cookie_value_end(val_beg, hdr_end);
7372
7373 /* make val_end point to the first white space or delimitor after the value */
7374 val_end = next;
7375 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7376 val_end--;
7377 } else {
7378 /* <equal> points to next comma, semi-colon or EOL */
7379 val_beg = val_end = next = equal;
7380 }
7381
7382 if (next < hdr_end) {
7383 /* Set-Cookie2 supports multiple cookies, and <next> points to
7384 * a colon or semi-colon before the end. So skip all attr-value
7385 * pairs and look for the next comma. For Set-Cookie, since
7386 * commas are permitted in values, skip to the end.
7387 */
7388 if (is_cookie2)
7389 next = find_hdr_value_end(next, hdr_end);
7390 else
7391 next = hdr_end;
7392 }
7393
7394 /* Now everything is as on the diagram above */
7395
7396 /* Ignore cookies with no equal sign */
7397 if (equal == val_end)
7398 continue;
7399
7400 /* If there are spaces around the equal sign, we need to
7401 * strip them otherwise we'll get trouble for cookie captures,
7402 * or even for rewrites. Since this happens extremely rarely,
7403 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007404 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007405 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7406 int stripped_before = 0;
7407 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408
Willy Tarreau24581ba2010-08-31 22:39:35 +02007409 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007410 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007411 equal += stripped_before;
7412 val_beg += stripped_before;
7413 }
7414
7415 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007416 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007417 val_beg += stripped_after;
7418 stripped_before += stripped_after;
7419 }
7420
7421 val_end += stripped_before;
7422 next += stripped_before;
7423 hdr_end += stripped_before;
7424 hdr_next += stripped_before;
7425 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007426 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007427 }
7428
7429 /* First, let's see if we want to capture this cookie. We check
7430 * that we don't already have a server side cookie, because we
7431 * can only capture one. Also as an optimisation, we ignore
7432 * cookies shorter than the declared name.
7433 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007434 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007435 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007436 (val_end - att_beg >= t->fe->capture_namelen) &&
7437 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7438 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007439 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440 Alert("HTTP logging : out of memory.\n");
7441 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007442 else {
7443 if (log_len > t->fe->capture_len)
7444 log_len = t->fe->capture_len;
7445 memcpy(txn->srv_cookie, att_beg, log_len);
7446 txn->srv_cookie[log_len] = 0;
7447 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007448 }
7449
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007450 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007451 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007452 if (!(t->flags & SN_IGNORE_PRST) &&
7453 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7454 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007455 /* assume passive cookie by default */
7456 txn->flags &= ~TX_SCK_MASK;
7457 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007458
7459 /* If the cookie is in insert mode on a known server, we'll delete
7460 * this occurrence because we'll insert another one later.
7461 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007462 * a direct access.
7463 */
Willy Tarreau67402132012-05-31 20:40:20 +02007464 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007465 /* The "preserve" flag was set, we don't want to touch the
7466 * server's cookie.
7467 */
7468 }
Willy Tarreau67402132012-05-31 20:40:20 +02007469 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7470 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007471 /* this cookie must be deleted */
7472 if (*prev == ':' && next == hdr_end) {
7473 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007474 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007475 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7476 txn->hdr_idx.used--;
7477 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007478 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007479 hdr_next += delta;
7480 http_msg_move_end(&txn->rsp, delta);
7481 /* note: while both invalid now, <next> and <hdr_end>
7482 * are still equal, so the for() will stop as expected.
7483 */
7484 } else {
7485 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007486 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007487 next = prev;
7488 hdr_end += delta;
7489 hdr_next += delta;
7490 cur_hdr->len += delta;
7491 http_msg_move_end(&txn->rsp, delta);
7492 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007493 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007494 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007495 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496 }
Willy Tarreau67402132012-05-31 20:40:20 +02007497 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007498 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007499 * with this server since we know it.
7500 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007501 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 next += delta;
7503 hdr_end += delta;
7504 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007505 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007506 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007507
Willy Tarreauf1348312010-10-07 15:54:11 +02007508 txn->flags &= ~TX_SCK_MASK;
7509 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007511 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007513 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007514 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007515 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007516 next += delta;
7517 hdr_end += delta;
7518 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007520 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007521
Willy Tarreau827aee92011-03-10 16:55:02 +01007522 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007523 txn->flags &= ~TX_SCK_MASK;
7524 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525 }
7526 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007527 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7528 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007529 int cmp_len, value_len;
7530 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007531
Cyril Bontéb21570a2009-11-29 20:04:48 +01007532 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007533 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7534 value_begin = att_beg + t->be->appsession_name_len;
7535 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007536 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007537 cmp_len = att_end - att_beg;
7538 value_begin = val_beg;
7539 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007540 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007541
Cyril Bonté17530c32010-04-06 21:11:10 +02007542 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007543 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7544 /* free a possibly previously allocated memory */
7545 pool_free2(apools.sessid, txn->sessid);
7546
Cyril Bontéb21570a2009-11-29 20:04:48 +01007547 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007548 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007549 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7550 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7551 return;
7552 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007553 memcpy(txn->sessid, value_begin, value_len);
7554 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007556 }
7557 /* that's done for this cookie, check the next one on the same
7558 * line when next != hdr_end (only if is_cookie2).
7559 */
7560 }
7561 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007562 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007563 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007564
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007565 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007566 appsess *asession = NULL;
7567 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007568 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007569 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007570 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007571 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7572 Alert("Not enough Memory process_srv():asession:calloc().\n");
7573 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7574 return;
7575 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007576 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7577
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007578 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7579 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7580 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007581 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007582 return;
7583 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007584 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007585 asession->sessid[t->be->appsession_len] = 0;
7586
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007587 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007588 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007589 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007590 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007591 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007592 return;
7593 }
7594 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007595 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007596
7597 asession->request_count = 0;
7598 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7599 }
7600
7601 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7602 asession->request_count++;
7603 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604}
7605
7606
Willy Tarreaua15645d2007-03-18 16:22:39 +01007607/*
7608 * Check if response is cacheable or not. Updates t->flags.
7609 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007610void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611{
7612 struct http_txn *txn = &t->txn;
7613 char *p1, *p2;
7614
7615 char *cur_ptr, *cur_end, *cur_next;
7616 int cur_idx;
7617
Willy Tarreau5df51872007-11-25 16:20:08 +01007618 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007619 return;
7620
7621 /* Iterate through the headers.
7622 * we start with the start line.
7623 */
7624 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007625 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007626
7627 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7628 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007629 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007630
7631 cur_hdr = &txn->hdr_idx.v[cur_idx];
7632 cur_ptr = cur_next;
7633 cur_end = cur_ptr + cur_hdr->len;
7634 cur_next = cur_end + cur_hdr->cr + 1;
7635
7636 /* We have one full header between cur_ptr and cur_end, and the
7637 * next header starts at cur_next. We're only interested in
7638 * "Cookie:" headers.
7639 */
7640
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007641 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7642 if (val) {
7643 if ((cur_end - (cur_ptr + val) >= 8) &&
7644 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7645 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7646 return;
7647 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007648 }
7649
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007650 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7651 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007652 continue;
7653
7654 /* OK, right now we know we have a cache-control header at cur_ptr */
7655
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007656 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657
7658 if (p1 >= cur_end) /* no more info */
7659 continue;
7660
7661 /* p1 is at the beginning of the value */
7662 p2 = p1;
7663
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007664 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 p2++;
7666
7667 /* we have a complete value between p1 and p2 */
7668 if (p2 < cur_end && *p2 == '=') {
7669 /* we have something of the form no-cache="set-cookie" */
7670 if ((cur_end - p1 >= 21) &&
7671 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7672 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007673 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674 continue;
7675 }
7676
7677 /* OK, so we know that either p2 points to the end of string or to a comma */
7678 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007679 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007680 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7681 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7682 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007683 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007684 return;
7685 }
7686
7687 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007688 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007689 continue;
7690 }
7691 }
7692}
7693
7694
Willy Tarreau58f10d72006-12-04 02:26:12 +01007695/*
7696 * Try to retrieve a known appsession in the URI, then the associated server.
7697 * If the server is found, it's assigned to the session.
7698 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007699void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007701 char *end_params, *first_param, *cur_param, *next_param;
7702 char separator;
7703 int value_len;
7704
7705 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007707 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007708 (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 +01007709 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007710 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007711
Cyril Bontéb21570a2009-11-29 20:04:48 +01007712 first_param = NULL;
7713 switch (mode) {
7714 case PR_O2_AS_M_PP:
7715 first_param = memchr(begin, ';', len);
7716 break;
7717 case PR_O2_AS_M_QS:
7718 first_param = memchr(begin, '?', len);
7719 break;
7720 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721
Cyril Bontéb21570a2009-11-29 20:04:48 +01007722 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007724 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007725
Cyril Bontéb21570a2009-11-29 20:04:48 +01007726 switch (mode) {
7727 case PR_O2_AS_M_PP:
7728 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7729 end_params = (char *) begin + len;
7730 }
7731 separator = ';';
7732 break;
7733 case PR_O2_AS_M_QS:
7734 end_params = (char *) begin + len;
7735 separator = '&';
7736 break;
7737 default:
7738 /* unknown mode, shouldn't happen */
7739 return;
7740 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007741
Cyril Bontéb21570a2009-11-29 20:04:48 +01007742 cur_param = next_param = end_params;
7743 while (cur_param > first_param) {
7744 cur_param--;
7745 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7746 /* let's see if this is the appsession parameter */
7747 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7748 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7749 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7750 /* Cool... it's the right one */
7751 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7752 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7753 if (value_len > 0) {
7754 manage_client_side_appsession(t, cur_param, value_len);
7755 }
7756 break;
7757 }
7758 next_param = cur_param;
7759 }
7760 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007761#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007762 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007763 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007764#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007765}
7766
Willy Tarreaub2513902006-12-17 14:52:38 +01007767/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007768 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007769 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007770 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007771 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007772 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007773 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007774 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007775 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007776int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007777{
7778 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007779 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007780 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007781
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007782 if (!uri_auth)
7783 return 0;
7784
Cyril Bonté70be45d2010-10-12 00:14:35 +02007785 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007786 return 0;
7787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007788 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007789 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007790 return 0;
7791
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007792 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007793 return 0;
7794
Willy Tarreaub2513902006-12-17 14:52:38 +01007795 return 1;
7796}
7797
Willy Tarreau4076a152009-04-02 15:18:36 +02007798/*
7799 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007800 * By default it tries to report the error position as msg->err_pos. However if
7801 * this one is not set, it will then report msg->next, which is the last known
7802 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007803 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007804 */
7805void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007806 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007807 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007808{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007809 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007810 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007811
Willy Tarreau9b28e032012-10-12 23:49:43 +02007812 es->len = MIN(chn->buf->i, sizeof(es->buf));
7813 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007814 len1 = MIN(len1, es->len);
7815 len2 = es->len - len1; /* remaining data if buffer wraps */
7816
Willy Tarreau9b28e032012-10-12 23:49:43 +02007817 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007818 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007819 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007820
Willy Tarreau4076a152009-04-02 15:18:36 +02007821 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007822 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007823 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007824 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007825
Willy Tarreau4076a152009-04-02 15:18:36 +02007826 es->when = date; // user-visible date
7827 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007828 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007829 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007830 if (objt_conn(s->req->prod->end))
7831 es->src = __objt_conn(s->req->prod->end)->addr.from;
7832 else
7833 memset(&es->src, 0, sizeof(es->src));
7834
Willy Tarreau078272e2010-12-12 12:46:33 +01007835 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007836 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007837 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007838 es->s_flags = s->flags;
7839 es->t_flags = s->txn.flags;
7840 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007841 es->b_out = chn->buf->o;
7842 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007843 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007844 es->m_clen = msg->chunk_len;
7845 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007846}
Willy Tarreaub2513902006-12-17 14:52:38 +01007847
Willy Tarreau294c4732011-12-16 21:35:50 +01007848/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7849 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7850 * performed over the whole headers. Otherwise it must contain a valid header
7851 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7852 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7853 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7854 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007855 * -1. The value fetch stops at commas, so this function is suited for use with
7856 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007857 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007858 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007859unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007860 struct hdr_idx *idx, int occ,
7861 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007862{
Willy Tarreau294c4732011-12-16 21:35:50 +01007863 struct hdr_ctx local_ctx;
7864 char *ptr_hist[MAX_HDR_HISTORY];
7865 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007866 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007867 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007868
Willy Tarreau294c4732011-12-16 21:35:50 +01007869 if (!ctx) {
7870 local_ctx.idx = 0;
7871 ctx = &local_ctx;
7872 }
7873
Willy Tarreaubce70882009-09-07 11:51:47 +02007874 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007875 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007876 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007877 occ--;
7878 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007879 *vptr = ctx->line + ctx->val;
7880 *vlen = ctx->vlen;
7881 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007882 }
7883 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007884 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007885 }
7886
7887 /* negative occurrence, we scan all the list then walk back */
7888 if (-occ > MAX_HDR_HISTORY)
7889 return 0;
7890
Willy Tarreau294c4732011-12-16 21:35:50 +01007891 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007892 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007893 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7894 len_hist[hist_ptr] = ctx->vlen;
7895 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007896 hist_ptr = 0;
7897 found++;
7898 }
7899 if (-occ > found)
7900 return 0;
7901 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007902 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7903 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7904 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007905 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007906 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007907 if (hist_ptr >= MAX_HDR_HISTORY)
7908 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007909 *vptr = ptr_hist[hist_ptr];
7910 *vlen = len_hist[hist_ptr];
7911 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007912}
7913
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007914/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7915 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7916 * performed over the whole headers. Otherwise it must contain a valid header
7917 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7918 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7919 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7920 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7921 * -1. This function differs from http_get_hdr() in that it only returns full
7922 * line header values and does not stop at commas.
7923 * The return value is 0 if nothing was found, or non-zero otherwise.
7924 */
7925unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7926 struct hdr_idx *idx, int occ,
7927 struct hdr_ctx *ctx, char **vptr, int *vlen)
7928{
7929 struct hdr_ctx local_ctx;
7930 char *ptr_hist[MAX_HDR_HISTORY];
7931 int len_hist[MAX_HDR_HISTORY];
7932 unsigned int hist_ptr;
7933 int found;
7934
7935 if (!ctx) {
7936 local_ctx.idx = 0;
7937 ctx = &local_ctx;
7938 }
7939
7940 if (occ >= 0) {
7941 /* search from the beginning */
7942 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7943 occ--;
7944 if (occ <= 0) {
7945 *vptr = ctx->line + ctx->val;
7946 *vlen = ctx->vlen;
7947 return 1;
7948 }
7949 }
7950 return 0;
7951 }
7952
7953 /* negative occurrence, we scan all the list then walk back */
7954 if (-occ > MAX_HDR_HISTORY)
7955 return 0;
7956
7957 found = hist_ptr = 0;
7958 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7959 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7960 len_hist[hist_ptr] = ctx->vlen;
7961 if (++hist_ptr >= MAX_HDR_HISTORY)
7962 hist_ptr = 0;
7963 found++;
7964 }
7965 if (-occ > found)
7966 return 0;
7967 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7968 * find occurrence -occ, so we have to check [hist_ptr+occ].
7969 */
7970 hist_ptr += occ;
7971 if (hist_ptr >= MAX_HDR_HISTORY)
7972 hist_ptr -= MAX_HDR_HISTORY;
7973 *vptr = ptr_hist[hist_ptr];
7974 *vlen = len_hist[hist_ptr];
7975 return 1;
7976}
7977
Willy Tarreaubaaee002006-06-26 02:48:02 +02007978/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007979 * Print a debug line with a header. Always stop at the first CR or LF char,
7980 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7981 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 */
7983void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7984{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007985 int max;
7986 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007987 dir,
7988 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
7989 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 +02007990
7991 for (max = 0; start + max < end; max++)
7992 if (start[max] == '\r' || start[max] == '\n')
7993 break;
7994
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007995 UBOUND(max, trash.size - trash.len - 3);
7996 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7997 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007998 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007999}
8000
Willy Tarreau0937bc42009-12-22 15:03:09 +01008001/*
8002 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8003 * the required fields are properly allocated and that we only need to (re)init
8004 * them. This should be used before processing any new request.
8005 */
8006void http_init_txn(struct session *s)
8007{
8008 struct http_txn *txn = &s->txn;
8009 struct proxy *fe = s->fe;
8010
8011 txn->flags = 0;
8012 txn->status = -1;
8013
Willy Tarreauf64d1412010-10-07 20:06:11 +02008014 txn->cookie_first_date = 0;
8015 txn->cookie_last_date = 0;
8016
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008017 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008018 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008019 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008020 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008021 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008022 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008023 txn->req.chunk_len = 0LL;
8024 txn->req.body_len = 0LL;
8025 txn->rsp.chunk_len = 0LL;
8026 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008027 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8028 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008029 txn->req.chn = s->req;
8030 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008031
8032 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008033
8034 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8035 if (fe->options2 & PR_O2_REQBUG_OK)
8036 txn->req.err_pos = -1; /* let buggy requests pass */
8037
Willy Tarreau46023632010-01-07 22:51:47 +01008038 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008039 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8040
Willy Tarreau46023632010-01-07 22:51:47 +01008041 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008042 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8043
8044 if (txn->hdr_idx.v)
8045 hdr_idx_init(&txn->hdr_idx);
8046}
8047
8048/* to be used at the end of a transaction */
8049void http_end_txn(struct session *s)
8050{
8051 struct http_txn *txn = &s->txn;
8052
8053 /* these ones will have been dynamically allocated */
8054 pool_free2(pool2_requri, txn->uri);
8055 pool_free2(pool2_capture, txn->cli_cookie);
8056 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008057 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008058 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008059
William Lallemanda73203e2012-03-12 12:48:57 +01008060 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008061 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008062 txn->uri = NULL;
8063 txn->srv_cookie = NULL;
8064 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008065
8066 if (txn->req.cap) {
8067 struct cap_hdr *h;
8068 for (h = s->fe->req_cap; h; h = h->next)
8069 pool_free2(h->pool, txn->req.cap[h->index]);
8070 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8071 }
8072
8073 if (txn->rsp.cap) {
8074 struct cap_hdr *h;
8075 for (h = s->fe->rsp_cap; h; h = h->next)
8076 pool_free2(h->pool, txn->rsp.cap[h->index]);
8077 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8078 }
8079
Willy Tarreau0937bc42009-12-22 15:03:09 +01008080}
8081
8082/* to be used at the end of a transaction to prepare a new one */
8083void http_reset_txn(struct session *s)
8084{
8085 http_end_txn(s);
8086 http_init_txn(s);
8087
8088 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008089 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008090 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008091 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008092 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008093 /* re-init store persistence */
8094 s->store_count = 0;
8095
Willy Tarreau0937bc42009-12-22 15:03:09 +01008096 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008097
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008098 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008099
Willy Tarreau739cfba2010-01-25 23:11:14 +01008100 /* We must trim any excess data from the response buffer, because we
8101 * may have blocked an invalid response from a server that we don't
8102 * want to accidentely forward once we disable the analysers, nor do
8103 * we want those data to come along with next response. A typical
8104 * example of such data would be from a buggy server responding to
8105 * a HEAD with some data, or sending more than the advertised
8106 * content-length.
8107 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008108 if (unlikely(s->rep->buf->i))
8109 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008110
Willy Tarreau0937bc42009-12-22 15:03:09 +01008111 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008112 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008113
Willy Tarreaud04e8582010-05-31 12:31:35 +02008114 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008115 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008116
8117 s->req->rex = TICK_ETERNITY;
8118 s->req->wex = TICK_ETERNITY;
8119 s->req->analyse_exp = TICK_ETERNITY;
8120 s->rep->rex = TICK_ETERNITY;
8121 s->rep->wex = TICK_ETERNITY;
8122 s->rep->analyse_exp = TICK_ETERNITY;
8123}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008124
Willy Tarreauff011f22011-01-06 17:51:27 +01008125void free_http_req_rules(struct list *r) {
8126 struct http_req_rule *tr, *pr;
8127
8128 list_for_each_entry_safe(pr, tr, r, list) {
8129 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008130 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008131 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008132
8133 free(pr);
8134 }
8135}
8136
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008137/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008138struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8139{
8140 struct http_req_rule *rule;
8141 int cur_arg;
8142
8143 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8144 if (!rule) {
8145 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008146 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008147 }
8148
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008149 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008150 rule->action = HTTP_REQ_ACT_ALLOW;
8151 cur_arg = 1;
8152 } else if (!strcmp(args[0], "deny")) {
8153 rule->action = HTTP_REQ_ACT_DENY;
8154 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008155 } else if (!strcmp(args[0], "tarpit")) {
8156 rule->action = HTTP_REQ_ACT_TARPIT;
8157 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008158 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008159 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008160 cur_arg = 1;
8161
8162 while(*args[cur_arg]) {
8163 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008164 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008165 cur_arg+=2;
8166 continue;
8167 } else
8168 break;
8169 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008170 } else if (!strcmp(args[0], "set-nice")) {
8171 rule->action = HTTP_REQ_ACT_SET_NICE;
8172 cur_arg = 1;
8173
8174 if (!*args[cur_arg] ||
8175 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8176 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8177 file, linenum, args[0]);
8178 goto out_err;
8179 }
8180 rule->arg.nice = atoi(args[cur_arg]);
8181 if (rule->arg.nice < -1024)
8182 rule->arg.nice = -1024;
8183 else if (rule->arg.nice > 1024)
8184 rule->arg.nice = 1024;
8185 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008186 } else if (!strcmp(args[0], "set-tos")) {
8187#ifdef IP_TOS
8188 char *err;
8189 rule->action = HTTP_REQ_ACT_SET_TOS;
8190 cur_arg = 1;
8191
8192 if (!*args[cur_arg] ||
8193 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8194 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8195 file, linenum, args[0]);
8196 goto out_err;
8197 }
8198
8199 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8200 if (err && *err != '\0') {
8201 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8202 file, linenum, err, args[0]);
8203 goto out_err;
8204 }
8205 cur_arg++;
8206#else
8207 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8208 goto out_err;
8209#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008210 } else if (!strcmp(args[0], "set-mark")) {
8211#ifdef SO_MARK
8212 char *err;
8213 rule->action = HTTP_REQ_ACT_SET_MARK;
8214 cur_arg = 1;
8215
8216 if (!*args[cur_arg] ||
8217 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8218 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8219 file, linenum, args[0]);
8220 goto out_err;
8221 }
8222
8223 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8224 if (err && *err != '\0') {
8225 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8226 file, linenum, err, args[0]);
8227 goto out_err;
8228 }
8229 cur_arg++;
8230 global.last_checks |= LSTCHK_NETADM;
8231#else
8232 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8233 goto out_err;
8234#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008235 } else if (!strcmp(args[0], "set-log-level")) {
8236 rule->action = HTTP_REQ_ACT_SET_LOGL;
8237 cur_arg = 1;
8238
8239 if (!*args[cur_arg] ||
8240 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8241 bad_log_level:
8242 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8243 file, linenum, args[0]);
8244 goto out_err;
8245 }
8246 if (strcmp(args[cur_arg], "silent") == 0)
8247 rule->arg.loglevel = -1;
8248 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8249 goto bad_log_level;
8250 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008251 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8252 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8253 cur_arg = 1;
8254
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008255 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8256 (*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 +01008257 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8258 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008259 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008260 }
8261
8262 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8263 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8264 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008265
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008266 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008267 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8268 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008269 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008270 } else if (strcmp(args[0], "redirect") == 0) {
8271 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008272 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008273
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008274 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008275 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8276 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8277 goto out_err;
8278 }
8279
8280 /* this redirect rule might already contain a parsed condition which
8281 * we'll pass to the http-request rule.
8282 */
8283 rule->action = HTTP_REQ_ACT_REDIR;
8284 rule->arg.redir = redir;
8285 rule->cond = redir->cond;
8286 redir->cond = NULL;
8287 cur_arg = 2;
8288 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008289 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008290 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 +01008291 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008292 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008293 }
8294
8295 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8296 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008297 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008298
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008299 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8300 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8301 file, linenum, args[0], errmsg);
8302 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008303 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008304 }
8305 rule->cond = cond;
8306 }
8307 else if (*args[cur_arg]) {
8308 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8309 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8310 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008311 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008312 }
8313
8314 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008315 out_err:
8316 free(rule);
8317 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008318}
8319
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008320/* parse an "http-respose" rule */
8321struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8322{
8323 struct http_res_rule *rule;
8324 int cur_arg;
8325
8326 rule = calloc(1, sizeof(*rule));
8327 if (!rule) {
8328 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8329 goto out_err;
8330 }
8331
8332 if (!strcmp(args[0], "allow")) {
8333 rule->action = HTTP_RES_ACT_ALLOW;
8334 cur_arg = 1;
8335 } else if (!strcmp(args[0], "deny")) {
8336 rule->action = HTTP_RES_ACT_DENY;
8337 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008338 } else if (!strcmp(args[0], "set-nice")) {
8339 rule->action = HTTP_RES_ACT_SET_NICE;
8340 cur_arg = 1;
8341
8342 if (!*args[cur_arg] ||
8343 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8344 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8345 file, linenum, args[0]);
8346 goto out_err;
8347 }
8348 rule->arg.nice = atoi(args[cur_arg]);
8349 if (rule->arg.nice < -1024)
8350 rule->arg.nice = -1024;
8351 else if (rule->arg.nice > 1024)
8352 rule->arg.nice = 1024;
8353 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008354 } else if (!strcmp(args[0], "set-tos")) {
8355#ifdef IP_TOS
8356 char *err;
8357 rule->action = HTTP_RES_ACT_SET_TOS;
8358 cur_arg = 1;
8359
8360 if (!*args[cur_arg] ||
8361 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8362 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8363 file, linenum, args[0]);
8364 goto out_err;
8365 }
8366
8367 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8368 if (err && *err != '\0') {
8369 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8370 file, linenum, err, args[0]);
8371 goto out_err;
8372 }
8373 cur_arg++;
8374#else
8375 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8376 goto out_err;
8377#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008378 } else if (!strcmp(args[0], "set-mark")) {
8379#ifdef SO_MARK
8380 char *err;
8381 rule->action = HTTP_RES_ACT_SET_MARK;
8382 cur_arg = 1;
8383
8384 if (!*args[cur_arg] ||
8385 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8386 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8387 file, linenum, args[0]);
8388 goto out_err;
8389 }
8390
8391 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8392 if (err && *err != '\0') {
8393 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8394 file, linenum, err, args[0]);
8395 goto out_err;
8396 }
8397 cur_arg++;
8398 global.last_checks |= LSTCHK_NETADM;
8399#else
8400 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8401 goto out_err;
8402#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008403 } else if (!strcmp(args[0], "set-log-level")) {
8404 rule->action = HTTP_RES_ACT_SET_LOGL;
8405 cur_arg = 1;
8406
8407 if (!*args[cur_arg] ||
8408 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8409 bad_log_level:
8410 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8411 file, linenum, args[0]);
8412 goto out_err;
8413 }
8414 if (strcmp(args[cur_arg], "silent") == 0)
8415 rule->arg.loglevel = -1;
8416 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8417 goto bad_log_level;
8418 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008419 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8420 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8421 cur_arg = 1;
8422
8423 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8424 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8425 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8426 file, linenum, args[0]);
8427 goto out_err;
8428 }
8429
8430 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8431 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8432 LIST_INIT(&rule->arg.hdr_add.fmt);
8433
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008434 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008435 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8436 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
8437 cur_arg += 2;
8438 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008439 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 +02008440 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8441 goto out_err;
8442 }
8443
8444 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8445 struct acl_cond *cond;
8446 char *errmsg = NULL;
8447
8448 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8449 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8450 file, linenum, args[0], errmsg);
8451 free(errmsg);
8452 goto out_err;
8453 }
8454 rule->cond = cond;
8455 }
8456 else if (*args[cur_arg]) {
8457 Alert("parsing [%s:%d]: 'http-response %s' expects"
8458 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8459 file, linenum, args[0], args[cur_arg]);
8460 goto out_err;
8461 }
8462
8463 return rule;
8464 out_err:
8465 free(rule);
8466 return NULL;
8467}
8468
Willy Tarreau4baae242012-12-27 12:00:31 +01008469/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008470 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8471 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008472 */
8473struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008474 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008475{
8476 struct redirect_rule *rule;
8477 int cur_arg;
8478 int type = REDIRECT_TYPE_NONE;
8479 int code = 302;
8480 const char *destination = NULL;
8481 const char *cookie = NULL;
8482 int cookie_set = 0;
8483 unsigned int flags = REDIRECT_FLAG_NONE;
8484 struct acl_cond *cond = NULL;
8485
8486 cur_arg = 0;
8487 while (*(args[cur_arg])) {
8488 if (strcmp(args[cur_arg], "location") == 0) {
8489 if (!*args[cur_arg + 1])
8490 goto missing_arg;
8491
8492 type = REDIRECT_TYPE_LOCATION;
8493 cur_arg++;
8494 destination = args[cur_arg];
8495 }
8496 else if (strcmp(args[cur_arg], "prefix") == 0) {
8497 if (!*args[cur_arg + 1])
8498 goto missing_arg;
8499
8500 type = REDIRECT_TYPE_PREFIX;
8501 cur_arg++;
8502 destination = args[cur_arg];
8503 }
8504 else if (strcmp(args[cur_arg], "scheme") == 0) {
8505 if (!*args[cur_arg + 1])
8506 goto missing_arg;
8507
8508 type = REDIRECT_TYPE_SCHEME;
8509 cur_arg++;
8510 destination = args[cur_arg];
8511 }
8512 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8513 if (!*args[cur_arg + 1])
8514 goto missing_arg;
8515
8516 cur_arg++;
8517 cookie = args[cur_arg];
8518 cookie_set = 1;
8519 }
8520 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8521 if (!*args[cur_arg + 1])
8522 goto missing_arg;
8523
8524 cur_arg++;
8525 cookie = args[cur_arg];
8526 cookie_set = 0;
8527 }
8528 else if (strcmp(args[cur_arg], "code") == 0) {
8529 if (!*args[cur_arg + 1])
8530 goto missing_arg;
8531
8532 cur_arg++;
8533 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008534 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008535 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008536 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008537 args[cur_arg - 1], args[cur_arg]);
8538 return NULL;
8539 }
8540 }
8541 else if (!strcmp(args[cur_arg],"drop-query")) {
8542 flags |= REDIRECT_FLAG_DROP_QS;
8543 }
8544 else if (!strcmp(args[cur_arg],"append-slash")) {
8545 flags |= REDIRECT_FLAG_APPEND_SLASH;
8546 }
8547 else if (strcmp(args[cur_arg], "if") == 0 ||
8548 strcmp(args[cur_arg], "unless") == 0) {
8549 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8550 if (!cond) {
8551 memprintf(errmsg, "error in condition: %s", *errmsg);
8552 return NULL;
8553 }
8554 break;
8555 }
8556 else {
8557 memprintf(errmsg,
8558 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8559 args[cur_arg]);
8560 return NULL;
8561 }
8562 cur_arg++;
8563 }
8564
8565 if (type == REDIRECT_TYPE_NONE) {
8566 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8567 return NULL;
8568 }
8569
8570 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8571 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008572 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008573
8574 if (!use_fmt) {
8575 /* old-style static redirect rule */
8576 rule->rdr_str = strdup(destination);
8577 rule->rdr_len = strlen(destination);
8578 }
8579 else {
8580 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008581
8582 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8583 * if prefix == "/", we don't want to add anything, otherwise it
8584 * makes it hard for the user to configure a self-redirection.
8585 */
8586 proxy->conf.args.ctx = ARGC_RDR;
8587 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8588 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8589 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
8590 }
8591 }
8592
Willy Tarreau4baae242012-12-27 12:00:31 +01008593 if (cookie) {
8594 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8595 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8596 */
8597 rule->cookie_len = strlen(cookie);
8598 if (cookie_set) {
8599 rule->cookie_str = malloc(rule->cookie_len + 10);
8600 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8601 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8602 rule->cookie_len += 9;
8603 } else {
8604 rule->cookie_str = malloc(rule->cookie_len + 21);
8605 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8606 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8607 rule->cookie_len += 20;
8608 }
8609 }
8610 rule->type = type;
8611 rule->code = code;
8612 rule->flags = flags;
8613 LIST_INIT(&rule->list);
8614 return rule;
8615
8616 missing_arg:
8617 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8618 return NULL;
8619}
8620
Willy Tarreau8797c062007-05-07 00:55:35 +02008621/************************************************************************/
8622/* The code below is dedicated to ACL parsing and matching */
8623/************************************************************************/
8624
8625
Willy Tarreau14174bc2012-04-16 14:34:04 +02008626/* This function ensures that the prerequisites for an L7 fetch are ready,
8627 * which means that a request or response is ready. If some data is missing,
8628 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008629 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8630 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008631 *
8632 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008633 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8634 * decide whether or not an HTTP message is present ;
8635 * 0 if the requested data cannot be fetched or if it is certain that
8636 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008637 * 1 if an HTTP message is ready
8638 */
8639static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008640smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008641 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008642{
8643 struct http_txn *txn = l7;
8644 struct http_msg *msg = &txn->req;
8645
8646 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8647 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8648 */
8649
8650 if (unlikely(!s || !txn))
8651 return 0;
8652
8653 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008654 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008655
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008656 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008657 if (unlikely(!s->req))
8658 return 0;
8659
Willy Tarreauaae75e32013-03-29 12:31:49 +01008660 /* If the buffer does not leave enough free space at the end,
8661 * we must first realign it.
8662 */
8663 if (s->req->buf->p > s->req->buf->data &&
8664 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8665 buffer_slow_realign(s->req->buf);
8666
Willy Tarreau14174bc2012-04-16 14:34:04 +02008667 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008668 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008669 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008670
8671 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008672 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008673 http_msg_analyzer(msg, &txn->hdr_idx);
8674
8675 /* Still no valid request ? */
8676 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008677 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008678 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008679 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008680 }
8681 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008682 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008683 return 0;
8684 }
8685
8686 /* OK we just got a valid HTTP request. We have some minor
8687 * preparation to perform so that further checks can rely
8688 * on HTTP tests.
8689 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008690
8691 /* If the request was parsed but was too large, we must absolutely
8692 * return an error so that it is not processed. At the moment this
8693 * cannot happen, but if the parsers are to change in the future,
8694 * we want this check to be maintained.
8695 */
8696 if (unlikely(s->req->buf->i + s->req->buf->p >
8697 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8698 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008699 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008700 return 1;
8701 }
8702
Willy Tarreau9b28e032012-10-12 23:49:43 +02008703 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008704 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8705 s->flags |= SN_REDIRECTABLE;
8706
Willy Tarreau506d0502013-07-06 13:29:24 +02008707 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8708 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008709 }
8710
Willy Tarreau506d0502013-07-06 13:29:24 +02008711 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008712 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008713 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008714
8715 /* otherwise everything's ready for the request */
8716 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008717 else {
8718 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008719 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8720 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008721 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008722 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008723 }
8724
8725 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008726 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008727 return 1;
8728}
Willy Tarreau8797c062007-05-07 00:55:35 +02008729
Willy Tarreauc0239e02012-04-16 14:42:55 +02008730#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008731 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 +02008732
Willy Tarreau24e32d82012-04-23 23:55:44 +02008733#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008734 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 +02008735
Willy Tarreau8797c062007-05-07 00:55:35 +02008736
8737/* 1. Check on METHOD
8738 * We use the pre-parsed method if it is known, and store its number as an
8739 * integer. If it is unknown, we use the pointer and the length.
8740 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008741static 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 +02008742{
8743 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008744 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008745
Willy Tarreauae8b7962007-06-09 23:10:04 +02008746 len = strlen(*text);
8747 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008748
8749 pattern->val.i = meth;
8750 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008751 if (usage == PAT_U_COMPILE) {
8752 pattern->ptr.str = strdup(*text);
8753 if (!pattern->ptr.str) {
8754 memprintf(err, "out of memory while loading pattern");
8755 return 0;
8756 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008757 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008758 else {
8759 trash = get_trash_chunk();
8760 if (trash->size < len) {
8761 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8762 len, trash->size);
8763 return 0;
8764 }
8765 pattern->ptr.str = trash->str;
8766 }
8767 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008768 pattern->len = len;
8769 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008770 else
8771 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008772 return 1;
8773}
8774
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008775/* This function fetches the method of current HTTP request and stores
8776 * it in the global pattern struct as a chunk. There are two possibilities :
8777 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8778 * in <len> and <ptr> is NULL ;
8779 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8780 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008781 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008782 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008783static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008784smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008785 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008786{
8787 int meth;
8788 struct http_txn *txn = l7;
8789
Willy Tarreau24e32d82012-04-23 23:55:44 +02008790 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008791
Willy Tarreau8797c062007-05-07 00:55:35 +02008792 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008793 smp->type = SMP_T_UINT;
8794 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008795 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008796 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8797 /* ensure the indexes are not affected */
8798 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008799 smp->type = SMP_T_CSTR;
8800 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008801 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008802 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008803 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008804 return 1;
8805}
8806
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008807/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008808static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008809{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008810 int icase;
8811
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008812
Willy Tarreauf853c462012-04-23 18:53:56 +02008813 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008814 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008815 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008816 return PAT_MATCH;
8817 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008818 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008819
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008820 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8821 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008822 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008823
8824 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008825 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008826 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008827
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008828 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008829 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8830 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008831 return PAT_NOMATCH;
8832 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02008833}
8834
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008835static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008836smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008837 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008838{
8839 struct http_txn *txn = l7;
8840 char *ptr;
8841 int len;
8842
Willy Tarreauc0239e02012-04-16 14:42:55 +02008843 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008844
Willy Tarreau8797c062007-05-07 00:55:35 +02008845 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008846 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008847
8848 while ((len-- > 0) && (*ptr++ != '/'));
8849 if (len <= 0)
8850 return 0;
8851
Willy Tarreauf853c462012-04-23 18:53:56 +02008852 smp->type = SMP_T_CSTR;
8853 smp->data.str.str = ptr;
8854 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008855
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008856 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008857 return 1;
8858}
8859
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008860static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008861smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008862 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008863{
8864 struct http_txn *txn = l7;
8865 char *ptr;
8866 int len;
8867
Willy Tarreauc0239e02012-04-16 14:42:55 +02008868 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008869
Willy Tarreauf26b2522012-12-14 08:33:14 +01008870 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8871 return 0;
8872
Willy Tarreau8797c062007-05-07 00:55:35 +02008873 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008874 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008875
8876 while ((len-- > 0) && (*ptr++ != '/'));
8877 if (len <= 0)
8878 return 0;
8879
Willy Tarreauf853c462012-04-23 18:53:56 +02008880 smp->type = SMP_T_CSTR;
8881 smp->data.str.str = ptr;
8882 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008883
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008884 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008885 return 1;
8886}
8887
8888/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008889static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008890smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008891 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008892{
8893 struct http_txn *txn = l7;
8894 char *ptr;
8895 int len;
8896
Willy Tarreauc0239e02012-04-16 14:42:55 +02008897 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008898
Willy Tarreauf26b2522012-12-14 08:33:14 +01008899 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8900 return 0;
8901
Willy Tarreau8797c062007-05-07 00:55:35 +02008902 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008903 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008904
Willy Tarreauf853c462012-04-23 18:53:56 +02008905 smp->type = SMP_T_UINT;
8906 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008907 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008908 return 1;
8909}
8910
8911/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008912static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008913smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008914 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008915{
8916 struct http_txn *txn = l7;
8917
Willy Tarreauc0239e02012-04-16 14:42:55 +02008918 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008919
Willy Tarreauf853c462012-04-23 18:53:56 +02008920 smp->type = SMP_T_CSTR;
8921 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008922 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008923 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008924 return 1;
8925}
8926
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008927static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008928smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008929 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008930{
8931 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008932 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008933
Willy Tarreauc0239e02012-04-16 14:42:55 +02008934 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008935
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008936 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8937 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008938 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008939
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008940 smp->type = SMP_T_IPV4;
8941 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02008942 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008943 return 1;
8944}
8945
8946static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008947smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008948 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008949{
8950 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008951 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008952
Willy Tarreauc0239e02012-04-16 14:42:55 +02008953 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008954
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008955 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
8956 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
8957 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008958
Willy Tarreau4c804ec2013-09-30 14:37:14 +02008959 smp->type = SMP_T_UINT;
8960 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008961 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008962 return 1;
8963}
8964
Willy Tarreau185b5c42012-04-26 15:11:51 +02008965/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8966 * Accepts an optional argument of type string containing the header field name,
8967 * and an optional argument of type signed or unsigned integer to request an
8968 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008969 * headers are considered from the first one. It does not stop on commas and
8970 * returns full lines instead (useful for User-Agent or Date for example).
8971 */
8972static int
8973smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008974 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008975{
8976 struct http_txn *txn = l7;
8977 struct hdr_idx *idx = &txn->hdr_idx;
8978 struct hdr_ctx *ctx = smp->ctx.a[0];
8979 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
8980 int occ = 0;
8981 const char *name_str = NULL;
8982 int name_len = 0;
8983
8984 if (!ctx) {
8985 /* first call */
8986 ctx = &static_hdr_ctx;
8987 ctx->idx = 0;
8988 smp->ctx.a[0] = ctx;
8989 }
8990
8991 if (args) {
8992 if (args[0].type != ARGT_STR)
8993 return 0;
8994 name_str = args[0].data.str.str;
8995 name_len = args[0].data.str.len;
8996
8997 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8998 occ = args[1].data.uint;
8999 }
9000
9001 CHECK_HTTP_MESSAGE_FIRST();
9002
9003 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9004 /* search for header from the beginning */
9005 ctx->idx = 0;
9006
9007 if (!occ && !(opt & SMP_OPT_ITERATE))
9008 /* no explicit occurrence and single fetch => last header by default */
9009 occ = -1;
9010
9011 if (!occ)
9012 /* prepare to report multiple occurrences for ACL fetches */
9013 smp->flags |= SMP_F_NOT_LAST;
9014
9015 smp->type = SMP_T_CSTR;
9016 smp->flags |= SMP_F_VOL_HDR;
9017 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9018 return 1;
9019
9020 smp->flags &= ~SMP_F_NOT_LAST;
9021 return 0;
9022}
9023
9024/* 6. Check on HTTP header count. The number of occurrences is returned.
9025 * Accepts exactly 1 argument of type string. It does not stop on commas and
9026 * returns full lines instead (useful for User-Agent or Date for example).
9027 */
9028static int
9029smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009030 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009031{
9032 struct http_txn *txn = l7;
9033 struct hdr_idx *idx = &txn->hdr_idx;
9034 struct hdr_ctx ctx;
9035 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9036 int cnt;
9037
9038 if (!args || args->type != ARGT_STR)
9039 return 0;
9040
9041 CHECK_HTTP_MESSAGE_FIRST();
9042
9043 ctx.idx = 0;
9044 cnt = 0;
9045 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9046 cnt++;
9047
9048 smp->type = SMP_T_UINT;
9049 smp->data.uint = cnt;
9050 smp->flags = SMP_F_VOL_HDR;
9051 return 1;
9052}
9053
9054/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9055 * Accepts an optional argument of type string containing the header field name,
9056 * and an optional argument of type signed or unsigned integer to request an
9057 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009058 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009059 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009060static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009061smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009062 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009063{
9064 struct http_txn *txn = l7;
9065 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009066 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009067 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009068 int occ = 0;
9069 const char *name_str = NULL;
9070 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009071
Willy Tarreaua890d072013-04-02 12:01:06 +02009072 if (!ctx) {
9073 /* first call */
9074 ctx = &static_hdr_ctx;
9075 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009076 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009077 }
9078
Willy Tarreau185b5c42012-04-26 15:11:51 +02009079 if (args) {
9080 if (args[0].type != ARGT_STR)
9081 return 0;
9082 name_str = args[0].data.str.str;
9083 name_len = args[0].data.str.len;
9084
9085 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9086 occ = args[1].data.uint;
9087 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009088
Willy Tarreaue333ec92012-04-16 16:26:40 +02009089 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009090
Willy Tarreau185b5c42012-04-26 15:11:51 +02009091 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009092 /* search for header from the beginning */
9093 ctx->idx = 0;
9094
Willy Tarreau185b5c42012-04-26 15:11:51 +02009095 if (!occ && !(opt & SMP_OPT_ITERATE))
9096 /* no explicit occurrence and single fetch => last header by default */
9097 occ = -1;
9098
9099 if (!occ)
9100 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009101 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009102
Willy Tarreau185b5c42012-04-26 15:11:51 +02009103 smp->type = SMP_T_CSTR;
9104 smp->flags |= SMP_F_VOL_HDR;
9105 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 +02009106 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009107
Willy Tarreau37406352012-04-23 16:16:37 +02009108 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009109 return 0;
9110}
9111
Willy Tarreauc11416f2007-06-17 16:58:38 +02009112/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009113 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009114 */
9115static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009116smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009117 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009118{
9119 struct http_txn *txn = l7;
9120 struct hdr_idx *idx = &txn->hdr_idx;
9121 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009122 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009123 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009124
Willy Tarreau24e32d82012-04-23 23:55:44 +02009125 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009126 return 0;
9127
Willy Tarreaue333ec92012-04-16 16:26:40 +02009128 CHECK_HTTP_MESSAGE_FIRST();
9129
Willy Tarreau33a7e692007-06-10 19:45:56 +02009130 ctx.idx = 0;
9131 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009132 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 +02009133 cnt++;
9134
Willy Tarreauf853c462012-04-23 18:53:56 +02009135 smp->type = SMP_T_UINT;
9136 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009137 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009138 return 1;
9139}
9140
Willy Tarreau185b5c42012-04-26 15:11:51 +02009141/* Fetch an HTTP header's integer value. The integer value is returned. It
9142 * takes a mandatory argument of type string and an optional one of type int
9143 * to designate a specific occurrence. It returns an unsigned integer, which
9144 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009145 */
9146static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009147smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009148 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009149{
Willy Tarreauef38c392013-07-22 16:29:32 +02009150 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009151
Willy Tarreauf853c462012-04-23 18:53:56 +02009152 if (ret > 0) {
9153 smp->type = SMP_T_UINT;
9154 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9155 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009156
Willy Tarreaud53e2422012-04-16 17:21:11 +02009157 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009158}
9159
Cyril Bonté69fa9922012-10-25 00:01:06 +02009160/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9161 * and an optional one of type int to designate a specific occurrence.
9162 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009163 */
9164static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009165smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009166 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009167{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009168 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009169
Willy Tarreauef38c392013-07-22 16:29:32 +02009170 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009171 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9172 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009173 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009174 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009175 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009176 if (smp->data.str.len < temp->size - 1) {
9177 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9178 temp->str[smp->data.str.len] = '\0';
9179 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9180 smp->type = SMP_T_IPV6;
9181 break;
9182 }
9183 }
9184 }
9185
Willy Tarreaud53e2422012-04-16 17:21:11 +02009186 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009187 if (!(smp->flags & SMP_F_NOT_LAST))
9188 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009189 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009190 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009191}
9192
Willy Tarreau737b0c12007-06-10 21:28:46 +02009193/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9194 * the first '/' after the possible hostname, and ends before the possible '?'.
9195 */
9196static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009197smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009198 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009199{
9200 struct http_txn *txn = l7;
9201 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009202
Willy Tarreauc0239e02012-04-16 14:42:55 +02009203 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009204
Willy Tarreau9b28e032012-10-12 23:49:43 +02009205 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009206 ptr = http_get_path(txn);
9207 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009208 return 0;
9209
9210 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009211 smp->type = SMP_T_CSTR;
9212 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009213
9214 while (ptr < end && *ptr != '?')
9215 ptr++;
9216
Willy Tarreauf853c462012-04-23 18:53:56 +02009217 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009218 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009219 return 1;
9220}
9221
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009222/* This produces a concatenation of the first occurrence of the Host header
9223 * followed by the path component if it begins with a slash ('/'). This means
9224 * that '*' will not be added, resulting in exactly the first Host entry.
9225 * If no Host header is found, then the path is returned as-is. The returned
9226 * value is stored in the trash so it does not need to be marked constant.
9227 */
9228static int
9229smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009230 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009231{
9232 struct http_txn *txn = l7;
9233 char *ptr, *end, *beg;
9234 struct hdr_ctx ctx;
9235
9236 CHECK_HTTP_MESSAGE_FIRST();
9237
9238 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009239 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 +02009240 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009241 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009242
9243 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009244 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009245 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009246 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009247 smp->data.str.len = ctx.vlen;
9248
9249 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009250 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 +02009251 beg = http_get_path(txn);
9252 if (!beg)
9253 beg = end;
9254
9255 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9256
9257 if (beg < ptr && *beg == '/') {
9258 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9259 smp->data.str.len += ptr - beg;
9260 }
9261
9262 smp->flags = SMP_F_VOL_1ST;
9263 return 1;
9264}
9265
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009266/* This produces a 32-bit hash of the concatenation of the first occurrence of
9267 * the Host header followed by the path component if it begins with a slash ('/').
9268 * This means that '*' will not be added, resulting in exactly the first Host
9269 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009270 * is hashed using the path hash followed by a full avalanche hash and provides a
9271 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009272 * high-traffic sites without having to store whole paths.
9273 */
9274static int
9275smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009276 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009277{
9278 struct http_txn *txn = l7;
9279 struct hdr_ctx ctx;
9280 unsigned int hash = 0;
9281 char *ptr, *beg, *end;
9282 int len;
9283
9284 CHECK_HTTP_MESSAGE_FIRST();
9285
9286 ctx.idx = 0;
9287 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9288 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9289 ptr = ctx.line + ctx.val;
9290 len = ctx.vlen;
9291 while (len--)
9292 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9293 }
9294
9295 /* now retrieve the path */
9296 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9297 beg = http_get_path(txn);
9298 if (!beg)
9299 beg = end;
9300
9301 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9302
9303 if (beg < ptr && *beg == '/') {
9304 while (beg < ptr)
9305 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9306 }
9307 hash = full_hash(hash);
9308
9309 smp->type = SMP_T_UINT;
9310 smp->data.uint = hash;
9311 smp->flags = SMP_F_VOL_1ST;
9312 return 1;
9313}
9314
Willy Tarreau4a550602012-12-09 14:53:32 +01009315/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009316 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9317 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9318 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009319 * that in environments where IPv6 is insignificant, truncating the output to
9320 * 8 bytes would still work.
9321 */
9322static int
9323smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009324 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009325{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009326 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009327 struct connection *cli_conn = objt_conn(l4->si[0].end);
9328
9329 if (!cli_conn)
9330 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009331
Willy Tarreauef38c392013-07-22 16:29:32 +02009332 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009333 return 0;
9334
Willy Tarreau47ca5452012-12-23 20:22:19 +01009335 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009336 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9337 temp->len += sizeof(smp->data.uint);
9338
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009339 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009340 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009341 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009342 temp->len += 4;
9343 break;
9344 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009345 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009346 temp->len += 16;
9347 break;
9348 default:
9349 return 0;
9350 }
9351
9352 smp->data.str = *temp;
9353 smp->type = SMP_T_BIN;
9354 return 1;
9355}
9356
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009357static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009358smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009359 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009360{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009361 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9362 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9363 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009364
Willy Tarreau24e32d82012-04-23 23:55:44 +02009365 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009366
Willy Tarreauf853c462012-04-23 18:53:56 +02009367 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009368 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009369 return 1;
9370}
9371
Willy Tarreau7f18e522010-10-22 20:04:13 +02009372/* return a valid test if the current request is the first one on the connection */
9373static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009374smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009375 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009376{
9377 if (!s)
9378 return 0;
9379
Willy Tarreauf853c462012-04-23 18:53:56 +02009380 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009381 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009382 return 1;
9383}
9384
Willy Tarreau34db1082012-04-19 17:16:54 +02009385/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009386static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009387smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009388 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009389{
9390
Willy Tarreau24e32d82012-04-23 23:55:44 +02009391 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009392 return 0;
9393
Willy Tarreauc0239e02012-04-16 14:42:55 +02009394 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009395
Willy Tarreauc0239e02012-04-16 14:42:55 +02009396 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009397 return 0;
9398
Willy Tarreauf853c462012-04-23 18:53:56 +02009399 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009400 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 +01009401 return 1;
9402}
Willy Tarreau8797c062007-05-07 00:55:35 +02009403
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009404/* Accepts exactly 1 argument of type userlist */
9405static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009406smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009407 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009408{
9409
9410 if (!args || args->type != ARGT_USR)
9411 return 0;
9412
9413 CHECK_HTTP_MESSAGE_FIRST();
9414
9415 if (!get_http_auth(l4))
9416 return 0;
9417
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009418 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009419 smp->ctx.a[0] = args->data.usr; /* user list */
9420 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9421 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9422
9423 /* if the user does not belong to the userlist or has a wrong password,
9424 * report that it unconditionally does not match. Otherwise we return
9425 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009426 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009427 */
9428 smp->type = SMP_T_BOOL;
9429 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9430 if (smp->data.uint)
9431 smp->type = SMP_T_UINT;
9432
9433 return 1;
9434}
9435
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009436/* Try to find the next occurrence of a cookie name in a cookie header value.
9437 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9438 * the cookie value is returned into *value and *value_l, and the function
9439 * returns a pointer to the next pointer to search from if the value was found.
9440 * Otherwise if the cookie was not found, NULL is returned and neither value
9441 * nor value_l are touched. The input <hdr> string should first point to the
9442 * header's value, and the <hdr_end> pointer must point to the first character
9443 * not part of the value. <list> must be non-zero if value may represent a list
9444 * of values (cookie headers). This makes it faster to abort parsing when no
9445 * list is expected.
9446 */
9447static char *
9448extract_cookie_value(char *hdr, const char *hdr_end,
9449 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009450 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009451{
9452 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9453 char *next;
9454
9455 /* we search at least a cookie name followed by an equal, and more
9456 * generally something like this :
9457 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9458 */
9459 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9460 /* Iterate through all cookies on this line */
9461
9462 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9463 att_beg++;
9464
9465 /* find att_end : this is the first character after the last non
9466 * space before the equal. It may be equal to hdr_end.
9467 */
9468 equal = att_end = att_beg;
9469
9470 while (equal < hdr_end) {
9471 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9472 break;
9473 if (http_is_spht[(unsigned char)*equal++])
9474 continue;
9475 att_end = equal;
9476 }
9477
9478 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9479 * is between <att_beg> and <equal>, both may be identical.
9480 */
9481
9482 /* look for end of cookie if there is an equal sign */
9483 if (equal < hdr_end && *equal == '=') {
9484 /* look for the beginning of the value */
9485 val_beg = equal + 1;
9486 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9487 val_beg++;
9488
9489 /* find the end of the value, respecting quotes */
9490 next = find_cookie_value_end(val_beg, hdr_end);
9491
9492 /* make val_end point to the first white space or delimitor after the value */
9493 val_end = next;
9494 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9495 val_end--;
9496 } else {
9497 val_beg = val_end = next = equal;
9498 }
9499
9500 /* We have nothing to do with attributes beginning with '$'. However,
9501 * they will automatically be removed if a header before them is removed,
9502 * since they're supposed to be linked together.
9503 */
9504 if (*att_beg == '$')
9505 continue;
9506
9507 /* Ignore cookies with no equal sign */
9508 if (equal == next)
9509 continue;
9510
9511 /* Now we have the cookie name between att_beg and att_end, and
9512 * its value between val_beg and val_end.
9513 */
9514
9515 if (att_end - att_beg == cookie_name_l &&
9516 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9517 /* let's return this value and indicate where to go on from */
9518 *value = val_beg;
9519 *value_l = val_end - val_beg;
9520 return next + 1;
9521 }
9522
9523 /* Set-Cookie headers only have the name in the first attr=value part */
9524 if (!list)
9525 break;
9526 }
9527
9528 return NULL;
9529}
9530
Willy Tarreaue333ec92012-04-16 16:26:40 +02009531/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009532 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009533 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009534 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009535 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009536 * Accepts exactly 1 argument of type string. If the input options indicate
9537 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009538 */
9539static int
Willy Tarreau51539362012-05-08 12:46:28 +02009540smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009541 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009542{
9543 struct http_txn *txn = l7;
9544 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009545 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009546 const struct http_msg *msg;
9547 const char *hdr_name;
9548 int hdr_name_len;
9549 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009550 int occ = 0;
9551 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009552
Willy Tarreau24e32d82012-04-23 23:55:44 +02009553 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009554 return 0;
9555
Willy Tarreaua890d072013-04-02 12:01:06 +02009556 if (!ctx) {
9557 /* first call */
9558 ctx = &static_hdr_ctx;
9559 ctx->idx = 0;
9560 smp->ctx.a[2] = ctx;
9561 }
9562
Willy Tarreaue333ec92012-04-16 16:26:40 +02009563 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009564
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009565 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009566 msg = &txn->req;
9567 hdr_name = "Cookie";
9568 hdr_name_len = 6;
9569 } else {
9570 msg = &txn->rsp;
9571 hdr_name = "Set-Cookie";
9572 hdr_name_len = 10;
9573 }
9574
Willy Tarreau28376d62012-04-26 21:26:10 +02009575 if (!occ && !(opt & SMP_OPT_ITERATE))
9576 /* no explicit occurrence and single fetch => last cookie by default */
9577 occ = -1;
9578
9579 /* OK so basically here, either we want only one value and it's the
9580 * last one, or we want to iterate over all of them and we fetch the
9581 * next one.
9582 */
9583
Willy Tarreau9b28e032012-10-12 23:49:43 +02009584 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009585 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009586 /* search for the header from the beginning, we must first initialize
9587 * the search parameters.
9588 */
Willy Tarreau37406352012-04-23 16:16:37 +02009589 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009590 ctx->idx = 0;
9591 }
9592
Willy Tarreau28376d62012-04-26 21:26:10 +02009593 smp->flags |= SMP_F_VOL_HDR;
9594
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009595 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009596 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9597 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009598 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9599 goto out;
9600
Willy Tarreau24e32d82012-04-23 23:55:44 +02009601 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009602 continue;
9603
Willy Tarreau37406352012-04-23 16:16:37 +02009604 smp->ctx.a[0] = ctx->line + ctx->val;
9605 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009606 }
9607
Willy Tarreauf853c462012-04-23 18:53:56 +02009608 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009609 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009610 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009611 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009612 &smp->data.str.str,
9613 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009614 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009615 found = 1;
9616 if (occ >= 0) {
9617 /* one value was returned into smp->data.str.{str,len} */
9618 smp->flags |= SMP_F_NOT_LAST;
9619 return 1;
9620 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009621 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009622 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009623 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009624 /* all cookie headers and values were scanned. If we're looking for the
9625 * last occurrence, we may return it now.
9626 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009627 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009628 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009629 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009630}
9631
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009632/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009633 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009634 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009635 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009636 */
9637static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009638smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009639 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009640{
9641 struct http_txn *txn = l7;
9642 struct hdr_idx *idx = &txn->hdr_idx;
9643 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009644 const struct http_msg *msg;
9645 const char *hdr_name;
9646 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009647 int cnt;
9648 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009649 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009650
Willy Tarreau24e32d82012-04-23 23:55:44 +02009651 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009652 return 0;
9653
Willy Tarreaue333ec92012-04-16 16:26:40 +02009654 CHECK_HTTP_MESSAGE_FIRST();
9655
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009656 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009657 msg = &txn->req;
9658 hdr_name = "Cookie";
9659 hdr_name_len = 6;
9660 } else {
9661 msg = &txn->rsp;
9662 hdr_name = "Set-Cookie";
9663 hdr_name_len = 10;
9664 }
9665
Willy Tarreau9b28e032012-10-12 23:49:43 +02009666 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009667 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009668 ctx.idx = 0;
9669 cnt = 0;
9670
9671 while (1) {
9672 /* Note: val_beg == NULL every time we need to fetch a new header */
9673 if (!val_beg) {
9674 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9675 break;
9676
Willy Tarreau24e32d82012-04-23 23:55:44 +02009677 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009678 continue;
9679
9680 val_beg = ctx.line + ctx.val;
9681 val_end = val_beg + ctx.vlen;
9682 }
9683
Willy Tarreauf853c462012-04-23 18:53:56 +02009684 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009685 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009686 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009687 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009688 &smp->data.str.str,
9689 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009690 cnt++;
9691 }
9692 }
9693
Willy Tarreauf853c462012-04-23 18:53:56 +02009694 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009695 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009696 return 1;
9697}
9698
Willy Tarreau51539362012-05-08 12:46:28 +02009699/* Fetch an cookie's integer value. The integer value is returned. It
9700 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9701 */
9702static int
9703smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009704 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +02009705{
Willy Tarreauef38c392013-07-22 16:29:32 +02009706 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +02009707
9708 if (ret > 0) {
9709 smp->type = SMP_T_UINT;
9710 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9711 }
9712
9713 return ret;
9714}
9715
Willy Tarreau8797c062007-05-07 00:55:35 +02009716/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009717/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009718/************************************************************************/
9719
David Cournapeau16023ee2010-12-23 20:55:41 +09009720/*
9721 * Given a path string and its length, find the position of beginning of the
9722 * query string. Returns NULL if no query string is found in the path.
9723 *
9724 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9725 *
9726 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9727 */
bedis4c75cca2012-10-05 08:38:24 +02009728static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009729{
9730 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009731
bedis4c75cca2012-10-05 08:38:24 +02009732 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009733 return p ? p + 1 : NULL;
9734}
9735
bedis4c75cca2012-10-05 08:38:24 +02009736static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009737{
bedis4c75cca2012-10-05 08:38:24 +02009738 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009739}
9740
9741/*
9742 * Given a url parameter, find the starting position of the first occurence,
9743 * or NULL if the parameter is not found.
9744 *
9745 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9746 * the function will return query_string+8.
9747 */
9748static char*
9749find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009750 char* url_param_name, size_t url_param_name_l,
9751 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009752{
9753 char *pos, *last;
9754
9755 pos = query_string;
9756 last = query_string + query_string_l - url_param_name_l - 1;
9757
9758 while (pos <= last) {
9759 if (pos[url_param_name_l] == '=') {
9760 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9761 return pos;
9762 pos += url_param_name_l + 1;
9763 }
bedis4c75cca2012-10-05 08:38:24 +02009764 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009765 pos++;
9766 pos++;
9767 }
9768 return NULL;
9769}
9770
9771/*
9772 * Given a url parameter name, returns its value and size into *value and
9773 * *value_l respectively, and returns non-zero. If the parameter is not found,
9774 * zero is returned and value/value_l are not touched.
9775 */
9776static int
9777find_url_param_value(char* path, size_t path_l,
9778 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009779 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009780{
9781 char *query_string, *qs_end;
9782 char *arg_start;
9783 char *value_start, *value_end;
9784
bedis4c75cca2012-10-05 08:38:24 +02009785 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009786 if (!query_string)
9787 return 0;
9788
9789 qs_end = path + path_l;
9790 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009791 url_param_name, url_param_name_l,
9792 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009793 if (!arg_start)
9794 return 0;
9795
9796 value_start = arg_start + url_param_name_l + 1;
9797 value_end = value_start;
9798
bedis4c75cca2012-10-05 08:38:24 +02009799 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009800 value_end++;
9801
9802 *value = value_start;
9803 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009804 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009805}
9806
9807static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009808smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009809 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +09009810{
bedis4c75cca2012-10-05 08:38:24 +02009811 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009812 struct http_txn *txn = l7;
9813 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009814
bedis4c75cca2012-10-05 08:38:24 +02009815 if (!args || args[0].type != ARGT_STR ||
9816 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009817 return 0;
9818
9819 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009820
bedis4c75cca2012-10-05 08:38:24 +02009821 if (args[1].type)
9822 delim = *args[1].data.str.str;
9823
Willy Tarreau9b28e032012-10-12 23:49:43 +02009824 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009825 args->data.str.str, args->data.str.len,
9826 &smp->data.str.str, &smp->data.str.len,
9827 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009828 return 0;
9829
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009830 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009831 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009832 return 1;
9833}
9834
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009835/* Return the signed integer value for the specified url parameter (see url_param
9836 * above).
9837 */
9838static int
9839smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009840 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009841{
Willy Tarreauef38c392013-07-22 16:29:32 +02009842 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009843
9844 if (ret > 0) {
9845 smp->type = SMP_T_UINT;
9846 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9847 }
9848
9849 return ret;
9850}
9851
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009852/* This produces a 32-bit hash of the concatenation of the first occurrence of
9853 * the Host header followed by the path component if it begins with a slash ('/').
9854 * This means that '*' will not be added, resulting in exactly the first Host
9855 * entry. If no Host header is found, then the path is used. The resulting value
9856 * is hashed using the url hash followed by a full avalanche hash and provides a
9857 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
9858 * high-traffic sites without having to store whole paths.
9859 * this differs from the base32 functions in that it includes the url parameters
9860 * as well as the path
9861 */
9862static int
9863smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009864 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009865{
9866 struct http_txn *txn = l7;
9867 struct hdr_ctx ctx;
9868 unsigned int hash = 0;
9869 char *ptr, *beg, *end;
9870 int len;
9871
9872 CHECK_HTTP_MESSAGE_FIRST();
9873
9874 ctx.idx = 0;
9875 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9876 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9877 ptr = ctx.line + ctx.val;
9878 len = ctx.vlen;
9879 while (len--)
9880 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9881 }
9882
9883 /* now retrieve the path */
9884 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9885 beg = http_get_path(txn);
9886 if (!beg)
9887 beg = end;
9888
9889 for (ptr = beg; ptr < end ; ptr++);
9890
9891 if (beg < ptr && *beg == '/') {
9892 while (beg < ptr)
9893 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9894 }
9895 hash = full_hash(hash);
9896
9897 smp->type = SMP_T_UINT;
9898 smp->data.uint = hash;
9899 smp->flags = SMP_F_VOL_1ST;
9900 return 1;
9901}
9902
9903/* This concatenates the source address with the 32-bit hash of the Host and
9904 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
9905 * per-url counters. The result is a binary block from 8 to 20 bytes depending
9906 * on the source address length. The URL hash is stored before the address so
9907 * that in environments where IPv6 is insignificant, truncating the output to
9908 * 8 bytes would still work.
9909 */
9910static int
9911smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +01009912 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009913{
9914 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009915 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009916
Willy Tarreaue155ec22013-11-18 18:33:22 +01009917 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009918 return 0;
9919
9920 temp = get_trash_chunk();
9921 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9922 temp->len += sizeof(smp->data.uint);
9923
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009924 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009925 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009926 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009927 temp->len += 4;
9928 break;
9929 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009930 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009931 temp->len += 16;
9932 break;
9933 default:
9934 return 0;
9935 }
9936
9937 smp->data.str = *temp;
9938 smp->type = SMP_T_BIN;
9939 return 1;
9940}
9941
Willy Tarreau185b5c42012-04-26 15:11:51 +02009942/* This function is used to validate the arguments passed to any "hdr" fetch
9943 * keyword. These keywords support an optional positive or negative occurrence
9944 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9945 * is assumed that the types are already the correct ones. Returns 0 on error,
9946 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9947 * error message in case of error, that the caller is responsible for freeing.
9948 * The initial location must either be freeable or NULL.
9949 */
9950static int val_hdr(struct arg *arg, char **err_msg)
9951{
9952 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009953 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009954 return 0;
9955 }
9956 return 1;
9957}
9958
Willy Tarreau276fae92013-07-25 14:36:01 +02009959/* takes an UINT value on input supposed to represent the time since EPOCH,
9960 * adds an optional offset found in args[0] and emits a string representing
9961 * the date in RFC-1123/5322 format.
9962 */
9963static int sample_conv_http_date(const struct arg *args, struct sample *smp)
9964{
9965 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
9966 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
9967 struct chunk *temp;
9968 struct tm *tm;
9969 time_t curr_date = smp->data.uint;
9970
9971 /* add offset */
9972 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
9973 curr_date += args[0].data.sint;
9974
9975 tm = gmtime(&curr_date);
9976
9977 temp = get_trash_chunk();
9978 temp->len = snprintf(temp->str, temp->size - temp->len,
9979 "%s, %02d %s %04d %02d:%02d:%02d GMT",
9980 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
9981 tm->tm_hour, tm->tm_min, tm->tm_sec);
9982
9983 smp->data.str = *temp;
9984 smp->type = SMP_T_STR;
9985 return 1;
9986}
9987
Willy Tarreau4a568972010-05-12 08:08:50 +02009988/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009989/* All supported ACL keywords must be declared here. */
9990/************************************************************************/
9991
9992/* Note: must not be declared <const> as its list will be overwritten.
9993 * Please take care of keeping this list alphabetically sorted.
9994 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009995static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009996 { "base", "base", pat_parse_str, pat_match_str },
9997 { "base_beg", "base", pat_parse_str, pat_match_beg },
9998 { "base_dir", "base", pat_parse_str, pat_match_dir },
9999 { "base_dom", "base", pat_parse_str, pat_match_dom },
10000 { "base_end", "base", pat_parse_str, pat_match_end },
10001 { "base_len", "base", pat_parse_int, pat_match_len },
10002 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10003 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010004
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010005 { "cook", "req.cook", pat_parse_str, pat_match_str },
10006 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10007 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10008 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10009 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10010 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10011 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10012 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010013
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010014 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10015 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10016 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10017 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10018 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10019 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10020 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10021 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010022
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010023 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010024
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010025 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010026
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010027 { "path", "path", pat_parse_str, pat_match_str },
10028 { "path_beg", "path", pat_parse_str, pat_match_beg },
10029 { "path_dir", "path", pat_parse_str, pat_match_dir },
10030 { "path_dom", "path", pat_parse_str, pat_match_dom },
10031 { "path_end", "path", pat_parse_str, pat_match_end },
10032 { "path_len", "path", pat_parse_int, pat_match_len },
10033 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10034 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010035
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010036 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10037 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010038
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010039 { "scook", "res.cook", pat_parse_str, pat_match_str },
10040 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10041 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10042 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10043 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10044 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10045 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10046 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010047
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010048 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10049 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10050 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10051 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10052 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10053 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10054 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10055 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010056
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010057 { "url", "url", pat_parse_str, pat_match_str },
10058 { "url_beg", "url", pat_parse_str, pat_match_beg },
10059 { "url_dir", "url", pat_parse_str, pat_match_dir },
10060 { "url_dom", "url", pat_parse_str, pat_match_dom },
10061 { "url_end", "url", pat_parse_str, pat_match_end },
10062 { "url_len", "url", pat_parse_int, pat_match_len },
10063 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10064 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010065
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010066 { "urlp", "urlp", pat_parse_str, pat_match_str },
10067 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10068 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10069 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10070 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10071 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10072 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10073 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010074
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010075 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010076}};
10077
10078/************************************************************************/
10079/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010080/************************************************************************/
10081/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010082static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010083 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10084 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10085 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10086
10087 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10088 * are only here to match the ACL's name, are request-only and are used
10089 * for ACL compatibility only.
10090 */
10091 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10092 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10093 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10094 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10095
10096 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10097 * only here to match the ACL's name, are request-only and are used for
10098 * ACL compatibility only.
10099 */
10100 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10101 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10102 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10103 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10104
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010105 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10106 { "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 +010010107 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10108 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10109 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010110
10111 /* HTTP protocol on the request path */
10112 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010113 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010114
10115 /* HTTP version on the request path */
10116 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010117 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010118
10119 /* HTTP version on the response path */
10120 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010121 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10122
Willy Tarreau18ed2562013-01-14 15:56:36 +010010123 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10124 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10125 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10126 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10127
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010128 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10129 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010130 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10131 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10132 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10133 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10134
10135 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10136 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10137 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10138 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10139
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010140 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10141 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010142 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10143 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10144 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10145 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10146
Willy Tarreau409bcde2013-01-08 00:31:00 +010010147 /* scook is valid only on the response and is used for ACL compatibility */
10148 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10149 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10150 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10151 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10152
10153 /* shdr is valid only on the response and is used for ACL compatibility */
10154 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10155 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10156 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10157 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10158
10159 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10160 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010161 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10162 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010163 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10164 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10165 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10166 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10167 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10168 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010169}};
10170
Willy Tarreau8797c062007-05-07 00:55:35 +020010171
Willy Tarreau276fae92013-07-25 14:36:01 +020010172/* Note: must not be declared <const> as its list will be overwritten */
10173static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10174 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10175 { NULL, NULL, 0, 0, 0 },
10176}};
10177
Willy Tarreau8797c062007-05-07 00:55:35 +020010178__attribute__((constructor))
10179static void __http_protocol_init(void)
10180{
10181 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010182 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010183 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010184}
10185
10186
Willy Tarreau58f10d72006-12-04 02:26:12 +010010187/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010188 * Local variables:
10189 * c-indent-level: 8
10190 * c-basic-offset: 8
10191 * End:
10192 */