blob: c0be2898979def1214c36a92878d3f8b905cb7c7 [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
William Lallemand65ad6e12014-01-31 15:08:02 +0100838/* Parse the URI from the given string and look for the "/" beginning the PATH.
839 * If not found, return NULL. It is returned otherwise.
840 */
841static char *
842http_get_path_from_string(char *str)
843{
844 char *ptr = str;
845
846 /* RFC2616, par. 5.1.2 :
847 * Request-URI = "*" | absuri | abspath | authority
848 */
849
850 if (*ptr == '*')
851 return NULL;
852
853 if (isalpha((unsigned char)*ptr)) {
854 /* this is a scheme as described by RFC3986, par. 3.1 */
855 ptr++;
856 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
857 ptr++;
858 /* skip '://' */
859 if (*ptr == '\0' || *ptr++ != ':')
860 return NULL;
861 if (*ptr == '\0' || *ptr++ != '/')
862 return NULL;
863 if (*ptr == '\0' || *ptr++ != '/')
864 return NULL;
865 }
866 /* skip [user[:passwd]@]host[:[port]] */
867
868 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
869 ptr++;
870
871 if (*ptr == '\0' || *ptr == ' ')
872 return NULL;
873
874 /* OK, we got the '/' ! */
875 return ptr;
876}
877
Willy Tarreau71241ab2012-12-27 11:30:54 +0100878/* Returns a 302 for a redirectable request that reaches a server working in
879 * in redirect mode. This may only be called just after the stream interface
880 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
881 * follow normal proxy processing. NOTE: this function is designed to support
882 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100883 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100884void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100885{
886 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100887 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100888 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200889 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100890
891 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100892 trash.len = strlen(HTTP_302);
893 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100894
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100895 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100896
Willy Tarreauefb453c2008-10-26 20:49:47 +0100897 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100898 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100899 return;
900
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100901 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100902 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100903 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
904 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100905 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200907 /* 3: add the request URI. Since it was already forwarded, we need
908 * to temporarily rewind the buffer.
909 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100910 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200911 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200912
Willy Tarreauefb453c2008-10-26 20:49:47 +0100913 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200914 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 +0200915
Willy Tarreau9b28e032012-10-12 23:49:43 +0200916 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200917
Willy Tarreauefb453c2008-10-26 20:49:47 +0100918 if (!path)
919 return;
920
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100921 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100922 return;
923
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100924 memcpy(trash.str + trash.len, path, len);
925 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100926
927 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100928 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
929 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100930 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100931 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
932 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100933 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934
935 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200936 si_shutr(si);
937 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100938 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 si->state = SI_ST_CLO;
940
941 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200942 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100943
944 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100945 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500946 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100947}
948
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100949/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950 * that the server side is closed. Note that err_type is actually a
951 * bitmask, where almost only aborts may be cumulated with other
952 * values. We consider that aborted operations are more important
953 * than timeouts or errors due to the fact that nobody else in the
954 * logs might explain incomplete retries. All others should avoid
955 * being cumulated. It should normally not be possible to have multiple
956 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100957 * Note that connection errors appearing on the second request of a keep-alive
958 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100959 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100960void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100961{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100962 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963
964 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100965 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200966 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100968 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100969 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
970 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100971 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100972 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200973 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100974 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100975 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200976 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100977 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100978 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100979 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
980 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100981 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100982 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +0100983 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100984 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200985 else if (err_type & SI_ET_CONN_RES)
986 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100987 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
988 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100989 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100990 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200991 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992}
993
Willy Tarreau42250582007-04-01 01:30:43 +0200994extern const char sess_term_cond[8];
995extern const char sess_fin_state[8];
996extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200997struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100998struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100999struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001000
Willy Tarreau117f59e2007-03-04 18:17:17 +01001001/*
1002 * Capture headers from message starting at <som> according to header list
1003 * <cap_hdr>, and fill the <idx> structure appropriately.
1004 */
1005void capture_headers(char *som, struct hdr_idx *idx,
1006 char **cap, struct cap_hdr *cap_hdr)
1007{
1008 char *eol, *sol, *col, *sov;
1009 int cur_idx;
1010 struct cap_hdr *h;
1011 int len;
1012
1013 sol = som + hdr_idx_first_pos(idx);
1014 cur_idx = hdr_idx_first_idx(idx);
1015
1016 while (cur_idx) {
1017 eol = sol + idx->v[cur_idx].len;
1018
1019 col = sol;
1020 while (col < eol && *col != ':')
1021 col++;
1022
1023 sov = col + 1;
1024 while (sov < eol && http_is_lws[(unsigned char)*sov])
1025 sov++;
1026
1027 for (h = cap_hdr; h; h = h->next) {
1028 if ((h->namelen == col - sol) &&
1029 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1030 if (cap[h->index] == NULL)
1031 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001032 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001033
1034 if (cap[h->index] == NULL) {
1035 Alert("HTTP capture : out of memory.\n");
1036 continue;
1037 }
1038
1039 len = eol - sov;
1040 if (len > h->len)
1041 len = h->len;
1042
1043 memcpy(cap[h->index], sov, len);
1044 cap[h->index][len]=0;
1045 }
1046 }
1047 sol = eol + idx->v[cur_idx].cr + 1;
1048 cur_idx = idx->v[cur_idx].next;
1049 }
1050}
1051
1052
Willy Tarreau42250582007-04-01 01:30:43 +02001053/* either we find an LF at <ptr> or we jump to <bad>.
1054 */
1055#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1056
1057/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1058 * otherwise to <http_msg_ood> with <state> set to <st>.
1059 */
1060#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1061 ptr++; \
1062 if (likely(ptr < end)) \
1063 goto good; \
1064 else { \
1065 state = (st); \
1066 goto http_msg_ood; \
1067 } \
1068 } while (0)
1069
1070
Willy Tarreaubaaee002006-06-26 02:48:02 +02001071/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001072 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001073 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1074 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1075 * will give undefined results.
1076 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1077 * and that msg->sol points to the beginning of the response.
1078 * If a complete line is found (which implies that at least one CR or LF is
1079 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1080 * returned indicating an incomplete line (which does not mean that parts have
1081 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1082 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1083 * upon next call.
1084 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001085 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001086 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1087 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001088 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001090const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001091 enum ht_state state, const char *ptr, const char *end,
1092 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001093{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001094 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001095
Willy Tarreau8973c702007-01-21 23:58:29 +01001096 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001097 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001098 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001099 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1101
1102 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001103 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001104 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1105 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001106 state = HTTP_MSG_ERROR;
1107 break;
1108
Willy Tarreau8973c702007-01-21 23:58:29 +01001109 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001110 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001111 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001112 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 goto http_msg_rpcode;
1114 }
1115 if (likely(HTTP_IS_SPHT(*ptr)))
1116 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1117 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001118 state = HTTP_MSG_ERROR;
1119 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001120
Willy Tarreau8973c702007-01-21 23:58:29 +01001121 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001122 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001123 if (likely(!HTTP_IS_LWS(*ptr)))
1124 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1125
1126 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001127 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001128 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1129 }
1130
1131 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001132 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 http_msg_rsp_reason:
1134 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001135 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 msg->sl.st.r_l = 0;
1137 goto http_msg_rpline_eol;
1138
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001140 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001141 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001142 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001143 goto http_msg_rpreason;
1144 }
1145 if (likely(HTTP_IS_SPHT(*ptr)))
1146 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1147 /* so it's a CR/LF, so there is no reason phrase */
1148 goto http_msg_rsp_reason;
1149
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001151 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 if (likely(!HTTP_IS_CRLF(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001154 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001155 http_msg_rpline_eol:
1156 /* We have seen the end of line. Note that we do not
1157 * necessarily have the \n yet, but at least we know that we
1158 * have EITHER \r OR \n, otherwise the response would not be
1159 * complete. We can then record the response length and return
1160 * to the caller which will be able to register it.
1161 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001162 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001163 return ptr;
1164
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001166#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001167 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1168 exit(1);
1169#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001170 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001171 }
1172
1173 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001174 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001175 if (ret_state)
1176 *ret_state = state;
1177 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001178 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001180}
1181
Willy Tarreau8973c702007-01-21 23:58:29 +01001182/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 * This function parses a request line between <ptr> and <end>, starting with
1184 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1185 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1186 * will give undefined results.
1187 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1188 * and that msg->sol points to the beginning of the request.
1189 * If a complete line is found (which implies that at least one CR or LF is
1190 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1191 * returned indicating an incomplete line (which does not mean that parts have
1192 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1193 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1194 * upon next call.
1195 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001196 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1198 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001199 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001201const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001202 enum ht_state state, const char *ptr, const char *end,
1203 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001204{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001205 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001206
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001207 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001208 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001209 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 if (likely(HTTP_IS_TOKEN(*ptr)))
1211 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001212
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001213 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001214 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1216 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001217
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001218 if (likely(HTTP_IS_CRLF(*ptr))) {
1219 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001220 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001221 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001222 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001224 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001226 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 msg->sl.rq.v_l = 0;
1228 goto http_msg_rqline_eol;
1229 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001230 state = HTTP_MSG_ERROR;
1231 break;
1232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001234 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001236 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 goto http_msg_rquri;
1238 }
1239 if (likely(HTTP_IS_SPHT(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1241 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1242 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001243
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001245 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001246 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001250 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1252 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001253
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001254 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001255 /* non-ASCII chars are forbidden unless option
1256 * accept-invalid-http-request is enabled in the frontend.
1257 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001258 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001259 if (msg->err_pos < -1)
1260 goto invalid_char;
1261 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001262 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001263 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1264 }
1265
1266 if (likely(HTTP_IS_CRLF(*ptr))) {
1267 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1268 goto http_msg_req09_uri_e;
1269 }
1270
1271 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001272 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001273 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001274 state = HTTP_MSG_ERROR;
1275 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001278 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001280 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 goto http_msg_rqver;
1282 }
1283 if (likely(HTTP_IS_SPHT(*ptr)))
1284 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1285 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1286 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001287
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001289 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001290 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001292
1293 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001294 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001295 http_msg_rqline_eol:
1296 /* We have seen the end of line. Note that we do not
1297 * necessarily have the \n yet, but at least we know that we
1298 * have EITHER \r OR \n, otherwise the request would not be
1299 * complete. We can then record the request length and return
1300 * to the caller which will be able to register it.
1301 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001302 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001303 return ptr;
1304 }
1305
1306 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001307 state = HTTP_MSG_ERROR;
1308 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001311#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1313 exit(1);
1314#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001315 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001319 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 if (ret_state)
1321 *ret_state = state;
1322 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001323 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001326
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001327/*
1328 * Returns the data from Authorization header. Function may be called more
1329 * than once so data is stored in txn->auth_data. When no header is found
1330 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001331 * searching again for something we are unable to find anyway. However, if
1332 * the result if valid, the cache is not reused because we would risk to
1333 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001334 */
1335
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001336char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001337
1338int
1339get_http_auth(struct session *s)
1340{
1341
1342 struct http_txn *txn = &s->txn;
1343 struct chunk auth_method;
1344 struct hdr_ctx ctx;
1345 char *h, *p;
1346 int len;
1347
1348#ifdef DEBUG_AUTH
1349 printf("Auth for session %p: %d\n", s, txn->auth.method);
1350#endif
1351
1352 if (txn->auth.method == HTTP_AUTH_WRONG)
1353 return 0;
1354
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001355 txn->auth.method = HTTP_AUTH_WRONG;
1356
1357 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001358
1359 if (txn->flags & TX_USE_PX_CONN) {
1360 h = "Proxy-Authorization";
1361 len = strlen(h);
1362 } else {
1363 h = "Authorization";
1364 len = strlen(h);
1365 }
1366
Willy Tarreau9b28e032012-10-12 23:49:43 +02001367 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001368 return 0;
1369
1370 h = ctx.line + ctx.val;
1371
1372 p = memchr(h, ' ', ctx.vlen);
1373 if (!p || p == h)
1374 return 0;
1375
1376 chunk_initlen(&auth_method, h, 0, p-h);
1377 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1378
1379 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1380
1381 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001382 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001383
1384 if (len < 0)
1385 return 0;
1386
1387
1388 get_http_auth_buff[len] = '\0';
1389
1390 p = strchr(get_http_auth_buff, ':');
1391
1392 if (!p)
1393 return 0;
1394
1395 txn->auth.user = get_http_auth_buff;
1396 *p = '\0';
1397 txn->auth.pass = p+1;
1398
1399 txn->auth.method = HTTP_AUTH_BASIC;
1400 return 1;
1401 }
1402
1403 return 0;
1404}
1405
Willy Tarreau58f10d72006-12-04 02:26:12 +01001406
Willy Tarreau8973c702007-01-21 23:58:29 +01001407/*
1408 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001409 * depending on the initial msg->msg_state. The caller is responsible for
1410 * ensuring that the message does not wrap. The function can be preempted
1411 * everywhere when data are missing and recalled at the exact same location
1412 * with no information loss. The message may even be realigned between two
1413 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001414 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001415 * fields. Note that msg->sol will be initialized after completing the first
1416 * state, so that none of the msg pointers has to be initialized prior to the
1417 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001418 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001419void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420{
Willy Tarreau3770f232013-12-07 00:01:53 +01001421 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001423 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001425 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001426 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001427 ptr = buf->p + msg->next;
1428 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 if (unlikely(ptr >= end))
1431 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 /*
1435 * First, states that are specific to the response only.
1436 * We check them first so that request and headers are
1437 * closer to each other (accessed more often).
1438 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001440 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001442 /* we have a start of message, but we have to check
1443 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001444 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001445 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001446 if (unlikely(ptr != buf->p)) {
1447 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001448 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001449 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001450 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 }
Willy Tarreau26927362012-05-18 23:22:52 +02001452 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001453 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001454 hdr_idx_init(idx);
1455 state = HTTP_MSG_RPVER;
1456 goto http_msg_rpver;
1457 }
1458
1459 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1460 goto http_msg_invalid;
1461
1462 if (unlikely(*ptr == '\n'))
1463 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1464 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1465 /* stop here */
1466
Willy Tarreau8973c702007-01-21 23:58:29 +01001467 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001468 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 EXPECT_LF_HERE(ptr, http_msg_invalid);
1470 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1471 /* stop here */
1472
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001474 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001475 case HTTP_MSG_RPVER_SP:
1476 case HTTP_MSG_RPCODE:
1477 case HTTP_MSG_RPCODE_SP:
1478 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001479 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001480 state, ptr, end,
1481 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 if (unlikely(!ptr))
1483 return;
1484
1485 /* we have a full response and we know that we have either a CR
1486 * or an LF at <ptr>.
1487 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001488 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1489
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001490 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001491 if (likely(*ptr == '\r'))
1492 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1493 goto http_msg_rpline_end;
1494
Willy Tarreau8973c702007-01-21 23:58:29 +01001495 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001496 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 /* msg->sol must point to the first of CR or LF. */
1498 EXPECT_LF_HERE(ptr, http_msg_invalid);
1499 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1500 /* stop here */
1501
1502 /*
1503 * Second, states that are specific to the request only
1504 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001506 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001508 /* we have a start of message, but we have to check
1509 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001510 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001511 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001512 if (likely(ptr != buf->p)) {
1513 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001514 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001515 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001516 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 }
Willy Tarreau26927362012-05-18 23:22:52 +02001518 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001519 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 state = HTTP_MSG_RQMETH;
1521 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1525 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(*ptr == '\n'))
1528 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1529 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001533 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 EXPECT_LF_HERE(ptr, http_msg_invalid);
1535 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001536 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001539 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_RQMETH_SP:
1541 case HTTP_MSG_RQURI:
1542 case HTTP_MSG_RQURI_SP:
1543 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001544 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001545 state, ptr, end,
1546 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 if (unlikely(!ptr))
1548 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 /* we have a full request and we know that we have either a CR
1551 * or an LF at <ptr>.
1552 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001554
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001555 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (likely(*ptr == '\r'))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001561 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 /* check for HTTP/0.9 request : no version information available.
1563 * msg->sol must point to the first of CR or LF.
1564 */
1565 if (unlikely(msg->sl.rq.v_l == 0))
1566 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 EXPECT_LF_HERE(ptr, http_msg_invalid);
1569 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001570 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001571
Willy Tarreau8973c702007-01-21 23:58:29 +01001572 /*
1573 * Common states below
1574 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001577 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 if (likely(!HTTP_IS_CRLF(*ptr))) {
1579 goto http_msg_hdr_name;
1580 }
1581
1582 if (likely(*ptr == '\r'))
1583 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1584 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001587 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 /* assumes msg->sol points to the first char */
1589 if (likely(HTTP_IS_TOKEN(*ptr)))
1590 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001592 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001595 if (likely(msg->err_pos < -1) || *ptr == '\n')
1596 goto http_msg_invalid;
1597
1598 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001599 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001600
1601 /* and we still accept this non-token character */
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001605 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001606 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 if (likely(HTTP_IS_SPHT(*ptr)))
1608 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001611 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 if (likely(!HTTP_IS_CRLF(*ptr))) {
1614 goto http_msg_hdr_val;
1615 }
1616
1617 if (likely(*ptr == '\r'))
1618 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1619 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001620
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001622 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 EXPECT_LF_HERE(ptr, http_msg_invalid);
1624 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001627 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 if (likely(HTTP_IS_SPHT(*ptr))) {
1629 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 for (; buf->p + msg->sov < ptr; msg->sov++)
1631 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 goto http_msg_hdr_l1_sp;
1633 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001634 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001635 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 goto http_msg_complete_header;
1637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001639 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001640 /* assumes msg->sol points to the first char, and msg->sov
1641 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 */
1643 if (likely(!HTTP_IS_CRLF(*ptr)))
1644 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001646 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 /* Note: we could also copy eol into ->eoh so that we have the
1648 * real header end in case it ends with lots of LWS, but is this
1649 * really needed ?
1650 */
1651 if (likely(*ptr == '\r'))
1652 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1653 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001654
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001656 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 EXPECT_LF_HERE(ptr, http_msg_invalid);
1658 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001661 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1663 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001664 for (; buf->p + msg->eol < ptr; msg->eol++)
1665 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 goto http_msg_hdr_val;
1667 }
1668 http_msg_complete_header:
1669 /*
1670 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001671 * Assumes msg->sol points to the first char, msg->sov points
1672 * to the first character of the value and msg->eol to the
1673 * first CR or LF so we know how the line ends. We insert last
1674 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001676 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 idx, idx->tail) < 0))
1678 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001679
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001680 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 if (likely(!HTTP_IS_CRLF(*ptr))) {
1682 goto http_msg_hdr_name;
1683 }
1684
1685 if (likely(*ptr == '\r'))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1687 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001690 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 /* Assumes msg->sol points to the first of either CR or LF */
1692 EXPECT_LF_HERE(ptr, http_msg_invalid);
1693 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001694 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001695 msg->eoh = msg->sol;
1696 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001697 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001699
1700 case HTTP_MSG_ERROR:
1701 /* this may only happen if we call http_msg_analyser() twice with an error */
1702 break;
1703
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001705#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1707 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001708#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001709 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 }
1711 http_msg_ood:
1712 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001713 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001714 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 http_msg_invalid:
1718 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001719 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001720 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 return;
1722}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001723
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001724/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1725 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1726 * nothing is done and 1 is returned.
1727 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001728static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001729{
1730 int delta;
1731 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001732 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001733
1734 if (msg->sl.rq.v_l != 0)
1735 return 1;
1736
Willy Tarreau9b28e032012-10-12 23:49:43 +02001737 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001738 delta = 0;
1739
1740 if (msg->sl.rq.u_l == 0) {
1741 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001742 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001743 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001744 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001745 }
1746 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001747 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001748 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001749 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001750 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001751 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001752 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001753 NULL, NULL);
1754 if (unlikely(!cur_end))
1755 return 0;
1756
1757 /* we have a full HTTP/1.0 request now and we know that
1758 * we have either a CR or an LF at <ptr>.
1759 */
1760 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1761 return 1;
1762}
1763
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001764/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001765 * and "keep-alive" values. If we already know that some headers may safely
1766 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001767 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1768 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001769 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001770 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1771 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1772 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001773 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001774 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001775void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001776{
Willy Tarreau5b154472009-12-21 20:11:07 +01001777 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001778 const char *hdr_val = "Connection";
1779 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001780
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001781 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001782 return;
1783
Willy Tarreau88d349d2010-01-25 12:15:43 +01001784 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1785 hdr_val = "Proxy-Connection";
1786 hdr_len = 16;
1787 }
1788
Willy Tarreau5b154472009-12-21 20:11:07 +01001789 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001790 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001791 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001792 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1793 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001794 if (to_del & 2)
1795 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001796 else
1797 txn->flags |= TX_CON_KAL_SET;
1798 }
1799 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1800 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001801 if (to_del & 1)
1802 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803 else
1804 txn->flags |= TX_CON_CLO_SET;
1805 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001806 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1807 txn->flags |= TX_HDR_CONN_UPG;
1808 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001809 }
1810
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001811 txn->flags |= TX_HDR_CONN_PRS;
1812 return;
1813}
Willy Tarreau5b154472009-12-21 20:11:07 +01001814
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001815/* Apply desired changes on the Connection: header. Values may be removed and/or
1816 * added depending on the <wanted> flags, which are exclusively composed of
1817 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1818 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1819 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001820void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821{
1822 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001823 const char *hdr_val = "Connection";
1824 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001825
1826 ctx.idx = 0;
1827
Willy Tarreau88d349d2010-01-25 12:15:43 +01001828
1829 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1830 hdr_val = "Proxy-Connection";
1831 hdr_len = 16;
1832 }
1833
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001835 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001836 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1837 if (wanted & TX_CON_KAL_SET)
1838 txn->flags |= TX_CON_KAL_SET;
1839 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001841 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001842 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1843 if (wanted & TX_CON_CLO_SET)
1844 txn->flags |= TX_CON_CLO_SET;
1845 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001846 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001847 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001848 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849
1850 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1851 return;
1852
1853 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1854 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001855 hdr_val = "Connection: close";
1856 hdr_len = 17;
1857 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1858 hdr_val = "Proxy-Connection: close";
1859 hdr_len = 23;
1860 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001861 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001862 }
1863
1864 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1865 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866 hdr_val = "Connection: keep-alive";
1867 hdr_len = 22;
1868 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1869 hdr_val = "Proxy-Connection: keep-alive";
1870 hdr_len = 28;
1871 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001872 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001873 }
1874 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001875}
1876
Willy Tarreaua458b672012-03-05 11:17:50 +01001877/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001879 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001880 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001882 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001883static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001884{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001885 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001886 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001887 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001888 const char *end = buf->data + buf->size;
1889 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001890 unsigned int chunk = 0;
1891
1892 /* The chunk size is in the following form, though we are only
1893 * interested in the size and CRLF :
1894 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1895 */
1896 while (1) {
1897 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001898 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001899 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001901 if (c < 0) /* not a hex digit anymore */
1902 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001903 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001905 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001906 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 chunk = (chunk << 4) + c;
1908 }
1909
Willy Tarreaud98cf932009-12-27 22:54:55 +01001910 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001911 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001912 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913
1914 while (http_is_spht[(unsigned char)*ptr]) {
1915 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001917 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001919 }
1920
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 /* Up to there, we know that at least one byte is present at *ptr. Check
1922 * for the end of chunk size.
1923 */
1924 while (1) {
1925 if (likely(HTTP_IS_CRLF(*ptr))) {
1926 /* we now have a CR or an LF at ptr */
1927 if (likely(*ptr == '\r')) {
1928 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001929 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001930 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 return 0;
1932 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001933
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001935 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001936 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001937 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001938 /* done */
1939 break;
1940 }
1941 else if (*ptr == ';') {
1942 /* chunk extension, ends at next CRLF */
1943 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001945 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001946 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947
1948 while (!HTTP_IS_CRLF(*ptr)) {
1949 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001950 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001951 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 return 0;
1953 }
1954 /* we have a CRLF now, loop above */
1955 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001956 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001958 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001962 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001964 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001965 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001966 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001967 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001968 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001969 msg->chunk_len = chunk;
1970 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001972 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001973 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001974 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001975 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001976}
1977
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001978/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001979 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001980 * the trailers is found, it is automatically scheduled to be forwarded,
1981 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1982 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001983 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001984 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001985 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001986 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1987 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001988 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001989 * matches the length of trailers already parsed and not forwarded. It is also
1990 * important to note that this function is designed to be able to parse wrapped
1991 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001993static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001994{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001995 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001996
Willy Tarreaua458b672012-03-05 11:17:50 +01001997 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001999 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002000 const char *ptr = b_ptr(buf, msg->next);
2001 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002002 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003
2004 /* scan current line and stop at LF or CRLF */
2005 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002006 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002007 return 0;
2008
2009 if (*ptr == '\n') {
2010 if (!p1)
2011 p1 = ptr;
2012 p2 = ptr;
2013 break;
2014 }
2015
2016 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002017 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002018 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002019 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002020 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002021 p1 = ptr;
2022 }
2023
2024 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002025 if (ptr >= buf->data + buf->size)
2026 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002027 }
2028
2029 /* after LF; point to beginning of next line */
2030 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002031 if (p2 >= buf->data + buf->size)
2032 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002034 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002035 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002036 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002037
2038 /* schedule this line for forwarding */
2039 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 if (msg->sov >= buf->size)
2041 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002043 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002044 /* LF/CRLF at beginning of line => end of trailers at p2.
2045 * Everything was scheduled for forwarding, there's nothing
2046 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002047 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002048 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002049 msg->msg_state = HTTP_MSG_DONE;
2050 return 1;
2051 }
2052 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002054 }
2055}
2056
Willy Tarreau54d23df2012-10-25 19:04:45 +02002057/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002059 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002060 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2062 * not enough data are available, the function does not change anything and
2063 * returns zero. If a parse error is encountered, the function returns < 0 and
2064 * does not change anything. Note: this function is designed to parse wrapped
2065 * CRLF at the end of the buffer.
2066 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002067static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002069 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002070 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002071 int bytes;
2072
2073 /* NB: we'll check data availabilty at the end. It's not a
2074 * problem because whatever we match first will be checked
2075 * against the correct length.
2076 */
2077 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002079 if (*ptr == '\r') {
2080 bytes++;
2081 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 if (ptr >= buf->data + buf->size)
2083 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 }
2085
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002086 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002087 return 0;
2088
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002089 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002090 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002091 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002092 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002093
2094 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002095 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002096 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002097 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2098 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002099 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002100 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2101 return 1;
2102}
Willy Tarreau5b154472009-12-21 20:11:07 +01002103
William Lallemand82fe75c2012-10-23 10:25:10 +02002104
2105/*
2106 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002107 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002108int select_compression_request_header(struct session *s, struct buffer *req)
2109{
2110 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002111 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002112 struct hdr_ctx ctx;
2113 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002114 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002115
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002116 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2117 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002118 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2119 */
2120 ctx.idx = 0;
2121 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2122 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002123 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2124 (ctx.vlen < 31 ||
2125 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2126 ctx.line[ctx.val + 30] < '6' ||
2127 (ctx.line[ctx.val + 30] == '6' &&
2128 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2129 s->comp_algo = NULL;
2130 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002131 }
2132
William Lallemand82fe75c2012-10-23 10:25:10 +02002133 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002134 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 +02002135 ctx.idx = 0;
2136 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002137 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002138 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2139 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002140
2141 /* remove all occurrences of the header when "compression offload" is set */
2142
2143 if ((s->be->comp && s->be->comp->offload) ||
2144 (s->fe->comp && s->fe->comp->offload)) {
2145 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2146 ctx.idx = 0;
2147 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2148 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2149 }
2150 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002151 return 1;
2152 }
2153 }
2154 }
2155 }
2156
2157 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002158 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2159 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002160 if (comp_algo->add_data == identity_add_data) {
2161 s->comp_algo = comp_algo;
2162 return 1;
2163 }
2164 }
2165 }
2166
2167 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002168 return 0;
2169}
2170
2171/*
2172 * Selects a comression algorithm depending of the server response.
2173 */
2174int select_compression_response_header(struct session *s, struct buffer *res)
2175{
2176 struct http_txn *txn = &s->txn;
2177 struct http_msg *msg = &txn->rsp;
2178 struct hdr_ctx ctx;
2179 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002180
2181 /* no common compression algorithm was found in request header */
2182 if (s->comp_algo == NULL)
2183 goto fail;
2184
2185 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002186 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002187 goto fail;
2188
William Lallemandd3002612012-11-26 14:34:47 +01002189 /* 200 only */
2190 if (txn->status != 200)
2191 goto fail;
2192
William Lallemand82fe75c2012-10-23 10:25:10 +02002193 /* Content-Length is null */
2194 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2195 goto fail;
2196
Willy Tarreau667c2a32013-04-09 08:13:58 +02002197 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2198 if (msg->flags & HTTP_MSGF_TE_CHNK)
2199 goto fail;
2200
William Lallemand82fe75c2012-10-23 10:25:10 +02002201 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002202 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002203 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2204 goto fail;
2205
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002206 /* no compression when Cache-Control: no-transform is present in the message */
2207 ctx.idx = 0;
2208 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2209 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2210 goto fail;
2211 }
2212
William Lallemand82fe75c2012-10-23 10:25:10 +02002213 comp_type = NULL;
2214
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002215 /* we don't want to compress multipart content-types, nor content-types that are
2216 * not listed in the "compression type" directive if any. If no content-type was
2217 * found but configuration requires one, we don't compress either. Backend has
2218 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002219 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002220 ctx.idx = 0;
2221 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2222 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2223 goto fail;
2224
2225 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2226 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002227 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002228 if (ctx.vlen >= comp_type->name_len &&
2229 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002230 /* this Content-Type should be compressed */
2231 break;
2232 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002233 /* this Content-Type should not be compressed */
2234 if (comp_type == NULL)
2235 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002236 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002237 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002238 else { /* no content-type header */
2239 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2240 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002241 }
2242
William Lallemandd85f9172012-11-09 17:05:39 +01002243 /* limit compression rate */
2244 if (global.comp_rate_lim > 0)
2245 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2246 goto fail;
2247
William Lallemand072a2bf2012-11-20 17:01:01 +01002248 /* limit cpu usage */
2249 if (idle_pct < compress_min_idle)
2250 goto fail;
2251
William Lallemand4c49fae2012-11-07 15:00:23 +01002252 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002253 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002254 goto fail;
2255
William Lallemandec3e3892012-11-12 17:02:18 +01002256 s->flags |= SN_COMP_READY;
2257
William Lallemand82fe75c2012-10-23 10:25:10 +02002258 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002259 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002260 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2261 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2262
2263 /* add Transfer-Encoding header */
2264 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2265 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2266
2267 /*
2268 * Add Content-Encoding header when it's not identity encoding.
2269 * RFC 2616 : Identity encoding: This content-coding is used only in the
2270 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2271 * header.
2272 */
2273 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002274 trash.len = 18;
2275 memcpy(trash.str, "Content-Encoding: ", trash.len);
2276 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2277 trash.len += s->comp_algo->name_len;
2278 trash.str[trash.len] = '\0';
2279 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002281 return 1;
2282
2283fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002284 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002285 return 0;
2286}
2287
2288
Willy Tarreaud787e662009-07-07 10:14:51 +02002289/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2290 * processing can continue on next analysers, or zero if it either needs more
2291 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2292 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2293 * when it has nothing left to do, and may remove any analyser when it wants to
2294 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002295 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002296int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002297{
Willy Tarreau59234e92008-11-30 23:51:27 +01002298 /*
2299 * We will parse the partial (or complete) lines.
2300 * We will check the request syntax, and also join multi-line
2301 * headers. An index of all the lines will be elaborated while
2302 * parsing.
2303 *
2304 * For the parsing, we use a 28 states FSM.
2305 *
2306 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002307 * req->buf->p = beginning of request
2308 * req->buf->p + msg->eoh = end of processed headers / start of current one
2309 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002310 * msg->eol = end of current header or line (LF or CRLF)
2311 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002312 *
2313 * At end of parsing, we may perform a capture of the error (if any), and
2314 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2315 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2316 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002320 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 struct http_txn *txn = &s->txn;
2322 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002323 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002324
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002325 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 +01002326 now_ms, __FUNCTION__,
2327 s,
2328 req,
2329 req->rex, req->wex,
2330 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002331 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002332 req->analysers);
2333
Willy Tarreau52a0c602009-08-16 22:45:38 +02002334 /* we're speaking HTTP here, so let's speak HTTP to the client */
2335 s->srv_error = http_return_srv_error;
2336
Willy Tarreau83e3af02009-12-28 17:39:57 +01002337 /* There's a protected area at the end of the buffer for rewriting
2338 * purposes. We don't want to start to parse the request if the
2339 * protected area is affected, because we may have to move processed
2340 * data later, which is much more complicated.
2341 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002342 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002343 if (txn->flags & TX_NOT_FIRST) {
2344 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002345 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002346 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002347 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002348 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002349 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002350 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002351 return 0;
2352 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002353 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2354 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2355 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002356 }
2357
Willy Tarreau065e8332010-01-08 00:30:20 +01002358 /* Note that we have the same problem with the response ; we
2359 * may want to send a redirect, error or anything which requires
2360 * some spare space. So we'll ensure that we have at least
2361 * maxrewrite bytes available in the response buffer before
2362 * processing that one. This will only affect pipelined
2363 * keep-alive requests.
2364 */
2365 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002366 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002367 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2368 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2369 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002370 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002371 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002372 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002373 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002374 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002375 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002376 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002377 return 0;
2378 }
2379 }
2380
Willy Tarreau9b28e032012-10-12 23:49:43 +02002381 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002382 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002383 }
2384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 /* 1: we might have to print this header in debug mode */
2386 if (unlikely((global.mode & MODE_DEBUG) &&
2387 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002388 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002389 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002390
Willy Tarreau9b28e032012-10-12 23:49:43 +02002391 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002392 /* this is a bit complex : in case of error on the request line,
2393 * we know that rq.l is still zero, so we display only the part
2394 * up to the end of the line (truncated by debug_hdr).
2395 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002396 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002398
Willy Tarreau59234e92008-11-30 23:51:27 +01002399 sol += hdr_idx_first_pos(&txn->hdr_idx);
2400 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002401
Willy Tarreau59234e92008-11-30 23:51:27 +01002402 while (cur_idx) {
2403 eol = sol + txn->hdr_idx.v[cur_idx].len;
2404 debug_hdr("clihdr", s, sol, eol);
2405 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2406 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002407 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002408 }
2409
Willy Tarreau58f10d72006-12-04 02:26:12 +01002410
Willy Tarreau59234e92008-11-30 23:51:27 +01002411 /*
2412 * Now we quickly check if we have found a full valid request.
2413 * If not so, we check the FD and buffer states before leaving.
2414 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002415 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002416 * requests are checked first. When waiting for a second request
2417 * on a keep-alive session, if we encounter and error, close, t/o,
2418 * we note the error in the session flags but don't set any state.
2419 * Since the error will be noted there, it will not be counted by
2420 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002421 * Last, we may increase some tracked counters' http request errors on
2422 * the cases that are deliberately the client's fault. For instance,
2423 * a timeout or connection reset is not counted as an error. However
2424 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002426
Willy Tarreau655dce92009-11-08 13:10:58 +01002427 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002428 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002430 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002431 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002432 session_inc_http_req_ctr(s);
2433 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002434 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002435 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002436 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002437
Willy Tarreau59234e92008-11-30 23:51:27 +01002438 /* 1: Since we are in header mode, if there's no space
2439 * left for headers, we won't be able to free more
2440 * later, so the session will never terminate. We
2441 * must terminate it now.
2442 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002443 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 /* FIXME: check if URI is set and return Status
2445 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002446 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002447 session_inc_http_req_ctr(s);
2448 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002449 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002450 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002451 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 goto return_bad_req;
2453 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002454
Willy Tarreau59234e92008-11-30 23:51:27 +01002455 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002456 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002457 if (!(s->flags & SN_ERR_MASK))
2458 s->flags |= SN_ERR_CLICL;
2459
Willy Tarreaufcffa692010-01-10 14:21:19 +01002460 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002461 goto failed_keep_alive;
2462
Willy Tarreau59234e92008-11-30 23:51:27 +01002463 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002464 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002465 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002466 session_inc_http_err_ctr(s);
2467 }
2468
Willy Tarreaudc979f22012-12-04 10:39:01 +01002469 txn->status = 400;
2470 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 msg->msg_state = HTTP_MSG_ERROR;
2472 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002473
Willy Tarreauda7ff642010-06-23 11:44:09 +02002474 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002475 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002476 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002477 if (s->listener->counters)
2478 s->listener->counters->failed_req++;
2479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 if (!(s->flags & SN_FINST_MASK))
2481 s->flags |= SN_FINST_R;
2482 return 0;
2483 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002484
Willy Tarreau59234e92008-11-30 23:51:27 +01002485 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002486 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002487 if (!(s->flags & SN_ERR_MASK))
2488 s->flags |= SN_ERR_CLITO;
2489
Willy Tarreaufcffa692010-01-10 14:21:19 +01002490 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002491 goto failed_keep_alive;
2492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002494 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002495 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002496 session_inc_http_err_ctr(s);
2497 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002499 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 msg->msg_state = HTTP_MSG_ERROR;
2501 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502
Willy Tarreauda7ff642010-06-23 11:44:09 +02002503 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002504 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002505 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002506 if (s->listener->counters)
2507 s->listener->counters->failed_req++;
2508
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 if (!(s->flags & SN_FINST_MASK))
2510 s->flags |= SN_FINST_R;
2511 return 0;
2512 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002515 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002516 if (!(s->flags & SN_ERR_MASK))
2517 s->flags |= SN_ERR_CLICL;
2518
Willy Tarreaufcffa692010-01-10 14:21:19 +01002519 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002520 goto failed_keep_alive;
2521
Willy Tarreau4076a152009-04-02 15:18:36 +02002522 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002523 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002525 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 msg->msg_state = HTTP_MSG_ERROR;
2527 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002528
Willy Tarreauda7ff642010-06-23 11:44:09 +02002529 session_inc_http_err_ctr(s);
2530 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002531 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002532 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002533 if (s->listener->counters)
2534 s->listener->counters->failed_req++;
2535
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 if (!(s->flags & SN_FINST_MASK))
2537 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002538 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002539 }
2540
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002541 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002542 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2543 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002544#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002545 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002546 /* We need more data, we have to re-enable quick-ack in case we
2547 * previously disabled it, otherwise we might cause the client
2548 * to delay next data.
2549 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002550 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002551 }
2552#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002553
Willy Tarreaufcffa692010-01-10 14:21:19 +01002554 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2555 /* If the client starts to talk, let's fall back to
2556 * request timeout processing.
2557 */
2558 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002559 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002560 }
2561
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002563 if (!tick_isset(req->analyse_exp)) {
2564 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2565 (txn->flags & TX_WAIT_NEXT_RQ) &&
2566 tick_isset(s->be->timeout.httpka))
2567 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2568 else
2569 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2570 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /* we're not ready yet */
2573 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002574
2575 failed_keep_alive:
2576 /* Here we process low-level errors for keep-alive requests. In
2577 * short, if the request is not the first one and it experiences
2578 * a timeout, read error or shutdown, we just silently close so
2579 * that the client can try again.
2580 */
2581 txn->status = 0;
2582 msg->msg_state = HTTP_MSG_RQBEFORE;
2583 req->analysers = 0;
2584 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002585 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002586 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002587 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002588 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002590
Willy Tarreaud787e662009-07-07 10:14:51 +02002591 /* OK now we have a complete HTTP request with indexed headers. Let's
2592 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002593 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002594 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002595 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002596 * byte after the last LF. msg->sov points to the first byte of data.
2597 * msg->eol cannot be trusted because it may have been left uninitialized
2598 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002599 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002600
Willy Tarreauda7ff642010-06-23 11:44:09 +02002601 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002602 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2603
Willy Tarreaub16a5742010-01-10 14:46:16 +01002604 if (txn->flags & TX_WAIT_NEXT_RQ) {
2605 /* kill the pending keep-alive timeout */
2606 txn->flags &= ~TX_WAIT_NEXT_RQ;
2607 req->analyse_exp = TICK_ETERNITY;
2608 }
2609
2610
Willy Tarreaud787e662009-07-07 10:14:51 +02002611 /* Maybe we found in invalid header name while we were configured not
2612 * to block on that, so we have to capture it now.
2613 */
2614 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002615 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 /*
2618 * 1: identify the method
2619 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002620 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002621
2622 /* we can make use of server redirect on GET and HEAD */
2623 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2624 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 /*
2627 * 2: check if the URI matches the monitor_uri.
2628 * We have to do this for every request which gets in, because
2629 * the monitor-uri is defined by the frontend.
2630 */
2631 if (unlikely((s->fe->monitor_uri_len != 0) &&
2632 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002633 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 s->fe->monitor_uri,
2635 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002636 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002637 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002638 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002640
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002642 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002645 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002646 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 ret = acl_pass(ret);
2649 if (cond->pol == ACL_COND_UNLESS)
2650 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002651
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 if (ret) {
2653 /* we fail this request, let's return 503 service unavail */
2654 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002655 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002656 if (!(s->flags & SN_ERR_MASK))
2657 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002659 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002660 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002661
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 /* nothing to fail, let's reply normaly */
2663 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002664 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002665 if (!(s->flags & SN_ERR_MASK))
2666 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 goto return_prx_cond;
2668 }
2669
2670 /*
2671 * 3: Maybe we have to copy the original REQURI for the logs ?
2672 * Note: we cannot log anymore if the request has been
2673 * classified as invalid.
2674 */
2675 if (unlikely(s->logs.logwait & LW_REQ)) {
2676 /* we have a complete HTTP request that we must log */
2677 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2678 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 if (urilen >= REQURI_LEN)
2681 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002682 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002684
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002685 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002686 s->do_log(s);
2687 } else {
2688 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002689 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 }
Willy Tarreau06619262006-12-17 08:37:22 +01002691
Willy Tarreau59234e92008-11-30 23:51:27 +01002692 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002693 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002694 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002695
Willy Tarreau5b154472009-12-21 20:11:07 +01002696 /* ... and check if the request is HTTP/1.1 or above */
2697 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002698 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2699 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2700 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002701 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002702
2703 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002704 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 +01002705
Willy Tarreau88d349d2010-01-25 12:15:43 +01002706 /* if the frontend has "option http-use-proxy-header", we'll check if
2707 * we have what looks like a proxied connection instead of a connection,
2708 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2709 * Note that this is *not* RFC-compliant, however browsers and proxies
2710 * happen to do that despite being non-standard :-(
2711 * We consider that a request not beginning with either '/' or '*' is
2712 * a proxied connection, which covers both "scheme://location" and
2713 * CONNECT ip:port.
2714 */
2715 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002716 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002717 txn->flags |= TX_USE_PX_CONN;
2718
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002719 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002720 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002721
Willy Tarreau59234e92008-11-30 23:51:27 +01002722 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002723 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002724 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002726
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002727 /* 6: determine the transfer-length.
2728 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2729 * the presence of a message-body in a REQUEST and its transfer length
2730 * must be determined that way (in order of precedence) :
2731 * 1. The presence of a message-body in a request is signaled by the
2732 * inclusion of a Content-Length or Transfer-Encoding header field
2733 * in the request's header fields. When a request message contains
2734 * both a message-body of non-zero length and a method that does
2735 * not define any semantics for that request message-body, then an
2736 * origin server SHOULD either ignore the message-body or respond
2737 * with an appropriate error message (e.g., 413). A proxy or
2738 * gateway, when presented the same request, SHOULD either forward
2739 * the request inbound with the message- body or ignore the
2740 * message-body when determining a response.
2741 *
2742 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2743 * and the "chunked" transfer-coding (Section 6.2) is used, the
2744 * transfer-length is defined by the use of this transfer-coding.
2745 * If a Transfer-Encoding header field is present and the "chunked"
2746 * transfer-coding is not present, the transfer-length is defined
2747 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002748 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002749 * 3. If a Content-Length header field is present, its decimal value in
2750 * OCTETs represents both the entity-length and the transfer-length.
2751 * If a message is received with both a Transfer-Encoding header
2752 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002753 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002754 * 4. By the server closing the connection. (Closing the connection
2755 * cannot be used to indicate the end of a request body, since that
2756 * would leave no possibility for the server to send back a response.)
2757 *
2758 * Whenever a transfer-coding is applied to a message-body, the set of
2759 * transfer-codings MUST include "chunked", unless the message indicates
2760 * it is terminated by closing the connection. When the "chunked"
2761 * transfer-coding is used, it MUST be the last transfer-coding applied
2762 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002763 */
2764
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002765 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002766 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002767 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002768 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002769 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002770 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002771 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2772 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002773 /* bad transfer-encoding (chunked followed by something else) */
2774 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002775 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002776 break;
2777 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002778 }
2779
Willy Tarreau32b47f42009-10-18 20:55:02 +02002780 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002781 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002782 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002783 signed long long cl;
2784
Willy Tarreauad14f752011-09-02 20:33:27 +02002785 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002786 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002787 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002788 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002789
Willy Tarreauad14f752011-09-02 20:33:27 +02002790 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002791 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002792 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002793 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002794
Willy Tarreauad14f752011-09-02 20:33:27 +02002795 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002796 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002797 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002798 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002799
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002800 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002801 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002802 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002803 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002804
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002805 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002806 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002807 }
2808
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002809 /* bodyless requests have a known length */
2810 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002811 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002812
Willy Tarreaud787e662009-07-07 10:14:51 +02002813 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002814 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002815 req->analyse_exp = TICK_ETERNITY;
2816 return 1;
2817
2818 return_bad_req:
2819 /* We centralize bad requests processing here */
2820 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2821 /* we detected a parsing error. We want to archive this request
2822 * in the dedicated proxy area for later troubleshooting.
2823 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002824 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002825 }
2826
2827 txn->req.msg_state = HTTP_MSG_ERROR;
2828 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002829 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002830
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002831 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002832 if (s->listener->counters)
2833 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002834
2835 return_prx_cond:
2836 if (!(s->flags & SN_ERR_MASK))
2837 s->flags |= SN_ERR_PRXCOND;
2838 if (!(s->flags & SN_FINST_MASK))
2839 s->flags |= SN_FINST_R;
2840
2841 req->analysers = 0;
2842 req->analyse_exp = TICK_ETERNITY;
2843 return 0;
2844}
2845
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002846
Willy Tarreau347a35d2013-11-22 17:51:09 +01002847/* This function prepares an applet to handle the stats. It can deal with the
2848 * "100-continue" expectation, check that admin rules are met for POST requests,
2849 * and program a response message if something was unexpected. It cannot fail
2850 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002851 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002852 * s->target which is supposed to already point to the stats applet. The caller
2853 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002854 */
2855int http_handle_stats(struct session *s, struct channel *req)
2856{
2857 struct stats_admin_rule *stats_admin_rule;
2858 struct stream_interface *si = s->rep->prod;
2859 struct http_txn *txn = &s->txn;
2860 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002861 struct uri_auth *uri_auth = s->be->uri_auth;
2862 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002863 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002864
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002865 appctx = si_appctx(si);
2866 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2867 appctx->st1 = appctx->st2 = 0;
2868 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2869 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002870
2871 uri = msg->chn->buf->p + msg->sl.rq.u;
2872 lookup = uri + uri_auth->uri_len;
2873
2874 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2875 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002876 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002877 break;
2878 }
2879 }
2880
2881 if (uri_auth->refresh) {
2882 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2883 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002884 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002885 break;
2886 }
2887 }
2888 }
2889
2890 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2891 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002892 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002893 break;
2894 }
2895 }
2896
2897 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2898 if (memcmp(h, ";st=", 4) == 0) {
2899 int i;
2900 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002901 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002902 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2903 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002904 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002905 break;
2906 }
2907 }
2908 break;
2909 }
2910 }
2911
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002912 appctx->ctx.stats.scope_str = 0;
2913 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002914 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2915 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2916 int itx = 0;
2917 const char *h2;
2918 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2919 const char *err;
2920
2921 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2922 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002923 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002924 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2925 itx++;
2926 h++;
2927 }
2928
2929 if (itx > STAT_SCOPE_TXT_MAXLEN)
2930 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002931 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002932
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002933 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002934 memcpy(scope_txt, h2, itx);
2935 scope_txt[itx] = '\0';
2936 err = invalid_char(scope_txt);
2937 if (err) {
2938 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002939 appctx->ctx.stats.scope_str = 0;
2940 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002941 }
2942 break;
2943 }
2944 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002945
2946 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002947 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002948 int ret = 1;
2949
2950 if (stats_admin_rule->cond) {
2951 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2952 ret = acl_pass(ret);
2953 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2954 ret = !ret;
2955 }
2956
2957 if (ret) {
2958 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002959 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002960 break;
2961 }
2962 }
2963
2964 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002965 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002966 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002967 if (msg->msg_state < HTTP_MSG_100_SENT) {
2968 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2969 * send an HTTP/1.1 100 Continue intermediate response.
2970 */
2971 if (msg->flags & HTTP_MSGF_VER_11) {
2972 struct hdr_ctx ctx;
2973 ctx.idx = 0;
2974 /* Expect is allowed in 1.1, look for it */
2975 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2976 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2977 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2978 }
2979 }
2980 msg->msg_state = HTTP_MSG_100_SENT;
2981 s->logs.tv_request = now; /* update the request timer to reflect full request */
2982 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002983 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002984 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002985 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002986 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2987 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002988 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002989 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002990 else {
2991 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002992 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002993 }
2994
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002995 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002996 return 1;
2997}
2998
Lukas Tribus67db8df2013-06-23 17:37:13 +02002999/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3000 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3001 */
3002static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3003{
3004#ifdef IP_TOS
3005 if (from.ss_family == AF_INET)
3006 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3007#endif
3008#ifdef IPV6_TCLASS
3009 if (from.ss_family == AF_INET6) {
3010 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3011 /* v4-mapped addresses need IP_TOS */
3012 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3013 else
3014 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3015 }
3016#endif
3017}
3018
Willy Tarreau20b0de52012-12-24 15:45:22 +01003019/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003020 * transaction <txn>. Returns the first rule that prevents further processing
3021 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3022 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3023 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003024 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003025static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003026http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003027{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003028 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003029 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003030 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003031
Willy Tarreauff011f22011-01-06 17:51:27 +01003032 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003033 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003034 continue;
3035
Willy Tarreau96257ec2012-12-27 10:46:37 +01003036 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003037 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003038 int ret;
3039
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003040 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003041 ret = acl_pass(ret);
3042
Willy Tarreauff011f22011-01-06 17:51:27 +01003043 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003044 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003045
3046 if (!ret) /* condition not matched */
3047 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003048 }
3049
Willy Tarreau20b0de52012-12-24 15:45:22 +01003050
Willy Tarreau96257ec2012-12-27 10:46:37 +01003051 switch (rule->action) {
3052 case HTTP_REQ_ACT_ALLOW:
3053 return NULL; /* "allow" rules are OK */
3054
3055 case HTTP_REQ_ACT_DENY:
3056 txn->flags |= TX_CLDENY;
3057 return rule;
3058
Willy Tarreauccbcc372012-12-27 12:37:57 +01003059 case HTTP_REQ_ACT_TARPIT:
3060 txn->flags |= TX_CLTARPIT;
3061 return rule;
3062
Willy Tarreau96257ec2012-12-27 10:46:37 +01003063 case HTTP_REQ_ACT_AUTH:
3064 return rule;
3065
Willy Tarreau81499eb2012-12-27 12:19:02 +01003066 case HTTP_REQ_ACT_REDIR:
3067 return rule;
3068
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003069 case HTTP_REQ_ACT_SET_NICE:
3070 s->task->nice = rule->arg.nice;
3071 break;
3072
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003073 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003074 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003075 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003076 break;
3077
Willy Tarreau51347ed2013-06-11 19:34:13 +02003078 case HTTP_REQ_ACT_SET_MARK:
3079#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003080 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003081 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003082#endif
3083 break;
3084
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003085 case HTTP_REQ_ACT_SET_LOGL:
3086 s->logs.level = rule->arg.loglevel;
3087 break;
3088
Willy Tarreau96257ec2012-12-27 10:46:37 +01003089 case HTTP_REQ_ACT_SET_HDR:
3090 ctx.idx = 0;
3091 /* remove all occurrences of the header */
3092 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3093 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3094 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003095 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003096 /* now fall through to header addition */
3097
3098 case HTTP_REQ_ACT_ADD_HDR:
3099 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3100 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3101 trash.len = rule->arg.hdr_add.name_len;
3102 trash.str[trash.len++] = ':';
3103 trash.str[trash.len++] = ' ';
3104 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3105 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3106 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003107 }
3108 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003109
3110 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003111 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003112}
3113
Willy Tarreau71241ab2012-12-27 11:30:54 +01003114
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003115/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3116 * transaction <txn>. Returns the first rule that prevents further processing
3117 * of the response (deny, ...) or NULL if it executed all rules or stopped
3118 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3119 * rule.
3120 */
3121static struct http_res_rule *
3122http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3123{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003124 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003125 struct http_res_rule *rule;
3126 struct hdr_ctx ctx;
3127
3128 list_for_each_entry(rule, rules, list) {
3129 if (rule->action >= HTTP_RES_ACT_MAX)
3130 continue;
3131
3132 /* check optional condition */
3133 if (rule->cond) {
3134 int ret;
3135
3136 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3137 ret = acl_pass(ret);
3138
3139 if (rule->cond->pol == ACL_COND_UNLESS)
3140 ret = !ret;
3141
3142 if (!ret) /* condition not matched */
3143 continue;
3144 }
3145
3146
3147 switch (rule->action) {
3148 case HTTP_RES_ACT_ALLOW:
3149 return NULL; /* "allow" rules are OK */
3150
3151 case HTTP_RES_ACT_DENY:
3152 txn->flags |= TX_SVDENY;
3153 return rule;
3154
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003155 case HTTP_RES_ACT_SET_NICE:
3156 s->task->nice = rule->arg.nice;
3157 break;
3158
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003159 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003160 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003161 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003162 break;
3163
Willy Tarreau51347ed2013-06-11 19:34:13 +02003164 case HTTP_RES_ACT_SET_MARK:
3165#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003166 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003167 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003168#endif
3169 break;
3170
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003171 case HTTP_RES_ACT_SET_LOGL:
3172 s->logs.level = rule->arg.loglevel;
3173 break;
3174
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003175 case HTTP_RES_ACT_SET_HDR:
3176 ctx.idx = 0;
3177 /* remove all occurrences of the header */
3178 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3179 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3180 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3181 }
3182 /* now fall through to header addition */
3183
3184 case HTTP_RES_ACT_ADD_HDR:
3185 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3186 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3187 trash.len = rule->arg.hdr_add.name_len;
3188 trash.str[trash.len++] = ':';
3189 trash.str[trash.len++] = ' ';
3190 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3191 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3192 break;
3193 }
3194 }
3195
3196 /* we reached the end of the rules, nothing to report */
3197 return NULL;
3198}
3199
3200
Willy Tarreau71241ab2012-12-27 11:30:54 +01003201/* Perform an HTTP redirect based on the information in <rule>. The function
3202 * returns non-zero on success, or zero in case of a, irrecoverable error such
3203 * as too large a request to build a valid response.
3204 */
3205static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3206{
3207 struct http_msg *msg = &txn->req;
3208 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003209 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003210
3211 /* build redirect message */
3212 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003213 case 308:
3214 msg_fmt = HTTP_308;
3215 break;
3216 case 307:
3217 msg_fmt = HTTP_307;
3218 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003219 case 303:
3220 msg_fmt = HTTP_303;
3221 break;
3222 case 301:
3223 msg_fmt = HTTP_301;
3224 break;
3225 case 302:
3226 default:
3227 msg_fmt = HTTP_302;
3228 break;
3229 }
3230
3231 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3232 return 0;
3233
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003234 location = trash.str + trash.len;
3235
Willy Tarreau71241ab2012-12-27 11:30:54 +01003236 switch(rule->type) {
3237 case REDIRECT_TYPE_SCHEME: {
3238 const char *path;
3239 const char *host;
3240 struct hdr_ctx ctx;
3241 int pathlen;
3242 int hostlen;
3243
3244 host = "";
3245 hostlen = 0;
3246 ctx.idx = 0;
3247 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3248 host = ctx.line + ctx.val;
3249 hostlen = ctx.vlen;
3250 }
3251
3252 path = http_get_path(txn);
3253 /* build message using path */
3254 if (path) {
3255 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3256 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3257 int qs = 0;
3258 while (qs < pathlen) {
3259 if (path[qs] == '?') {
3260 pathlen = qs;
3261 break;
3262 }
3263 qs++;
3264 }
3265 }
3266 } else {
3267 path = "/";
3268 pathlen = 1;
3269 }
3270
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003271 if (rule->rdr_str) { /* this is an old "redirect" rule */
3272 /* check if we can add scheme + "://" + host + path */
3273 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3274 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003275
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003276 /* add scheme */
3277 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3278 trash.len += rule->rdr_len;
3279 }
3280 else {
3281 /* add scheme with executing log format */
3282 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003283
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003284 /* check if we can add scheme + "://" + host + path */
3285 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3286 return 0;
3287 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003288 /* add "://" */
3289 memcpy(trash.str + trash.len, "://", 3);
3290 trash.len += 3;
3291
3292 /* add host */
3293 memcpy(trash.str + trash.len, host, hostlen);
3294 trash.len += hostlen;
3295
3296 /* add path */
3297 memcpy(trash.str + trash.len, path, pathlen);
3298 trash.len += pathlen;
3299
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003300 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003301 if (trash.len && trash.str[trash.len - 1] != '/' &&
3302 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3303 if (trash.len > trash.size - 5)
3304 return 0;
3305 trash.str[trash.len] = '/';
3306 trash.len++;
3307 }
3308
3309 break;
3310 }
3311 case REDIRECT_TYPE_PREFIX: {
3312 const char *path;
3313 int pathlen;
3314
3315 path = http_get_path(txn);
3316 /* build message using path */
3317 if (path) {
3318 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3319 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3320 int qs = 0;
3321 while (qs < pathlen) {
3322 if (path[qs] == '?') {
3323 pathlen = qs;
3324 break;
3325 }
3326 qs++;
3327 }
3328 }
3329 } else {
3330 path = "/";
3331 pathlen = 1;
3332 }
3333
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003334 if (rule->rdr_str) { /* this is an old "redirect" rule */
3335 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3336 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003337
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003338 /* add prefix. Note that if prefix == "/", we don't want to
3339 * add anything, otherwise it makes it hard for the user to
3340 * configure a self-redirection.
3341 */
3342 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3343 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3344 trash.len += rule->rdr_len;
3345 }
3346 }
3347 else {
3348 /* add prefix with executing log format */
3349 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3350
3351 /* Check length */
3352 if (trash.len + pathlen > trash.size - 4)
3353 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003354 }
3355
3356 /* add path */
3357 memcpy(trash.str + trash.len, path, pathlen);
3358 trash.len += pathlen;
3359
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003360 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003361 if (trash.len && trash.str[trash.len - 1] != '/' &&
3362 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3363 if (trash.len > trash.size - 5)
3364 return 0;
3365 trash.str[trash.len] = '/';
3366 trash.len++;
3367 }
3368
3369 break;
3370 }
3371 case REDIRECT_TYPE_LOCATION:
3372 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003373 if (rule->rdr_str) { /* this is an old "redirect" rule */
3374 if (trash.len + rule->rdr_len > trash.size - 4)
3375 return 0;
3376
3377 /* add location */
3378 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3379 trash.len += rule->rdr_len;
3380 }
3381 else {
3382 /* add location with executing log format */
3383 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003384
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003385 /* Check left length */
3386 if (trash.len > trash.size - 4)
3387 return 0;
3388 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003389 break;
3390 }
3391
3392 if (rule->cookie_len) {
3393 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3394 trash.len += 14;
3395 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3396 trash.len += rule->cookie_len;
3397 memcpy(trash.str + trash.len, "\r\n", 2);
3398 trash.len += 2;
3399 }
3400
3401 /* add end of headers and the keep-alive/close status.
3402 * We may choose to set keep-alive if the Location begins
3403 * with a slash, because the client will come back to the
3404 * same server.
3405 */
3406 txn->status = rule->code;
3407 /* let's log the request time */
3408 s->logs.tv_request = now;
3409
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003410 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003411 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3412 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3413 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3414 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3415 /* keep-alive possible */
3416 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3417 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3418 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3419 trash.len += 30;
3420 } else {
3421 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3422 trash.len += 24;
3423 }
3424 }
3425 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3426 trash.len += 4;
3427 bo_inject(txn->rsp.chn, trash.str, trash.len);
3428 /* "eat" the request */
3429 bi_fast_delete(txn->req.chn->buf, msg->sov);
3430 msg->sov = 0;
3431 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3432 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3433 txn->req.msg_state = HTTP_MSG_CLOSED;
3434 txn->rsp.msg_state = HTTP_MSG_DONE;
3435 } else {
3436 /* keep-alive not possible */
3437 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3438 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3439 trash.len += 29;
3440 } else {
3441 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3442 trash.len += 23;
3443 }
3444 stream_int_retnclose(txn->req.chn->prod, &trash);
3445 txn->req.chn->analysers = 0;
3446 }
3447
3448 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003449 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003450 if (!(s->flags & SN_FINST_MASK))
3451 s->flags |= SN_FINST_R;
3452
3453 return 1;
3454}
3455
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456/* This stream analyser runs all HTTP request processing which is common to
3457 * frontends and backends, which means blocking ACLs, filters, connection-close,
3458 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003459 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003460 * either needs more data or wants to immediately abort the request (eg: deny,
3461 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003462 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003463int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003464{
Willy Tarreaud787e662009-07-07 10:14:51 +02003465 struct http_txn *txn = &s->txn;
3466 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003467 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003468 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003469 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003470 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003471
Willy Tarreau655dce92009-11-08 13:10:58 +01003472 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003473 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003474 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003475 return 0;
3476 }
3477
Willy Tarreau3a816292009-07-07 10:55:49 +02003478 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003479 req->analyse_exp = TICK_ETERNITY;
3480
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003481 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 +02003482 now_ms, __FUNCTION__,
3483 s,
3484 req,
3485 req->rex, req->wex,
3486 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003487 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003488 req->analysers);
3489
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003490 /* first check whether we have some ACLs set to block this request */
3491 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003492 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003493
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003494 ret = acl_pass(ret);
3495 if (cond->pol == ACL_COND_UNLESS)
3496 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003497
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003498 if (ret) {
3499 txn->status = 403;
3500 /* let's log the request time */
3501 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003502 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003503 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003504 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003505 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003506 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003507
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003508 /* just in case we have some per-backend tracking */
3509 session_inc_be_http_req_ctr(s);
3510
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003511 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003512 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003513
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003514 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003515 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003516 if (stats_check_uri(s->rep->prod, txn, px)) {
3517 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003518 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3519 txn->status = 500;
3520 s->logs.tv_request = now;
3521 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003522
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003523 if (!(s->flags & SN_ERR_MASK))
3524 s->flags |= SN_ERR_RESOURCE;
3525 goto return_prx_cond;
3526 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003527 /* parse the whole stats request and extract the relevant information */
3528 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003529 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 +01003530 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003531 }
3532
Willy Tarreau3b44e722013-11-16 10:28:23 +01003533 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3534 * blocked by an http-request rule.
3535 */
3536 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003537 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003538 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003539 }
Willy Tarreau06619262006-12-17 08:37:22 +01003540
Willy Tarreau3b44e722013-11-16 10:28:23 +01003541 /* return a 403 if either rule has blocked */
3542 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003543 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003544 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003545 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003546 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003547 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003548 s->fe->fe_counters.denied_req++;
3549 if (s->fe != s->be)
3550 s->be->be_counters.denied_req++;
3551 if (s->listener->counters)
3552 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003553 goto return_prx_cond;
3554 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003555
3556 /* When a connection is tarpitted, we use the tarpit timeout,
3557 * which may be the same as the connect timeout if unspecified.
3558 * If unset, then set it to zero because we really want it to
3559 * eventually expire. We build the tarpit as an analyser.
3560 */
3561 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003562 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003563 /* wipe the request out so that we can drop the connection early
3564 * if the client closes first.
3565 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003566 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003567 req->analysers = 0; /* remove switching rules etc... */
3568 req->analysers |= AN_REQ_HTTP_TARPIT;
3569 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3570 if (!req->analyse_exp)
3571 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003572 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003573 s->fe->fe_counters.denied_req++;
3574 if (s->fe != s->be)
3575 s->be->be_counters.denied_req++;
3576 if (s->listener->counters)
3577 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003578 return 1;
3579 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003580 }
Willy Tarreau06619262006-12-17 08:37:22 +01003581
Willy Tarreau70dffda2014-01-30 03:07:23 +01003582 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003583 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003584 * Option httpclose by itself sets tunnel mode where headers are mangled.
3585 * However, if another mode is set, it will affect it (eg: server-close/
3586 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3587 * if FE and BE have the same settings (common). The method consists in
3588 * checking if options changed between the two calls (implying that either
3589 * one is non-null, or one of them is non-null and we are there for the first
3590 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003591 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003592
Willy Tarreau416ce612014-01-31 15:45:34 +01003593 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003594 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003595 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003596
Willy Tarreau70dffda2014-01-30 03:07:23 +01003597 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3598 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3599 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3600 tmp = TX_CON_WANT_TUN;
3601
3602 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3603 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3604 tmp = TX_CON_WANT_TUN;
3605 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003606
3607 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003608 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3609 /* option httpclose + server_close => forceclose */
3610 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3611 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3612 tmp = TX_CON_WANT_CLO;
3613 else
3614 tmp = TX_CON_WANT_SCL;
3615 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003616
3617 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3618 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003619 tmp = TX_CON_WANT_CLO;
3620
Willy Tarreau5b154472009-12-21 20:11:07 +01003621 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3622 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003623
Willy Tarreau416ce612014-01-31 15:45:34 +01003624 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3625 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003626 /* parse the Connection header and possibly clean it */
3627 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003628 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003629 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3630 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003631 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003632 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003633 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003634 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003635 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003636
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003637 /* check if client or config asks for explicit close in KAL/SCL */
3638 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3639 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3640 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003641 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003642 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003643 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003644 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3645 }
Willy Tarreau78599912009-10-17 20:12:21 +02003646
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003647 /* we can be blocked here because the request needs to be authenticated,
3648 * either to pass or to access stats.
3649 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003650 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003651 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003652
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003653 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003654 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003655
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003656 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003657 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003658 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003659 /* on 401 we still count one error, because normal browsing
3660 * won't significantly increase the counter but brute force
3661 * attempts will.
3662 */
3663 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003664 goto return_prx_cond;
3665 }
3666
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003667 /* add request headers from the rule sets in the same order */
3668 list_for_each_entry(wl, &px->req_add, list) {
3669 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003670 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003671 ret = acl_pass(ret);
3672 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3673 ret = !ret;
3674 if (!ret)
3675 continue;
3676 }
3677
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003678 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003679 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003680 }
3681
3682 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3683 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3684 goto return_bad_req;
3685 req->analyse_exp = TICK_ETERNITY;
3686 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003687 }
3688
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003689 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003690 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003691 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3692 s->fe->fe_counters.intercepted_req++;
3693
3694 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3695 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3696 if (!(s->flags & SN_FINST_MASK))
3697 s->flags |= SN_FINST_R;
3698
3699 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003700 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003701 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003702 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003703
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003704 /* check whether we have some ACLs set to redirect this request */
3705 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003706 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003707 int ret;
3708
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003709 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003710 ret = acl_pass(ret);
3711 if (rule->cond->pol == ACL_COND_UNLESS)
3712 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003713 if (!ret)
3714 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003715 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003716 if (!http_apply_redirect_rule(rule, s, txn))
3717 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003718
Willy Tarreau71241ab2012-12-27 11:30:54 +01003719 req->analyse_exp = TICK_ETERNITY;
3720 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003721 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003722
Willy Tarreau2be39392010-01-03 17:24:51 +01003723 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3724 * If this happens, then the data will not come immediately, so we must
3725 * send all what we have without waiting. Note that due to the small gain
3726 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003727 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003728 * itself once used.
3729 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003730 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003731
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003732 /* that's OK for us now, let's move on to next analysers */
3733 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003734
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003735 return_bad_req:
3736 /* We centralize bad requests processing here */
3737 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3738 /* we detected a parsing error. We want to archive this request
3739 * in the dedicated proxy area for later troubleshooting.
3740 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003741 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003742 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003743
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003744 txn->req.msg_state = HTTP_MSG_ERROR;
3745 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003746 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003747
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003748 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003749 if (s->listener->counters)
3750 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003752 return_prx_cond:
3753 if (!(s->flags & SN_ERR_MASK))
3754 s->flags |= SN_ERR_PRXCOND;
3755 if (!(s->flags & SN_FINST_MASK))
3756 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003757
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003758 req->analysers = 0;
3759 req->analyse_exp = TICK_ETERNITY;
3760 return 0;
3761}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003762
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003763/* This function performs all the processing enabled for the current request.
3764 * It returns 1 if the processing can continue on next analysers, or zero if it
3765 * needs more data, encounters an error, or wants to immediately abort the
3766 * request. It relies on buffers flags, and updates s->req->analysers.
3767 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003768int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003769{
3770 struct http_txn *txn = &s->txn;
3771 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003772 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003773
Willy Tarreau655dce92009-11-08 13:10:58 +01003774 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003775 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003776 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003777 return 0;
3778 }
3779
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003780 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 +02003781 now_ms, __FUNCTION__,
3782 s,
3783 req,
3784 req->rex, req->wex,
3785 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003786 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003787 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003788
William Lallemand82fe75c2012-10-23 10:25:10 +02003789 if (s->fe->comp || s->be->comp)
3790 select_compression_request_header(s, req->buf);
3791
Willy Tarreau59234e92008-11-30 23:51:27 +01003792 /*
3793 * Right now, we know that we have processed the entire headers
3794 * and that unwanted requests have been filtered out. We can do
3795 * whatever we want with the remaining request. Also, now we
3796 * may have separate values for ->fe, ->be.
3797 */
Willy Tarreau06619262006-12-17 08:37:22 +01003798
Willy Tarreau59234e92008-11-30 23:51:27 +01003799 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003800 * If HTTP PROXY is set we simply get remote server address parsing
3801 * incoming request. Note that this requires that a connection is
3802 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 */
3804 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003805 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003806 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003807
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003808 /* Note that for now we don't reuse existing proxy connections */
3809 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003810 txn->req.msg_state = HTTP_MSG_ERROR;
3811 txn->status = 500;
3812 req->analysers = 0;
3813 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3814
3815 if (!(s->flags & SN_ERR_MASK))
3816 s->flags |= SN_ERR_RESOURCE;
3817 if (!(s->flags & SN_FINST_MASK))
3818 s->flags |= SN_FINST_R;
3819
3820 return 0;
3821 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003822
3823 path = http_get_path(txn);
3824 url2sa(req->buf->p + msg->sl.rq.u,
3825 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3826 &conn->addr.to);
3827 /* if the path was found, we have to remove everything between
3828 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3829 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3830 * u_l characters by a single "/".
3831 */
3832 if (path) {
3833 char *cur_ptr = req->buf->p;
3834 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3835 int delta;
3836
3837 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3838 http_msg_move_end(&txn->req, delta);
3839 cur_end += delta;
3840 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3841 goto return_bad_req;
3842 }
3843 else {
3844 char *cur_ptr = req->buf->p;
3845 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3846 int delta;
3847
3848 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3849 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3850 http_msg_move_end(&txn->req, delta);
3851 cur_end += delta;
3852 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3853 goto return_bad_req;
3854 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003855 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003856
Willy Tarreau59234e92008-11-30 23:51:27 +01003857 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003858 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003859 * Note that doing so might move headers in the request, but
3860 * the fields will stay coherent and the URI will not move.
3861 * This should only be performed in the backend.
3862 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003863 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003864 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3865 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003866
Willy Tarreau59234e92008-11-30 23:51:27 +01003867 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003868 * 8: the appsession cookie was looked up very early in 1.2,
3869 * so let's do the same now.
3870 */
3871
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003872 /* It needs to look into the URI unless persistence must be ignored */
3873 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003874 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 +01003875 }
3876
William Lallemanda73203e2012-03-12 12:48:57 +01003877 /* add unique-id if "header-unique-id" is specified */
3878
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003879 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3880 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3881 goto return_bad_req;
3882 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003883 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003884 }
William Lallemanda73203e2012-03-12 12:48:57 +01003885
3886 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003887 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3888 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003889 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003890 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003891 goto return_bad_req;
3892 }
3893
Cyril Bontéb21570a2009-11-29 20:04:48 +01003894 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003895 * 9: add X-Forwarded-For if either the frontend or the backend
3896 * asks for it.
3897 */
3898 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003899 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003900 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003901 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3902 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003903 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003904 /* The header is set to be added only if none is present
3905 * and we found it, so don't do anything.
3906 */
3907 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003908 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003909 /* Add an X-Forwarded-For header unless the source IP is
3910 * in the 'except' network range.
3911 */
3912 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003913 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003914 != s->fe->except_net.s_addr) &&
3915 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003916 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003917 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003918 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003919 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003920 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003921
3922 /* Note: we rely on the backend to get the header name to be used for
3923 * x-forwarded-for, because the header is really meant for the backends.
3924 * However, if the backend did not specify any option, we have to rely
3925 * on the frontend's header name.
3926 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003927 if (s->be->fwdfor_hdr_len) {
3928 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003929 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003930 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003931 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003932 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003933 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003934 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003935
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003936 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003937 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003938 }
3939 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003940 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003941 /* FIXME: for the sake of completeness, we should also support
3942 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003943 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003944 int len;
3945 char pn[INET6_ADDRSTRLEN];
3946 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003947 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003948 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003949
Willy Tarreau59234e92008-11-30 23:51:27 +01003950 /* Note: we rely on the backend to get the header name to be used for
3951 * x-forwarded-for, because the header is really meant for the backends.
3952 * However, if the backend did not specify any option, we have to rely
3953 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003954 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003955 if (s->be->fwdfor_hdr_len) {
3956 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003957 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003958 } else {
3959 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003960 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003961 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003962 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003963
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003964 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 goto return_bad_req;
3966 }
3967 }
3968
3969 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003970 * 10: add X-Original-To if either the frontend or the backend
3971 * asks for it.
3972 */
3973 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3974
3975 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003976 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003977 /* Add an X-Original-To header unless the destination IP is
3978 * in the 'except' network range.
3979 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003980 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003981
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003982 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003983 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003984 (((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 +02003985 != s->fe->except_to.s_addr) &&
3986 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003987 (((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 +02003988 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003989 int len;
3990 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003991 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003992
3993 /* Note: we rely on the backend to get the header name to be used for
3994 * x-original-to, because the header is really meant for the backends.
3995 * However, if the backend did not specify any option, we have to rely
3996 * on the frontend's header name.
3997 */
3998 if (s->be->orgto_hdr_len) {
3999 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004000 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004001 } else {
4002 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004003 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004004 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004005 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004006
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004007 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004008 goto return_bad_req;
4009 }
4010 }
4011 }
4012
Willy Tarreau50fc7772012-11-11 22:19:57 +01004013 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4014 * If an "Upgrade" token is found, the header is left untouched in order not to have
4015 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4016 * "Upgrade" is present in the Connection header.
4017 */
4018 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4019 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004020 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4021 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004022 unsigned int want_flags = 0;
4023
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004024 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004025 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004026 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4027 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004028 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004029 want_flags |= TX_CON_CLO_SET;
4030 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004031 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004032 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4033 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004034 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004035 want_flags |= TX_CON_KAL_SET;
4036 }
4037
4038 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004039 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004040 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004041
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004042
Willy Tarreau522d6c02009-12-06 18:49:18 +01004043 /* If we have no server assigned yet and we're balancing on url_param
4044 * with a POST request, we may be interested in checking the body for
4045 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004046 */
4047 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4048 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004049 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004050 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004051 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004052 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004053 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004054
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004055 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004056 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004057#ifdef TCP_QUICKACK
4058 /* We expect some data from the client. Unless we know for sure
4059 * we already have a full request, we have to re-enable quick-ack
4060 * in case we previously disabled it, otherwise we might cause
4061 * the client to delay further data.
4062 */
4063 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004064 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004065 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004066 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004067 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004068#endif
4069 }
Willy Tarreau03945942009-12-22 16:50:27 +01004070
Willy Tarreau59234e92008-11-30 23:51:27 +01004071 /*************************************************************
4072 * OK, that's finished for the headers. We have done what we *
4073 * could. Let's switch to the DATA state. *
4074 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004075 req->analyse_exp = TICK_ETERNITY;
4076 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004077
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004078 /* if the server closes the connection, we want to immediately react
4079 * and close the socket to save packets and syscalls.
4080 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004081 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4082 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004083
Willy Tarreau59234e92008-11-30 23:51:27 +01004084 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004085 /* OK let's go on with the BODY now */
4086 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004087
Willy Tarreau59234e92008-11-30 23:51:27 +01004088 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004089 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004090 /* we detected a parsing error. We want to archive this request
4091 * in the dedicated proxy area for later troubleshooting.
4092 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004093 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004094 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004095
Willy Tarreau59234e92008-11-30 23:51:27 +01004096 txn->req.msg_state = HTTP_MSG_ERROR;
4097 txn->status = 400;
4098 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004099 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004100
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004101 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004102 if (s->listener->counters)
4103 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004104
Willy Tarreau59234e92008-11-30 23:51:27 +01004105 if (!(s->flags & SN_ERR_MASK))
4106 s->flags |= SN_ERR_PRXCOND;
4107 if (!(s->flags & SN_FINST_MASK))
4108 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004109 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004110}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004111
Willy Tarreau60b85b02008-11-30 23:28:40 +01004112/* This function is an analyser which processes the HTTP tarpit. It always
4113 * returns zero, at the beginning because it prevents any other processing
4114 * from occurring, and at the end because it terminates the request.
4115 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004116int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004117{
4118 struct http_txn *txn = &s->txn;
4119
4120 /* This connection is being tarpitted. The CLIENT side has
4121 * already set the connect expiration date to the right
4122 * timeout. We just have to check that the client is still
4123 * there and that the timeout has not expired.
4124 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004125 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004126 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004127 !tick_is_expired(req->analyse_exp, now_ms))
4128 return 0;
4129
4130 /* We will set the queue timer to the time spent, just for
4131 * logging purposes. We fake a 500 server error, so that the
4132 * attacker will not suspect his connection has been tarpitted.
4133 * It will not cause trouble to the logs because we can exclude
4134 * the tarpitted connections by filtering on the 'PT' status flags.
4135 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004136 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4137
4138 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004139 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004140 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004141
4142 req->analysers = 0;
4143 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004144
Willy Tarreau60b85b02008-11-30 23:28:40 +01004145 if (!(s->flags & SN_ERR_MASK))
4146 s->flags |= SN_ERR_PRXCOND;
4147 if (!(s->flags & SN_FINST_MASK))
4148 s->flags |= SN_FINST_T;
4149 return 0;
4150}
4151
Willy Tarreaud34af782008-11-30 23:36:37 +01004152/* This function is an analyser which processes the HTTP request body. It looks
4153 * for parameters to be used for the load balancing algorithm (url_param). It
4154 * must only be called after the standard HTTP request processing has occurred,
4155 * because it expects the request to be parsed. It returns zero if it needs to
4156 * read more data, or 1 once it has completed its analysis.
4157 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004158int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004159{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004160 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004161 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004162 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004163
4164 /* We have to parse the HTTP request body to find any required data.
4165 * "balance url_param check_post" should have been the only way to get
4166 * into this. We were brought here after HTTP header analysis, so all
4167 * related structures are ready.
4168 */
4169
Willy Tarreau522d6c02009-12-06 18:49:18 +01004170 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4171 goto missing_data;
4172
4173 if (msg->msg_state < HTTP_MSG_100_SENT) {
4174 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4175 * send an HTTP/1.1 100 Continue intermediate response.
4176 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004177 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004178 struct hdr_ctx ctx;
4179 ctx.idx = 0;
4180 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004181 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004182 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004183 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184 }
4185 }
4186 msg->msg_state = HTTP_MSG_100_SENT;
4187 }
4188
4189 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004190 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004191 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004192 * is still null. We must save the body in msg->next because it
4193 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004194 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004195 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004196
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004197 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004198 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4199 else
4200 msg->msg_state = HTTP_MSG_DATA;
4201 }
4202
4203 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004204 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004205 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004206 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004207 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004208 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004209
Willy Tarreau115acb92009-12-26 13:56:06 +01004210 if (!ret)
4211 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004212 else if (ret < 0) {
4213 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004214 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004215 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004216 }
4217
Willy Tarreaud98cf932009-12-27 22:54:55 +01004218 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004219 * We have the first data byte is in msg->sov. We're waiting for at
4220 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004221 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004222
Willy Tarreau124d9912011-03-01 20:30:48 +01004223 if (msg->body_len < limit)
4224 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004225
Willy Tarreau9b28e032012-10-12 23:49:43 +02004226 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004227 goto http_end;
4228
4229 missing_data:
4230 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004231 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004232 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004233 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004234 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004235
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004236 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004237 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004238 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004239
4240 if (!(s->flags & SN_ERR_MASK))
4241 s->flags |= SN_ERR_CLITO;
4242 if (!(s->flags & SN_FINST_MASK))
4243 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004244 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004245 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004246
4247 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004248 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004249 /* Not enough data. We'll re-use the http-request
4250 * timeout here. Ideally, we should set the timeout
4251 * relative to the accept() date. We just set the
4252 * request timeout once at the beginning of the
4253 * request.
4254 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004255 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004256 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004257 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004258 return 0;
4259 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004260
4261 http_end:
4262 /* The situation will not evolve, so let's give up on the analysis. */
4263 s->logs.tv_request = now; /* update the request timer to reflect full request */
4264 req->analysers &= ~an_bit;
4265 req->analyse_exp = TICK_ETERNITY;
4266 return 1;
4267
4268 return_bad_req: /* let's centralize all bad requests */
4269 txn->req.msg_state = HTTP_MSG_ERROR;
4270 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004271 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004272
Willy Tarreau79ebac62010-06-07 13:47:49 +02004273 if (!(s->flags & SN_ERR_MASK))
4274 s->flags |= SN_ERR_PRXCOND;
4275 if (!(s->flags & SN_FINST_MASK))
4276 s->flags |= SN_FINST_R;
4277
Willy Tarreau522d6c02009-12-06 18:49:18 +01004278 return_err_msg:
4279 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004280 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004281 if (s->listener->counters)
4282 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004283 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004284}
4285
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004286/* send a server's name with an outgoing request over an established connection.
4287 * Note: this function is designed to be called once the request has been scheduled
4288 * for being forwarded. This is the reason why it rewinds the buffer before
4289 * proceeding.
4290 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004291int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004292
4293 struct hdr_ctx ctx;
4294
Mark Lamourinec2247f02012-01-04 13:02:01 -05004295 char *hdr_name = be->server_id_hdr_name;
4296 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004297 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004298 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004299 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004300
William Lallemandd9e90662012-01-30 17:27:17 +01004301 ctx.idx = 0;
4302
Willy Tarreau9b28e032012-10-12 23:49:43 +02004303 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004304 if (old_o) {
4305 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004306 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004307 }
4308
Willy Tarreau9b28e032012-10-12 23:49:43 +02004309 old_i = chn->buf->i;
4310 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 -05004311 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004312 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004313 }
4314
4315 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004316 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004317 memcpy(hdr_val, hdr_name, hdr_name_len);
4318 hdr_val += hdr_name_len;
4319 *hdr_val++ = ':';
4320 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004321 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4322 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004323
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004324 if (old_o) {
4325 /* If this was a forwarded request, we must readjust the amount of
4326 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004327 * variations. Note that if the request was already scheduled for
4328 * forwarding, it had its req->sol pointing to the body, which
4329 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004330 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004331 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004332 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004333 }
4334
Mark Lamourinec2247f02012-01-04 13:02:01 -05004335 return 0;
4336}
4337
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338/* Terminate current transaction and prepare a new one. This is very tricky
4339 * right now but it works.
4340 */
4341void http_end_txn_clean_session(struct session *s)
4342{
Willy Tarreau068621e2013-12-23 15:11:25 +01004343 int prev_status = s->txn.status;
4344
Willy Tarreau610ecce2010-01-04 21:15:02 +01004345 /* FIXME: We need a more portable way of releasing a backend's and a
4346 * server's connections. We need a safer way to reinitialize buffer
4347 * flags. We also need a more accurate method for computing per-request
4348 * data.
4349 */
4350 http_silent_debug(__LINE__, s);
4351
Willy Tarreau4213a112013-12-15 10:25:42 +01004352 /* unless we're doing keep-alive, we want to quickly close the connection
4353 * to the server.
4354 */
4355 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4356 !si_conn_ready(s->req->cons)) {
4357 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4358 si_shutr(s->req->cons);
4359 si_shutw(s->req->cons);
4360 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361
4362 http_silent_debug(__LINE__, s);
4363
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004364 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004365 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004366 if (unlikely(s->srv_conn))
4367 sess_change_server(s, NULL);
4368 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004369
4370 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4371 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004372 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373
4374 if (s->txn.status) {
4375 int n;
4376
4377 n = s->txn.status / 100;
4378 if (n < 1 || n > 5)
4379 n = 0;
4380
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004381 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004382 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004383 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004384 s->fe->fe_counters.p.http.comp_rsp++;
4385 }
Willy Tarreau24657792010-02-26 10:30:28 +01004386 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004387 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004388 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004389 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004390 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004391 s->be->be_counters.p.http.comp_rsp++;
4392 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004393 }
4394
4395 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004396 s->logs.bytes_in -= s->req->buf->i;
4397 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004398
4399 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004400 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401 !(s->flags & SN_MONITOR) &&
4402 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4403 s->do_log(s);
4404 }
4405
4406 s->logs.accept_date = date; /* user-visible date for logging */
4407 s->logs.tv_accept = now; /* corrected date for internal use */
4408 tv_zero(&s->logs.tv_request);
4409 s->logs.t_queue = -1;
4410 s->logs.t_connect = -1;
4411 s->logs.t_data = -1;
4412 s->logs.t_close = 0;
4413 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4414 s->logs.srv_queue_size = 0; /* we will get this number soon */
4415
Willy Tarreau9b28e032012-10-12 23:49:43 +02004416 s->logs.bytes_in = s->req->total = s->req->buf->i;
4417 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004418
4419 if (s->pend_pos)
4420 pendconn_free(s->pend_pos);
4421
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004422 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004423 if (s->flags & SN_CURR_SESS) {
4424 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004425 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004426 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004427 if (may_dequeue_tasks(objt_server(s->target), s->be))
4428 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 }
4430
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004431 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004432
Willy Tarreau4213a112013-12-15 10:25:42 +01004433 /* only release our endpoint if we don't intend to reuse the
4434 * connection.
4435 */
4436 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4437 !si_conn_ready(s->req->cons)) {
4438 si_release_endpoint(s->req->cons);
4439 }
4440
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004443 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004445 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004446 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4447 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 +02004448 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau36346242014-02-24 18:26:30 +01004449 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004450
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 s->txn.meth = 0;
4452 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004453 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004454
4455 if (prev_status == 401 || prev_status == 407) {
4456 /* In HTTP keep-alive mode, if we receive a 401, we still have
4457 * a chance of being able to send the visitor again to the same
4458 * server over the same connection. This is required by some
4459 * broken protocols such as NTLM, and anyway whenever there is
4460 * an opportunity for sending the challenge to the proper place,
4461 * it's better to do it (at least it helps with debugging).
4462 */
4463 s->txn.flags |= TX_PREFER_LAST;
4464 }
4465
Willy Tarreauee55dc02010-06-01 10:56:34 +02004466 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 s->req->cons->flags |= SI_FL_INDEP_STR;
4468
Willy Tarreau96e31212011-05-30 18:10:30 +02004469 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004470 s->req->flags |= CF_NEVER_WAIT;
4471 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004472 }
4473
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474 /* if the request buffer is not empty, it means we're
4475 * about to process another request, so send pending
4476 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004477 * Just don't do this if the buffer is close to be full,
4478 * because the request will wait for it to flush a little
4479 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004481 if (s->req->buf->i) {
4482 if (s->rep->buf->o &&
4483 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4484 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004485 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004486 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004487
4488 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004489 channel_auto_read(s->req);
4490 channel_auto_close(s->req);
4491 channel_auto_read(s->rep);
4492 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493
Willy Tarreau27375622013-12-17 00:00:28 +01004494 /* we're in keep-alive with an idle connection, monitor it */
4495 si_idle_conn(s->req->cons);
4496
Willy Tarreau342b11c2010-11-24 16:22:09 +01004497 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004498 s->rep->analysers = 0;
4499
4500 http_silent_debug(__LINE__, s);
4501}
4502
4503
4504/* This function updates the request state machine according to the response
4505 * state machine and buffer flags. It returns 1 if it changes anything (flag
4506 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4507 * it is only used to find when a request/response couple is complete. Both
4508 * this function and its equivalent should loop until both return zero. It
4509 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4510 */
4511int http_sync_req_state(struct session *s)
4512{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004513 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004515 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004516 unsigned int old_state = txn->req.msg_state;
4517
4518 http_silent_debug(__LINE__, s);
4519 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4520 return 0;
4521
4522 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004523 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004524 * We can shut the read side unless we want to abort_on_close,
4525 * or we have a POST request. The issue with POST requests is
4526 * that some browsers still send a CRLF after the request, and
4527 * this CRLF must be read so that it does not remain in the kernel
4528 * buffers, otherwise a close could cause an RST on some systems
4529 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004530 * Note that if we're using keep-alive on the client side, we'd
4531 * rather poll now and keep the polling enabled for the whole
4532 * session's life than enabling/disabling it between each
4533 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004534 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004535 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4536 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4537 !(s->be->options & PR_O_ABRT_CLOSE) &&
4538 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004539 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540
Willy Tarreau40f151a2012-12-20 12:10:09 +01004541 /* if the server closes the connection, we want to immediately react
4542 * and close the socket to save packets and syscalls.
4543 */
4544 chn->cons->flags |= SI_FL_NOHALF;
4545
Willy Tarreau610ecce2010-01-04 21:15:02 +01004546 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4547 goto wait_other_side;
4548
4549 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4550 /* The server has not finished to respond, so we
4551 * don't want to move in order not to upset it.
4552 */
4553 goto wait_other_side;
4554 }
4555
4556 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4557 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004558 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004560 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004561 goto wait_other_side;
4562 }
4563
4564 /* When we get here, it means that both the request and the
4565 * response have finished receiving. Depending on the connection
4566 * mode, we'll have to wait for the last bytes to leave in either
4567 * direction, and sometimes for a close to be effective.
4568 */
4569
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004570 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4571 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004572 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4573 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004574 }
4575 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4576 /* Option forceclose is set, or either side wants to close,
4577 * let's enforce it now that we're not expecting any new
4578 * data to come. The caller knows the session is complete
4579 * once both states are CLOSED.
4580 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004581 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4582 channel_shutr_now(chn);
4583 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004584 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004585 }
4586 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004587 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4588 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004589 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004590 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4591 channel_auto_read(chn);
4592 txn->req.msg_state = HTTP_MSG_TUNNEL;
4593 chn->flags |= CF_NEVER_WAIT;
4594 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 }
4596
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004597 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004598 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004599 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004600
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 txn->req.msg_state = HTTP_MSG_CLOSING;
4603 goto http_msg_closing;
4604 }
4605 else {
4606 txn->req.msg_state = HTTP_MSG_CLOSED;
4607 goto http_msg_closed;
4608 }
4609 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004610 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 }
4612
4613 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4614 http_msg_closing:
4615 /* nothing else to forward, just waiting for the output buffer
4616 * to be empty and for the shutw_now to take effect.
4617 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004618 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 txn->req.msg_state = HTTP_MSG_CLOSED;
4620 goto http_msg_closed;
4621 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004622 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004623 txn->req.msg_state = HTTP_MSG_ERROR;
4624 goto wait_other_side;
4625 }
4626 }
4627
4628 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4629 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004630 /* see above in MSG_DONE why we only do this in these states */
4631 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4632 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4633 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004634 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 goto wait_other_side;
4636 }
4637
4638 wait_other_side:
4639 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004640 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641}
4642
4643
4644/* This function updates the response state machine according to the request
4645 * state machine and buffer flags. It returns 1 if it changes anything (flag
4646 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4647 * it is only used to find when a request/response couple is complete. Both
4648 * this function and its equivalent should loop until both return zero. It
4649 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4650 */
4651int http_sync_res_state(struct session *s)
4652{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004653 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004654 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004655 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 unsigned int old_state = txn->rsp.msg_state;
4657
4658 http_silent_debug(__LINE__, s);
4659 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4660 return 0;
4661
4662 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4663 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004664 * still monitor the server connection for a possible close
4665 * while the request is being uploaded, so we don't disable
4666 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004667 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004668 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669
4670 if (txn->req.msg_state == HTTP_MSG_ERROR)
4671 goto wait_other_side;
4672
4673 if (txn->req.msg_state < HTTP_MSG_DONE) {
4674 /* The client seems to still be sending data, probably
4675 * because we got an error response during an upload.
4676 * We have the choice of either breaking the connection
4677 * or letting it pass through. Let's do the later.
4678 */
4679 goto wait_other_side;
4680 }
4681
4682 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4683 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004684 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004686 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687 goto wait_other_side;
4688 }
4689
4690 /* When we get here, it means that both the request and the
4691 * response have finished receiving. Depending on the connection
4692 * mode, we'll have to wait for the last bytes to leave in either
4693 * direction, and sometimes for a close to be effective.
4694 */
4695
4696 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4697 /* Server-close mode : shut read and wait for the request
4698 * side to close its output buffer. The caller will detect
4699 * when we're in DONE and the other is in CLOSED and will
4700 * catch that for the final cleanup.
4701 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004702 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4703 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004705 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4706 /* Option forceclose is set, or either side wants to close,
4707 * let's enforce it now that we're not expecting any new
4708 * data to come. The caller knows the session is complete
4709 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004711 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4712 channel_shutr_now(chn);
4713 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004714 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004715 }
4716 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004717 /* The last possible modes are keep-alive and tunnel. Tunnel will
4718 * need to forward remaining data. Keep-alive will need to monitor
4719 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004721 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004722 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004723 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4724 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004725 }
4726
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004727 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004728 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004729 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4731 goto http_msg_closing;
4732 }
4733 else {
4734 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4735 goto http_msg_closed;
4736 }
4737 }
4738 goto wait_other_side;
4739 }
4740
4741 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4742 http_msg_closing:
4743 /* nothing else to forward, just waiting for the output buffer
4744 * to be empty and for the shutw_now to take effect.
4745 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004746 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004747 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4748 goto http_msg_closed;
4749 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004750 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004752 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004753 if (objt_server(s->target))
4754 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004755 goto wait_other_side;
4756 }
4757 }
4758
4759 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4760 http_msg_closed:
4761 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004762 bi_erase(chn);
4763 channel_auto_close(chn);
4764 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004765 goto wait_other_side;
4766 }
4767
4768 wait_other_side:
4769 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004770 /* We force the response to leave immediately if we're waiting for the
4771 * other side, since there is no pending shutdown to push it out.
4772 */
4773 if (!channel_is_empty(chn))
4774 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004775 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004776}
4777
4778
4779/* Resync the request and response state machines. Return 1 if either state
4780 * changes.
4781 */
4782int http_resync_states(struct session *s)
4783{
4784 struct http_txn *txn = &s->txn;
4785 int old_req_state = txn->req.msg_state;
4786 int old_res_state = txn->rsp.msg_state;
4787
4788 http_silent_debug(__LINE__, s);
4789 http_sync_req_state(s);
4790 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004791 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004792 if (!http_sync_res_state(s))
4793 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004794 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004795 if (!http_sync_req_state(s))
4796 break;
4797 }
4798 http_silent_debug(__LINE__, s);
4799 /* OK, both state machines agree on a compatible state.
4800 * There are a few cases we're interested in :
4801 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4802 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4803 * directions, so let's simply disable both analysers.
4804 * - HTTP_MSG_CLOSED on the response only means we must abort the
4805 * request.
4806 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4807 * with server-close mode means we've completed one request and we
4808 * must re-initialize the server connection.
4809 */
4810
4811 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4812 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4813 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4814 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4815 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004816 channel_auto_close(s->req);
4817 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004818 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004819 channel_auto_close(s->rep);
4820 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004822 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4823 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004824 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004825 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004826 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004827 channel_auto_close(s->rep);
4828 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004829 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004830 channel_abort(s->req);
4831 channel_auto_close(s->req);
4832 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004833 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004834 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004835 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4836 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004838 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4839 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4840 /* server-close/keep-alive: terminate this transaction,
4841 * possibly killing the server connection and reinitialize
4842 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004843 */
4844 http_end_txn_clean_session(s);
4845 }
4846
4847 http_silent_debug(__LINE__, s);
4848 return txn->req.msg_state != old_req_state ||
4849 txn->rsp.msg_state != old_res_state;
4850}
4851
Willy Tarreaud98cf932009-12-27 22:54:55 +01004852/* This function is an analyser which forwards request body (including chunk
4853 * sizes if any). It is called as soon as we must forward, even if we forward
4854 * zero byte. The only situation where it must not be called is when we're in
4855 * tunnel mode and we want to forward till the close. It's used both to forward
4856 * remaining data and to resync after end of body. It expects the msg_state to
4857 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4858 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004859 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004860 * bytes of pending data + the headers if not already done (between sol and sov).
4861 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004862 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004863int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004864{
4865 struct http_txn *txn = &s->txn;
4866 struct http_msg *msg = &s->txn.req;
4867
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004868 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4869 return 0;
4870
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004871 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004872 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004873 /* Output closed while we were sending data. We must abort and
4874 * wake the other side up.
4875 */
4876 msg->msg_state = HTTP_MSG_ERROR;
4877 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004878 return 1;
4879 }
4880
Willy Tarreau80a92c02014-03-12 10:41:13 +01004881 /* Some post-connect processing might want us to refrain from starting to
4882 * forward data. Currently, the only reason for this is "balance url_param"
4883 * whichs need to parse/process the request after we've enabled forwarding.
4884 */
4885 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
4886 if (!(s->rep->flags & CF_READ_ATTACHED)) {
4887 channel_auto_connect(req);
4888 goto missing_data;
4889 }
4890 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4891 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004892
4893 /* Note that we don't have to send 100-continue back because we don't
4894 * need the data to complete our job, and it's up to the server to
4895 * decide whether to return 100, 417 or anything else in return of
4896 * an "Expect: 100-continue" header.
4897 */
4898
4899 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004900 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004901 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004902 * is still null. We must save the body in msg->next because it
4903 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004904 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004905 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004906
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004907 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004908 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004909 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004910 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004911 }
4912
Willy Tarreau80a92c02014-03-12 10:41:13 +01004913 /* in most states, we should abort in case of early close */
4914 channel_auto_close(req);
4915
Willy Tarreaud98cf932009-12-27 22:54:55 +01004916 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004917 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004918
Willy Tarreau610ecce2010-01-04 21:15:02 +01004919 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004920 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004921 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004922 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004923 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004924 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004925 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004926 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004927 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004928
Willy Tarreaucaabe412010-01-03 23:08:28 +01004929 if (msg->msg_state == HTTP_MSG_DATA) {
4930 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004931 if (req->to_forward) {
4932 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004933 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004934 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01004935
4936 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004937 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004938 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004940 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004941 }
4942 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004943 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004944 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004945 * TRAILERS state.
4946 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004947 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004948
Willy Tarreau54d23df2012-10-25 19:04:45 +02004949 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004950 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004951 else if (ret < 0) {
4952 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004953 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004954 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004955 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004956 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004957 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004958 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004959 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004960 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004961 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004962
4963 if (ret == 0)
4964 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004965 else if (ret < 0) {
4966 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004967 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004968 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004969 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004970 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004971 /* we're in MSG_CHUNK_SIZE now */
4972 }
4973 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004974 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004975
4976 if (ret == 0)
4977 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004978 else if (ret < 0) {
4979 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004980 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004981 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004982 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004983 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984 /* we're in HTTP_MSG_DONE now */
4985 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004987 int old_state = msg->msg_state;
4988
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004990 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004991 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4992 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004993 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994 if (http_resync_states(s)) {
4995 /* some state changes occurred, maybe the analyser
4996 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004997 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004998 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004999 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005000 /* request errors are most likely due to
5001 * the server aborting the transfer.
5002 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005003 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005004 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005005 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005006 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005007 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005008 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005010 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005011
5012 /* If "option abortonclose" is set on the backend, we
5013 * want to monitor the client's connection and forward
5014 * any shutdown notification to the server, which will
5015 * decide whether to close or to go on processing the
5016 * request.
5017 */
5018 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005019 channel_auto_read(req);
5020 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005021 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005022 else if (s->txn.meth == HTTP_METH_POST) {
5023 /* POST requests may require to read extra CRLF
5024 * sent by broken browsers and which could cause
5025 * an RST to be sent upon close on some systems
5026 * (eg: Linux).
5027 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005028 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005029 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005030
Willy Tarreau610ecce2010-01-04 21:15:02 +01005031 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005032 }
5033 }
5034
Willy Tarreaud98cf932009-12-27 22:54:55 +01005035 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005036 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005037 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005038 if (!(s->flags & SN_ERR_MASK))
5039 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005040 if (!(s->flags & SN_FINST_MASK)) {
5041 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5042 s->flags |= SN_FINST_H;
5043 else
5044 s->flags |= SN_FINST_D;
5045 }
5046
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005047 s->fe->fe_counters.cli_aborts++;
5048 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005049 if (objt_server(s->target))
5050 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005051
5052 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005053 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005054
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005055 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005056 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005057 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005059 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005060 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005061 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005062 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005063 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005064
Willy Tarreau5c620922011-05-11 19:56:11 +02005065 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005066 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005067 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005068 * modes are already handled by the stream sock layer. We must not do
5069 * this in content-length mode because it could present the MSG_MORE
5070 * flag with the last block of forwarded data, which would cause an
5071 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005072 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005073 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005074 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005075
Willy Tarreau610ecce2010-01-04 21:15:02 +01005076 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005077 return 0;
5078
Willy Tarreaud98cf932009-12-27 22:54:55 +01005079 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005080 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005081 if (s->listener->counters)
5082 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005083 return_bad_req_stats_ok:
5084 txn->req.msg_state = HTTP_MSG_ERROR;
5085 if (txn->status) {
5086 /* Note: we don't send any error if some data were already sent */
5087 stream_int_retnclose(req->prod, NULL);
5088 } else {
5089 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005090 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005091 }
5092 req->analysers = 0;
5093 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005094
5095 if (!(s->flags & SN_ERR_MASK))
5096 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005097 if (!(s->flags & SN_FINST_MASK)) {
5098 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5099 s->flags |= SN_FINST_H;
5100 else
5101 s->flags |= SN_FINST_D;
5102 }
5103 return 0;
5104
5105 aborted_xfer:
5106 txn->req.msg_state = HTTP_MSG_ERROR;
5107 if (txn->status) {
5108 /* Note: we don't send any error if some data were already sent */
5109 stream_int_retnclose(req->prod, NULL);
5110 } else {
5111 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005112 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005113 }
5114 req->analysers = 0;
5115 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5116
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005117 s->fe->fe_counters.srv_aborts++;
5118 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005119 if (objt_server(s->target))
5120 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005121
5122 if (!(s->flags & SN_ERR_MASK))
5123 s->flags |= SN_ERR_SRVCL;
5124 if (!(s->flags & SN_FINST_MASK)) {
5125 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5126 s->flags |= SN_FINST_H;
5127 else
5128 s->flags |= SN_FINST_D;
5129 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005130 return 0;
5131}
5132
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005133/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5134 * processing can continue on next analysers, or zero if it either needs more
5135 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5136 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5137 * when it has nothing left to do, and may remove any analyser when it wants to
5138 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005139 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005140int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005141{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142 struct http_txn *txn = &s->txn;
5143 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005144 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005145 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005147 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005148
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005149 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005150 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005152 rep,
5153 rep->rex, rep->wex,
5154 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005155 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005156 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005157
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005158 /*
5159 * Now parse the partial (or complete) lines.
5160 * We will check the response syntax, and also join multi-line
5161 * headers. An index of all the lines will be elaborated while
5162 * parsing.
5163 *
5164 * For the parsing, we use a 28 states FSM.
5165 *
5166 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005167 * rep->buf->p = beginning of response
5168 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5169 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005170 * msg->eol = end of current header or line (LF or CRLF)
5171 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005172 */
5173
Willy Tarreau83e3af02009-12-28 17:39:57 +01005174 /* There's a protected area at the end of the buffer for rewriting
5175 * purposes. We don't want to start to parse the request if the
5176 * protected area is affected, because we may have to move processed
5177 * data later, which is much more complicated.
5178 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005179 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005180 if (unlikely(!channel_reserved(rep))) {
5181 /* some data has still not left the buffer, wake us once that's done */
5182 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5183 goto abort_response;
5184 channel_dont_close(rep);
5185 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005186 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005187 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005188 }
5189
Willy Tarreau379357a2013-06-08 12:55:46 +02005190 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5191 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5192 buffer_slow_realign(rep->buf);
5193
Willy Tarreau9b28e032012-10-12 23:49:43 +02005194 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005195 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005196 }
5197
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005198 /* 1: we might have to print this header in debug mode */
5199 if (unlikely((global.mode & MODE_DEBUG) &&
5200 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005201 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203
Willy Tarreau9b28e032012-10-12 23:49:43 +02005204 sol = rep->buf->p;
5205 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005206 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005208 sol += hdr_idx_first_pos(&txn->hdr_idx);
5209 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005210
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005211 while (cur_idx) {
5212 eol = sol + txn->hdr_idx.v[cur_idx].len;
5213 debug_hdr("srvhdr", s, sol, eol);
5214 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5215 cur_idx = txn->hdr_idx.v[cur_idx].next;
5216 }
5217 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005218
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219 /*
5220 * Now we quickly check if we have found a full valid response.
5221 * If not so, we check the FD and buffer states before leaving.
5222 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005223 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005224 * responses are checked first.
5225 *
5226 * Depending on whether the client is still there or not, we
5227 * may send an error response back or not. Note that normally
5228 * we should only check for HTTP status there, and check I/O
5229 * errors somewhere else.
5230 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005231
Willy Tarreau655dce92009-11-08 13:10:58 +01005232 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233 /* Invalid response */
5234 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5235 /* we detected a parsing error. We want to archive this response
5236 * in the dedicated proxy area for later troubleshooting.
5237 */
5238 hdr_response_bad:
5239 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005240 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005241
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005242 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005243 if (objt_server(s->target)) {
5244 objt_server(s->target)->counters.failed_resp++;
5245 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005246 }
Willy Tarreau64648412010-03-05 10:41:54 +01005247 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005248 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 rep->analysers = 0;
5250 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005251 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005252 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005253 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005254
5255 if (!(s->flags & SN_ERR_MASK))
5256 s->flags |= SN_ERR_PRXCOND;
5257 if (!(s->flags & SN_FINST_MASK))
5258 s->flags |= SN_FINST_H;
5259
5260 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005261 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005262
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005264 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005265 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005266 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005267 goto hdr_response_bad;
5268 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005269
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005270 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005271 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005272 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005273 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005274 else if (txn->flags & TX_NOT_FIRST)
5275 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005276
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005277 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005278 if (objt_server(s->target)) {
5279 objt_server(s->target)->counters.failed_resp++;
5280 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005281 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005282
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005283 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005284 rep->analysers = 0;
5285 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005286 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005287 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005288 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005289
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290 if (!(s->flags & SN_ERR_MASK))
5291 s->flags |= SN_ERR_SRVCL;
5292 if (!(s->flags & SN_FINST_MASK))
5293 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005294 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005295 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005296
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005297 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005298 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005299 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005300 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005301 else if (txn->flags & TX_NOT_FIRST)
5302 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005303
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005304 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005305 if (objt_server(s->target)) {
5306 objt_server(s->target)->counters.failed_resp++;
5307 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005308 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005309
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005310 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005311 rep->analysers = 0;
5312 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005313 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005314 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005315 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005316
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005317 if (!(s->flags & SN_ERR_MASK))
5318 s->flags |= SN_ERR_SRVTO;
5319 if (!(s->flags & SN_FINST_MASK))
5320 s->flags |= SN_FINST_H;
5321 return 0;
5322 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005323
Willy Tarreauf003d372012-11-26 13:35:37 +01005324 /* client abort with an abortonclose */
5325 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5326 s->fe->fe_counters.cli_aborts++;
5327 s->be->be_counters.cli_aborts++;
5328 if (objt_server(s->target))
5329 objt_server(s->target)->counters.cli_aborts++;
5330
5331 rep->analysers = 0;
5332 channel_auto_close(rep);
5333
5334 txn->status = 400;
5335 bi_erase(rep);
5336 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5337
5338 if (!(s->flags & SN_ERR_MASK))
5339 s->flags |= SN_ERR_CLICL;
5340 if (!(s->flags & SN_FINST_MASK))
5341 s->flags |= SN_FINST_H;
5342
5343 /* process_session() will take care of the error */
5344 return 0;
5345 }
5346
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005347 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005348 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005349 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005350 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005351 else if (txn->flags & TX_NOT_FIRST)
5352 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005353
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005354 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005355 if (objt_server(s->target)) {
5356 objt_server(s->target)->counters.failed_resp++;
5357 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005358 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005359
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005360 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005361 rep->analysers = 0;
5362 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005363 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005364 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005365 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005366
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005367 if (!(s->flags & SN_ERR_MASK))
5368 s->flags |= SN_ERR_SRVCL;
5369 if (!(s->flags & SN_FINST_MASK))
5370 s->flags |= SN_FINST_H;
5371 return 0;
5372 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005373
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005375 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005376 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005377 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005378 else if (txn->flags & TX_NOT_FIRST)
5379 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005380
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005381 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005382 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005383 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005384
5385 if (!(s->flags & SN_ERR_MASK))
5386 s->flags |= SN_ERR_CLICL;
5387 if (!(s->flags & SN_FINST_MASK))
5388 s->flags |= SN_FINST_H;
5389
5390 /* process_session() will take care of the error */
5391 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005392 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005393
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005394 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005395 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005396 return 0;
5397 }
5398
5399 /* More interesting part now : we know that we have a complete
5400 * response which at least looks like HTTP. We have an indicator
5401 * of each header's length, so we can parse them quickly.
5402 */
5403
5404 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005405 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005406
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005407 /*
5408 * 1: get the status code
5409 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005410 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005411 if (n < 1 || n > 5)
5412 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005413 /* when the client triggers a 4xx from the server, it's most often due
5414 * to a missing object or permission. These events should be tracked
5415 * because if they happen often, it may indicate a brute force or a
5416 * vulnerability scan.
5417 */
5418 if (n == 4)
5419 session_inc_http_err_ctr(s);
5420
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005421 if (objt_server(s->target))
5422 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005423
Willy Tarreau5b154472009-12-21 20:11:07 +01005424 /* check if the response is HTTP/1.1 or above */
5425 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005426 ((rep->buf->p[5] > '1') ||
5427 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005428 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005429
5430 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005431 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 +01005432
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005433 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005434 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005435
Willy Tarreau9b28e032012-10-12 23:49:43 +02005436 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005437
Willy Tarreau39650402010-03-15 19:44:39 +01005438 /* Adjust server's health based on status code. Note: status codes 501
5439 * and 505 are triggered on demand by client request, so we must not
5440 * count them as server failures.
5441 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005442 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005443 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005444 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005445 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005446 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005447 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005448
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005449 /*
5450 * 2: check for cacheability.
5451 */
5452
5453 switch (txn->status) {
5454 case 200:
5455 case 203:
5456 case 206:
5457 case 300:
5458 case 301:
5459 case 410:
5460 /* RFC2616 @13.4:
5461 * "A response received with a status code of
5462 * 200, 203, 206, 300, 301 or 410 MAY be stored
5463 * by a cache (...) unless a cache-control
5464 * directive prohibits caching."
5465 *
5466 * RFC2616 @9.5: POST method :
5467 * "Responses to this method are not cacheable,
5468 * unless the response includes appropriate
5469 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005470 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005471 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005472 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005473 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5474 break;
5475 default:
5476 break;
5477 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005478
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005479 /*
5480 * 3: we may need to capture headers
5481 */
5482 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005483 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005484 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005485 txn->rsp.cap, s->fe->rsp_cap);
5486
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005487 /* 4: determine the transfer-length.
5488 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5489 * the presence of a message-body in a RESPONSE and its transfer length
5490 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005491 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005492 * All responses to the HEAD request method MUST NOT include a
5493 * message-body, even though the presence of entity-header fields
5494 * might lead one to believe they do. All 1xx (informational), 204
5495 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5496 * message-body. All other responses do include a message-body,
5497 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005498 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005499 * 1. Any response which "MUST NOT" include a message-body (such as the
5500 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5501 * always terminated by the first empty line after the header fields,
5502 * regardless of the entity-header fields present in the message.
5503 *
5504 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5505 * the "chunked" transfer-coding (Section 6.2) is used, the
5506 * transfer-length is defined by the use of this transfer-coding.
5507 * If a Transfer-Encoding header field is present and the "chunked"
5508 * transfer-coding is not present, the transfer-length is defined by
5509 * the sender closing the connection.
5510 *
5511 * 3. If a Content-Length header field is present, its decimal value in
5512 * OCTETs represents both the entity-length and the transfer-length.
5513 * If a message is received with both a Transfer-Encoding header
5514 * field and a Content-Length header field, the latter MUST be ignored.
5515 *
5516 * 4. If the message uses the media type "multipart/byteranges", and
5517 * the transfer-length is not otherwise specified, then this self-
5518 * delimiting media type defines the transfer-length. This media
5519 * type MUST NOT be used unless the sender knows that the recipient
5520 * can parse it; the presence in a request of a Range header with
5521 * multiple byte-range specifiers from a 1.1 client implies that the
5522 * client can parse multipart/byteranges responses.
5523 *
5524 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005525 */
5526
5527 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005528 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005529 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005530 * FIXME: should we parse anyway and return an error on chunked encoding ?
5531 */
5532 if (txn->meth == HTTP_METH_HEAD ||
5533 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005534 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005535 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005536 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005537 goto skip_content_length;
5538 }
5539
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005540 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005541 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005542 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005543 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005544 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005545 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5546 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005547 /* bad transfer-encoding (chunked followed by something else) */
5548 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005549 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005550 break;
5551 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005552 }
5553
5554 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5555 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005556 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005557 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005558 signed long long cl;
5559
Willy Tarreauad14f752011-09-02 20:33:27 +02005560 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005561 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005562 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005563 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005564
Willy Tarreauad14f752011-09-02 20:33:27 +02005565 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005566 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005567 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005568 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005569
Willy Tarreauad14f752011-09-02 20:33:27 +02005570 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005571 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005573 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005575 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005576 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005577 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005578 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005579
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005580 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005581 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005582 }
5583
William Lallemand82fe75c2012-10-23 10:25:10 +02005584 if (s->fe->comp || s->be->comp)
5585 select_compression_response_header(s, rep->buf);
5586
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005587 /* FIXME: we should also implement the multipart/byterange method.
5588 * For now on, we resort to close mode in this case (unknown length).
5589 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005590skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005591
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005592 /* end of job, return OK */
5593 rep->analysers &= ~an_bit;
5594 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005595 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005596 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005597
5598 abort_keep_alive:
5599 /* A keep-alive request to the server failed on a network error.
5600 * The client is required to retry. We need to close without returning
5601 * any other information so that the client retries.
5602 */
5603 txn->status = 0;
5604 rep->analysers = 0;
5605 s->req->analysers = 0;
5606 channel_auto_close(rep);
5607 s->logs.logwait = 0;
5608 s->logs.level = 0;
5609 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5610 bi_erase(rep);
5611 stream_int_retnclose(rep->cons, NULL);
5612 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005613}
5614
5615/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005616 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5617 * and updates t->rep->analysers. It might make sense to explode it into several
5618 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005619 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005620int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005621{
5622 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005623 struct http_msg *msg = &txn->rsp;
5624 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005625 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005626 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005627
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005628 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 +02005629 now_ms, __FUNCTION__,
5630 t,
5631 rep,
5632 rep->rex, rep->wex,
5633 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005634 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005635 rep->analysers);
5636
Willy Tarreau655dce92009-11-08 13:10:58 +01005637 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005638 return 0;
5639
5640 rep->analysers &= ~an_bit;
5641 rep->analyse_exp = TICK_ETERNITY;
5642
Willy Tarreau5b154472009-12-21 20:11:07 +01005643 /* Now we have to check if we need to modify the Connection header.
5644 * This is more difficult on the response than it is on the request,
5645 * because we can have two different HTTP versions and we don't know
5646 * how the client will interprete a response. For instance, let's say
5647 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5648 * HTTP/1.1 response without any header. Maybe it will bound itself to
5649 * HTTP/1.0 because it only knows about it, and will consider the lack
5650 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5651 * the lack of header as a keep-alive. Thus we will use two flags
5652 * indicating how a request MAY be understood by the client. In case
5653 * of multiple possibilities, we'll fix the header to be explicit. If
5654 * ambiguous cases such as both close and keepalive are seen, then we
5655 * will fall back to explicit close. Note that we won't take risks with
5656 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005657 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005658 */
5659
Willy Tarreaudc008c52010-02-01 16:20:08 +01005660 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5661 txn->status == 101)) {
5662 /* Either we've established an explicit tunnel, or we're
5663 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005664 * to understand the next protocols. We have to switch to tunnel
5665 * mode, so that we transfer the request and responses then let
5666 * this protocol pass unmodified. When we later implement specific
5667 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005668 * header which contains information about that protocol for
5669 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005670 */
5671 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5672 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005673 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5674 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005675 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5676 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005677 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005678
Willy Tarreau70dffda2014-01-30 03:07:23 +01005679 /* this situation happens when combining pretend-keepalive with httpclose. */
5680 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5681 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5682 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5683 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5684
Willy Tarreau60466522010-01-18 19:08:45 +01005685 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005686 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005687 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5688 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005689
Willy Tarreau60466522010-01-18 19:08:45 +01005690 /* now adjust header transformations depending on current state */
5691 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5692 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5693 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005694 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005695 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005696 }
Willy Tarreau60466522010-01-18 19:08:45 +01005697 else { /* SCL / KAL */
5698 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005699 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005700 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005701 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005702
Willy Tarreau60466522010-01-18 19:08:45 +01005703 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005704 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005705
Willy Tarreau60466522010-01-18 19:08:45 +01005706 /* Some keep-alive responses are converted to Server-close if
5707 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005708 */
Willy Tarreau60466522010-01-18 19:08:45 +01005709 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5710 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005711 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005712 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005713 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005714 }
5715
Willy Tarreau7959a552013-09-23 16:44:27 +02005716 /* we want to have the response time before we start processing it */
5717 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5718
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005719 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005720 /*
5721 * 3: we will have to evaluate the filters.
5722 * As opposed to version 1.2, now they will be evaluated in the
5723 * filters order and not in the header order. This means that
5724 * each filter has to be validated among all headers.
5725 *
5726 * Filters are tried with ->be first, then with ->fe if it is
5727 * different from ->be.
5728 */
5729
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005730 cur_proxy = t->be;
5731 while (1) {
5732 struct proxy *rule_set = cur_proxy;
5733
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005734 /* evaluate http-response rules */
5735 if (!http_res_last_rule)
5736 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5737
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005738 /* try headers filters */
5739 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005740 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005741 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005742 if (objt_server(t->target)) {
5743 objt_server(t->target)->counters.failed_resp++;
5744 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005745 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005746 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005747 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005748 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005749 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005750 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005751 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005752 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005753 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005754 if (!(t->flags & SN_ERR_MASK))
5755 t->flags |= SN_ERR_PRXCOND;
5756 if (!(t->flags & SN_FINST_MASK))
5757 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005758 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005759 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005760 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005761
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005762 /* has the response been denied ? */
5763 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005764 if (objt_server(t->target))
5765 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005766
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005767 t->be->be_counters.denied_resp++;
5768 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005769 if (t->listener->counters)
5770 t->listener->counters->denied_resp++;
5771
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005772 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005773 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005774
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005775 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005776 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005777 if (txn->status < 200)
5778 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005779 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005780 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005781 ret = acl_pass(ret);
5782 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5783 ret = !ret;
5784 if (!ret)
5785 continue;
5786 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005787 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005788 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005789 }
5790
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005791 /* check whether we're already working on the frontend */
5792 if (cur_proxy == t->fe)
5793 break;
5794 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005795 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005796
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005797 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005798 * We may be facing a 100-continue response, in which case this
5799 * is not the right response, and we're waiting for the next one.
5800 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005801 * next one.
5802 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005803 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005804 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005805 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005806 msg->msg_state = HTTP_MSG_RPBEFORE;
5807 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005808 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005809 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5810 return 1;
5811 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005812 else if (unlikely(txn->status < 200))
5813 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005814
5815 /* we don't have any 1xx status code now */
5816
5817 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005818 * 4: check for server cookie.
5819 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005820 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5821 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005822 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005823
Willy Tarreaubaaee002006-06-26 02:48:02 +02005824
Willy Tarreaua15645d2007-03-18 16:22:39 +01005825 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005826 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005827 */
Willy Tarreau67402132012-05-31 20:40:20 +02005828 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005829 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005830
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005831 /*
5832 * 6: add server cookie in the response if needed
5833 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005834 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005835 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005836 (!(t->flags & SN_DIRECT) ||
5837 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5838 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5839 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5840 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005841 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005842 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005843 /* the server is known, it's not the one the client requested, or the
5844 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005845 * insert a set-cookie here, except if we want to insert only on POST
5846 * requests and this one isn't. Note that servers which don't have cookies
5847 * (eg: some backup servers) will return a full cookie removal request.
5848 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005849 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005850 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005851 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5852 t->be->cookie_name);
5853 }
5854 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005855 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005856
5857 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5858 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005859 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5860 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5861 trash.len += 5;
5862
Willy Tarreauef4f3912010-10-07 21:00:29 +02005863 if (t->be->cookie_maxlife) {
5864 /* emit first_date, which is either the original one or
5865 * the current date.
5866 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005867 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005868 s30tob64(txn->cookie_first_date ?
5869 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005870 (date.tv_sec+3) >> 2, trash.str + trash.len);
5871 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005872 }
5873 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005874 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005875 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005876
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005877 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005878 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005879
Willy Tarreau4992dd22012-05-31 21:02:17 +02005880 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005881 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005882
5883 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005884 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005885
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005886 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005887 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005888
Willy Tarreauf1348312010-10-07 15:54:11 +02005889 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005890 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005891 /* the server did not change, only the date was updated */
5892 txn->flags |= TX_SCK_UPDATED;
5893 else
5894 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005895
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005896 /* Here, we will tell an eventual cache on the client side that we don't
5897 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5898 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5899 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5900 */
Willy Tarreau67402132012-05-31 20:40:20 +02005901 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005902
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005903 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5904
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005905 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005906 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005907 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005908 }
5909 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005910
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005911 /*
5912 * 7: check if result will be cacheable with a cookie.
5913 * We'll block the response if security checks have caught
5914 * nasty things such as a cacheable cookie.
5915 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005916 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5917 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005918 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005919
5920 /* we're in presence of a cacheable response containing
5921 * a set-cookie header. We'll block it as requested by
5922 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005923 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005924 if (objt_server(t->target))
5925 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005926
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005927 t->be->be_counters.denied_resp++;
5928 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005929 if (t->listener->counters)
5930 t->listener->counters->denied_resp++;
5931
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005932 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005933 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005934 send_log(t->be, LOG_ALERT,
5935 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005936 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937 goto return_srv_prx_502;
5938 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939
5940 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005941 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005942 * If an "Upgrade" token is found, the header is left untouched in order
5943 * not to have to deal with some client bugs : some of them fail an upgrade
5944 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005945 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005946 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5947 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005948 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5949 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005950 unsigned int want_flags = 0;
5951
5952 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5953 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5954 /* we want a keep-alive response here. Keep-alive header
5955 * required if either side is not 1.1.
5956 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005957 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005958 want_flags |= TX_CON_KAL_SET;
5959 }
5960 else {
5961 /* we want a close response here. Close header required if
5962 * the server is 1.1, regardless of the client.
5963 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005964 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005965 want_flags |= TX_CON_CLO_SET;
5966 }
5967
5968 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005969 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005970 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005972 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005973 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005974 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005975 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005977 /*************************************************************
5978 * OK, that's finished for the headers. We have done what we *
5979 * could. Let's switch to the DATA state. *
5980 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005981
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005982 /* if the user wants to log as soon as possible, without counting
5983 * bytes from the server, then this is the right moment. We have
5984 * to temporarily assign bytes_out to log what we currently have.
5985 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005986 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005987 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5988 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005989 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005990 t->logs.bytes_out = 0;
5991 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005992
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005993 /* Note: we must not try to cheat by jumping directly to DATA,
5994 * otherwise we would not let the client side wake up.
5995 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005996
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005997 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005998 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005999 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006000}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006001
Willy Tarreaud98cf932009-12-27 22:54:55 +01006002/* This function is an analyser which forwards response body (including chunk
6003 * sizes if any). It is called as soon as we must forward, even if we forward
6004 * zero byte. The only situation where it must not be called is when we're in
6005 * tunnel mode and we want to forward till the close. It's used both to forward
6006 * remaining data and to resync after end of body. It expects the msg_state to
6007 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6008 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006009 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006010 * bytes of pending data + the headers if not already done (between sol and sov).
6011 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006012 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006013int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006014{
6015 struct http_txn *txn = &s->txn;
6016 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02006017 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02006018 static struct buffer *tmpbuf = NULL;
6019 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006020 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006021 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006022
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006023 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6024 return 0;
6025
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006026 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006027 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006028 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006029 /* Output closed while we were sending data. We must abort and
6030 * wake the other side up.
6031 */
6032 msg->msg_state = HTTP_MSG_ERROR;
6033 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006034 return 1;
6035 }
6036
Willy Tarreau4fe41902010-06-07 22:27:41 +02006037 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006038 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006039
William Lallemand82fe75c2012-10-23 10:25:10 +02006040 /* this is the first time we need the compression buffer */
6041 if (s->comp_algo != NULL && tmpbuf == NULL) {
6042 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6043 goto aborted_xfer; /* no memory */
6044 }
6045
Willy Tarreaud98cf932009-12-27 22:54:55 +01006046 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006047 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006048 * rep->buf.p still points to the beginning of the message and msg->sol
6049 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006050 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006051 channel_forward(res, msg->sov);
6052 msg->next = 0;
6053 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006054
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006055 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006056 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006057 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006058 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006059 }
6060
William Lallemand82fe75c2012-10-23 10:25:10 +02006061 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006062 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006063 if (ret < 0) {
6064 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006065 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006066 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006067 compressing = 1;
6068 }
6069
Willy Tarreaud98cf932009-12-27 22:54:55 +01006070 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006071 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006072 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006073 if (s->comp_algo == NULL) {
6074 bytes = msg->sov - msg->sol;
6075 if (msg->chunk_len || bytes) {
6076 msg->sol = msg->sov;
6077 msg->next -= bytes; /* will be forwarded */
6078 msg->chunk_len += bytes;
6079 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6080 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006081 }
6082
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006083 switch (msg->msg_state - HTTP_MSG_DATA) {
6084 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006085 if (compressing) {
6086 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6087 if (ret < 0)
6088 goto aborted_xfer;
6089 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006090
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006091 if (res->to_forward || msg->chunk_len) {
6092 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006093 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006094 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006095
6096 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006097 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006098 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006099 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006100 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006101 if (compressing && consumed_data) {
6102 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6103 compressing = 0;
6104 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006105 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006106 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006107 /* fall through for HTTP_MSG_CHUNK_CRLF */
6108
6109 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6110 /* we want the CRLF after the data */
6111
6112 ret = http_skip_chunk_crlf(msg);
6113 if (ret == 0)
6114 goto missing_data;
6115 else if (ret < 0) {
6116 if (msg->err_pos >= 0)
6117 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6118 goto return_bad_res;
6119 }
6120 /* skipping data in buffer for compression */
6121 if (compressing) {
6122 b_adv(res->buf, msg->next);
6123 msg->next = 0;
6124 msg->sov = 0;
6125 msg->sol = 0;
6126 }
6127 /* we're in MSG_CHUNK_SIZE now, fall through */
6128
6129 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006130 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006131 * set ->sov and ->next to point to the body and switch to DATA or
6132 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006133 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006134
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006135 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006136 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006137 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006138 else if (ret < 0) {
6139 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006140 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006141 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006142 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006143 if (compressing) {
6144 if (likely(msg->chunk_len > 0)) {
6145 /* skipping data if we are in compression mode */
6146 b_adv(res->buf, msg->next);
6147 msg->next = 0;
6148 msg->sov = 0;
6149 msg->sol = 0;
6150 } else {
6151 if (consumed_data) {
6152 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6153 compressing = 0;
6154 }
6155 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006156 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006157 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006158 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006159
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006160 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6161 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006162 if (ret == 0)
6163 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006164 else if (ret < 0) {
6165 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006166 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006167 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006168 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006169 if (s->comp_algo != NULL) {
6170 /* forwarding trailers */
6171 channel_forward(res, msg->next);
6172 msg->next = 0;
6173 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006174 /* we're in HTTP_MSG_DONE now, but we might still have
6175 * some data pending, so let's loop over once.
6176 */
6177 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006178
6179 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006180 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006181
6182 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006183 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006184 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6185 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006186 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006187 if (http_resync_states(s)) {
6188 http_silent_debug(__LINE__, s);
6189 /* some state changes occurred, maybe the analyser
6190 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006191 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006192 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006193 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006194 /* response errors are most likely due to
6195 * the client aborting the transfer.
6196 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006197 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006198 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006199 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006200 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006201 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006202 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006203 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006204 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006205 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006206 }
6207 }
6208
Willy Tarreaud98cf932009-12-27 22:54:55 +01006209 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006210 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006211 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6212 compressing = 0;
6213 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006214
6215 if (res->flags & CF_SHUTW)
6216 goto aborted_xfer;
6217
6218 /* stop waiting for data if the input is closed before the end. If the
6219 * client side was already closed, it means that the client has aborted,
6220 * so we don't want to count this as a server abort. Otherwise it's a
6221 * server abort.
6222 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006223 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006224 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6225 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006226 if (!(s->flags & SN_ERR_MASK))
6227 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006228 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006229 if (objt_server(s->target))
6230 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006231 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006232 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006233
Willy Tarreau40dba092010-03-04 18:14:51 +01006234 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006235 if (!s->req->analysers)
6236 goto return_bad_res;
6237
Willy Tarreauea953162012-05-18 23:41:28 +02006238 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006239 if (s->comp_algo == NULL) {
6240 bytes = msg->sov - msg->sol;
6241 if (msg->chunk_len || bytes) {
6242 msg->sol = msg->sov;
6243 msg->next -= bytes; /* will be forwarded */
6244 msg->chunk_len += bytes;
6245 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6246 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006247 }
6248
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006249 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006250 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006251 * Similarly, with keep-alive on the client side, we don't want to forward a
6252 * close.
6253 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006254 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006255 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6256 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006257 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006258
Willy Tarreau5c620922011-05-11 19:56:11 +02006259 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006260 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006261 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006262 * modes are already handled by the stream sock layer. We must not do
6263 * this in content-length mode because it could present the MSG_MORE
6264 * flag with the last block of forwarded data, which would cause an
6265 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006266 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006267 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006268 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006269
Willy Tarreaud98cf932009-12-27 22:54:55 +01006270 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006271 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006272 return 0;
6273
Willy Tarreau40dba092010-03-04 18:14:51 +01006274 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006275 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006276 if (objt_server(s->target))
6277 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006278
6279 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006280 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006281 /* don't send any error message as we're in the body */
6282 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006283 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006284 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006285 if (objt_server(s->target))
6286 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006287
6288 if (!(s->flags & SN_ERR_MASK))
6289 s->flags |= SN_ERR_PRXCOND;
6290 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006291 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006292 return 0;
6293
6294 aborted_xfer:
6295 txn->rsp.msg_state = HTTP_MSG_ERROR;
6296 /* don't send any error message as we're in the body */
6297 stream_int_retnclose(res->cons, NULL);
6298 res->analysers = 0;
6299 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6300
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006301 s->fe->fe_counters.cli_aborts++;
6302 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006303 if (objt_server(s->target))
6304 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006305
6306 if (!(s->flags & SN_ERR_MASK))
6307 s->flags |= SN_ERR_CLICL;
6308 if (!(s->flags & SN_FINST_MASK))
6309 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006310 return 0;
6311}
6312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006313/* Iterate the same filter through all request headers.
6314 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006315 * Since it can manage the switch to another backend, it updates the per-proxy
6316 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006318int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006320 char term;
6321 char *cur_ptr, *cur_end, *cur_next;
6322 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006323 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006324 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006325 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006327 last_hdr = 0;
6328
Willy Tarreau9b28e032012-10-12 23:49:43 +02006329 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006330 old_idx = 0;
6331
6332 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006333 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006334 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006335 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006336 (exp->action == ACT_ALLOW ||
6337 exp->action == ACT_DENY ||
6338 exp->action == ACT_TARPIT))
6339 return 0;
6340
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006341 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006342 if (!cur_idx)
6343 break;
6344
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006345 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006346 cur_ptr = cur_next;
6347 cur_end = cur_ptr + cur_hdr->len;
6348 cur_next = cur_end + cur_hdr->cr + 1;
6349
6350 /* Now we have one header between cur_ptr and cur_end,
6351 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352 */
6353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006354 /* The annoying part is that pattern matching needs
6355 * that we modify the contents to null-terminate all
6356 * strings before testing them.
6357 */
6358
6359 term = *cur_end;
6360 *cur_end = '\0';
6361
6362 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6363 switch (exp->action) {
6364 case ACT_SETBE:
6365 /* It is not possible to jump a second time.
6366 * FIXME: should we return an HTTP/500 here so that
6367 * the admin knows there's a problem ?
6368 */
6369 if (t->be != t->fe)
6370 break;
6371
6372 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006373 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006374 last_hdr = 1;
6375 break;
6376
6377 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006378 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006379 last_hdr = 1;
6380 break;
6381
6382 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006383 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006384 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006385 break;
6386
6387 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006388 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006390 break;
6391
6392 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006393 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6394 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006395 /* FIXME: if the user adds a newline in the replacement, the
6396 * index will not be recalculated for now, and the new line
6397 * will not be counted as a new header.
6398 */
6399
6400 cur_end += delta;
6401 cur_next += delta;
6402 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006403 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006404 break;
6405
6406 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006407 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006408 cur_next += delta;
6409
Willy Tarreaufa355d42009-11-29 18:12:29 +01006410 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006411 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6412 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006413 cur_hdr->len = 0;
6414 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006415 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006416 break;
6417
6418 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006419 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006420 if (cur_end)
6421 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006423 /* keep the link from this header to next one in case of later
6424 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426 old_idx = cur_idx;
6427 }
6428 return 0;
6429}
6430
6431
6432/* Apply the filter to the request line.
6433 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6434 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006435 * Since it can manage the switch to another backend, it updates the per-proxy
6436 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006437 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006438int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006439{
6440 char term;
6441 char *cur_ptr, *cur_end;
6442 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006443 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006444 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006445
Willy Tarreau3d300592007-03-18 18:34:41 +01006446 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006448 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 (exp->action == ACT_ALLOW ||
6450 exp->action == ACT_DENY ||
6451 exp->action == ACT_TARPIT))
6452 return 0;
6453 else if (exp->action == ACT_REMOVE)
6454 return 0;
6455
6456 done = 0;
6457
Willy Tarreau9b28e032012-10-12 23:49:43 +02006458 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006459 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006460
6461 /* Now we have the request line between cur_ptr and cur_end */
6462
6463 /* The annoying part is that pattern matching needs
6464 * that we modify the contents to null-terminate all
6465 * strings before testing them.
6466 */
6467
6468 term = *cur_end;
6469 *cur_end = '\0';
6470
6471 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6472 switch (exp->action) {
6473 case ACT_SETBE:
6474 /* It is not possible to jump a second time.
6475 * FIXME: should we return an HTTP/500 here so that
6476 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006477 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 if (t->be != t->fe)
6479 break;
6480
6481 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006482 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 done = 1;
6484 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006487 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 done = 1;
6489 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006492 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493 done = 1;
6494 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006497 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006498 done = 1;
6499 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006501 case ACT_REPLACE:
6502 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006503 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6504 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 /* FIXME: if the user adds a newline in the replacement, the
6506 * index will not be recalculated for now, and the new line
6507 * will not be counted as a new header.
6508 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006509
Willy Tarreaufa355d42009-11-29 18:12:29 +01006510 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006512 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006513 HTTP_MSG_RQMETH,
6514 cur_ptr, cur_end + 1,
6515 NULL, NULL);
6516 if (unlikely(!cur_end))
6517 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 /* we have a full request and we know that we have either a CR
6520 * or an LF at <ptr>.
6521 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006522 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6523 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 /* there is no point trying this regex on headers */
6525 return 1;
6526 }
6527 }
6528 *cur_end = term; /* restore the string terminator */
6529 return done;
6530}
Willy Tarreau97de6242006-12-27 17:18:38 +01006531
Willy Tarreau58f10d72006-12-04 02:26:12 +01006532
Willy Tarreau58f10d72006-12-04 02:26:12 +01006533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006534/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006535 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 * unparsable request. Since it can manage the switch to another backend, it
6538 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006540int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006542 struct http_txn *txn = &s->txn;
6543 struct hdr_exp *exp;
6544
6545 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 /*
6549 * The interleaving of transformations and verdicts
6550 * makes it difficult to decide to continue or stop
6551 * the evaluation.
6552 */
6553
Willy Tarreau6c123b12010-01-28 20:22:06 +01006554 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6555 break;
6556
Willy Tarreau3d300592007-03-18 18:34:41 +01006557 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006558 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006559 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006561
6562 /* if this filter had a condition, evaluate it now and skip to
6563 * next filter if the condition does not match.
6564 */
6565 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006566 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006567 ret = acl_pass(ret);
6568 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6569 ret = !ret;
6570
6571 if (!ret)
6572 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006573 }
6574
6575 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006576 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006577 if (unlikely(ret < 0))
6578 return -1;
6579
6580 if (likely(ret == 0)) {
6581 /* The filter did not match the request, it can be
6582 * iterated through all headers.
6583 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006584 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006585 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006586 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006587 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588}
6589
6590
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591
Willy Tarreau58f10d72006-12-04 02:26:12 +01006592/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006593 * Try to retrieve the server associated to the appsession.
6594 * If the server is found, it's assigned to the session.
6595 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006596void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006597 struct http_txn *txn = &t->txn;
6598 appsess *asession = NULL;
6599 char *sessid_temp = NULL;
6600
Cyril Bontéb21570a2009-11-29 20:04:48 +01006601 if (len > t->be->appsession_len) {
6602 len = t->be->appsession_len;
6603 }
6604
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006605 if (t->be->options2 & PR_O2_AS_REQL) {
6606 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006607 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006608 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006609 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006610 }
6611
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006612 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006613 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6614 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6615 return;
6616 }
6617
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006618 memcpy(txn->sessid, buf, len);
6619 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006620 }
6621
6622 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6623 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6624 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6625 return;
6626 }
6627
Cyril Bontéb21570a2009-11-29 20:04:48 +01006628 memcpy(sessid_temp, buf, len);
6629 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006630
6631 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6632 /* free previously allocated memory */
6633 pool_free2(apools.sessid, sessid_temp);
6634
6635 if (asession != NULL) {
6636 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6637 if (!(t->be->options2 & PR_O2_AS_REQL))
6638 asession->request_count++;
6639
6640 if (asession->serverid != NULL) {
6641 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006642
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006643 while (srv) {
6644 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006645 if ((srv->state & SRV_RUNNING) ||
6646 (t->be->options & PR_O_PERSIST) ||
6647 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006648 /* we found the server and it's usable */
6649 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006650 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006651 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006652 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006653
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006654 break;
6655 } else {
6656 txn->flags &= ~TX_CK_MASK;
6657 txn->flags |= TX_CK_DOWN;
6658 }
6659 }
6660 srv = srv->next;
6661 }
6662 }
6663 }
6664}
6665
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666/* Find the end of a cookie value contained between <s> and <e>. It works the
6667 * same way as with headers above except that the semi-colon also ends a token.
6668 * See RFC2965 for more information. Note that it requires a valid header to
6669 * return a valid result.
6670 */
6671char *find_cookie_value_end(char *s, const char *e)
6672{
6673 int quoted, qdpair;
6674
6675 quoted = qdpair = 0;
6676 for (; s < e; s++) {
6677 if (qdpair) qdpair = 0;
6678 else if (quoted) {
6679 if (*s == '\\') qdpair = 1;
6680 else if (*s == '"') quoted = 0;
6681 }
6682 else if (*s == '"') quoted = 1;
6683 else if (*s == ',' || *s == ';') return s;
6684 }
6685 return s;
6686}
6687
6688/* Delete a value in a header between delimiters <from> and <next> in buffer
6689 * <buf>. The number of characters displaced is returned, and the pointer to
6690 * the first delimiter is updated if required. The function tries as much as
6691 * possible to respect the following principles :
6692 * - replace <from> delimiter by the <next> one unless <from> points to a
6693 * colon, in which case <next> is simply removed
6694 * - set exactly one space character after the new first delimiter, unless
6695 * there are not enough characters in the block being moved to do so.
6696 * - remove unneeded spaces before the previous delimiter and after the new
6697 * one.
6698 *
6699 * It is the caller's responsibility to ensure that :
6700 * - <from> points to a valid delimiter or the colon ;
6701 * - <next> points to a valid delimiter or the final CR/LF ;
6702 * - there are non-space chars before <from> ;
6703 * - there is a CR/LF at or after <next>.
6704 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006705int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006706{
6707 char *prev = *from;
6708
6709 if (*prev == ':') {
6710 /* We're removing the first value, preserve the colon and add a
6711 * space if possible.
6712 */
6713 if (!http_is_crlf[(unsigned char)*next])
6714 next++;
6715 prev++;
6716 if (prev < next)
6717 *prev++ = ' ';
6718
6719 while (http_is_spht[(unsigned char)*next])
6720 next++;
6721 } else {
6722 /* Remove useless spaces before the old delimiter. */
6723 while (http_is_spht[(unsigned char)*(prev-1)])
6724 prev--;
6725 *from = prev;
6726
6727 /* copy the delimiter and if possible a space if we're
6728 * not at the end of the line.
6729 */
6730 if (!http_is_crlf[(unsigned char)*next]) {
6731 *prev++ = *next++;
6732 if (prev + 1 < next)
6733 *prev++ = ' ';
6734 while (http_is_spht[(unsigned char)*next])
6735 next++;
6736 }
6737 }
6738 return buffer_replace2(buf, prev, next, NULL, 0);
6739}
6740
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006741/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006742 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 * desirable to call it only when needed. This code is quite complex because
6744 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6745 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006747void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006748{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006749 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006750 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006751 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006752 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6753 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006756 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006757 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006759 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006761 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006762
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006763 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 hdr_beg = hdr_next;
6765 hdr_end = hdr_beg + cur_hdr->len;
6766 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 /* We have one full header between hdr_beg and hdr_end, and the
6769 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770 * "Cookie:" headers.
6771 */
6772
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006773 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006774 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006775 old_idx = cur_idx;
6776 continue;
6777 }
6778
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006779 del_from = NULL; /* nothing to be deleted */
6780 preserve_hdr = 0; /* assume we may kill the whole header */
6781
Willy Tarreau58f10d72006-12-04 02:26:12 +01006782 /* Now look for cookies. Conforming to RFC2109, we have to support
6783 * attributes whose name begin with a '$', and associate them with
6784 * the right cookie, if we want to delete this cookie.
6785 * So there are 3 cases for each cookie read :
6786 * 1) it's a special attribute, beginning with a '$' : ignore it.
6787 * 2) it's a server id cookie that we *MAY* want to delete : save
6788 * some pointers on it (last semi-colon, beginning of cookie...)
6789 * 3) it's an application cookie : we *MAY* have to delete a previous
6790 * "special" cookie.
6791 * At the end of loop, if a "special" cookie remains, we may have to
6792 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 * *MUST* delete it.
6794 *
6795 * Note: RFC2965 is unclear about the processing of spaces around
6796 * the equal sign in the ATTR=VALUE form. A careful inspection of
6797 * the RFC explicitly allows spaces before it, and not within the
6798 * tokens (attrs or values). An inspection of RFC2109 allows that
6799 * too but section 10.1.3 lets one think that spaces may be allowed
6800 * after the equal sign too, resulting in some (rare) buggy
6801 * implementations trying to do that. So let's do what servers do.
6802 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6803 * allowed quoted strings in values, with any possible character
6804 * after a backslash, including control chars and delimitors, which
6805 * causes parsing to become ambiguous. Browsers also allow spaces
6806 * within values even without quotes.
6807 *
6808 * We have to keep multiple pointers in order to support cookie
6809 * removal at the beginning, middle or end of header without
6810 * corrupting the header. All of these headers are valid :
6811 *
6812 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6813 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6814 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6815 * | | | | | | | | |
6816 * | | | | | | | | hdr_end <--+
6817 * | | | | | | | +--> next
6818 * | | | | | | +----> val_end
6819 * | | | | | +-----------> val_beg
6820 * | | | | +--------------> equal
6821 * | | | +----------------> att_end
6822 * | | +---------------------> att_beg
6823 * | +--------------------------> prev
6824 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006825 */
6826
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006827 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6828 /* Iterate through all cookies on this line */
6829
6830 /* find att_beg */
6831 att_beg = prev + 1;
6832 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6833 att_beg++;
6834
6835 /* find att_end : this is the first character after the last non
6836 * space before the equal. It may be equal to hdr_end.
6837 */
6838 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006839
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 while (equal < hdr_end) {
6841 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006842 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006843 if (http_is_spht[(unsigned char)*equal++])
6844 continue;
6845 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006846 }
6847
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006848 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6849 * is between <att_beg> and <equal>, both may be identical.
6850 */
6851
6852 /* look for end of cookie if there is an equal sign */
6853 if (equal < hdr_end && *equal == '=') {
6854 /* look for the beginning of the value */
6855 val_beg = equal + 1;
6856 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6857 val_beg++;
6858
6859 /* find the end of the value, respecting quotes */
6860 next = find_cookie_value_end(val_beg, hdr_end);
6861
6862 /* make val_end point to the first white space or delimitor after the value */
6863 val_end = next;
6864 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6865 val_end--;
6866 } else {
6867 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006868 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006869
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 /* We have nothing to do with attributes beginning with '$'. However,
6871 * they will automatically be removed if a header before them is removed,
6872 * since they're supposed to be linked together.
6873 */
6874 if (*att_beg == '$')
6875 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006876
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 /* Ignore cookies with no equal sign */
6878 if (equal == next) {
6879 /* This is not our cookie, so we must preserve it. But if we already
6880 * scheduled another cookie for removal, we cannot remove the
6881 * complete header, but we can remove the previous block itself.
6882 */
6883 preserve_hdr = 1;
6884 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006885 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 val_end += delta;
6887 next += delta;
6888 hdr_end += delta;
6889 hdr_next += delta;
6890 cur_hdr->len += delta;
6891 http_msg_move_end(&txn->req, delta);
6892 prev = del_from;
6893 del_from = NULL;
6894 }
6895 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006896 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 /* if there are spaces around the equal sign, we need to
6899 * strip them otherwise we'll get trouble for cookie captures,
6900 * or even for rewrites. Since this happens extremely rarely,
6901 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006902 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6904 int stripped_before = 0;
6905 int stripped_after = 0;
6906
6907 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006908 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 equal += stripped_before;
6910 val_beg += stripped_before;
6911 }
6912
6913 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006914 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 val_beg += stripped_after;
6916 stripped_before += stripped_after;
6917 }
6918
6919 val_end += stripped_before;
6920 next += stripped_before;
6921 hdr_end += stripped_before;
6922 hdr_next += stripped_before;
6923 cur_hdr->len += stripped_before;
6924 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006928 /* First, let's see if we want to capture this cookie. We check
6929 * that we don't already have a client side cookie, because we
6930 * can only capture one. Also as an optimisation, we ignore
6931 * cookies shorter than the declared name.
6932 */
6933 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6934 (val_end - att_beg >= t->fe->capture_namelen) &&
6935 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6936 int log_len = val_end - att_beg;
6937
6938 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6939 Alert("HTTP logging : out of memory.\n");
6940 } else {
6941 if (log_len > t->fe->capture_len)
6942 log_len = t->fe->capture_len;
6943 memcpy(txn->cli_cookie, att_beg, log_len);
6944 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006947
Willy Tarreaubca99692010-10-06 19:25:55 +02006948 /* Persistence cookies in passive, rewrite or insert mode have the
6949 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006951 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006953 * For cookies in prefix mode, the form is :
6954 *
6955 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 */
6957 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6958 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6959 struct server *srv = t->be->srv;
6960 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6963 * have the server ID between val_beg and delim, and the original cookie between
6964 * delim+1 and val_end. Otherwise, delim==val_end :
6965 *
6966 * Cookie: NAME=SRV; # in all but prefix modes
6967 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6968 * | || || | |+-> next
6969 * | || || | +--> val_end
6970 * | || || +---------> delim
6971 * | || |+------------> val_beg
6972 * | || +-------------> att_end = equal
6973 * | |+-----------------> att_beg
6974 * | +------------------> prev
6975 * +-------------------------> hdr_beg
6976 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006977
Willy Tarreau67402132012-05-31 20:40:20 +02006978 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 for (delim = val_beg; delim < val_end; delim++)
6980 if (*delim == COOKIE_DELIM)
6981 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006982 } else {
6983 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006984 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006985 /* Now check if the cookie contains a date field, which would
6986 * appear after a vertical bar ('|') just after the server name
6987 * and before the delimiter.
6988 */
6989 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6990 if (vbar1) {
6991 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006992 * right is the last seen date. It is a base64 encoded
6993 * 30-bit value representing the UNIX date since the
6994 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006995 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006996 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006997 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006998 if (val_end - vbar1 >= 5) {
6999 val = b64tos30(vbar1);
7000 if (val > 0)
7001 txn->cookie_last_date = val << 2;
7002 }
7003 /* look for a second vertical bar */
7004 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7005 if (vbar1 && (val_end - vbar1 > 5)) {
7006 val = b64tos30(vbar1 + 1);
7007 if (val > 0)
7008 txn->cookie_first_date = val << 2;
7009 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007010 }
7011 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007012
Willy Tarreauf64d1412010-10-07 20:06:11 +02007013 /* if the cookie has an expiration date and the proxy wants to check
7014 * it, then we do that now. We first check if the cookie is too old,
7015 * then only if it has expired. We detect strict overflow because the
7016 * time resolution here is not great (4 seconds). Cookies with dates
7017 * in the future are ignored if their offset is beyond one day. This
7018 * allows an admin to fix timezone issues without expiring everyone
7019 * and at the same time avoids keeping unwanted side effects for too
7020 * long.
7021 */
7022 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007023 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7024 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007025 txn->flags &= ~TX_CK_MASK;
7026 txn->flags |= TX_CK_OLD;
7027 delim = val_beg; // let's pretend we have not found the cookie
7028 txn->cookie_first_date = 0;
7029 txn->cookie_last_date = 0;
7030 }
7031 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007032 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7033 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007034 txn->flags &= ~TX_CK_MASK;
7035 txn->flags |= TX_CK_EXPIRED;
7036 delim = val_beg; // let's pretend we have not found the cookie
7037 txn->cookie_first_date = 0;
7038 txn->cookie_last_date = 0;
7039 }
7040
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007041 /* Here, we'll look for the first running server which supports the cookie.
7042 * This allows to share a same cookie between several servers, for example
7043 * to dedicate backup servers to specific servers only.
7044 * However, to prevent clients from sticking to cookie-less backup server
7045 * when they have incidentely learned an empty cookie, we simply ignore
7046 * empty cookies and mark them as invalid.
7047 * The same behaviour is applied when persistence must be ignored.
7048 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007049 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007050 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007051
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007052 while (srv) {
7053 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7054 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7055 if ((srv->state & SRV_RUNNING) ||
7056 (t->be->options & PR_O_PERSIST) ||
7057 (t->flags & SN_FORCE_PRST)) {
7058 /* we found the server and we can use it */
7059 txn->flags &= ~TX_CK_MASK;
7060 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7061 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007062 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007063 break;
7064 } else {
7065 /* we found a server, but it's down,
7066 * mark it as such and go on in case
7067 * another one is available.
7068 */
7069 txn->flags &= ~TX_CK_MASK;
7070 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007073 srv = srv->next;
7074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075
Willy Tarreauf64d1412010-10-07 20:06:11 +02007076 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007077 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007078 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007079 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7080 txn->flags |= TX_CK_UNUSED;
7081 else
7082 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007083 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007084
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 /* depending on the cookie mode, we may have to either :
7086 * - delete the complete cookie if we're in insert+indirect mode, so that
7087 * the server never sees it ;
7088 * - remove the server id from the cookie value, and tag the cookie as an
7089 * application cookie so that it does not get accidentely removed later,
7090 * if we're in cookie prefix mode
7091 */
Willy Tarreau67402132012-05-31 20:40:20 +02007092 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007093 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreau9b28e032012-10-12 23:49:43 +02007095 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007096 val_end += delta;
7097 next += delta;
7098 hdr_end += delta;
7099 hdr_next += delta;
7100 cur_hdr->len += delta;
7101 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 del_from = NULL;
7104 preserve_hdr = 1; /* we want to keep this cookie */
7105 }
7106 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007107 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 del_from = prev;
7109 }
7110 } else {
7111 /* This is not our cookie, so we must preserve it. But if we already
7112 * scheduled another cookie for removal, we cannot remove the
7113 * complete header, but we can remove the previous block itself.
7114 */
7115 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007117 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007118 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007119 if (att_beg >= del_from)
7120 att_beg += delta;
7121 if (att_end >= del_from)
7122 att_end += delta;
7123 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 val_end += delta;
7125 next += delta;
7126 hdr_end += delta;
7127 hdr_next += delta;
7128 cur_hdr->len += delta;
7129 http_msg_move_end(&txn->req, delta);
7130 prev = del_from;
7131 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007134
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007135 /* Look for the appsession cookie unless persistence must be ignored */
7136 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7137 int cmp_len, value_len;
7138 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007139
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007140 if (t->be->options2 & PR_O2_AS_PFX) {
7141 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7142 value_begin = att_beg + t->be->appsession_name_len;
7143 value_len = val_end - att_beg - t->be->appsession_name_len;
7144 } else {
7145 cmp_len = att_end - att_beg;
7146 value_begin = val_beg;
7147 value_len = val_end - val_beg;
7148 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007149
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007150 /* let's see if the cookie is our appcookie */
7151 if (cmp_len == t->be->appsession_name_len &&
7152 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7153 manage_client_side_appsession(t, value_begin, value_len);
7154 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007155 }
7156
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007157 /* continue with next cookie on this header line */
7158 att_beg = next;
7159 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007161 /* There are no more cookies on this line.
7162 * We may still have one (or several) marked for deletion at the
7163 * end of the line. We must do this now in two ways :
7164 * - if some cookies must be preserved, we only delete from the
7165 * mark to the end of line ;
7166 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007167 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007168 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007169 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007170 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007171 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007172 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007173 cur_hdr->len += delta;
7174 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007175 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176
7177 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007178 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7179 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007180 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007181 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007182 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007183 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007184 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007185 }
7186
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007187 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007188 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007189 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007190}
7191
7192
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193/* Iterate the same filter through all response headers contained in <rtr>.
7194 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7195 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007196int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197{
7198 char term;
7199 char *cur_ptr, *cur_end, *cur_next;
7200 int cur_idx, old_idx, last_hdr;
7201 struct http_txn *txn = &t->txn;
7202 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007203 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204
7205 last_hdr = 0;
7206
Willy Tarreau9b28e032012-10-12 23:49:43 +02007207 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007208 old_idx = 0;
7209
7210 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007211 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007213 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 (exp->action == ACT_ALLOW ||
7215 exp->action == ACT_DENY))
7216 return 0;
7217
7218 cur_idx = txn->hdr_idx.v[old_idx].next;
7219 if (!cur_idx)
7220 break;
7221
7222 cur_hdr = &txn->hdr_idx.v[cur_idx];
7223 cur_ptr = cur_next;
7224 cur_end = cur_ptr + cur_hdr->len;
7225 cur_next = cur_end + cur_hdr->cr + 1;
7226
7227 /* Now we have one header between cur_ptr and cur_end,
7228 * and the next header starts at cur_next.
7229 */
7230
7231 /* The annoying part is that pattern matching needs
7232 * that we modify the contents to null-terminate all
7233 * strings before testing them.
7234 */
7235
7236 term = *cur_end;
7237 *cur_end = '\0';
7238
7239 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7240 switch (exp->action) {
7241 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007242 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 last_hdr = 1;
7244 break;
7245
7246 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007247 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248 last_hdr = 1;
7249 break;
7250
7251 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007252 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7253 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 /* FIXME: if the user adds a newline in the replacement, the
7255 * index will not be recalculated for now, and the new line
7256 * will not be counted as a new header.
7257 */
7258
7259 cur_end += delta;
7260 cur_next += delta;
7261 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007262 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 break;
7264
7265 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007266 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267 cur_next += delta;
7268
Willy Tarreaufa355d42009-11-29 18:12:29 +01007269 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7271 txn->hdr_idx.used--;
7272 cur_hdr->len = 0;
7273 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007274 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 break;
7276
7277 }
7278 }
7279 if (cur_end)
7280 *cur_end = term; /* restore the string terminator */
7281
7282 /* keep the link from this header to next one in case of later
7283 * removal of next header.
7284 */
7285 old_idx = cur_idx;
7286 }
7287 return 0;
7288}
7289
7290
7291/* Apply the filter to the status line in the response buffer <rtr>.
7292 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7293 * or -1 if a replacement resulted in an invalid status line.
7294 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007295int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296{
7297 char term;
7298 char *cur_ptr, *cur_end;
7299 int done;
7300 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007301 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302
7303
Willy Tarreau3d300592007-03-18 18:34:41 +01007304 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007306 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 (exp->action == ACT_ALLOW ||
7308 exp->action == ACT_DENY))
7309 return 0;
7310 else if (exp->action == ACT_REMOVE)
7311 return 0;
7312
7313 done = 0;
7314
Willy Tarreau9b28e032012-10-12 23:49:43 +02007315 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007316 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007317
7318 /* Now we have the status line between cur_ptr and cur_end */
7319
7320 /* The annoying part is that pattern matching needs
7321 * that we modify the contents to null-terminate all
7322 * strings before testing them.
7323 */
7324
7325 term = *cur_end;
7326 *cur_end = '\0';
7327
7328 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7329 switch (exp->action) {
7330 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007331 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007332 done = 1;
7333 break;
7334
7335 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007336 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 done = 1;
7338 break;
7339
7340 case ACT_REPLACE:
7341 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007342 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7343 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344 /* FIXME: if the user adds a newline in the replacement, the
7345 * index will not be recalculated for now, and the new line
7346 * will not be counted as a new header.
7347 */
7348
Willy Tarreaufa355d42009-11-29 18:12:29 +01007349 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007351 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007352 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 cur_ptr, cur_end + 1,
7354 NULL, NULL);
7355 if (unlikely(!cur_end))
7356 return -1;
7357
7358 /* we have a full respnse and we know that we have either a CR
7359 * or an LF at <ptr>.
7360 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007361 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007362 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007363 /* there is no point trying this regex on headers */
7364 return 1;
7365 }
7366 }
7367 *cur_end = term; /* restore the string terminator */
7368 return done;
7369}
7370
7371
7372
7373/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007374 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7376 * unparsable response.
7377 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007378int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007379{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007380 struct http_txn *txn = &s->txn;
7381 struct hdr_exp *exp;
7382
7383 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384 int ret;
7385
7386 /*
7387 * The interleaving of transformations and verdicts
7388 * makes it difficult to decide to continue or stop
7389 * the evaluation.
7390 */
7391
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007392 if (txn->flags & TX_SVDENY)
7393 break;
7394
Willy Tarreau3d300592007-03-18 18:34:41 +01007395 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7397 exp->action == ACT_PASS)) {
7398 exp = exp->next;
7399 continue;
7400 }
7401
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007402 /* if this filter had a condition, evaluate it now and skip to
7403 * next filter if the condition does not match.
7404 */
7405 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007406 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007407 ret = acl_pass(ret);
7408 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7409 ret = !ret;
7410 if (!ret)
7411 continue;
7412 }
7413
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007415 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 if (unlikely(ret < 0))
7417 return -1;
7418
7419 if (likely(ret == 0)) {
7420 /* The filter did not match the response, it can be
7421 * iterated through all headers.
7422 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007423 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007424 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007425 }
7426 return 0;
7427}
7428
7429
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007431 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007432 * desirable to call it only when needed. This function is also used when we
7433 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007434 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007435void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007436{
7437 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007438 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007439 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007441 char *hdr_beg, *hdr_end, *hdr_next;
7442 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444 /* Iterate through the headers.
7445 * we start with the start line.
7446 */
7447 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007448 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007449
7450 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7451 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007452 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453
7454 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007455 hdr_beg = hdr_next;
7456 hdr_end = hdr_beg + cur_hdr->len;
7457 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007458
Willy Tarreau24581ba2010-08-31 22:39:35 +02007459 /* We have one full header between hdr_beg and hdr_end, and the
7460 * next header starts at hdr_next. We're only interested in
7461 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007462 */
7463
Willy Tarreau24581ba2010-08-31 22:39:35 +02007464 is_cookie2 = 0;
7465 prev = hdr_beg + 10;
7466 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007467 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007468 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7469 if (!val) {
7470 old_idx = cur_idx;
7471 continue;
7472 }
7473 is_cookie2 = 1;
7474 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475 }
7476
Willy Tarreau24581ba2010-08-31 22:39:35 +02007477 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7478 * <prev> points to the colon.
7479 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007480 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007481
Willy Tarreau24581ba2010-08-31 22:39:35 +02007482 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7483 * check-cache is enabled) and we are not interested in checking
7484 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007485 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007486 if (t->be->cookie_name == NULL &&
7487 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007488 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007489 return;
7490
Willy Tarreau24581ba2010-08-31 22:39:35 +02007491 /* OK so now we know we have to process this response cookie.
7492 * The format of the Set-Cookie header is slightly different
7493 * from the format of the Cookie header in that it does not
7494 * support the comma as a cookie delimiter (thus the header
7495 * cannot be folded) because the Expires attribute described in
7496 * the original Netscape's spec may contain an unquoted date
7497 * with a comma inside. We have to live with this because
7498 * many browsers don't support Max-Age and some browsers don't
7499 * support quoted strings. However the Set-Cookie2 header is
7500 * clean.
7501 *
7502 * We have to keep multiple pointers in order to support cookie
7503 * removal at the beginning, middle or end of header without
7504 * corrupting the header (in case of set-cookie2). A special
7505 * pointer, <scav> points to the beginning of the set-cookie-av
7506 * fields after the first semi-colon. The <next> pointer points
7507 * either to the end of line (set-cookie) or next unquoted comma
7508 * (set-cookie2). All of these headers are valid :
7509 *
7510 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7511 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7512 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7513 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7514 * | | | | | | | | | |
7515 * | | | | | | | | +-> next hdr_end <--+
7516 * | | | | | | | +------------> scav
7517 * | | | | | | +--------------> val_end
7518 * | | | | | +--------------------> val_beg
7519 * | | | | +----------------------> equal
7520 * | | | +------------------------> att_end
7521 * | | +----------------------------> att_beg
7522 * | +------------------------------> prev
7523 * +-----------------------------------------> hdr_beg
7524 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525
Willy Tarreau24581ba2010-08-31 22:39:35 +02007526 for (; prev < hdr_end; prev = next) {
7527 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007528
Willy Tarreau24581ba2010-08-31 22:39:35 +02007529 /* find att_beg */
7530 att_beg = prev + 1;
7531 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7532 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007533
Willy Tarreau24581ba2010-08-31 22:39:35 +02007534 /* find att_end : this is the first character after the last non
7535 * space before the equal. It may be equal to hdr_end.
7536 */
7537 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007538
Willy Tarreau24581ba2010-08-31 22:39:35 +02007539 while (equal < hdr_end) {
7540 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7541 break;
7542 if (http_is_spht[(unsigned char)*equal++])
7543 continue;
7544 att_end = equal;
7545 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007546
Willy Tarreau24581ba2010-08-31 22:39:35 +02007547 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7548 * is between <att_beg> and <equal>, both may be identical.
7549 */
7550
7551 /* look for end of cookie if there is an equal sign */
7552 if (equal < hdr_end && *equal == '=') {
7553 /* look for the beginning of the value */
7554 val_beg = equal + 1;
7555 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7556 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557
Willy Tarreau24581ba2010-08-31 22:39:35 +02007558 /* find the end of the value, respecting quotes */
7559 next = find_cookie_value_end(val_beg, hdr_end);
7560
7561 /* make val_end point to the first white space or delimitor after the value */
7562 val_end = next;
7563 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7564 val_end--;
7565 } else {
7566 /* <equal> points to next comma, semi-colon or EOL */
7567 val_beg = val_end = next = equal;
7568 }
7569
7570 if (next < hdr_end) {
7571 /* Set-Cookie2 supports multiple cookies, and <next> points to
7572 * a colon or semi-colon before the end. So skip all attr-value
7573 * pairs and look for the next comma. For Set-Cookie, since
7574 * commas are permitted in values, skip to the end.
7575 */
7576 if (is_cookie2)
7577 next = find_hdr_value_end(next, hdr_end);
7578 else
7579 next = hdr_end;
7580 }
7581
7582 /* Now everything is as on the diagram above */
7583
7584 /* Ignore cookies with no equal sign */
7585 if (equal == val_end)
7586 continue;
7587
7588 /* If there are spaces around the equal sign, we need to
7589 * strip them otherwise we'll get trouble for cookie captures,
7590 * or even for rewrites. Since this happens extremely rarely,
7591 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007592 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007593 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7594 int stripped_before = 0;
7595 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007596
Willy Tarreau24581ba2010-08-31 22:39:35 +02007597 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007598 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007599 equal += stripped_before;
7600 val_beg += stripped_before;
7601 }
7602
7603 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007604 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007605 val_beg += stripped_after;
7606 stripped_before += stripped_after;
7607 }
7608
7609 val_end += stripped_before;
7610 next += stripped_before;
7611 hdr_end += stripped_before;
7612 hdr_next += stripped_before;
7613 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007614 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007615 }
7616
7617 /* First, let's see if we want to capture this cookie. We check
7618 * that we don't already have a server side cookie, because we
7619 * can only capture one. Also as an optimisation, we ignore
7620 * cookies shorter than the declared name.
7621 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007622 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007623 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007624 (val_end - att_beg >= t->fe->capture_namelen) &&
7625 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7626 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007627 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628 Alert("HTTP logging : out of memory.\n");
7629 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007630 else {
7631 if (log_len > t->fe->capture_len)
7632 log_len = t->fe->capture_len;
7633 memcpy(txn->srv_cookie, att_beg, log_len);
7634 txn->srv_cookie[log_len] = 0;
7635 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007636 }
7637
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007638 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007640 if (!(t->flags & SN_IGNORE_PRST) &&
7641 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7642 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007643 /* assume passive cookie by default */
7644 txn->flags &= ~TX_SCK_MASK;
7645 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007646
7647 /* If the cookie is in insert mode on a known server, we'll delete
7648 * this occurrence because we'll insert another one later.
7649 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007650 * a direct access.
7651 */
Willy Tarreau67402132012-05-31 20:40:20 +02007652 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007653 /* The "preserve" flag was set, we don't want to touch the
7654 * server's cookie.
7655 */
7656 }
Willy Tarreau67402132012-05-31 20:40:20 +02007657 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7658 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007659 /* this cookie must be deleted */
7660 if (*prev == ':' && next == hdr_end) {
7661 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007662 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007663 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7664 txn->hdr_idx.used--;
7665 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007666 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 hdr_next += delta;
7668 http_msg_move_end(&txn->rsp, delta);
7669 /* note: while both invalid now, <next> and <hdr_end>
7670 * are still equal, so the for() will stop as expected.
7671 */
7672 } else {
7673 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007674 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007675 next = prev;
7676 hdr_end += delta;
7677 hdr_next += delta;
7678 cur_hdr->len += delta;
7679 http_msg_move_end(&txn->rsp, delta);
7680 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007681 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007682 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007683 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007684 }
Willy Tarreau67402132012-05-31 20:40:20 +02007685 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007686 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687 * with this server since we know it.
7688 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007689 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007690 next += delta;
7691 hdr_end += delta;
7692 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007693 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007694 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695
Willy Tarreauf1348312010-10-07 15:54:11 +02007696 txn->flags &= ~TX_SCK_MASK;
7697 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007698 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007699 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007700 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007701 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007703 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007704 next += delta;
7705 hdr_end += delta;
7706 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007707 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007708 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007709
Willy Tarreau827aee92011-03-10 16:55:02 +01007710 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007711 txn->flags &= ~TX_SCK_MASK;
7712 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007713 }
7714 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007715 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7716 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007717 int cmp_len, value_len;
7718 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007719
Cyril Bontéb21570a2009-11-29 20:04:48 +01007720 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007721 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7722 value_begin = att_beg + t->be->appsession_name_len;
7723 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007724 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007725 cmp_len = att_end - att_beg;
7726 value_begin = val_beg;
7727 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007728 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007729
Cyril Bonté17530c32010-04-06 21:11:10 +02007730 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007731 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7732 /* free a possibly previously allocated memory */
7733 pool_free2(apools.sessid, txn->sessid);
7734
Cyril Bontéb21570a2009-11-29 20:04:48 +01007735 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007736 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007737 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7738 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7739 return;
7740 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007741 memcpy(txn->sessid, value_begin, value_len);
7742 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007744 }
7745 /* that's done for this cookie, check the next one on the same
7746 * line when next != hdr_end (only if is_cookie2).
7747 */
7748 }
7749 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007750 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007751 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007752
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007753 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007754 appsess *asession = NULL;
7755 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007756 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007757 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007758 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007759 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7760 Alert("Not enough Memory process_srv():asession:calloc().\n");
7761 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7762 return;
7763 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007764 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7765
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007766 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7767 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7768 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007769 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007770 return;
7771 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007772 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007773 asession->sessid[t->be->appsession_len] = 0;
7774
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007775 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007776 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007777 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007778 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007779 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007780 return;
7781 }
7782 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007783 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007784
7785 asession->request_count = 0;
7786 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7787 }
7788
7789 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7790 asession->request_count++;
7791 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007792}
7793
7794
Willy Tarreaua15645d2007-03-18 16:22:39 +01007795/*
7796 * Check if response is cacheable or not. Updates t->flags.
7797 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007798void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799{
7800 struct http_txn *txn = &t->txn;
7801 char *p1, *p2;
7802
7803 char *cur_ptr, *cur_end, *cur_next;
7804 int cur_idx;
7805
Willy Tarreau5df51872007-11-25 16:20:08 +01007806 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007807 return;
7808
7809 /* Iterate through the headers.
7810 * we start with the start line.
7811 */
7812 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007814
7815 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7816 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007817 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818
7819 cur_hdr = &txn->hdr_idx.v[cur_idx];
7820 cur_ptr = cur_next;
7821 cur_end = cur_ptr + cur_hdr->len;
7822 cur_next = cur_end + cur_hdr->cr + 1;
7823
7824 /* We have one full header between cur_ptr and cur_end, and the
7825 * next header starts at cur_next. We're only interested in
7826 * "Cookie:" headers.
7827 */
7828
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007829 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7830 if (val) {
7831 if ((cur_end - (cur_ptr + val) >= 8) &&
7832 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7833 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7834 return;
7835 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007836 }
7837
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007838 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7839 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840 continue;
7841
7842 /* OK, right now we know we have a cache-control header at cur_ptr */
7843
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007844 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845
7846 if (p1 >= cur_end) /* no more info */
7847 continue;
7848
7849 /* p1 is at the beginning of the value */
7850 p2 = p1;
7851
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007852 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 p2++;
7854
7855 /* we have a complete value between p1 and p2 */
7856 if (p2 < cur_end && *p2 == '=') {
7857 /* we have something of the form no-cache="set-cookie" */
7858 if ((cur_end - p1 >= 21) &&
7859 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7860 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007861 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 continue;
7863 }
7864
7865 /* OK, so we know that either p2 points to the end of string or to a comma */
7866 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007867 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7869 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7870 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007871 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872 return;
7873 }
7874
7875 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007876 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877 continue;
7878 }
7879 }
7880}
7881
7882
Willy Tarreau58f10d72006-12-04 02:26:12 +01007883/*
7884 * Try to retrieve a known appsession in the URI, then the associated server.
7885 * If the server is found, it's assigned to the session.
7886 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007887void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007889 char *end_params, *first_param, *cur_param, *next_param;
7890 char separator;
7891 int value_len;
7892
7893 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007894
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007895 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007896 (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 +01007897 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007898 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007899
Cyril Bontéb21570a2009-11-29 20:04:48 +01007900 first_param = NULL;
7901 switch (mode) {
7902 case PR_O2_AS_M_PP:
7903 first_param = memchr(begin, ';', len);
7904 break;
7905 case PR_O2_AS_M_QS:
7906 first_param = memchr(begin, '?', len);
7907 break;
7908 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007909
Cyril Bontéb21570a2009-11-29 20:04:48 +01007910 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007911 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007912 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007913
Cyril Bontéb21570a2009-11-29 20:04:48 +01007914 switch (mode) {
7915 case PR_O2_AS_M_PP:
7916 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7917 end_params = (char *) begin + len;
7918 }
7919 separator = ';';
7920 break;
7921 case PR_O2_AS_M_QS:
7922 end_params = (char *) begin + len;
7923 separator = '&';
7924 break;
7925 default:
7926 /* unknown mode, shouldn't happen */
7927 return;
7928 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007929
Cyril Bontéb21570a2009-11-29 20:04:48 +01007930 cur_param = next_param = end_params;
7931 while (cur_param > first_param) {
7932 cur_param--;
7933 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7934 /* let's see if this is the appsession parameter */
7935 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7936 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7937 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7938 /* Cool... it's the right one */
7939 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7940 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7941 if (value_len > 0) {
7942 manage_client_side_appsession(t, cur_param, value_len);
7943 }
7944 break;
7945 }
7946 next_param = cur_param;
7947 }
7948 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007949#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007950 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007951 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007952#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007953}
7954
Willy Tarreaub2513902006-12-17 14:52:38 +01007955/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007956 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007957 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007958 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007959 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007960 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007961 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007962 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007963 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007964int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007965{
7966 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007967 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007968 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007969
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007970 if (!uri_auth)
7971 return 0;
7972
Cyril Bonté70be45d2010-10-12 00:14:35 +02007973 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007974 return 0;
7975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007976 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007977 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007978 return 0;
7979
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007980 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007981 return 0;
7982
Willy Tarreaub2513902006-12-17 14:52:38 +01007983 return 1;
7984}
7985
Willy Tarreau4076a152009-04-02 15:18:36 +02007986/*
7987 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007988 * By default it tries to report the error position as msg->err_pos. However if
7989 * this one is not set, it will then report msg->next, which is the last known
7990 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007991 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007992 */
7993void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007994 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007995 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007996{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007997 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007998 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007999
Willy Tarreau9b28e032012-10-12 23:49:43 +02008000 es->len = MIN(chn->buf->i, sizeof(es->buf));
8001 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008002 len1 = MIN(len1, es->len);
8003 len2 = es->len - len1; /* remaining data if buffer wraps */
8004
Willy Tarreau9b28e032012-10-12 23:49:43 +02008005 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008006 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008007 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008008
Willy Tarreau4076a152009-04-02 15:18:36 +02008009 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008010 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008011 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008012 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008013
Willy Tarreau4076a152009-04-02 15:18:36 +02008014 es->when = date; // user-visible date
8015 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008016 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008017 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008018 if (objt_conn(s->req->prod->end))
8019 es->src = __objt_conn(s->req->prod->end)->addr.from;
8020 else
8021 memset(&es->src, 0, sizeof(es->src));
8022
Willy Tarreau078272e2010-12-12 12:46:33 +01008023 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008024 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008025 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008026 es->s_flags = s->flags;
8027 es->t_flags = s->txn.flags;
8028 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008029 es->b_out = chn->buf->o;
8030 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008031 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008032 es->m_clen = msg->chunk_len;
8033 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008034}
Willy Tarreaub2513902006-12-17 14:52:38 +01008035
Willy Tarreau294c4732011-12-16 21:35:50 +01008036/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8037 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8038 * performed over the whole headers. Otherwise it must contain a valid header
8039 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8040 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8041 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8042 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008043 * -1. The value fetch stops at commas, so this function is suited for use with
8044 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008045 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008046 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008047unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008048 struct hdr_idx *idx, int occ,
8049 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008050{
Willy Tarreau294c4732011-12-16 21:35:50 +01008051 struct hdr_ctx local_ctx;
8052 char *ptr_hist[MAX_HDR_HISTORY];
8053 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008054 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008055 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008056
Willy Tarreau294c4732011-12-16 21:35:50 +01008057 if (!ctx) {
8058 local_ctx.idx = 0;
8059 ctx = &local_ctx;
8060 }
8061
Willy Tarreaubce70882009-09-07 11:51:47 +02008062 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008063 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008064 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008065 occ--;
8066 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008067 *vptr = ctx->line + ctx->val;
8068 *vlen = ctx->vlen;
8069 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008070 }
8071 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008072 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008073 }
8074
8075 /* negative occurrence, we scan all the list then walk back */
8076 if (-occ > MAX_HDR_HISTORY)
8077 return 0;
8078
Willy Tarreau294c4732011-12-16 21:35:50 +01008079 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008080 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008081 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8082 len_hist[hist_ptr] = ctx->vlen;
8083 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008084 hist_ptr = 0;
8085 found++;
8086 }
8087 if (-occ > found)
8088 return 0;
8089 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008090 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8091 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8092 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008093 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008094 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008095 if (hist_ptr >= MAX_HDR_HISTORY)
8096 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008097 *vptr = ptr_hist[hist_ptr];
8098 *vlen = len_hist[hist_ptr];
8099 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008100}
8101
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008102/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8103 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8104 * performed over the whole headers. Otherwise it must contain a valid header
8105 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8106 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8107 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8108 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8109 * -1. This function differs from http_get_hdr() in that it only returns full
8110 * line header values and does not stop at commas.
8111 * The return value is 0 if nothing was found, or non-zero otherwise.
8112 */
8113unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8114 struct hdr_idx *idx, int occ,
8115 struct hdr_ctx *ctx, char **vptr, int *vlen)
8116{
8117 struct hdr_ctx local_ctx;
8118 char *ptr_hist[MAX_HDR_HISTORY];
8119 int len_hist[MAX_HDR_HISTORY];
8120 unsigned int hist_ptr;
8121 int found;
8122
8123 if (!ctx) {
8124 local_ctx.idx = 0;
8125 ctx = &local_ctx;
8126 }
8127
8128 if (occ >= 0) {
8129 /* search from the beginning */
8130 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8131 occ--;
8132 if (occ <= 0) {
8133 *vptr = ctx->line + ctx->val;
8134 *vlen = ctx->vlen;
8135 return 1;
8136 }
8137 }
8138 return 0;
8139 }
8140
8141 /* negative occurrence, we scan all the list then walk back */
8142 if (-occ > MAX_HDR_HISTORY)
8143 return 0;
8144
8145 found = hist_ptr = 0;
8146 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8147 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8148 len_hist[hist_ptr] = ctx->vlen;
8149 if (++hist_ptr >= MAX_HDR_HISTORY)
8150 hist_ptr = 0;
8151 found++;
8152 }
8153 if (-occ > found)
8154 return 0;
8155 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8156 * find occurrence -occ, so we have to check [hist_ptr+occ].
8157 */
8158 hist_ptr += occ;
8159 if (hist_ptr >= MAX_HDR_HISTORY)
8160 hist_ptr -= MAX_HDR_HISTORY;
8161 *vptr = ptr_hist[hist_ptr];
8162 *vlen = len_hist[hist_ptr];
8163 return 1;
8164}
8165
Willy Tarreaubaaee002006-06-26 02:48:02 +02008166/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008167 * Print a debug line with a header. Always stop at the first CR or LF char,
8168 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8169 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008170 */
8171void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8172{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008173 int max;
8174 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008175 dir,
8176 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8177 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 +02008178
8179 for (max = 0; start + max < end; max++)
8180 if (start[max] == '\r' || start[max] == '\n')
8181 break;
8182
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008183 UBOUND(max, trash.size - trash.len - 3);
8184 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8185 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008186 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008187}
8188
Willy Tarreau0937bc42009-12-22 15:03:09 +01008189/*
8190 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8191 * the required fields are properly allocated and that we only need to (re)init
8192 * them. This should be used before processing any new request.
8193 */
8194void http_init_txn(struct session *s)
8195{
8196 struct http_txn *txn = &s->txn;
8197 struct proxy *fe = s->fe;
8198
8199 txn->flags = 0;
8200 txn->status = -1;
8201
Willy Tarreauf64d1412010-10-07 20:06:11 +02008202 txn->cookie_first_date = 0;
8203 txn->cookie_last_date = 0;
8204
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008205 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008206 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008207 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008208 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008209 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008210 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008211 txn->req.chunk_len = 0LL;
8212 txn->req.body_len = 0LL;
8213 txn->rsp.chunk_len = 0LL;
8214 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008215 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8216 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008217 txn->req.chn = s->req;
8218 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008219
8220 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008221
8222 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8223 if (fe->options2 & PR_O2_REQBUG_OK)
8224 txn->req.err_pos = -1; /* let buggy requests pass */
8225
Willy Tarreau46023632010-01-07 22:51:47 +01008226 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008227 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8228
Willy Tarreau46023632010-01-07 22:51:47 +01008229 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008230 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8231
8232 if (txn->hdr_idx.v)
8233 hdr_idx_init(&txn->hdr_idx);
8234}
8235
8236/* to be used at the end of a transaction */
8237void http_end_txn(struct session *s)
8238{
8239 struct http_txn *txn = &s->txn;
8240
Willy Tarreau75195602014-03-11 15:48:55 +01008241 /* release any possible compression context */
8242 if (s->flags & SN_COMP_READY)
8243 s->comp_algo->end(&s->comp_ctx);
8244 s->comp_algo = NULL;
8245 s->flags &= ~SN_COMP_READY;
8246
Willy Tarreau0937bc42009-12-22 15:03:09 +01008247 /* these ones will have been dynamically allocated */
8248 pool_free2(pool2_requri, txn->uri);
8249 pool_free2(pool2_capture, txn->cli_cookie);
8250 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008251 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008252 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008253
William Lallemanda73203e2012-03-12 12:48:57 +01008254 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008255 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008256 txn->uri = NULL;
8257 txn->srv_cookie = NULL;
8258 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008259
8260 if (txn->req.cap) {
8261 struct cap_hdr *h;
8262 for (h = s->fe->req_cap; h; h = h->next)
8263 pool_free2(h->pool, txn->req.cap[h->index]);
8264 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8265 }
8266
8267 if (txn->rsp.cap) {
8268 struct cap_hdr *h;
8269 for (h = s->fe->rsp_cap; h; h = h->next)
8270 pool_free2(h->pool, txn->rsp.cap[h->index]);
8271 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8272 }
8273
Willy Tarreau0937bc42009-12-22 15:03:09 +01008274}
8275
8276/* to be used at the end of a transaction to prepare a new one */
8277void http_reset_txn(struct session *s)
8278{
8279 http_end_txn(s);
8280 http_init_txn(s);
8281
8282 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008283 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008284 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008285 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008286 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008287 /* re-init store persistence */
8288 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008289 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008290
Willy Tarreau0937bc42009-12-22 15:03:09 +01008291 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008292
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008293 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008294
Willy Tarreau739cfba2010-01-25 23:11:14 +01008295 /* We must trim any excess data from the response buffer, because we
8296 * may have blocked an invalid response from a server that we don't
8297 * want to accidentely forward once we disable the analysers, nor do
8298 * we want those data to come along with next response. A typical
8299 * example of such data would be from a buggy server responding to
8300 * a HEAD with some data, or sending more than the advertised
8301 * content-length.
8302 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008303 if (unlikely(s->rep->buf->i))
8304 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008305
Willy Tarreau0937bc42009-12-22 15:03:09 +01008306 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008307 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008308
Willy Tarreaud04e8582010-05-31 12:31:35 +02008309 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008310 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008311
8312 s->req->rex = TICK_ETERNITY;
8313 s->req->wex = TICK_ETERNITY;
8314 s->req->analyse_exp = TICK_ETERNITY;
8315 s->rep->rex = TICK_ETERNITY;
8316 s->rep->wex = TICK_ETERNITY;
8317 s->rep->analyse_exp = TICK_ETERNITY;
8318}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008319
Willy Tarreauff011f22011-01-06 17:51:27 +01008320void free_http_req_rules(struct list *r) {
8321 struct http_req_rule *tr, *pr;
8322
8323 list_for_each_entry_safe(pr, tr, r, list) {
8324 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008325 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008326 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008327
8328 free(pr);
8329 }
8330}
8331
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008332/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008333struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8334{
8335 struct http_req_rule *rule;
8336 int cur_arg;
8337
8338 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8339 if (!rule) {
8340 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008341 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008342 }
8343
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008344 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008345 rule->action = HTTP_REQ_ACT_ALLOW;
8346 cur_arg = 1;
8347 } else if (!strcmp(args[0], "deny")) {
8348 rule->action = HTTP_REQ_ACT_DENY;
8349 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008350 } else if (!strcmp(args[0], "tarpit")) {
8351 rule->action = HTTP_REQ_ACT_TARPIT;
8352 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008353 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008354 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008355 cur_arg = 1;
8356
8357 while(*args[cur_arg]) {
8358 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008359 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008360 cur_arg+=2;
8361 continue;
8362 } else
8363 break;
8364 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008365 } else if (!strcmp(args[0], "set-nice")) {
8366 rule->action = HTTP_REQ_ACT_SET_NICE;
8367 cur_arg = 1;
8368
8369 if (!*args[cur_arg] ||
8370 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8371 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8372 file, linenum, args[0]);
8373 goto out_err;
8374 }
8375 rule->arg.nice = atoi(args[cur_arg]);
8376 if (rule->arg.nice < -1024)
8377 rule->arg.nice = -1024;
8378 else if (rule->arg.nice > 1024)
8379 rule->arg.nice = 1024;
8380 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008381 } else if (!strcmp(args[0], "set-tos")) {
8382#ifdef IP_TOS
8383 char *err;
8384 rule->action = HTTP_REQ_ACT_SET_TOS;
8385 cur_arg = 1;
8386
8387 if (!*args[cur_arg] ||
8388 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8389 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8390 file, linenum, args[0]);
8391 goto out_err;
8392 }
8393
8394 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8395 if (err && *err != '\0') {
8396 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8397 file, linenum, err, args[0]);
8398 goto out_err;
8399 }
8400 cur_arg++;
8401#else
8402 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8403 goto out_err;
8404#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008405 } else if (!strcmp(args[0], "set-mark")) {
8406#ifdef SO_MARK
8407 char *err;
8408 rule->action = HTTP_REQ_ACT_SET_MARK;
8409 cur_arg = 1;
8410
8411 if (!*args[cur_arg] ||
8412 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8413 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8414 file, linenum, args[0]);
8415 goto out_err;
8416 }
8417
8418 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8419 if (err && *err != '\0') {
8420 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8421 file, linenum, err, args[0]);
8422 goto out_err;
8423 }
8424 cur_arg++;
8425 global.last_checks |= LSTCHK_NETADM;
8426#else
8427 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8428 goto out_err;
8429#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008430 } else if (!strcmp(args[0], "set-log-level")) {
8431 rule->action = HTTP_REQ_ACT_SET_LOGL;
8432 cur_arg = 1;
8433
8434 if (!*args[cur_arg] ||
8435 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8436 bad_log_level:
8437 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8438 file, linenum, args[0]);
8439 goto out_err;
8440 }
8441 if (strcmp(args[cur_arg], "silent") == 0)
8442 rule->arg.loglevel = -1;
8443 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8444 goto bad_log_level;
8445 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008446 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8447 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8448 cur_arg = 1;
8449
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008450 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8451 (*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 +01008452 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8453 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008454 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008455 }
8456
8457 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8458 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8459 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008460
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008461 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008462 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8463 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008464 free(proxy->conf.lfs_file);
8465 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8466 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008467 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008468 } else if (strcmp(args[0], "redirect") == 0) {
8469 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008470 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008471
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008472 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008473 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8474 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8475 goto out_err;
8476 }
8477
8478 /* this redirect rule might already contain a parsed condition which
8479 * we'll pass to the http-request rule.
8480 */
8481 rule->action = HTTP_REQ_ACT_REDIR;
8482 rule->arg.redir = redir;
8483 rule->cond = redir->cond;
8484 redir->cond = NULL;
8485 cur_arg = 2;
8486 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008487 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008488 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 +01008489 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008490 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008491 }
8492
8493 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8494 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008495 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008496
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008497 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8498 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8499 file, linenum, args[0], errmsg);
8500 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008501 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008502 }
8503 rule->cond = cond;
8504 }
8505 else if (*args[cur_arg]) {
8506 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8507 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8508 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008509 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008510 }
8511
8512 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008513 out_err:
8514 free(rule);
8515 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008516}
8517
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008518/* parse an "http-respose" rule */
8519struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8520{
8521 struct http_res_rule *rule;
8522 int cur_arg;
8523
8524 rule = calloc(1, sizeof(*rule));
8525 if (!rule) {
8526 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8527 goto out_err;
8528 }
8529
8530 if (!strcmp(args[0], "allow")) {
8531 rule->action = HTTP_RES_ACT_ALLOW;
8532 cur_arg = 1;
8533 } else if (!strcmp(args[0], "deny")) {
8534 rule->action = HTTP_RES_ACT_DENY;
8535 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008536 } else if (!strcmp(args[0], "set-nice")) {
8537 rule->action = HTTP_RES_ACT_SET_NICE;
8538 cur_arg = 1;
8539
8540 if (!*args[cur_arg] ||
8541 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8542 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8543 file, linenum, args[0]);
8544 goto out_err;
8545 }
8546 rule->arg.nice = atoi(args[cur_arg]);
8547 if (rule->arg.nice < -1024)
8548 rule->arg.nice = -1024;
8549 else if (rule->arg.nice > 1024)
8550 rule->arg.nice = 1024;
8551 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008552 } else if (!strcmp(args[0], "set-tos")) {
8553#ifdef IP_TOS
8554 char *err;
8555 rule->action = HTTP_RES_ACT_SET_TOS;
8556 cur_arg = 1;
8557
8558 if (!*args[cur_arg] ||
8559 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8560 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8561 file, linenum, args[0]);
8562 goto out_err;
8563 }
8564
8565 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8566 if (err && *err != '\0') {
8567 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8568 file, linenum, err, args[0]);
8569 goto out_err;
8570 }
8571 cur_arg++;
8572#else
8573 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8574 goto out_err;
8575#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008576 } else if (!strcmp(args[0], "set-mark")) {
8577#ifdef SO_MARK
8578 char *err;
8579 rule->action = HTTP_RES_ACT_SET_MARK;
8580 cur_arg = 1;
8581
8582 if (!*args[cur_arg] ||
8583 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8584 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8585 file, linenum, args[0]);
8586 goto out_err;
8587 }
8588
8589 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8590 if (err && *err != '\0') {
8591 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8592 file, linenum, err, args[0]);
8593 goto out_err;
8594 }
8595 cur_arg++;
8596 global.last_checks |= LSTCHK_NETADM;
8597#else
8598 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8599 goto out_err;
8600#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008601 } else if (!strcmp(args[0], "set-log-level")) {
8602 rule->action = HTTP_RES_ACT_SET_LOGL;
8603 cur_arg = 1;
8604
8605 if (!*args[cur_arg] ||
8606 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8607 bad_log_level:
8608 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8609 file, linenum, args[0]);
8610 goto out_err;
8611 }
8612 if (strcmp(args[cur_arg], "silent") == 0)
8613 rule->arg.loglevel = -1;
8614 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8615 goto bad_log_level;
8616 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008617 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8618 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8619 cur_arg = 1;
8620
8621 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8622 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8623 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8624 file, linenum, args[0]);
8625 goto out_err;
8626 }
8627
8628 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8629 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8630 LIST_INIT(&rule->arg.hdr_add.fmt);
8631
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008632 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008633 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8634 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008635 free(proxy->conf.lfs_file);
8636 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8637 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008638 cur_arg += 2;
8639 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008640 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 +02008641 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8642 goto out_err;
8643 }
8644
8645 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8646 struct acl_cond *cond;
8647 char *errmsg = NULL;
8648
8649 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8650 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8651 file, linenum, args[0], errmsg);
8652 free(errmsg);
8653 goto out_err;
8654 }
8655 rule->cond = cond;
8656 }
8657 else if (*args[cur_arg]) {
8658 Alert("parsing [%s:%d]: 'http-response %s' expects"
8659 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8660 file, linenum, args[0], args[cur_arg]);
8661 goto out_err;
8662 }
8663
8664 return rule;
8665 out_err:
8666 free(rule);
8667 return NULL;
8668}
8669
Willy Tarreau4baae242012-12-27 12:00:31 +01008670/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008671 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8672 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008673 */
8674struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008675 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008676{
8677 struct redirect_rule *rule;
8678 int cur_arg;
8679 int type = REDIRECT_TYPE_NONE;
8680 int code = 302;
8681 const char *destination = NULL;
8682 const char *cookie = NULL;
8683 int cookie_set = 0;
8684 unsigned int flags = REDIRECT_FLAG_NONE;
8685 struct acl_cond *cond = NULL;
8686
8687 cur_arg = 0;
8688 while (*(args[cur_arg])) {
8689 if (strcmp(args[cur_arg], "location") == 0) {
8690 if (!*args[cur_arg + 1])
8691 goto missing_arg;
8692
8693 type = REDIRECT_TYPE_LOCATION;
8694 cur_arg++;
8695 destination = args[cur_arg];
8696 }
8697 else if (strcmp(args[cur_arg], "prefix") == 0) {
8698 if (!*args[cur_arg + 1])
8699 goto missing_arg;
8700
8701 type = REDIRECT_TYPE_PREFIX;
8702 cur_arg++;
8703 destination = args[cur_arg];
8704 }
8705 else if (strcmp(args[cur_arg], "scheme") == 0) {
8706 if (!*args[cur_arg + 1])
8707 goto missing_arg;
8708
8709 type = REDIRECT_TYPE_SCHEME;
8710 cur_arg++;
8711 destination = args[cur_arg];
8712 }
8713 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8714 if (!*args[cur_arg + 1])
8715 goto missing_arg;
8716
8717 cur_arg++;
8718 cookie = args[cur_arg];
8719 cookie_set = 1;
8720 }
8721 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8722 if (!*args[cur_arg + 1])
8723 goto missing_arg;
8724
8725 cur_arg++;
8726 cookie = args[cur_arg];
8727 cookie_set = 0;
8728 }
8729 else if (strcmp(args[cur_arg], "code") == 0) {
8730 if (!*args[cur_arg + 1])
8731 goto missing_arg;
8732
8733 cur_arg++;
8734 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008735 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008736 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008737 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008738 args[cur_arg - 1], args[cur_arg]);
8739 return NULL;
8740 }
8741 }
8742 else if (!strcmp(args[cur_arg],"drop-query")) {
8743 flags |= REDIRECT_FLAG_DROP_QS;
8744 }
8745 else if (!strcmp(args[cur_arg],"append-slash")) {
8746 flags |= REDIRECT_FLAG_APPEND_SLASH;
8747 }
8748 else if (strcmp(args[cur_arg], "if") == 0 ||
8749 strcmp(args[cur_arg], "unless") == 0) {
8750 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8751 if (!cond) {
8752 memprintf(errmsg, "error in condition: %s", *errmsg);
8753 return NULL;
8754 }
8755 break;
8756 }
8757 else {
8758 memprintf(errmsg,
8759 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8760 args[cur_arg]);
8761 return NULL;
8762 }
8763 cur_arg++;
8764 }
8765
8766 if (type == REDIRECT_TYPE_NONE) {
8767 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8768 return NULL;
8769 }
8770
8771 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8772 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008773 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008774
8775 if (!use_fmt) {
8776 /* old-style static redirect rule */
8777 rule->rdr_str = strdup(destination);
8778 rule->rdr_len = strlen(destination);
8779 }
8780 else {
8781 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008782
8783 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8784 * if prefix == "/", we don't want to add anything, otherwise it
8785 * makes it hard for the user to configure a self-redirection.
8786 */
8787 proxy->conf.args.ctx = ARGC_RDR;
8788 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8789 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8790 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008791 free(curproxy->conf.lfs_file);
8792 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8793 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008794 }
8795 }
8796
Willy Tarreau4baae242012-12-27 12:00:31 +01008797 if (cookie) {
8798 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8799 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8800 */
8801 rule->cookie_len = strlen(cookie);
8802 if (cookie_set) {
8803 rule->cookie_str = malloc(rule->cookie_len + 10);
8804 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8805 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8806 rule->cookie_len += 9;
8807 } else {
8808 rule->cookie_str = malloc(rule->cookie_len + 21);
8809 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8810 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8811 rule->cookie_len += 20;
8812 }
8813 }
8814 rule->type = type;
8815 rule->code = code;
8816 rule->flags = flags;
8817 LIST_INIT(&rule->list);
8818 return rule;
8819
8820 missing_arg:
8821 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8822 return NULL;
8823}
8824
Willy Tarreau8797c062007-05-07 00:55:35 +02008825/************************************************************************/
8826/* The code below is dedicated to ACL parsing and matching */
8827/************************************************************************/
8828
8829
Willy Tarreau14174bc2012-04-16 14:34:04 +02008830/* This function ensures that the prerequisites for an L7 fetch are ready,
8831 * which means that a request or response is ready. If some data is missing,
8832 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008833 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8834 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008835 *
8836 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008837 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8838 * decide whether or not an HTTP message is present ;
8839 * 0 if the requested data cannot be fetched or if it is certain that
8840 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008841 * 1 if an HTTP message is ready
8842 */
8843static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008844smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008845 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008846{
8847 struct http_txn *txn = l7;
8848 struct http_msg *msg = &txn->req;
8849
8850 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8851 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8852 */
8853
8854 if (unlikely(!s || !txn))
8855 return 0;
8856
8857 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008858 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008859
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008860 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008861 if (unlikely(!s->req))
8862 return 0;
8863
Willy Tarreauaae75e32013-03-29 12:31:49 +01008864 /* If the buffer does not leave enough free space at the end,
8865 * we must first realign it.
8866 */
8867 if (s->req->buf->p > s->req->buf->data &&
8868 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8869 buffer_slow_realign(s->req->buf);
8870
Willy Tarreau14174bc2012-04-16 14:34:04 +02008871 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008872 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008873 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008874
8875 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008876 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008877 http_msg_analyzer(msg, &txn->hdr_idx);
8878
8879 /* Still no valid request ? */
8880 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008881 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008882 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008883 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008884 }
8885 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008886 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008887 return 0;
8888 }
8889
8890 /* OK we just got a valid HTTP request. We have some minor
8891 * preparation to perform so that further checks can rely
8892 * on HTTP tests.
8893 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008894
8895 /* If the request was parsed but was too large, we must absolutely
8896 * return an error so that it is not processed. At the moment this
8897 * cannot happen, but if the parsers are to change in the future,
8898 * we want this check to be maintained.
8899 */
8900 if (unlikely(s->req->buf->i + s->req->buf->p >
8901 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8902 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008903 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008904 return 1;
8905 }
8906
Willy Tarreau9b28e032012-10-12 23:49:43 +02008907 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008908 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8909 s->flags |= SN_REDIRECTABLE;
8910
Willy Tarreau506d0502013-07-06 13:29:24 +02008911 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8912 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008913 }
8914
Willy Tarreau506d0502013-07-06 13:29:24 +02008915 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008916 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008917 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008918
8919 /* otherwise everything's ready for the request */
8920 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008921 else {
8922 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008923 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8924 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008925 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008926 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008927 }
8928
8929 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008930 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008931 return 1;
8932}
Willy Tarreau8797c062007-05-07 00:55:35 +02008933
Willy Tarreauc0239e02012-04-16 14:42:55 +02008934#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008935 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 +02008936
Willy Tarreau24e32d82012-04-23 23:55:44 +02008937#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008938 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 +02008939
Willy Tarreau8797c062007-05-07 00:55:35 +02008940
8941/* 1. Check on METHOD
8942 * We use the pre-parsed method if it is known, and store its number as an
8943 * integer. If it is unknown, we use the pointer and the length.
8944 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008945static 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 +02008946{
8947 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008948 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008949
Willy Tarreauae8b7962007-06-09 23:10:04 +02008950 len = strlen(*text);
8951 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008952
8953 pattern->val.i = meth;
8954 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008955 if (usage == PAT_U_COMPILE) {
8956 pattern->ptr.str = strdup(*text);
8957 if (!pattern->ptr.str) {
8958 memprintf(err, "out of memory while loading pattern");
8959 return 0;
8960 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008961 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008962 else {
8963 trash = get_trash_chunk();
8964 if (trash->size < len) {
8965 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8966 len, trash->size);
8967 return 0;
8968 }
8969 pattern->ptr.str = trash->str;
8970 }
8971 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008972 pattern->len = len;
8973 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008974 else
8975 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008976 return 1;
8977}
8978
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008979/* This function fetches the method of current HTTP request and stores
8980 * it in the global pattern struct as a chunk. There are two possibilities :
8981 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8982 * in <len> and <ptr> is NULL ;
8983 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8984 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008985 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008986 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008987static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008988smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008989 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008990{
8991 int meth;
8992 struct http_txn *txn = l7;
8993
Willy Tarreau24e32d82012-04-23 23:55:44 +02008994 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008995
Willy Tarreau8797c062007-05-07 00:55:35 +02008996 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008997 smp->type = SMP_T_UINT;
8998 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008999 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009000 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9001 /* ensure the indexes are not affected */
9002 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02009003 smp->type = SMP_T_CSTR;
9004 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009005 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009006 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009007 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009008 return 1;
9009}
9010
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009011/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01009012static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02009013{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009014 int icase;
9015
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009016
Willy Tarreauf853c462012-04-23 18:53:56 +02009017 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009018 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009019 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009020 return PAT_MATCH;
9021 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009022 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009023
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009024 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9025 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009026 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009027
9028 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009029 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009030 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009031
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009032 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009033 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9034 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009035 return PAT_NOMATCH;
9036 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009037}
9038
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009039static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009040smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009041 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009042{
9043 struct http_txn *txn = l7;
9044 char *ptr;
9045 int len;
9046
Willy Tarreauc0239e02012-04-16 14:42:55 +02009047 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009048
Willy Tarreau8797c062007-05-07 00:55:35 +02009049 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009050 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009051
9052 while ((len-- > 0) && (*ptr++ != '/'));
9053 if (len <= 0)
9054 return 0;
9055
Willy Tarreauf853c462012-04-23 18:53:56 +02009056 smp->type = SMP_T_CSTR;
9057 smp->data.str.str = ptr;
9058 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009059
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009060 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009061 return 1;
9062}
9063
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009064static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009065smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009066 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009067{
9068 struct http_txn *txn = l7;
9069 char *ptr;
9070 int len;
9071
Willy Tarreauc0239e02012-04-16 14:42:55 +02009072 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009073
Willy Tarreauf26b2522012-12-14 08:33:14 +01009074 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9075 return 0;
9076
Willy Tarreau8797c062007-05-07 00:55:35 +02009077 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009078 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009079
9080 while ((len-- > 0) && (*ptr++ != '/'));
9081 if (len <= 0)
9082 return 0;
9083
Willy Tarreauf853c462012-04-23 18:53:56 +02009084 smp->type = SMP_T_CSTR;
9085 smp->data.str.str = ptr;
9086 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009087
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009088 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009089 return 1;
9090}
9091
9092/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009093static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009094smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009095 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009096{
9097 struct http_txn *txn = l7;
9098 char *ptr;
9099 int len;
9100
Willy Tarreauc0239e02012-04-16 14:42:55 +02009101 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009102
Willy Tarreauf26b2522012-12-14 08:33:14 +01009103 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9104 return 0;
9105
Willy Tarreau8797c062007-05-07 00:55:35 +02009106 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009107 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009108
Willy Tarreauf853c462012-04-23 18:53:56 +02009109 smp->type = SMP_T_UINT;
9110 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009111 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009112 return 1;
9113}
9114
9115/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009116static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009117smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009118 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009119{
9120 struct http_txn *txn = l7;
9121
Willy Tarreauc0239e02012-04-16 14:42:55 +02009122 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009123
Willy Tarreauf853c462012-04-23 18:53:56 +02009124 smp->type = SMP_T_CSTR;
9125 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009126 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009127 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009128 return 1;
9129}
9130
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009131static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009132smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009133 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009134{
9135 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009136 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009137
Willy Tarreauc0239e02012-04-16 14:42:55 +02009138 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009139
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009140 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9141 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009142 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009143
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009144 smp->type = SMP_T_IPV4;
9145 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009146 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009147 return 1;
9148}
9149
9150static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009151smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009152 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009153{
9154 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009155 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009156
Willy Tarreauc0239e02012-04-16 14:42:55 +02009157 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009158
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009159 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9160 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9161 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009162
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009163 smp->type = SMP_T_UINT;
9164 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009165 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009166 return 1;
9167}
9168
Willy Tarreau185b5c42012-04-26 15:11:51 +02009169/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9170 * Accepts an optional argument of type string containing the header field name,
9171 * and an optional argument of type signed or unsigned integer to request an
9172 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009173 * headers are considered from the first one. It does not stop on commas and
9174 * returns full lines instead (useful for User-Agent or Date for example).
9175 */
9176static int
9177smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009178 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009179{
9180 struct http_txn *txn = l7;
9181 struct hdr_idx *idx = &txn->hdr_idx;
9182 struct hdr_ctx *ctx = smp->ctx.a[0];
9183 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9184 int occ = 0;
9185 const char *name_str = NULL;
9186 int name_len = 0;
9187
9188 if (!ctx) {
9189 /* first call */
9190 ctx = &static_hdr_ctx;
9191 ctx->idx = 0;
9192 smp->ctx.a[0] = ctx;
9193 }
9194
9195 if (args) {
9196 if (args[0].type != ARGT_STR)
9197 return 0;
9198 name_str = args[0].data.str.str;
9199 name_len = args[0].data.str.len;
9200
9201 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9202 occ = args[1].data.uint;
9203 }
9204
9205 CHECK_HTTP_MESSAGE_FIRST();
9206
9207 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9208 /* search for header from the beginning */
9209 ctx->idx = 0;
9210
9211 if (!occ && !(opt & SMP_OPT_ITERATE))
9212 /* no explicit occurrence and single fetch => last header by default */
9213 occ = -1;
9214
9215 if (!occ)
9216 /* prepare to report multiple occurrences for ACL fetches */
9217 smp->flags |= SMP_F_NOT_LAST;
9218
9219 smp->type = SMP_T_CSTR;
9220 smp->flags |= SMP_F_VOL_HDR;
9221 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9222 return 1;
9223
9224 smp->flags &= ~SMP_F_NOT_LAST;
9225 return 0;
9226}
9227
9228/* 6. Check on HTTP header count. The number of occurrences is returned.
9229 * Accepts exactly 1 argument of type string. It does not stop on commas and
9230 * returns full lines instead (useful for User-Agent or Date for example).
9231 */
9232static int
9233smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009234 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009235{
9236 struct http_txn *txn = l7;
9237 struct hdr_idx *idx = &txn->hdr_idx;
9238 struct hdr_ctx ctx;
9239 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9240 int cnt;
9241
9242 if (!args || args->type != ARGT_STR)
9243 return 0;
9244
9245 CHECK_HTTP_MESSAGE_FIRST();
9246
9247 ctx.idx = 0;
9248 cnt = 0;
9249 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9250 cnt++;
9251
9252 smp->type = SMP_T_UINT;
9253 smp->data.uint = cnt;
9254 smp->flags = SMP_F_VOL_HDR;
9255 return 1;
9256}
9257
9258/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9259 * Accepts an optional argument of type string containing the header field name,
9260 * and an optional argument of type signed or unsigned integer to request an
9261 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009262 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009263 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009264static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009265smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009266 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009267{
9268 struct http_txn *txn = l7;
9269 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009270 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009271 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009272 int occ = 0;
9273 const char *name_str = NULL;
9274 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009275
Willy Tarreaua890d072013-04-02 12:01:06 +02009276 if (!ctx) {
9277 /* first call */
9278 ctx = &static_hdr_ctx;
9279 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009280 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009281 }
9282
Willy Tarreau185b5c42012-04-26 15:11:51 +02009283 if (args) {
9284 if (args[0].type != ARGT_STR)
9285 return 0;
9286 name_str = args[0].data.str.str;
9287 name_len = args[0].data.str.len;
9288
9289 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9290 occ = args[1].data.uint;
9291 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009292
Willy Tarreaue333ec92012-04-16 16:26:40 +02009293 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009294
Willy Tarreau185b5c42012-04-26 15:11:51 +02009295 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009296 /* search for header from the beginning */
9297 ctx->idx = 0;
9298
Willy Tarreau185b5c42012-04-26 15:11:51 +02009299 if (!occ && !(opt & SMP_OPT_ITERATE))
9300 /* no explicit occurrence and single fetch => last header by default */
9301 occ = -1;
9302
9303 if (!occ)
9304 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009305 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009306
Willy Tarreau185b5c42012-04-26 15:11:51 +02009307 smp->type = SMP_T_CSTR;
9308 smp->flags |= SMP_F_VOL_HDR;
9309 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 +02009310 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009311
Willy Tarreau37406352012-04-23 16:16:37 +02009312 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009313 return 0;
9314}
9315
Willy Tarreauc11416f2007-06-17 16:58:38 +02009316/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009317 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009318 */
9319static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009320smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009321 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009322{
9323 struct http_txn *txn = l7;
9324 struct hdr_idx *idx = &txn->hdr_idx;
9325 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009326 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009327 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009328
Willy Tarreau24e32d82012-04-23 23:55:44 +02009329 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009330 return 0;
9331
Willy Tarreaue333ec92012-04-16 16:26:40 +02009332 CHECK_HTTP_MESSAGE_FIRST();
9333
Willy Tarreau33a7e692007-06-10 19:45:56 +02009334 ctx.idx = 0;
9335 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009336 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 +02009337 cnt++;
9338
Willy Tarreauf853c462012-04-23 18:53:56 +02009339 smp->type = SMP_T_UINT;
9340 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009341 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009342 return 1;
9343}
9344
Willy Tarreau185b5c42012-04-26 15:11:51 +02009345/* Fetch an HTTP header's integer value. The integer value is returned. It
9346 * takes a mandatory argument of type string and an optional one of type int
9347 * to designate a specific occurrence. It returns an unsigned integer, which
9348 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009349 */
9350static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009351smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009352 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009353{
Willy Tarreauef38c392013-07-22 16:29:32 +02009354 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009355
Willy Tarreauf853c462012-04-23 18:53:56 +02009356 if (ret > 0) {
9357 smp->type = SMP_T_UINT;
9358 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9359 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009360
Willy Tarreaud53e2422012-04-16 17:21:11 +02009361 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009362}
9363
Cyril Bonté69fa9922012-10-25 00:01:06 +02009364/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9365 * and an optional one of type int to designate a specific occurrence.
9366 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009367 */
9368static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009369smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009370 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009371{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009372 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009373
Willy Tarreauef38c392013-07-22 16:29:32 +02009374 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009375 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9376 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009377 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009378 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009379 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009380 if (smp->data.str.len < temp->size - 1) {
9381 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9382 temp->str[smp->data.str.len] = '\0';
9383 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9384 smp->type = SMP_T_IPV6;
9385 break;
9386 }
9387 }
9388 }
9389
Willy Tarreaud53e2422012-04-16 17:21:11 +02009390 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009391 if (!(smp->flags & SMP_F_NOT_LAST))
9392 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009393 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009394 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009395}
9396
Willy Tarreau737b0c12007-06-10 21:28:46 +02009397/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9398 * the first '/' after the possible hostname, and ends before the possible '?'.
9399 */
9400static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009401smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009402 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009403{
9404 struct http_txn *txn = l7;
9405 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009406
Willy Tarreauc0239e02012-04-16 14:42:55 +02009407 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009408
Willy Tarreau9b28e032012-10-12 23:49:43 +02009409 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009410 ptr = http_get_path(txn);
9411 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009412 return 0;
9413
9414 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009415 smp->type = SMP_T_CSTR;
9416 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009417
9418 while (ptr < end && *ptr != '?')
9419 ptr++;
9420
Willy Tarreauf853c462012-04-23 18:53:56 +02009421 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009422 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009423 return 1;
9424}
9425
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009426/* This produces a concatenation of the first occurrence of the Host header
9427 * followed by the path component if it begins with a slash ('/'). This means
9428 * that '*' will not be added, resulting in exactly the first Host entry.
9429 * If no Host header is found, then the path is returned as-is. The returned
9430 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009431 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009432 */
9433static int
9434smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009435 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009436{
9437 struct http_txn *txn = l7;
9438 char *ptr, *end, *beg;
9439 struct hdr_ctx ctx;
9440
9441 CHECK_HTTP_MESSAGE_FIRST();
9442
9443 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009444 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 +02009445 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009446 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009447
9448 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009449 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009450 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009451 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009452 smp->data.str.len = ctx.vlen;
9453
9454 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009455 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 +02009456 beg = http_get_path(txn);
9457 if (!beg)
9458 beg = end;
9459
9460 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9461
9462 if (beg < ptr && *beg == '/') {
9463 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9464 smp->data.str.len += ptr - beg;
9465 }
9466
9467 smp->flags = SMP_F_VOL_1ST;
9468 return 1;
9469}
9470
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009471/* This produces a 32-bit hash of the concatenation of the first occurrence of
9472 * the Host header followed by the path component if it begins with a slash ('/').
9473 * This means that '*' will not be added, resulting in exactly the first Host
9474 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009475 * is hashed using the path hash followed by a full avalanche hash and provides a
9476 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009477 * high-traffic sites without having to store whole paths.
9478 */
9479static int
9480smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009481 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009482{
9483 struct http_txn *txn = l7;
9484 struct hdr_ctx ctx;
9485 unsigned int hash = 0;
9486 char *ptr, *beg, *end;
9487 int len;
9488
9489 CHECK_HTTP_MESSAGE_FIRST();
9490
9491 ctx.idx = 0;
9492 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9493 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9494 ptr = ctx.line + ctx.val;
9495 len = ctx.vlen;
9496 while (len--)
9497 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9498 }
9499
9500 /* now retrieve the path */
9501 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9502 beg = http_get_path(txn);
9503 if (!beg)
9504 beg = end;
9505
9506 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9507
9508 if (beg < ptr && *beg == '/') {
9509 while (beg < ptr)
9510 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9511 }
9512 hash = full_hash(hash);
9513
9514 smp->type = SMP_T_UINT;
9515 smp->data.uint = hash;
9516 smp->flags = SMP_F_VOL_1ST;
9517 return 1;
9518}
9519
Willy Tarreau4a550602012-12-09 14:53:32 +01009520/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009521 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9522 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9523 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009524 * that in environments where IPv6 is insignificant, truncating the output to
9525 * 8 bytes would still work.
9526 */
9527static int
9528smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009529 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009530{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009531 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009532 struct connection *cli_conn = objt_conn(l4->si[0].end);
9533
9534 if (!cli_conn)
9535 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009536
Willy Tarreauef38c392013-07-22 16:29:32 +02009537 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009538 return 0;
9539
Willy Tarreau47ca5452012-12-23 20:22:19 +01009540 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009541 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9542 temp->len += sizeof(smp->data.uint);
9543
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009544 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009545 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009546 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009547 temp->len += 4;
9548 break;
9549 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009550 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009551 temp->len += 16;
9552 break;
9553 default:
9554 return 0;
9555 }
9556
9557 smp->data.str = *temp;
9558 smp->type = SMP_T_BIN;
9559 return 1;
9560}
9561
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009562static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009563smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009564 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009565{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009566 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9567 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9568 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009569
Willy Tarreau24e32d82012-04-23 23:55:44 +02009570 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009571
Willy Tarreauf853c462012-04-23 18:53:56 +02009572 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009573 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009574 return 1;
9575}
9576
Willy Tarreau7f18e522010-10-22 20:04:13 +02009577/* return a valid test if the current request is the first one on the connection */
9578static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009579smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009580 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009581{
9582 if (!s)
9583 return 0;
9584
Willy Tarreauf853c462012-04-23 18:53:56 +02009585 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009586 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009587 return 1;
9588}
9589
Willy Tarreau34db1082012-04-19 17:16:54 +02009590/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009591static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009592smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009593 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009594{
9595
Willy Tarreau24e32d82012-04-23 23:55:44 +02009596 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009597 return 0;
9598
Willy Tarreauc0239e02012-04-16 14:42:55 +02009599 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009600
Willy Tarreauc0239e02012-04-16 14:42:55 +02009601 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009602 return 0;
9603
Willy Tarreauf853c462012-04-23 18:53:56 +02009604 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009605 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 +01009606 return 1;
9607}
Willy Tarreau8797c062007-05-07 00:55:35 +02009608
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009609/* Accepts exactly 1 argument of type userlist */
9610static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009611smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009612 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009613{
9614
9615 if (!args || args->type != ARGT_USR)
9616 return 0;
9617
9618 CHECK_HTTP_MESSAGE_FIRST();
9619
9620 if (!get_http_auth(l4))
9621 return 0;
9622
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009623 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009624 smp->ctx.a[0] = args->data.usr; /* user list */
9625 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9626 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9627
9628 /* if the user does not belong to the userlist or has a wrong password,
9629 * report that it unconditionally does not match. Otherwise we return
9630 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009631 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009632 */
9633 smp->type = SMP_T_BOOL;
9634 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9635 if (smp->data.uint)
9636 smp->type = SMP_T_UINT;
9637
9638 return 1;
9639}
9640
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009641/* Try to find the next occurrence of a cookie name in a cookie header value.
9642 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9643 * the cookie value is returned into *value and *value_l, and the function
9644 * returns a pointer to the next pointer to search from if the value was found.
9645 * Otherwise if the cookie was not found, NULL is returned and neither value
9646 * nor value_l are touched. The input <hdr> string should first point to the
9647 * header's value, and the <hdr_end> pointer must point to the first character
9648 * not part of the value. <list> must be non-zero if value may represent a list
9649 * of values (cookie headers). This makes it faster to abort parsing when no
9650 * list is expected.
9651 */
9652static char *
9653extract_cookie_value(char *hdr, const char *hdr_end,
9654 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009655 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009656{
9657 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9658 char *next;
9659
9660 /* we search at least a cookie name followed by an equal, and more
9661 * generally something like this :
9662 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9663 */
9664 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9665 /* Iterate through all cookies on this line */
9666
9667 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9668 att_beg++;
9669
9670 /* find att_end : this is the first character after the last non
9671 * space before the equal. It may be equal to hdr_end.
9672 */
9673 equal = att_end = att_beg;
9674
9675 while (equal < hdr_end) {
9676 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9677 break;
9678 if (http_is_spht[(unsigned char)*equal++])
9679 continue;
9680 att_end = equal;
9681 }
9682
9683 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9684 * is between <att_beg> and <equal>, both may be identical.
9685 */
9686
9687 /* look for end of cookie if there is an equal sign */
9688 if (equal < hdr_end && *equal == '=') {
9689 /* look for the beginning of the value */
9690 val_beg = equal + 1;
9691 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9692 val_beg++;
9693
9694 /* find the end of the value, respecting quotes */
9695 next = find_cookie_value_end(val_beg, hdr_end);
9696
9697 /* make val_end point to the first white space or delimitor after the value */
9698 val_end = next;
9699 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9700 val_end--;
9701 } else {
9702 val_beg = val_end = next = equal;
9703 }
9704
9705 /* We have nothing to do with attributes beginning with '$'. However,
9706 * they will automatically be removed if a header before them is removed,
9707 * since they're supposed to be linked together.
9708 */
9709 if (*att_beg == '$')
9710 continue;
9711
9712 /* Ignore cookies with no equal sign */
9713 if (equal == next)
9714 continue;
9715
9716 /* Now we have the cookie name between att_beg and att_end, and
9717 * its value between val_beg and val_end.
9718 */
9719
9720 if (att_end - att_beg == cookie_name_l &&
9721 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9722 /* let's return this value and indicate where to go on from */
9723 *value = val_beg;
9724 *value_l = val_end - val_beg;
9725 return next + 1;
9726 }
9727
9728 /* Set-Cookie headers only have the name in the first attr=value part */
9729 if (!list)
9730 break;
9731 }
9732
9733 return NULL;
9734}
9735
William Lallemanda43ba4e2014-01-28 18:14:25 +01009736/* Fetch a captured HTTP request header. The index is the position of
9737 * the "capture" option in the configuration file
9738 */
9739static int
9740smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9741 const struct arg *args, struct sample *smp, const char *kw)
9742{
9743 struct proxy *fe = l4->fe;
9744 struct http_txn *txn = l7;
9745 int idx;
9746
9747 if (!args || args->type != ARGT_UINT)
9748 return 0;
9749
9750 idx = args->data.uint;
9751
9752 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9753 return 0;
9754
9755 smp->type = SMP_T_CSTR;
9756 smp->data.str.str = txn->req.cap[idx];
9757 smp->data.str.len = strlen(txn->req.cap[idx]);
9758
9759 return 1;
9760}
9761
9762/* Fetch a captured HTTP response header. The index is the position of
9763 * the "capture" option in the configuration file
9764 */
9765static int
9766smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9767 const struct arg *args, struct sample *smp, const char *kw)
9768{
9769 struct proxy *fe = l4->fe;
9770 struct http_txn *txn = l7;
9771 int idx;
9772
9773 if (!args || args->type != ARGT_UINT)
9774 return 0;
9775
9776 idx = args->data.uint;
9777
9778 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9779 return 0;
9780
9781 smp->type = SMP_T_CSTR;
9782 smp->data.str.str = txn->rsp.cap[idx];
9783 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9784
9785 return 1;
9786}
9787
William Lallemand65ad6e12014-01-31 15:08:02 +01009788/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9789static int
9790smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9791 const struct arg *args, struct sample *smp, const char *kw)
9792{
9793 struct chunk *temp;
9794 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009795 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009796
9797 if (!txn->uri)
9798 return 0;
9799
William Lallemand96a77852014-02-05 00:30:02 +01009800 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009801
William Lallemand96a77852014-02-05 00:30:02 +01009802 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9803 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009804
William Lallemand96a77852014-02-05 00:30:02 +01009805 temp = get_trash_chunk();
9806 temp->str = txn->uri;
9807 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009808 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009809 smp->type = SMP_T_CSTR;
William Lallemand65ad6e12014-01-31 15:08:02 +01009810
9811 return 1;
9812
9813}
9814
9815/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9816static int
9817smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9818 const struct arg *args, struct sample *smp, const char *kw)
9819{
9820 struct chunk *temp;
9821 struct http_txn *txn = l7;
9822 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009823
9824 if (!txn->uri)
9825 return 0;
William Lallemand96a77852014-02-05 00:30:02 +01009826
William Lallemand65ad6e12014-01-31 15:08:02 +01009827 ptr = txn->uri;
9828
9829 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9830 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +01009831
William Lallemand65ad6e12014-01-31 15:08:02 +01009832 if (!*ptr)
9833 return 0;
9834
9835 ptr++; /* skip the space */
9836
9837 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +01009838 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +01009839 if (!ptr)
9840 return 0;
9841 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
9842 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009843
9844 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +01009845 smp->data.str.len = ptr - temp->str;
9846 smp->type = SMP_T_CSTR;
William Lallemand65ad6e12014-01-31 15:08:02 +01009847
9848 return 1;
9849}
9850
9851
Willy Tarreaue333ec92012-04-16 16:26:40 +02009852/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009853 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009854 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009855 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009856 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009857 * Accepts exactly 1 argument of type string. If the input options indicate
9858 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009859 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009860 */
9861static int
Willy Tarreau51539362012-05-08 12:46:28 +02009862smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009863 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009864{
9865 struct http_txn *txn = l7;
9866 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009867 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009868 const struct http_msg *msg;
9869 const char *hdr_name;
9870 int hdr_name_len;
9871 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009872 int occ = 0;
9873 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009874
Willy Tarreau24e32d82012-04-23 23:55:44 +02009875 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009876 return 0;
9877
Willy Tarreaua890d072013-04-02 12:01:06 +02009878 if (!ctx) {
9879 /* first call */
9880 ctx = &static_hdr_ctx;
9881 ctx->idx = 0;
9882 smp->ctx.a[2] = ctx;
9883 }
9884
Willy Tarreaue333ec92012-04-16 16:26:40 +02009885 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009886
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009887 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009888 msg = &txn->req;
9889 hdr_name = "Cookie";
9890 hdr_name_len = 6;
9891 } else {
9892 msg = &txn->rsp;
9893 hdr_name = "Set-Cookie";
9894 hdr_name_len = 10;
9895 }
9896
Willy Tarreau28376d62012-04-26 21:26:10 +02009897 if (!occ && !(opt & SMP_OPT_ITERATE))
9898 /* no explicit occurrence and single fetch => last cookie by default */
9899 occ = -1;
9900
9901 /* OK so basically here, either we want only one value and it's the
9902 * last one, or we want to iterate over all of them and we fetch the
9903 * next one.
9904 */
9905
Willy Tarreau9b28e032012-10-12 23:49:43 +02009906 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009907 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009908 /* search for the header from the beginning, we must first initialize
9909 * the search parameters.
9910 */
Willy Tarreau37406352012-04-23 16:16:37 +02009911 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009912 ctx->idx = 0;
9913 }
9914
Willy Tarreau28376d62012-04-26 21:26:10 +02009915 smp->flags |= SMP_F_VOL_HDR;
9916
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009917 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009918 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9919 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009920 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9921 goto out;
9922
Willy Tarreau24e32d82012-04-23 23:55:44 +02009923 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009924 continue;
9925
Willy Tarreau37406352012-04-23 16:16:37 +02009926 smp->ctx.a[0] = ctx->line + ctx->val;
9927 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009928 }
9929
Willy Tarreauf853c462012-04-23 18:53:56 +02009930 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009931 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009932 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009933 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009934 &smp->data.str.str,
9935 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009936 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009937 found = 1;
9938 if (occ >= 0) {
9939 /* one value was returned into smp->data.str.{str,len} */
9940 smp->flags |= SMP_F_NOT_LAST;
9941 return 1;
9942 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009943 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009944 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009945 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009946 /* all cookie headers and values were scanned. If we're looking for the
9947 * last occurrence, we may return it now.
9948 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009949 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009950 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009951 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009952}
9953
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009954/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009955 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +01009956 * multiple cookies may be parsed on the same line. The returned sample is of
9957 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009958 */
9959static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009960smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009961 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009962{
9963 struct http_txn *txn = l7;
9964 struct hdr_idx *idx = &txn->hdr_idx;
9965 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009966 const struct http_msg *msg;
9967 const char *hdr_name;
9968 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009969 int cnt;
9970 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009971 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009972
Willy Tarreau24e32d82012-04-23 23:55:44 +02009973 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009974 return 0;
9975
Willy Tarreaue333ec92012-04-16 16:26:40 +02009976 CHECK_HTTP_MESSAGE_FIRST();
9977
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009978 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009979 msg = &txn->req;
9980 hdr_name = "Cookie";
9981 hdr_name_len = 6;
9982 } else {
9983 msg = &txn->rsp;
9984 hdr_name = "Set-Cookie";
9985 hdr_name_len = 10;
9986 }
9987
Willy Tarreau9b28e032012-10-12 23:49:43 +02009988 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009989 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009990 ctx.idx = 0;
9991 cnt = 0;
9992
9993 while (1) {
9994 /* Note: val_beg == NULL every time we need to fetch a new header */
9995 if (!val_beg) {
9996 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9997 break;
9998
Willy Tarreau24e32d82012-04-23 23:55:44 +02009999 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010000 continue;
10001
10002 val_beg = ctx.line + ctx.val;
10003 val_end = val_beg + ctx.vlen;
10004 }
10005
Willy Tarreauf853c462012-04-23 18:53:56 +020010006 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010007 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010008 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010009 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010010 &smp->data.str.str,
10011 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010012 cnt++;
10013 }
10014 }
10015
Willy Tarreaub169eba2013-12-16 15:14:43 +010010016 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010017 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010018 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010019 return 1;
10020}
10021
Willy Tarreau51539362012-05-08 12:46:28 +020010022/* Fetch an cookie's integer value. The integer value is returned. It
10023 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10024 */
10025static int
10026smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010027 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010028{
Willy Tarreauef38c392013-07-22 16:29:32 +020010029 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010030
10031 if (ret > 0) {
10032 smp->type = SMP_T_UINT;
10033 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10034 }
10035
10036 return ret;
10037}
10038
Willy Tarreau8797c062007-05-07 00:55:35 +020010039/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010040/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010041/************************************************************************/
10042
David Cournapeau16023ee2010-12-23 20:55:41 +090010043/*
10044 * Given a path string and its length, find the position of beginning of the
10045 * query string. Returns NULL if no query string is found in the path.
10046 *
10047 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10048 *
10049 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10050 */
bedis4c75cca2012-10-05 08:38:24 +020010051static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010052{
10053 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010054
bedis4c75cca2012-10-05 08:38:24 +020010055 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010056 return p ? p + 1 : NULL;
10057}
10058
bedis4c75cca2012-10-05 08:38:24 +020010059static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010060{
bedis4c75cca2012-10-05 08:38:24 +020010061 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010062}
10063
10064/*
10065 * Given a url parameter, find the starting position of the first occurence,
10066 * or NULL if the parameter is not found.
10067 *
10068 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10069 * the function will return query_string+8.
10070 */
10071static char*
10072find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010073 char* url_param_name, size_t url_param_name_l,
10074 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010075{
10076 char *pos, *last;
10077
10078 pos = query_string;
10079 last = query_string + query_string_l - url_param_name_l - 1;
10080
10081 while (pos <= last) {
10082 if (pos[url_param_name_l] == '=') {
10083 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10084 return pos;
10085 pos += url_param_name_l + 1;
10086 }
bedis4c75cca2012-10-05 08:38:24 +020010087 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010088 pos++;
10089 pos++;
10090 }
10091 return NULL;
10092}
10093
10094/*
10095 * Given a url parameter name, returns its value and size into *value and
10096 * *value_l respectively, and returns non-zero. If the parameter is not found,
10097 * zero is returned and value/value_l are not touched.
10098 */
10099static int
10100find_url_param_value(char* path, size_t path_l,
10101 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010102 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010103{
10104 char *query_string, *qs_end;
10105 char *arg_start;
10106 char *value_start, *value_end;
10107
bedis4c75cca2012-10-05 08:38:24 +020010108 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010109 if (!query_string)
10110 return 0;
10111
10112 qs_end = path + path_l;
10113 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010114 url_param_name, url_param_name_l,
10115 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010116 if (!arg_start)
10117 return 0;
10118
10119 value_start = arg_start + url_param_name_l + 1;
10120 value_end = value_start;
10121
bedis4c75cca2012-10-05 08:38:24 +020010122 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010123 value_end++;
10124
10125 *value = value_start;
10126 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010127 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010128}
10129
10130static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010131smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010132 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010133{
bedis4c75cca2012-10-05 08:38:24 +020010134 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010135 struct http_txn *txn = l7;
10136 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010137
bedis4c75cca2012-10-05 08:38:24 +020010138 if (!args || args[0].type != ARGT_STR ||
10139 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010140 return 0;
10141
10142 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010143
bedis4c75cca2012-10-05 08:38:24 +020010144 if (args[1].type)
10145 delim = *args[1].data.str.str;
10146
Willy Tarreau9b28e032012-10-12 23:49:43 +020010147 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010148 args->data.str.str, args->data.str.len,
10149 &smp->data.str.str, &smp->data.str.len,
10150 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010151 return 0;
10152
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010153 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010154 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010155 return 1;
10156}
10157
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010158/* Return the signed integer value for the specified url parameter (see url_param
10159 * above).
10160 */
10161static int
10162smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010163 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010164{
Willy Tarreauef38c392013-07-22 16:29:32 +020010165 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010166
10167 if (ret > 0) {
10168 smp->type = SMP_T_UINT;
10169 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10170 }
10171
10172 return ret;
10173}
10174
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010175/* This produces a 32-bit hash of the concatenation of the first occurrence of
10176 * the Host header followed by the path component if it begins with a slash ('/').
10177 * This means that '*' will not be added, resulting in exactly the first Host
10178 * entry. If no Host header is found, then the path is used. The resulting value
10179 * is hashed using the url hash followed by a full avalanche hash and provides a
10180 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10181 * high-traffic sites without having to store whole paths.
10182 * this differs from the base32 functions in that it includes the url parameters
10183 * as well as the path
10184 */
10185static int
10186smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010187 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010188{
10189 struct http_txn *txn = l7;
10190 struct hdr_ctx ctx;
10191 unsigned int hash = 0;
10192 char *ptr, *beg, *end;
10193 int len;
10194
10195 CHECK_HTTP_MESSAGE_FIRST();
10196
10197 ctx.idx = 0;
10198 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10199 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10200 ptr = ctx.line + ctx.val;
10201 len = ctx.vlen;
10202 while (len--)
10203 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10204 }
10205
10206 /* now retrieve the path */
10207 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10208 beg = http_get_path(txn);
10209 if (!beg)
10210 beg = end;
10211
10212 for (ptr = beg; ptr < end ; ptr++);
10213
10214 if (beg < ptr && *beg == '/') {
10215 while (beg < ptr)
10216 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10217 }
10218 hash = full_hash(hash);
10219
10220 smp->type = SMP_T_UINT;
10221 smp->data.uint = hash;
10222 smp->flags = SMP_F_VOL_1ST;
10223 return 1;
10224}
10225
10226/* This concatenates the source address with the 32-bit hash of the Host and
10227 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10228 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10229 * on the source address length. The URL hash is stored before the address so
10230 * that in environments where IPv6 is insignificant, truncating the output to
10231 * 8 bytes would still work.
10232 */
10233static int
10234smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010235 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010236{
10237 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010238 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010239
Willy Tarreaue155ec22013-11-18 18:33:22 +010010240 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010241 return 0;
10242
10243 temp = get_trash_chunk();
10244 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10245 temp->len += sizeof(smp->data.uint);
10246
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010247 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010248 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010249 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010250 temp->len += 4;
10251 break;
10252 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010253 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010254 temp->len += 16;
10255 break;
10256 default:
10257 return 0;
10258 }
10259
10260 smp->data.str = *temp;
10261 smp->type = SMP_T_BIN;
10262 return 1;
10263}
10264
Willy Tarreau185b5c42012-04-26 15:11:51 +020010265/* This function is used to validate the arguments passed to any "hdr" fetch
10266 * keyword. These keywords support an optional positive or negative occurrence
10267 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10268 * is assumed that the types are already the correct ones. Returns 0 on error,
10269 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10270 * error message in case of error, that the caller is responsible for freeing.
10271 * The initial location must either be freeable or NULL.
10272 */
10273static int val_hdr(struct arg *arg, char **err_msg)
10274{
10275 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010276 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010277 return 0;
10278 }
10279 return 1;
10280}
10281
Willy Tarreau276fae92013-07-25 14:36:01 +020010282/* takes an UINT value on input supposed to represent the time since EPOCH,
10283 * adds an optional offset found in args[0] and emits a string representing
10284 * the date in RFC-1123/5322 format.
10285 */
10286static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10287{
10288 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10289 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10290 struct chunk *temp;
10291 struct tm *tm;
10292 time_t curr_date = smp->data.uint;
10293
10294 /* add offset */
10295 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10296 curr_date += args[0].data.sint;
10297
10298 tm = gmtime(&curr_date);
10299
10300 temp = get_trash_chunk();
10301 temp->len = snprintf(temp->str, temp->size - temp->len,
10302 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10303 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10304 tm->tm_hour, tm->tm_min, tm->tm_sec);
10305
10306 smp->data.str = *temp;
10307 smp->type = SMP_T_STR;
10308 return 1;
10309}
10310
Willy Tarreau4a568972010-05-12 08:08:50 +020010311/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010312/* All supported ACL keywords must be declared here. */
10313/************************************************************************/
10314
10315/* Note: must not be declared <const> as its list will be overwritten.
10316 * Please take care of keeping this list alphabetically sorted.
10317 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010318static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010319 { "base", "base", pat_parse_str, pat_match_str },
10320 { "base_beg", "base", pat_parse_str, pat_match_beg },
10321 { "base_dir", "base", pat_parse_str, pat_match_dir },
10322 { "base_dom", "base", pat_parse_str, pat_match_dom },
10323 { "base_end", "base", pat_parse_str, pat_match_end },
10324 { "base_len", "base", pat_parse_int, pat_match_len },
10325 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10326 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010327
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010328 { "cook", "req.cook", pat_parse_str, pat_match_str },
10329 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10330 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10331 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10332 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10333 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10334 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10335 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010336
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010337 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10338 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10339 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10340 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10341 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10342 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10343 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10344 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010345
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010346 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010347
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010348 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010349
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010350 { "path", "path", pat_parse_str, pat_match_str },
10351 { "path_beg", "path", pat_parse_str, pat_match_beg },
10352 { "path_dir", "path", pat_parse_str, pat_match_dir },
10353 { "path_dom", "path", pat_parse_str, pat_match_dom },
10354 { "path_end", "path", pat_parse_str, pat_match_end },
10355 { "path_len", "path", pat_parse_int, pat_match_len },
10356 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10357 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010358
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010359 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10360 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010361
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010362 { "scook", "res.cook", pat_parse_str, pat_match_str },
10363 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10364 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10365 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10366 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10367 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10368 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10369 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010370
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010371 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10372 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10373 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10374 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10375 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10376 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10377 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10378 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010379
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010380 { "url", "url", pat_parse_str, pat_match_str },
10381 { "url_beg", "url", pat_parse_str, pat_match_beg },
10382 { "url_dir", "url", pat_parse_str, pat_match_dir },
10383 { "url_dom", "url", pat_parse_str, pat_match_dom },
10384 { "url_end", "url", pat_parse_str, pat_match_end },
10385 { "url_len", "url", pat_parse_int, pat_match_len },
10386 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10387 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010388
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010389 { "urlp", "urlp", pat_parse_str, pat_match_str },
10390 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10391 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10392 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10393 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10394 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10395 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10396 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010397
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010398 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010399}};
10400
10401/************************************************************************/
10402/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010403/************************************************************************/
10404/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010405static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010406 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10407 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10408 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10409
William Lallemand65ad6e12014-01-31 15:08:02 +010010410 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10411 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10412
William Lallemanda43ba4e2014-01-28 18:14:25 +010010413 /* capture are allocated and are permanent in the session */
10414 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10415 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRSHP },
10416
Willy Tarreau409bcde2013-01-08 00:31:00 +010010417 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10418 * are only here to match the ACL's name, are request-only and are used
10419 * for ACL compatibility only.
10420 */
10421 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10422 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10423 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10424 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10425
10426 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10427 * only here to match the ACL's name, are request-only and are used for
10428 * ACL compatibility only.
10429 */
10430 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10431 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10432 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10433 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10434
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010435 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10436 { "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 +010010437 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10438 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10439 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010440
10441 /* HTTP protocol on the request path */
10442 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010443 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010444
10445 /* HTTP version on the request path */
10446 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010447 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010448
10449 /* HTTP version on the response path */
10450 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010451 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10452
Willy Tarreau18ed2562013-01-14 15:56:36 +010010453 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10454 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10455 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10456 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10457
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010458 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10459 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010460 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10461 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10462 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10463 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10464
10465 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10466 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10467 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10468 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10469
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010470 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10471 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010472 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10473 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10474 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10475 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10476
Willy Tarreau409bcde2013-01-08 00:31:00 +010010477 /* scook is valid only on the response and is used for ACL compatibility */
10478 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10479 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10480 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10481 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10482
10483 /* shdr is valid only on the response and is used for ACL compatibility */
10484 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10485 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10486 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10487 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10488
10489 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10490 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010491 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10492 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010493 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10494 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10495 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10496 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10497 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10498 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010499}};
10500
Willy Tarreau8797c062007-05-07 00:55:35 +020010501
Willy Tarreau276fae92013-07-25 14:36:01 +020010502/* Note: must not be declared <const> as its list will be overwritten */
10503static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10504 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10505 { NULL, NULL, 0, 0, 0 },
10506}};
10507
Willy Tarreau8797c062007-05-07 00:55:35 +020010508__attribute__((constructor))
10509static void __http_protocol_init(void)
10510{
10511 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010512 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010513 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010514}
10515
10516
Willy Tarreau58f10d72006-12-04 02:26:12 +010010517/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010518 * Local variables:
10519 * c-indent-level: 8
10520 * c-basic-offset: 8
10521 * End:
10522 */