blob: 211a37af08f258480dba424b9a095948b566bcb3 [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);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100946}
947
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100948/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100949 * that the server side is closed. Note that err_type is actually a
950 * bitmask, where almost only aborts may be cumulated with other
951 * values. We consider that aborted operations are more important
952 * than timeouts or errors due to the fact that nobody else in the
953 * logs might explain incomplete retries. All others should avoid
954 * being cumulated. It should normally not be possible to have multiple
955 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100956 * Note that connection errors appearing on the second request of a keep-alive
957 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100958 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100959void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100960{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100961 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100962
963 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100964 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200965 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100966 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100967 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100968 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
969 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100970 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100971 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200972 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100973 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100974 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200975 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100977 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100978 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
979 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100980 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100981 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100982 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
983 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200984 else if (err_type & SI_ET_CONN_RES)
985 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100986 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
987 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100989 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200990 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100991}
992
Willy Tarreau42250582007-04-01 01:30:43 +0200993extern const char sess_term_cond[8];
994extern const char sess_fin_state[8];
995extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200996struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100997struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100998struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100999
Willy Tarreau117f59e2007-03-04 18:17:17 +01001000/*
1001 * Capture headers from message starting at <som> according to header list
1002 * <cap_hdr>, and fill the <idx> structure appropriately.
1003 */
1004void capture_headers(char *som, struct hdr_idx *idx,
1005 char **cap, struct cap_hdr *cap_hdr)
1006{
1007 char *eol, *sol, *col, *sov;
1008 int cur_idx;
1009 struct cap_hdr *h;
1010 int len;
1011
1012 sol = som + hdr_idx_first_pos(idx);
1013 cur_idx = hdr_idx_first_idx(idx);
1014
1015 while (cur_idx) {
1016 eol = sol + idx->v[cur_idx].len;
1017
1018 col = sol;
1019 while (col < eol && *col != ':')
1020 col++;
1021
1022 sov = col + 1;
1023 while (sov < eol && http_is_lws[(unsigned char)*sov])
1024 sov++;
1025
1026 for (h = cap_hdr; h; h = h->next) {
1027 if ((h->namelen == col - sol) &&
1028 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1029 if (cap[h->index] == NULL)
1030 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001031 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001032
1033 if (cap[h->index] == NULL) {
1034 Alert("HTTP capture : out of memory.\n");
1035 continue;
1036 }
1037
1038 len = eol - sov;
1039 if (len > h->len)
1040 len = h->len;
1041
1042 memcpy(cap[h->index], sov, len);
1043 cap[h->index][len]=0;
1044 }
1045 }
1046 sol = eol + idx->v[cur_idx].cr + 1;
1047 cur_idx = idx->v[cur_idx].next;
1048 }
1049}
1050
1051
Willy Tarreau42250582007-04-01 01:30:43 +02001052/* either we find an LF at <ptr> or we jump to <bad>.
1053 */
1054#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1055
1056/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1057 * otherwise to <http_msg_ood> with <state> set to <st>.
1058 */
1059#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1060 ptr++; \
1061 if (likely(ptr < end)) \
1062 goto good; \
1063 else { \
1064 state = (st); \
1065 goto http_msg_ood; \
1066 } \
1067 } while (0)
1068
1069
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001071 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001072 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1073 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1074 * will give undefined results.
1075 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1076 * and that msg->sol points to the beginning of the response.
1077 * If a complete line is found (which implies that at least one CR or LF is
1078 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1079 * returned indicating an incomplete line (which does not mean that parts have
1080 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1081 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1082 * upon next call.
1083 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001084 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001085 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1086 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001087 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001088 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001089const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001090 enum ht_state state, const char *ptr, const char *end,
1091 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001092{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001093 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001094
Willy Tarreau8973c702007-01-21 23:58:29 +01001095 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001096 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001097 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001098 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001099 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1100
1101 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001102 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001103 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1104 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001105 state = HTTP_MSG_ERROR;
1106 break;
1107
Willy Tarreau8973c702007-01-21 23:58:29 +01001108 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001109 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001110 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001111 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001112 goto http_msg_rpcode;
1113 }
1114 if (likely(HTTP_IS_SPHT(*ptr)))
1115 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1116 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001117 state = HTTP_MSG_ERROR;
1118 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001119
Willy Tarreau8973c702007-01-21 23:58:29 +01001120 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001121 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001122 if (likely(!HTTP_IS_LWS(*ptr)))
1123 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1124
1125 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001126 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001127 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1128 }
1129
1130 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001131 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001132 http_msg_rsp_reason:
1133 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001134 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001135 msg->sl.st.r_l = 0;
1136 goto http_msg_rpline_eol;
1137
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001142 goto http_msg_rpreason;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1146 /* so it's a CR/LF, so there is no reason phrase */
1147 goto http_msg_rsp_reason;
1148
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 if (likely(!HTTP_IS_CRLF(*ptr)))
1152 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001153 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001154 http_msg_rpline_eol:
1155 /* We have seen the end of line. Note that we do not
1156 * necessarily have the \n yet, but at least we know that we
1157 * have EITHER \r OR \n, otherwise the response would not be
1158 * complete. We can then record the response length and return
1159 * to the caller which will be able to register it.
1160 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001161 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 return ptr;
1163
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001165#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1167 exit(1);
1168#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001169 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 }
1171
1172 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001173 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001174 if (ret_state)
1175 *ret_state = state;
1176 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001177 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001178 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001179}
1180
Willy Tarreau8973c702007-01-21 23:58:29 +01001181/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 * This function parses a request line between <ptr> and <end>, starting with
1183 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1184 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1185 * will give undefined results.
1186 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1187 * and that msg->sol points to the beginning of the request.
1188 * If a complete line is found (which implies that at least one CR or LF is
1189 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1190 * returned indicating an incomplete line (which does not mean that parts have
1191 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1192 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1193 * upon next call.
1194 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001195 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1197 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001198 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001199 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001200const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001201 enum ht_state state, const char *ptr, const char *end,
1202 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001203{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001204 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001205
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001207 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001208 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001209 if (likely(HTTP_IS_TOKEN(*ptr)))
1210 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001211
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001213 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001214 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1215 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001216
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001217 if (likely(HTTP_IS_CRLF(*ptr))) {
1218 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001219 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001220 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001221 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001222 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001223 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 msg->sl.rq.v_l = 0;
1227 goto http_msg_rqline_eol;
1228 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001229 state = HTTP_MSG_ERROR;
1230 break;
1231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001232 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001233 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001235 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 goto http_msg_rquri;
1237 }
1238 if (likely(HTTP_IS_SPHT(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1240 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1241 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001244 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001245 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001246 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001247
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1251 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001252
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001253 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001254 /* non-ASCII chars are forbidden unless option
1255 * accept-invalid-http-request is enabled in the frontend.
1256 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001257 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001258 if (msg->err_pos < -1)
1259 goto invalid_char;
1260 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001261 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001262 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1263 }
1264
1265 if (likely(HTTP_IS_CRLF(*ptr))) {
1266 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1267 goto http_msg_req09_uri_e;
1268 }
1269
1270 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001271 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001272 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001273 state = HTTP_MSG_ERROR;
1274 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001277 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001279 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 goto http_msg_rqver;
1281 }
1282 if (likely(HTTP_IS_SPHT(*ptr)))
1283 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1284 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1285 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001288 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001289 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001291
1292 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001293 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001294 http_msg_rqline_eol:
1295 /* We have seen the end of line. Note that we do not
1296 * necessarily have the \n yet, but at least we know that we
1297 * have EITHER \r OR \n, otherwise the request would not be
1298 * complete. We can then record the request length and return
1299 * to the caller which will be able to register it.
1300 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001301 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001302 return ptr;
1303 }
1304
1305 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001306 state = HTTP_MSG_ERROR;
1307 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001310#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1312 exit(1);
1313#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001314 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001318 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (ret_state)
1320 *ret_state = state;
1321 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001322 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001326/*
1327 * Returns the data from Authorization header. Function may be called more
1328 * than once so data is stored in txn->auth_data. When no header is found
1329 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001330 * searching again for something we are unable to find anyway. However, if
1331 * the result if valid, the cache is not reused because we would risk to
1332 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001333 */
1334
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001335char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001336
1337int
1338get_http_auth(struct session *s)
1339{
1340
1341 struct http_txn *txn = &s->txn;
1342 struct chunk auth_method;
1343 struct hdr_ctx ctx;
1344 char *h, *p;
1345 int len;
1346
1347#ifdef DEBUG_AUTH
1348 printf("Auth for session %p: %d\n", s, txn->auth.method);
1349#endif
1350
1351 if (txn->auth.method == HTTP_AUTH_WRONG)
1352 return 0;
1353
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001354 txn->auth.method = HTTP_AUTH_WRONG;
1355
1356 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001357
1358 if (txn->flags & TX_USE_PX_CONN) {
1359 h = "Proxy-Authorization";
1360 len = strlen(h);
1361 } else {
1362 h = "Authorization";
1363 len = strlen(h);
1364 }
1365
Willy Tarreau9b28e032012-10-12 23:49:43 +02001366 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001367 return 0;
1368
1369 h = ctx.line + ctx.val;
1370
1371 p = memchr(h, ' ', ctx.vlen);
1372 if (!p || p == h)
1373 return 0;
1374
1375 chunk_initlen(&auth_method, h, 0, p-h);
1376 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1377
1378 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1379
1380 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001381 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001382
1383 if (len < 0)
1384 return 0;
1385
1386
1387 get_http_auth_buff[len] = '\0';
1388
1389 p = strchr(get_http_auth_buff, ':');
1390
1391 if (!p)
1392 return 0;
1393
1394 txn->auth.user = get_http_auth_buff;
1395 *p = '\0';
1396 txn->auth.pass = p+1;
1397
1398 txn->auth.method = HTTP_AUTH_BASIC;
1399 return 1;
1400 }
1401
1402 return 0;
1403}
1404
Willy Tarreau58f10d72006-12-04 02:26:12 +01001405
Willy Tarreau8973c702007-01-21 23:58:29 +01001406/*
1407 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001408 * depending on the initial msg->msg_state. The caller is responsible for
1409 * ensuring that the message does not wrap. The function can be preempted
1410 * everywhere when data are missing and recalled at the exact same location
1411 * with no information loss. The message may even be realigned between two
1412 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001413 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001414 * fields. Note that msg->sol will be initialized after completing the first
1415 * state, so that none of the msg pointers has to be initialized prior to the
1416 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001417 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001418void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419{
Willy Tarreau3770f232013-12-07 00:01:53 +01001420 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001422 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001423
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001424 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001425 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001426 ptr = buf->p + msg->next;
1427 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 if (unlikely(ptr >= end))
1430 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /*
1434 * First, states that are specific to the response only.
1435 * We check them first so that request and headers are
1436 * closer to each other (accessed more often).
1437 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001439 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001441 /* we have a start of message, but we have to check
1442 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001443 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001444 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001445 if (unlikely(ptr != buf->p)) {
1446 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001447 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001448 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001449 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 }
Willy Tarreau26927362012-05-18 23:22:52 +02001451 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001452 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 hdr_idx_init(idx);
1454 state = HTTP_MSG_RPVER;
1455 goto http_msg_rpver;
1456 }
1457
1458 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1459 goto http_msg_invalid;
1460
1461 if (unlikely(*ptr == '\n'))
1462 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1463 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1464 /* stop here */
1465
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001468 EXPECT_LF_HERE(ptr, http_msg_invalid);
1469 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1470 /* stop here */
1471
Willy Tarreau8973c702007-01-21 23:58:29 +01001472 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001474 case HTTP_MSG_RPVER_SP:
1475 case HTTP_MSG_RPCODE:
1476 case HTTP_MSG_RPCODE_SP:
1477 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001478 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001479 state, ptr, end,
1480 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 if (unlikely(!ptr))
1482 return;
1483
1484 /* we have a full response and we know that we have either a CR
1485 * or an LF at <ptr>.
1486 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1488
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001489 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 if (likely(*ptr == '\r'))
1491 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1492 goto http_msg_rpline_end;
1493
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001495 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001496 /* msg->sol must point to the first of CR or LF. */
1497 EXPECT_LF_HERE(ptr, http_msg_invalid);
1498 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1499 /* stop here */
1500
1501 /*
1502 * Second, states that are specific to the request only
1503 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001505 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001507 /* we have a start of message, but we have to check
1508 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001509 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001510 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001511 if (likely(ptr != buf->p)) {
1512 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001513 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001514 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001515 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 }
Willy Tarreau26927362012-05-18 23:22:52 +02001517 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001518 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001519 state = HTTP_MSG_RQMETH;
1520 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1524 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(*ptr == '\n'))
1527 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001532 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 EXPECT_LF_HERE(ptr, http_msg_invalid);
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 case HTTP_MSG_RQMETH_SP:
1540 case HTTP_MSG_RQURI:
1541 case HTTP_MSG_RQURI_SP:
1542 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001543 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001544 state, ptr, end,
1545 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(!ptr))
1547 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 /* we have a full request and we know that we have either a CR
1550 * or an LF at <ptr>.
1551 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001553
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001554 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001560 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 /* check for HTTP/0.9 request : no version information available.
1562 * msg->sol must point to the first of CR or LF.
1563 */
1564 if (unlikely(msg->sl.rq.v_l == 0))
1565 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 EXPECT_LF_HERE(ptr, http_msg_invalid);
1568 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 /*
1572 * Common states below
1573 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001575 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001576 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (likely(!HTTP_IS_CRLF(*ptr))) {
1578 goto http_msg_hdr_name;
1579 }
1580
1581 if (likely(*ptr == '\r'))
1582 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1583 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001586 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 /* assumes msg->sol points to the first char */
1588 if (likely(HTTP_IS_TOKEN(*ptr)))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001591 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001593
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001594 if (likely(msg->err_pos < -1) || *ptr == '\n')
1595 goto http_msg_invalid;
1596
1597 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001598 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001599
1600 /* and we still accept this non-token character */
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001605 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 if (likely(HTTP_IS_SPHT(*ptr)))
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001610 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001611
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 if (likely(!HTTP_IS_CRLF(*ptr))) {
1613 goto http_msg_hdr_val;
1614 }
1615
1616 if (likely(*ptr == '\r'))
1617 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1618 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001621 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 EXPECT_LF_HERE(ptr, http_msg_invalid);
1623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001626 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 if (likely(HTTP_IS_SPHT(*ptr))) {
1628 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 for (; buf->p + msg->sov < ptr; msg->sov++)
1630 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 goto http_msg_hdr_l1_sp;
1632 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001633 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001634 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 goto http_msg_complete_header;
1636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001639 /* assumes msg->sol points to the first char, and msg->sov
1640 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 */
1642 if (likely(!HTTP_IS_CRLF(*ptr)))
1643 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001645 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 /* Note: we could also copy eol into ->eoh so that we have the
1647 * real header end in case it ends with lots of LWS, but is this
1648 * really needed ?
1649 */
1650 if (likely(*ptr == '\r'))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1652 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 EXPECT_LF_HERE(ptr, http_msg_invalid);
1657 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001660 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1662 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001663 for (; buf->p + msg->eol < ptr; msg->eol++)
1664 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 goto http_msg_hdr_val;
1666 }
1667 http_msg_complete_header:
1668 /*
1669 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001670 * Assumes msg->sol points to the first char, msg->sov points
1671 * to the first character of the value and msg->eol to the
1672 * first CR or LF so we know how the line ends. We insert last
1673 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001675 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 idx, idx->tail) < 0))
1677 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001678
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001679 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 if (likely(!HTTP_IS_CRLF(*ptr))) {
1681 goto http_msg_hdr_name;
1682 }
1683
1684 if (likely(*ptr == '\r'))
1685 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1686 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001689 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 /* Assumes msg->sol points to the first of either CR or LF */
1691 EXPECT_LF_HERE(ptr, http_msg_invalid);
1692 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001693 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001694 msg->eoh = msg->sol;
1695 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001696 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001698
1699 case HTTP_MSG_ERROR:
1700 /* this may only happen if we call http_msg_analyser() twice with an error */
1701 break;
1702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001704#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1706 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001707#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001708 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 }
1710 http_msg_ood:
1711 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001712 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001713 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 http_msg_invalid:
1717 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001718 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001719 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 return;
1721}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001722
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001723/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1724 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1725 * nothing is done and 1 is returned.
1726 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001727static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001728{
1729 int delta;
1730 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001731 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001732
1733 if (msg->sl.rq.v_l != 0)
1734 return 1;
1735
Willy Tarreau9b28e032012-10-12 23:49:43 +02001736 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001737 delta = 0;
1738
1739 if (msg->sl.rq.u_l == 0) {
1740 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001741 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001742 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001743 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001744 }
1745 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001746 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001747 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001748 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001749 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001750 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001751 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001752 NULL, NULL);
1753 if (unlikely(!cur_end))
1754 return 0;
1755
1756 /* we have a full HTTP/1.0 request now and we know that
1757 * we have either a CR or an LF at <ptr>.
1758 */
1759 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1760 return 1;
1761}
1762
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001763/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001764 * and "keep-alive" values. If we already know that some headers may safely
1765 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001766 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1767 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001768 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001769 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1770 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1771 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001772 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001773 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001774void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001775{
Willy Tarreau5b154472009-12-21 20:11:07 +01001776 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001777 const char *hdr_val = "Connection";
1778 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001779
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001780 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001781 return;
1782
Willy Tarreau88d349d2010-01-25 12:15:43 +01001783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1784 hdr_val = "Proxy-Connection";
1785 hdr_len = 16;
1786 }
1787
Willy Tarreau5b154472009-12-21 20:11:07 +01001788 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001789 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001791 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1792 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001793 if (to_del & 2)
1794 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001795 else
1796 txn->flags |= TX_CON_KAL_SET;
1797 }
1798 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1799 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800 if (to_del & 1)
1801 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802 else
1803 txn->flags |= TX_CON_CLO_SET;
1804 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001805 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1806 txn->flags |= TX_HDR_CONN_UPG;
1807 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001808 }
1809
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001810 txn->flags |= TX_HDR_CONN_PRS;
1811 return;
1812}
Willy Tarreau5b154472009-12-21 20:11:07 +01001813
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001814/* Apply desired changes on the Connection: header. Values may be removed and/or
1815 * added depending on the <wanted> flags, which are exclusively composed of
1816 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1817 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1818 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001819void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001820{
1821 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001822 const char *hdr_val = "Connection";
1823 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001824
1825 ctx.idx = 0;
1826
Willy Tarreau88d349d2010-01-25 12:15:43 +01001827
1828 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1829 hdr_val = "Proxy-Connection";
1830 hdr_len = 16;
1831 }
1832
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001833 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001834 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001835 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1836 if (wanted & TX_CON_KAL_SET)
1837 txn->flags |= TX_CON_KAL_SET;
1838 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001840 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1842 if (wanted & TX_CON_CLO_SET)
1843 txn->flags |= TX_CON_CLO_SET;
1844 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001845 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001847 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001848
1849 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1850 return;
1851
1852 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1853 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001854 hdr_val = "Connection: close";
1855 hdr_len = 17;
1856 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1857 hdr_val = "Proxy-Connection: close";
1858 hdr_len = 23;
1859 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001860 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001861 }
1862
1863 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1864 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001865 hdr_val = "Connection: keep-alive";
1866 hdr_len = 22;
1867 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1868 hdr_val = "Proxy-Connection: keep-alive";
1869 hdr_len = 28;
1870 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001871 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 }
1873 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001874}
1875
Willy Tarreaua458b672012-03-05 11:17:50 +01001876/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001877 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001878 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001879 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001880 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001881 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001882static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001883{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001884 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001885 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001886 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001887 const char *end = buf->data + buf->size;
1888 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001889 unsigned int chunk = 0;
1890
1891 /* The chunk size is in the following form, though we are only
1892 * interested in the size and CRLF :
1893 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1894 */
1895 while (1) {
1896 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001897 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001898 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001899 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001900 if (c < 0) /* not a hex digit anymore */
1901 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001902 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001903 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001904 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001905 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001906 chunk = (chunk << 4) + c;
1907 }
1908
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001910 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001911 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912
1913 while (http_is_spht[(unsigned char)*ptr]) {
1914 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001916 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001917 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 }
1919
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 /* Up to there, we know that at least one byte is present at *ptr. Check
1921 * for the end of chunk size.
1922 */
1923 while (1) {
1924 if (likely(HTTP_IS_CRLF(*ptr))) {
1925 /* we now have a CR or an LF at ptr */
1926 if (likely(*ptr == '\r')) {
1927 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001929 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930 return 0;
1931 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001932
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001934 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001935 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001936 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 /* done */
1938 break;
1939 }
1940 else if (*ptr == ';') {
1941 /* chunk extension, ends at next CRLF */
1942 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001943 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001944 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001945 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946
1947 while (!HTTP_IS_CRLF(*ptr)) {
1948 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001949 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001950 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 return 0;
1952 }
1953 /* we have a CRLF now, loop above */
1954 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001955 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001957 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 }
1959
Willy Tarreaud98cf932009-12-27 22:54:55 +01001960 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001961 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001963 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001964 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001965 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001966 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001967 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001968 msg->chunk_len = chunk;
1969 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001971 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001972 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001973 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001974 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001975}
1976
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001977/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001978 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 * the trailers is found, it is automatically scheduled to be forwarded,
1980 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1981 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001982 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001983 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001984 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001985 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1986 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001987 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001988 * matches the length of trailers already parsed and not forwarded. It is also
1989 * important to note that this function is designed to be able to parse wrapped
1990 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001991 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001992static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001993{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001994 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001995
Willy Tarreaua458b672012-03-05 11:17:50 +01001996 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001998 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001999 const char *ptr = b_ptr(buf, msg->next);
2000 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002001 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002
2003 /* scan current line and stop at LF or CRLF */
2004 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002005 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 return 0;
2007
2008 if (*ptr == '\n') {
2009 if (!p1)
2010 p1 = ptr;
2011 p2 = ptr;
2012 break;
2013 }
2014
2015 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002017 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002018 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002019 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 p1 = ptr;
2021 }
2022
2023 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002024 if (ptr >= buf->data + buf->size)
2025 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002026 }
2027
2028 /* after LF; point to beginning of next line */
2029 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002030 if (p2 >= buf->data + buf->size)
2031 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002032
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002033 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002034 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002035 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002036
2037 /* schedule this line for forwarding */
2038 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 if (msg->sov >= buf->size)
2040 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002041
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002042 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002043 /* LF/CRLF at beginning of line => end of trailers at p2.
2044 * Everything was scheduled for forwarding, there's nothing
2045 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002046 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002047 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048 msg->msg_state = HTTP_MSG_DONE;
2049 return 1;
2050 }
2051 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002052 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053 }
2054}
2055
Willy Tarreau54d23df2012-10-25 19:04:45 +02002056/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01002057 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02002058 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002059 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2061 * not enough data are available, the function does not change anything and
2062 * returns zero. If a parse error is encountered, the function returns < 0 and
2063 * does not change anything. Note: this function is designed to parse wrapped
2064 * CRLF at the end of the buffer.
2065 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002066static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002067{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002068 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002069 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 int bytes;
2071
2072 /* NB: we'll check data availabilty at the end. It's not a
2073 * problem because whatever we match first will be checked
2074 * against the correct length.
2075 */
2076 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002078 if (*ptr == '\r') {
2079 bytes++;
2080 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002081 if (ptr >= buf->data + buf->size)
2082 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 }
2084
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002085 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 return 0;
2087
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002088 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002089 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002090 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002091 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092
2093 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002094 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002095 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02002096 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
2097 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002098 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2100 return 1;
2101}
Willy Tarreau5b154472009-12-21 20:11:07 +01002102
William Lallemand82fe75c2012-10-23 10:25:10 +02002103
2104/*
2105 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002106 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002107int select_compression_request_header(struct session *s, struct buffer *req)
2108{
2109 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002110 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002111 struct hdr_ctx ctx;
2112 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002113 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002114
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002115 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2116 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002117 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2118 */
2119 ctx.idx = 0;
2120 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2121 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002122 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2123 (ctx.vlen < 31 ||
2124 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2125 ctx.line[ctx.val + 30] < '6' ||
2126 (ctx.line[ctx.val + 30] == '6' &&
2127 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2128 s->comp_algo = NULL;
2129 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002130 }
2131
William Lallemand82fe75c2012-10-23 10:25:10 +02002132 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002133 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 +02002134 ctx.idx = 0;
2135 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002136 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002137 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2138 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002139
2140 /* remove all occurrences of the header when "compression offload" is set */
2141
2142 if ((s->be->comp && s->be->comp->offload) ||
2143 (s->fe->comp && s->fe->comp->offload)) {
2144 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2145 ctx.idx = 0;
2146 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2147 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2148 }
2149 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002150 return 1;
2151 }
2152 }
2153 }
2154 }
2155
2156 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002157 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2158 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002159 if (comp_algo->add_data == identity_add_data) {
2160 s->comp_algo = comp_algo;
2161 return 1;
2162 }
2163 }
2164 }
2165
2166 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002167 return 0;
2168}
2169
2170/*
2171 * Selects a comression algorithm depending of the server response.
2172 */
2173int select_compression_response_header(struct session *s, struct buffer *res)
2174{
2175 struct http_txn *txn = &s->txn;
2176 struct http_msg *msg = &txn->rsp;
2177 struct hdr_ctx ctx;
2178 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002179
2180 /* no common compression algorithm was found in request header */
2181 if (s->comp_algo == NULL)
2182 goto fail;
2183
2184 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002185 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002186 goto fail;
2187
William Lallemandd3002612012-11-26 14:34:47 +01002188 /* 200 only */
2189 if (txn->status != 200)
2190 goto fail;
2191
William Lallemand82fe75c2012-10-23 10:25:10 +02002192 /* Content-Length is null */
2193 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2194 goto fail;
2195
Willy Tarreau667c2a32013-04-09 08:13:58 +02002196 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2197 if (msg->flags & HTTP_MSGF_TE_CHNK)
2198 goto fail;
2199
William Lallemand82fe75c2012-10-23 10:25:10 +02002200 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002201 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002202 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2203 goto fail;
2204
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002205 /* no compression when Cache-Control: no-transform is present in the message */
2206 ctx.idx = 0;
2207 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2208 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2209 goto fail;
2210 }
2211
William Lallemand82fe75c2012-10-23 10:25:10 +02002212 comp_type = NULL;
2213
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002214 /* we don't want to compress multipart content-types, nor content-types that are
2215 * not listed in the "compression type" directive if any. If no content-type was
2216 * found but configuration requires one, we don't compress either. Backend has
2217 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002218 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002219 ctx.idx = 0;
2220 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2221 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2222 goto fail;
2223
2224 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2225 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002226 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002227 if (ctx.vlen >= comp_type->name_len &&
2228 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 /* this Content-Type should be compressed */
2230 break;
2231 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002232 /* this Content-Type should not be compressed */
2233 if (comp_type == NULL)
2234 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002235 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002236 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002237 else { /* no content-type header */
2238 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2239 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002240 }
2241
William Lallemandd85f9172012-11-09 17:05:39 +01002242 /* limit compression rate */
2243 if (global.comp_rate_lim > 0)
2244 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2245 goto fail;
2246
William Lallemand072a2bf2012-11-20 17:01:01 +01002247 /* limit cpu usage */
2248 if (idle_pct < compress_min_idle)
2249 goto fail;
2250
William Lallemand4c49fae2012-11-07 15:00:23 +01002251 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002252 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002253 goto fail;
2254
William Lallemandec3e3892012-11-12 17:02:18 +01002255 s->flags |= SN_COMP_READY;
2256
William Lallemand82fe75c2012-10-23 10:25:10 +02002257 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002258 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002259 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2260 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2261
2262 /* add Transfer-Encoding header */
2263 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2264 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2265
2266 /*
2267 * Add Content-Encoding header when it's not identity encoding.
2268 * RFC 2616 : Identity encoding: This content-coding is used only in the
2269 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2270 * header.
2271 */
2272 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002273 trash.len = 18;
2274 memcpy(trash.str, "Content-Encoding: ", trash.len);
2275 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2276 trash.len += s->comp_algo->name_len;
2277 trash.str[trash.len] = '\0';
2278 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002279 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 return 1;
2281
2282fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002283 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002284 return 0;
2285}
2286
2287
Willy Tarreaud787e662009-07-07 10:14:51 +02002288/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2289 * processing can continue on next analysers, or zero if it either needs more
2290 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2291 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2292 * when it has nothing left to do, and may remove any analyser when it wants to
2293 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002294 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002295int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002296{
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 /*
2298 * We will parse the partial (or complete) lines.
2299 * We will check the request syntax, and also join multi-line
2300 * headers. An index of all the lines will be elaborated while
2301 * parsing.
2302 *
2303 * For the parsing, we use a 28 states FSM.
2304 *
2305 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002306 * req->buf->p = beginning of request
2307 * req->buf->p + msg->eoh = end of processed headers / start of current one
2308 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002309 * msg->eol = end of current header or line (LF or CRLF)
2310 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002311 *
2312 * At end of parsing, we may perform a capture of the error (if any), and
2313 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2314 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2315 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002317
Willy Tarreau59234e92008-11-30 23:51:27 +01002318 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002319 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 struct http_txn *txn = &s->txn;
2321 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002322 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002323
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002324 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 +01002325 now_ms, __FUNCTION__,
2326 s,
2327 req,
2328 req->rex, req->wex,
2329 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002330 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002331 req->analysers);
2332
Willy Tarreau52a0c602009-08-16 22:45:38 +02002333 /* we're speaking HTTP here, so let's speak HTTP to the client */
2334 s->srv_error = http_return_srv_error;
2335
Willy Tarreau83e3af02009-12-28 17:39:57 +01002336 /* There's a protected area at the end of the buffer for rewriting
2337 * purposes. We don't want to start to parse the request if the
2338 * protected area is affected, because we may have to move processed
2339 * data later, which is much more complicated.
2340 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002341 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002342 if (txn->flags & TX_NOT_FIRST) {
2343 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002344 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002345 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002346 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002347 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002348 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002349 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002350 return 0;
2351 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002352 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2353 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2354 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002355 }
2356
Willy Tarreau065e8332010-01-08 00:30:20 +01002357 /* Note that we have the same problem with the response ; we
2358 * may want to send a redirect, error or anything which requires
2359 * some spare space. So we'll ensure that we have at least
2360 * maxrewrite bytes available in the response buffer before
2361 * processing that one. This will only affect pipelined
2362 * keep-alive requests.
2363 */
2364 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002365 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002366 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2367 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2368 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002369 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002370 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002371 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002372 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002373 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002374 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002375 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002376 return 0;
2377 }
2378 }
2379
Willy Tarreau9b28e032012-10-12 23:49:43 +02002380 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002381 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002382 }
2383
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 /* 1: we might have to print this header in debug mode */
2385 if (unlikely((global.mode & MODE_DEBUG) &&
2386 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002387 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002389
Willy Tarreau9b28e032012-10-12 23:49:43 +02002390 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002391 /* this is a bit complex : in case of error on the request line,
2392 * we know that rq.l is still zero, so we display only the part
2393 * up to the end of the line (truncated by debug_hdr).
2394 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002395 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002397
Willy Tarreau59234e92008-11-30 23:51:27 +01002398 sol += hdr_idx_first_pos(&txn->hdr_idx);
2399 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002400
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 while (cur_idx) {
2402 eol = sol + txn->hdr_idx.v[cur_idx].len;
2403 debug_hdr("clihdr", s, sol, eol);
2404 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2405 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002406 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 }
2408
Willy Tarreau58f10d72006-12-04 02:26:12 +01002409
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 /*
2411 * Now we quickly check if we have found a full valid request.
2412 * If not so, we check the FD and buffer states before leaving.
2413 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002414 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002415 * requests are checked first. When waiting for a second request
2416 * on a keep-alive session, if we encounter and error, close, t/o,
2417 * we note the error in the session flags but don't set any state.
2418 * Since the error will be noted there, it will not be counted by
2419 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002420 * Last, we may increase some tracked counters' http request errors on
2421 * the cases that are deliberately the client's fault. For instance,
2422 * a timeout or connection reset is not counted as an error. However
2423 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002424 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002425
Willy Tarreau655dce92009-11-08 13:10:58 +01002426 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002427 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002429 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002430 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002431 session_inc_http_req_ctr(s);
2432 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002433 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 /* 1: Since we are in header mode, if there's no space
2438 * left for headers, we won't be able to free more
2439 * later, so the session will never terminate. We
2440 * must terminate it now.
2441 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002442 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002443 /* FIXME: check if URI is set and return Status
2444 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002445 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002446 session_inc_http_req_ctr(s);
2447 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002448 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002449 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002450 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002451 goto return_bad_req;
2452 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002453
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002455 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002456 if (!(s->flags & SN_ERR_MASK))
2457 s->flags |= SN_ERR_CLICL;
2458
Willy Tarreaufcffa692010-01-10 14:21:19 +01002459 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002460 goto failed_keep_alive;
2461
Willy Tarreau59234e92008-11-30 23:51:27 +01002462 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002463 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002464 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002465 session_inc_http_err_ctr(s);
2466 }
2467
Willy Tarreaudc979f22012-12-04 10:39:01 +01002468 txn->status = 400;
2469 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 msg->msg_state = HTTP_MSG_ERROR;
2471 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002472
Willy Tarreauda7ff642010-06-23 11:44:09 +02002473 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002474 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002475 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002476 if (s->listener->counters)
2477 s->listener->counters->failed_req++;
2478
Willy Tarreau59234e92008-11-30 23:51:27 +01002479 if (!(s->flags & SN_FINST_MASK))
2480 s->flags |= SN_FINST_R;
2481 return 0;
2482 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002483
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002485 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002486 if (!(s->flags & SN_ERR_MASK))
2487 s->flags |= SN_ERR_CLITO;
2488
Willy Tarreaufcffa692010-01-10 14:21:19 +01002489 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002490 goto failed_keep_alive;
2491
Willy Tarreau59234e92008-11-30 23:51:27 +01002492 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002493 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002494 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002495 session_inc_http_err_ctr(s);
2496 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002498 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 msg->msg_state = HTTP_MSG_ERROR;
2500 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002501
Willy Tarreauda7ff642010-06-23 11:44:09 +02002502 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002503 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002504 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002505 if (s->listener->counters)
2506 s->listener->counters->failed_req++;
2507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 if (!(s->flags & SN_FINST_MASK))
2509 s->flags |= SN_FINST_R;
2510 return 0;
2511 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002514 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002515 if (!(s->flags & SN_ERR_MASK))
2516 s->flags |= SN_ERR_CLICL;
2517
Willy Tarreaufcffa692010-01-10 14:21:19 +01002518 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002519 goto failed_keep_alive;
2520
Willy Tarreau4076a152009-04-02 15:18:36 +02002521 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002522 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002524 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 msg->msg_state = HTTP_MSG_ERROR;
2526 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002527
Willy Tarreauda7ff642010-06-23 11:44:09 +02002528 session_inc_http_err_ctr(s);
2529 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002530 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002531 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002532 if (s->listener->counters)
2533 s->listener->counters->failed_req++;
2534
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 if (!(s->flags & SN_FINST_MASK))
2536 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002537 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002538 }
2539
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002540 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002541 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2542 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002543#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002544 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 +01002545 /* We need more data, we have to re-enable quick-ack in case we
2546 * previously disabled it, otherwise we might cause the client
2547 * to delay next data.
2548 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002549 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002550 }
2551#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002552
Willy Tarreaufcffa692010-01-10 14:21:19 +01002553 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2554 /* If the client starts to talk, let's fall back to
2555 * request timeout processing.
2556 */
2557 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002558 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002559 }
2560
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002562 if (!tick_isset(req->analyse_exp)) {
2563 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2564 (txn->flags & TX_WAIT_NEXT_RQ) &&
2565 tick_isset(s->be->timeout.httpka))
2566 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2567 else
2568 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /* we're not ready yet */
2572 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002573
2574 failed_keep_alive:
2575 /* Here we process low-level errors for keep-alive requests. In
2576 * short, if the request is not the first one and it experiences
2577 * a timeout, read error or shutdown, we just silently close so
2578 * that the client can try again.
2579 */
2580 txn->status = 0;
2581 msg->msg_state = HTTP_MSG_RQBEFORE;
2582 req->analysers = 0;
2583 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002584 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002585 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002586 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002587 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002589
Willy Tarreaud787e662009-07-07 10:14:51 +02002590 /* OK now we have a complete HTTP request with indexed headers. Let's
2591 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002592 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002593 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002594 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002595 * byte after the last LF. msg->sov points to the first byte of data.
2596 * msg->eol cannot be trusted because it may have been left uninitialized
2597 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002598 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002599
Willy Tarreauda7ff642010-06-23 11:44:09 +02002600 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002601 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2602
Willy Tarreaub16a5742010-01-10 14:46:16 +01002603 if (txn->flags & TX_WAIT_NEXT_RQ) {
2604 /* kill the pending keep-alive timeout */
2605 txn->flags &= ~TX_WAIT_NEXT_RQ;
2606 req->analyse_exp = TICK_ETERNITY;
2607 }
2608
2609
Willy Tarreaud787e662009-07-07 10:14:51 +02002610 /* Maybe we found in invalid header name while we were configured not
2611 * to block on that, so we have to capture it now.
2612 */
2613 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002614 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002615
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 /*
2617 * 1: identify the method
2618 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002619 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002620
2621 /* we can make use of server redirect on GET and HEAD */
2622 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2623 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002624
Willy Tarreau59234e92008-11-30 23:51:27 +01002625 /*
2626 * 2: check if the URI matches the monitor_uri.
2627 * We have to do this for every request which gets in, because
2628 * the monitor-uri is defined by the frontend.
2629 */
2630 if (unlikely((s->fe->monitor_uri_len != 0) &&
2631 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002632 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 s->fe->monitor_uri,
2634 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002635 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002637 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002639
Willy Tarreau59234e92008-11-30 23:51:27 +01002640 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002641 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002642
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002644 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002645 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002646
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 ret = acl_pass(ret);
2648 if (cond->pol == ACL_COND_UNLESS)
2649 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002650
Willy Tarreau59234e92008-11-30 23:51:27 +01002651 if (ret) {
2652 /* we fail this request, let's return 503 service unavail */
2653 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002654 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002655 if (!(s->flags & SN_ERR_MASK))
2656 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002658 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002659 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002660
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 /* nothing to fail, let's reply normaly */
2662 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002663 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002664 if (!(s->flags & SN_ERR_MASK))
2665 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002666 goto return_prx_cond;
2667 }
2668
2669 /*
2670 * 3: Maybe we have to copy the original REQURI for the logs ?
2671 * Note: we cannot log anymore if the request has been
2672 * classified as invalid.
2673 */
2674 if (unlikely(s->logs.logwait & LW_REQ)) {
2675 /* we have a complete HTTP request that we must log */
2676 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2677 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002678
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 if (urilen >= REQURI_LEN)
2680 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002681 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002683
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002684 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 s->do_log(s);
2686 } else {
2687 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002688 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002689 }
Willy Tarreau06619262006-12-17 08:37:22 +01002690
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002692 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002693 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002694
Willy Tarreau5b154472009-12-21 20:11:07 +01002695 /* ... and check if the request is HTTP/1.1 or above */
2696 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002697 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2698 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2699 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002700 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002701
2702 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002703 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 +01002704
Willy Tarreau88d349d2010-01-25 12:15:43 +01002705 /* if the frontend has "option http-use-proxy-header", we'll check if
2706 * we have what looks like a proxied connection instead of a connection,
2707 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2708 * Note that this is *not* RFC-compliant, however browsers and proxies
2709 * happen to do that despite being non-standard :-(
2710 * We consider that a request not beginning with either '/' or '*' is
2711 * a proxied connection, which covers both "scheme://location" and
2712 * CONNECT ip:port.
2713 */
2714 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002716 txn->flags |= TX_USE_PX_CONN;
2717
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002718 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002719 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002720
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002722 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002723 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002725
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002726 /* 6: determine the transfer-length.
2727 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2728 * the presence of a message-body in a REQUEST and its transfer length
2729 * must be determined that way (in order of precedence) :
2730 * 1. The presence of a message-body in a request is signaled by the
2731 * inclusion of a Content-Length or Transfer-Encoding header field
2732 * in the request's header fields. When a request message contains
2733 * both a message-body of non-zero length and a method that does
2734 * not define any semantics for that request message-body, then an
2735 * origin server SHOULD either ignore the message-body or respond
2736 * with an appropriate error message (e.g., 413). A proxy or
2737 * gateway, when presented the same request, SHOULD either forward
2738 * the request inbound with the message- body or ignore the
2739 * message-body when determining a response.
2740 *
2741 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2742 * and the "chunked" transfer-coding (Section 6.2) is used, the
2743 * transfer-length is defined by the use of this transfer-coding.
2744 * If a Transfer-Encoding header field is present and the "chunked"
2745 * transfer-coding is not present, the transfer-length is defined
2746 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002747 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002748 * 3. If a Content-Length header field is present, its decimal value in
2749 * OCTETs represents both the entity-length and the transfer-length.
2750 * If a message is received with both a Transfer-Encoding header
2751 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002752 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002753 * 4. By the server closing the connection. (Closing the connection
2754 * cannot be used to indicate the end of a request body, since that
2755 * would leave no possibility for the server to send back a response.)
2756 *
2757 * Whenever a transfer-coding is applied to a message-body, the set of
2758 * transfer-codings MUST include "chunked", unless the message indicates
2759 * it is terminated by closing the connection. When the "chunked"
2760 * transfer-coding is used, it MUST be the last transfer-coding applied
2761 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002762 */
2763
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002765 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002766 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002767 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002768 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002769 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002770 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2771 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002772 /* bad transfer-encoding (chunked followed by something else) */
2773 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002774 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002775 break;
2776 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002777 }
2778
Willy Tarreau32b47f42009-10-18 20:55:02 +02002779 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002780 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002781 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002782 signed long long cl;
2783
Willy Tarreauad14f752011-09-02 20:33:27 +02002784 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002785 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002786 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002787 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002788
Willy Tarreauad14f752011-09-02 20:33:27 +02002789 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002790 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002791 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002792 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002793
Willy Tarreauad14f752011-09-02 20:33:27 +02002794 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002795 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002796 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002797 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002798
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002799 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002800 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002801 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002802 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002803
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002804 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002805 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002806 }
2807
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002808 /* bodyless requests have a known length */
2809 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002810 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002811
Willy Tarreaud787e662009-07-07 10:14:51 +02002812 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002813 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002814 req->analyse_exp = TICK_ETERNITY;
2815 return 1;
2816
2817 return_bad_req:
2818 /* We centralize bad requests processing here */
2819 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2820 /* we detected a parsing error. We want to archive this request
2821 * in the dedicated proxy area for later troubleshooting.
2822 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002823 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 }
2825
2826 txn->req.msg_state = HTTP_MSG_ERROR;
2827 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002828 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002829
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002830 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002831 if (s->listener->counters)
2832 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002833
2834 return_prx_cond:
2835 if (!(s->flags & SN_ERR_MASK))
2836 s->flags |= SN_ERR_PRXCOND;
2837 if (!(s->flags & SN_FINST_MASK))
2838 s->flags |= SN_FINST_R;
2839
2840 req->analysers = 0;
2841 req->analyse_exp = TICK_ETERNITY;
2842 return 0;
2843}
2844
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002845
Willy Tarreau347a35d2013-11-22 17:51:09 +01002846/* This function prepares an applet to handle the stats. It can deal with the
2847 * "100-continue" expectation, check that admin rules are met for POST requests,
2848 * and program a response message if something was unexpected. It cannot fail
2849 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002850 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002851 * s->target which is supposed to already point to the stats applet. The caller
2852 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002853 */
2854int http_handle_stats(struct session *s, struct channel *req)
2855{
2856 struct stats_admin_rule *stats_admin_rule;
2857 struct stream_interface *si = s->rep->prod;
2858 struct http_txn *txn = &s->txn;
2859 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002860 struct uri_auth *uri_auth = s->be->uri_auth;
2861 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002862 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002863
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002864 appctx = si_appctx(si);
2865 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2866 appctx->st1 = appctx->st2 = 0;
2867 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2868 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002869
2870 uri = msg->chn->buf->p + msg->sl.rq.u;
2871 lookup = uri + uri_auth->uri_len;
2872
2873 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2874 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002875 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002876 break;
2877 }
2878 }
2879
2880 if (uri_auth->refresh) {
2881 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2882 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002883 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002884 break;
2885 }
2886 }
2887 }
2888
2889 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2890 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002892 break;
2893 }
2894 }
2895
2896 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2897 if (memcmp(h, ";st=", 4) == 0) {
2898 int i;
2899 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002900 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002901 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2902 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002903 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002904 break;
2905 }
2906 }
2907 break;
2908 }
2909 }
2910
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002911 appctx->ctx.stats.scope_str = 0;
2912 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002913 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2914 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2915 int itx = 0;
2916 const char *h2;
2917 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2918 const char *err;
2919
2920 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2921 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002922 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002923 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2924 itx++;
2925 h++;
2926 }
2927
2928 if (itx > STAT_SCOPE_TXT_MAXLEN)
2929 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002930 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002931
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002932 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002933 memcpy(scope_txt, h2, itx);
2934 scope_txt[itx] = '\0';
2935 err = invalid_char(scope_txt);
2936 if (err) {
2937 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002938 appctx->ctx.stats.scope_str = 0;
2939 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002940 }
2941 break;
2942 }
2943 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002944
2945 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002946 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002947 int ret = 1;
2948
2949 if (stats_admin_rule->cond) {
2950 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2951 ret = acl_pass(ret);
2952 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2953 ret = !ret;
2954 }
2955
2956 if (ret) {
2957 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002958 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002959 break;
2960 }
2961 }
2962
2963 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01002964 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002965 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002966 if (msg->msg_state < HTTP_MSG_100_SENT) {
2967 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2968 * send an HTTP/1.1 100 Continue intermediate response.
2969 */
2970 if (msg->flags & HTTP_MSGF_VER_11) {
2971 struct hdr_ctx ctx;
2972 ctx.idx = 0;
2973 /* Expect is allowed in 1.1, look for it */
2974 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2975 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2976 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2977 }
2978 }
2979 msg->msg_state = HTTP_MSG_100_SENT;
2980 s->logs.tv_request = now; /* update the request timer to reflect full request */
2981 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002982 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002983 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002984 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002985 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2986 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002987 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002988 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002989 else {
2990 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002991 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002992 }
2993
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002994 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002995 return 1;
2996}
2997
Lukas Tribus67db8df2013-06-23 17:37:13 +02002998/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2999 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3000 */
3001static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3002{
3003#ifdef IP_TOS
3004 if (from.ss_family == AF_INET)
3005 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3006#endif
3007#ifdef IPV6_TCLASS
3008 if (from.ss_family == AF_INET6) {
3009 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3010 /* v4-mapped addresses need IP_TOS */
3011 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3012 else
3013 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3014 }
3015#endif
3016}
3017
Willy Tarreau20b0de52012-12-24 15:45:22 +01003018/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003019 * transaction <txn>. Returns the first rule that prevents further processing
3020 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3021 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3022 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003023 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003024static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003025http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003026{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003027 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003028 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003029 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003030
Willy Tarreauff011f22011-01-06 17:51:27 +01003031 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003032 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003033 continue;
3034
Willy Tarreau96257ec2012-12-27 10:46:37 +01003035 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003036 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003037 int ret;
3038
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003039 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003040 ret = acl_pass(ret);
3041
Willy Tarreauff011f22011-01-06 17:51:27 +01003042 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003043 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003044
3045 if (!ret) /* condition not matched */
3046 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003047 }
3048
Willy Tarreau20b0de52012-12-24 15:45:22 +01003049
Willy Tarreau96257ec2012-12-27 10:46:37 +01003050 switch (rule->action) {
3051 case HTTP_REQ_ACT_ALLOW:
3052 return NULL; /* "allow" rules are OK */
3053
3054 case HTTP_REQ_ACT_DENY:
3055 txn->flags |= TX_CLDENY;
3056 return rule;
3057
Willy Tarreauccbcc372012-12-27 12:37:57 +01003058 case HTTP_REQ_ACT_TARPIT:
3059 txn->flags |= TX_CLTARPIT;
3060 return rule;
3061
Willy Tarreau96257ec2012-12-27 10:46:37 +01003062 case HTTP_REQ_ACT_AUTH:
3063 return rule;
3064
Willy Tarreau81499eb2012-12-27 12:19:02 +01003065 case HTTP_REQ_ACT_REDIR:
3066 return rule;
3067
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003068 case HTTP_REQ_ACT_SET_NICE:
3069 s->task->nice = rule->arg.nice;
3070 break;
3071
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003072 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003073 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003074 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003075 break;
3076
Willy Tarreau51347ed2013-06-11 19:34:13 +02003077 case HTTP_REQ_ACT_SET_MARK:
3078#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003079 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003080 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003081#endif
3082 break;
3083
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003084 case HTTP_REQ_ACT_SET_LOGL:
3085 s->logs.level = rule->arg.loglevel;
3086 break;
3087
Willy Tarreau96257ec2012-12-27 10:46:37 +01003088 case HTTP_REQ_ACT_SET_HDR:
3089 ctx.idx = 0;
3090 /* remove all occurrences of the header */
3091 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3092 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3093 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003094 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003095 /* now fall through to header addition */
3096
3097 case HTTP_REQ_ACT_ADD_HDR:
3098 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3099 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3100 trash.len = rule->arg.hdr_add.name_len;
3101 trash.str[trash.len++] = ':';
3102 trash.str[trash.len++] = ' ';
3103 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3104 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3105 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003106 }
3107 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003108
3109 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003110 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003111}
3112
Willy Tarreau71241ab2012-12-27 11:30:54 +01003113
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003114/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3115 * transaction <txn>. Returns the first rule that prevents further processing
3116 * of the response (deny, ...) or NULL if it executed all rules or stopped
3117 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3118 * rule.
3119 */
3120static struct http_res_rule *
3121http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3122{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003123 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003124 struct http_res_rule *rule;
3125 struct hdr_ctx ctx;
3126
3127 list_for_each_entry(rule, rules, list) {
3128 if (rule->action >= HTTP_RES_ACT_MAX)
3129 continue;
3130
3131 /* check optional condition */
3132 if (rule->cond) {
3133 int ret;
3134
3135 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3136 ret = acl_pass(ret);
3137
3138 if (rule->cond->pol == ACL_COND_UNLESS)
3139 ret = !ret;
3140
3141 if (!ret) /* condition not matched */
3142 continue;
3143 }
3144
3145
3146 switch (rule->action) {
3147 case HTTP_RES_ACT_ALLOW:
3148 return NULL; /* "allow" rules are OK */
3149
3150 case HTTP_RES_ACT_DENY:
3151 txn->flags |= TX_SVDENY;
3152 return rule;
3153
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003154 case HTTP_RES_ACT_SET_NICE:
3155 s->task->nice = rule->arg.nice;
3156 break;
3157
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003158 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003159 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003160 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003161 break;
3162
Willy Tarreau51347ed2013-06-11 19:34:13 +02003163 case HTTP_RES_ACT_SET_MARK:
3164#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003165 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003166 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003167#endif
3168 break;
3169
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003170 case HTTP_RES_ACT_SET_LOGL:
3171 s->logs.level = rule->arg.loglevel;
3172 break;
3173
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003174 case HTTP_RES_ACT_SET_HDR:
3175 ctx.idx = 0;
3176 /* remove all occurrences of the header */
3177 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3178 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3179 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3180 }
3181 /* now fall through to header addition */
3182
3183 case HTTP_RES_ACT_ADD_HDR:
3184 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3185 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3186 trash.len = rule->arg.hdr_add.name_len;
3187 trash.str[trash.len++] = ':';
3188 trash.str[trash.len++] = ' ';
3189 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3190 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3191 break;
3192 }
3193 }
3194
3195 /* we reached the end of the rules, nothing to report */
3196 return NULL;
3197}
3198
3199
Willy Tarreau71241ab2012-12-27 11:30:54 +01003200/* Perform an HTTP redirect based on the information in <rule>. The function
3201 * returns non-zero on success, or zero in case of a, irrecoverable error such
3202 * as too large a request to build a valid response.
3203 */
3204static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3205{
3206 struct http_msg *msg = &txn->req;
3207 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003208 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003209
3210 /* build redirect message */
3211 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003212 case 308:
3213 msg_fmt = HTTP_308;
3214 break;
3215 case 307:
3216 msg_fmt = HTTP_307;
3217 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003218 case 303:
3219 msg_fmt = HTTP_303;
3220 break;
3221 case 301:
3222 msg_fmt = HTTP_301;
3223 break;
3224 case 302:
3225 default:
3226 msg_fmt = HTTP_302;
3227 break;
3228 }
3229
3230 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3231 return 0;
3232
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003233 location = trash.str + trash.len;
3234
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235 switch(rule->type) {
3236 case REDIRECT_TYPE_SCHEME: {
3237 const char *path;
3238 const char *host;
3239 struct hdr_ctx ctx;
3240 int pathlen;
3241 int hostlen;
3242
3243 host = "";
3244 hostlen = 0;
3245 ctx.idx = 0;
3246 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3247 host = ctx.line + ctx.val;
3248 hostlen = ctx.vlen;
3249 }
3250
3251 path = http_get_path(txn);
3252 /* build message using path */
3253 if (path) {
3254 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3255 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3256 int qs = 0;
3257 while (qs < pathlen) {
3258 if (path[qs] == '?') {
3259 pathlen = qs;
3260 break;
3261 }
3262 qs++;
3263 }
3264 }
3265 } else {
3266 path = "/";
3267 pathlen = 1;
3268 }
3269
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003270 if (rule->rdr_str) { /* this is an old "redirect" rule */
3271 /* check if we can add scheme + "://" + host + path */
3272 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3273 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003274
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003275 /* add scheme */
3276 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3277 trash.len += rule->rdr_len;
3278 }
3279 else {
3280 /* add scheme with executing log format */
3281 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003282
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003283 /* check if we can add scheme + "://" + host + path */
3284 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3285 return 0;
3286 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003287 /* add "://" */
3288 memcpy(trash.str + trash.len, "://", 3);
3289 trash.len += 3;
3290
3291 /* add host */
3292 memcpy(trash.str + trash.len, host, hostlen);
3293 trash.len += hostlen;
3294
3295 /* add path */
3296 memcpy(trash.str + trash.len, path, pathlen);
3297 trash.len += pathlen;
3298
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003299 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003300 if (trash.len && trash.str[trash.len - 1] != '/' &&
3301 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3302 if (trash.len > trash.size - 5)
3303 return 0;
3304 trash.str[trash.len] = '/';
3305 trash.len++;
3306 }
3307
3308 break;
3309 }
3310 case REDIRECT_TYPE_PREFIX: {
3311 const char *path;
3312 int pathlen;
3313
3314 path = http_get_path(txn);
3315 /* build message using path */
3316 if (path) {
3317 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3318 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3319 int qs = 0;
3320 while (qs < pathlen) {
3321 if (path[qs] == '?') {
3322 pathlen = qs;
3323 break;
3324 }
3325 qs++;
3326 }
3327 }
3328 } else {
3329 path = "/";
3330 pathlen = 1;
3331 }
3332
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003333 if (rule->rdr_str) { /* this is an old "redirect" rule */
3334 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3335 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003337 /* add prefix. Note that if prefix == "/", we don't want to
3338 * add anything, otherwise it makes it hard for the user to
3339 * configure a self-redirection.
3340 */
3341 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3342 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3343 trash.len += rule->rdr_len;
3344 }
3345 }
3346 else {
3347 /* add prefix with executing log format */
3348 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3349
3350 /* Check length */
3351 if (trash.len + pathlen > trash.size - 4)
3352 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003353 }
3354
3355 /* add path */
3356 memcpy(trash.str + trash.len, path, pathlen);
3357 trash.len += pathlen;
3358
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003359 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003360 if (trash.len && trash.str[trash.len - 1] != '/' &&
3361 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3362 if (trash.len > trash.size - 5)
3363 return 0;
3364 trash.str[trash.len] = '/';
3365 trash.len++;
3366 }
3367
3368 break;
3369 }
3370 case REDIRECT_TYPE_LOCATION:
3371 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003372 if (rule->rdr_str) { /* this is an old "redirect" rule */
3373 if (trash.len + rule->rdr_len > trash.size - 4)
3374 return 0;
3375
3376 /* add location */
3377 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3378 trash.len += rule->rdr_len;
3379 }
3380 else {
3381 /* add location with executing log format */
3382 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003383
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003384 /* Check left length */
3385 if (trash.len > trash.size - 4)
3386 return 0;
3387 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003388 break;
3389 }
3390
3391 if (rule->cookie_len) {
3392 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3393 trash.len += 14;
3394 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3395 trash.len += rule->cookie_len;
3396 memcpy(trash.str + trash.len, "\r\n", 2);
3397 trash.len += 2;
3398 }
3399
3400 /* add end of headers and the keep-alive/close status.
3401 * We may choose to set keep-alive if the Location begins
3402 * with a slash, because the client will come back to the
3403 * same server.
3404 */
3405 txn->status = rule->code;
3406 /* let's log the request time */
3407 s->logs.tv_request = now;
3408
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003409 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003410 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3411 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3412 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3413 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3414 /* keep-alive possible */
3415 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3416 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3417 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3418 trash.len += 30;
3419 } else {
3420 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3421 trash.len += 24;
3422 }
3423 }
3424 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3425 trash.len += 4;
3426 bo_inject(txn->rsp.chn, trash.str, trash.len);
3427 /* "eat" the request */
3428 bi_fast_delete(txn->req.chn->buf, msg->sov);
3429 msg->sov = 0;
3430 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3431 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3432 txn->req.msg_state = HTTP_MSG_CLOSED;
3433 txn->rsp.msg_state = HTTP_MSG_DONE;
3434 } else {
3435 /* keep-alive not possible */
3436 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3437 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3438 trash.len += 29;
3439 } else {
3440 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3441 trash.len += 23;
3442 }
3443 stream_int_retnclose(txn->req.chn->prod, &trash);
3444 txn->req.chn->analysers = 0;
3445 }
3446
3447 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003448 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003449 if (!(s->flags & SN_FINST_MASK))
3450 s->flags |= SN_FINST_R;
3451
3452 return 1;
3453}
3454
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003455/* This stream analyser runs all HTTP request processing which is common to
3456 * frontends and backends, which means blocking ACLs, filters, connection-close,
3457 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003458 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003459 * either needs more data or wants to immediately abort the request (eg: deny,
3460 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003461 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003462int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003463{
Willy Tarreaud787e662009-07-07 10:14:51 +02003464 struct http_txn *txn = &s->txn;
3465 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003466 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003467 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003468 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003469 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003470
Willy Tarreau655dce92009-11-08 13:10:58 +01003471 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003472 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003473 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003474 return 0;
3475 }
3476
Willy Tarreau3a816292009-07-07 10:55:49 +02003477 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003478 req->analyse_exp = TICK_ETERNITY;
3479
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003480 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 +02003481 now_ms, __FUNCTION__,
3482 s,
3483 req,
3484 req->rex, req->wex,
3485 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003486 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003487 req->analysers);
3488
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003489 /* first check whether we have some ACLs set to block this request */
3490 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003491 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003492
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003493 ret = acl_pass(ret);
3494 if (cond->pol == ACL_COND_UNLESS)
3495 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003496
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003497 if (ret) {
3498 txn->status = 403;
3499 /* let's log the request time */
3500 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003501 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003502 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003503 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003504 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003505 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003506
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003507 /* just in case we have some per-backend tracking */
3508 session_inc_be_http_req_ctr(s);
3509
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003510 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003511 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003512
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003513 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003514 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003515 if (stats_check_uri(s->rep->prod, txn, px)) {
3516 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003517 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3518 txn->status = 500;
3519 s->logs.tv_request = now;
3520 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003521
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003522 if (!(s->flags & SN_ERR_MASK))
3523 s->flags |= SN_ERR_RESOURCE;
3524 goto return_prx_cond;
3525 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003526 /* parse the whole stats request and extract the relevant information */
3527 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003528 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 +01003529 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003530 }
3531
Willy Tarreau3b44e722013-11-16 10:28:23 +01003532 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3533 * blocked by an http-request rule.
3534 */
3535 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003536 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003537 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003538 }
Willy Tarreau06619262006-12-17 08:37:22 +01003539
Willy Tarreau3b44e722013-11-16 10:28:23 +01003540 /* return a 403 if either rule has blocked */
3541 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003542 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003543 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003544 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003545 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003546 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003547 s->fe->fe_counters.denied_req++;
3548 if (s->fe != s->be)
3549 s->be->be_counters.denied_req++;
3550 if (s->listener->counters)
3551 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003552 goto return_prx_cond;
3553 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003554
3555 /* When a connection is tarpitted, we use the tarpit timeout,
3556 * which may be the same as the connect timeout if unspecified.
3557 * If unset, then set it to zero because we really want it to
3558 * eventually expire. We build the tarpit as an analyser.
3559 */
3560 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003561 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003562 /* wipe the request out so that we can drop the connection early
3563 * if the client closes first.
3564 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003565 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003566 req->analysers = 0; /* remove switching rules etc... */
3567 req->analysers |= AN_REQ_HTTP_TARPIT;
3568 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3569 if (!req->analyse_exp)
3570 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003571 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003572 s->fe->fe_counters.denied_req++;
3573 if (s->fe != s->be)
3574 s->be->be_counters.denied_req++;
3575 if (s->listener->counters)
3576 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003577 return 1;
3578 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003579 }
Willy Tarreau06619262006-12-17 08:37:22 +01003580
Willy Tarreau70dffda2014-01-30 03:07:23 +01003581 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003582 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003583 * Option httpclose by itself sets tunnel mode where headers are mangled.
3584 * However, if another mode is set, it will affect it (eg: server-close/
3585 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3586 * if FE and BE have the same settings (common). The method consists in
3587 * checking if options changed between the two calls (implying that either
3588 * one is non-null, or one of them is non-null and we are there for the first
3589 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003590 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003591
Willy Tarreau416ce612014-01-31 15:45:34 +01003592 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003593 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003594 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003595
Willy Tarreau70dffda2014-01-30 03:07:23 +01003596 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3597 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3598 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3599 tmp = TX_CON_WANT_TUN;
3600
3601 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3602 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3603 tmp = TX_CON_WANT_TUN;
3604 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003605
3606 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003607 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3608 /* option httpclose + server_close => forceclose */
3609 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3610 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3611 tmp = TX_CON_WANT_CLO;
3612 else
3613 tmp = TX_CON_WANT_SCL;
3614 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003615
3616 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3617 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003618 tmp = TX_CON_WANT_CLO;
3619
Willy Tarreau5b154472009-12-21 20:11:07 +01003620 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3621 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003622
Willy Tarreau416ce612014-01-31 15:45:34 +01003623 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3624 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003625 /* parse the Connection header and possibly clean it */
3626 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003627 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003628 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3629 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003630 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003631 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003632 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003633 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003634 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003635
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003636 /* check if client or config asks for explicit close in KAL/SCL */
3637 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3638 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3639 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003640 (!(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 +01003641 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003642 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003643 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3644 }
Willy Tarreau78599912009-10-17 20:12:21 +02003645
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003646 /* we can be blocked here because the request needs to be authenticated,
3647 * either to pass or to access stats.
3648 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003649 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003650 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003651
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003652 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003653 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003654
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003655 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003656 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003657 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003658 /* on 401 we still count one error, because normal browsing
3659 * won't significantly increase the counter but brute force
3660 * attempts will.
3661 */
3662 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003663 goto return_prx_cond;
3664 }
3665
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003666 /* add request headers from the rule sets in the same order */
3667 list_for_each_entry(wl, &px->req_add, list) {
3668 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003669 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003670 ret = acl_pass(ret);
3671 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3672 ret = !ret;
3673 if (!ret)
3674 continue;
3675 }
3676
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003677 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003678 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003679 }
3680
3681 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3682 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3683 goto return_bad_req;
3684 req->analyse_exp = TICK_ETERNITY;
3685 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003686 }
3687
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003688 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003689 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003690 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3691 s->fe->fe_counters.intercepted_req++;
3692
3693 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3694 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3695 if (!(s->flags & SN_FINST_MASK))
3696 s->flags |= SN_FINST_R;
3697
3698 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003699 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003700 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003701 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003702
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003703 /* check whether we have some ACLs set to redirect this request */
3704 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003705 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003706 int ret;
3707
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003708 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003709 ret = acl_pass(ret);
3710 if (rule->cond->pol == ACL_COND_UNLESS)
3711 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003712 if (!ret)
3713 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003714 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003715 if (!http_apply_redirect_rule(rule, s, txn))
3716 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003717
Willy Tarreau71241ab2012-12-27 11:30:54 +01003718 req->analyse_exp = TICK_ETERNITY;
3719 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003720 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003721
Willy Tarreau2be39392010-01-03 17:24:51 +01003722 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3723 * If this happens, then the data will not come immediately, so we must
3724 * send all what we have without waiting. Note that due to the small gain
3725 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003726 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003727 * itself once used.
3728 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003729 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003730
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003731 /* that's OK for us now, let's move on to next analysers */
3732 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003733
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003734 return_bad_req:
3735 /* We centralize bad requests processing here */
3736 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3737 /* we detected a parsing error. We want to archive this request
3738 * in the dedicated proxy area for later troubleshooting.
3739 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003740 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003741 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003742
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003743 txn->req.msg_state = HTTP_MSG_ERROR;
3744 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003745 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003746
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003747 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003748 if (s->listener->counters)
3749 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003750
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003751 return_prx_cond:
3752 if (!(s->flags & SN_ERR_MASK))
3753 s->flags |= SN_ERR_PRXCOND;
3754 if (!(s->flags & SN_FINST_MASK))
3755 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003756
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003757 req->analysers = 0;
3758 req->analyse_exp = TICK_ETERNITY;
3759 return 0;
3760}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003761
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003762/* This function performs all the processing enabled for the current request.
3763 * It returns 1 if the processing can continue on next analysers, or zero if it
3764 * needs more data, encounters an error, or wants to immediately abort the
3765 * request. It relies on buffers flags, and updates s->req->analysers.
3766 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003767int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003768{
3769 struct http_txn *txn = &s->txn;
3770 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003771 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003772
Willy Tarreau655dce92009-11-08 13:10:58 +01003773 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003774 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003775 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003776 return 0;
3777 }
3778
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003779 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 +02003780 now_ms, __FUNCTION__,
3781 s,
3782 req,
3783 req->rex, req->wex,
3784 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003785 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003786 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003787
William Lallemand82fe75c2012-10-23 10:25:10 +02003788 if (s->fe->comp || s->be->comp)
3789 select_compression_request_header(s, req->buf);
3790
Willy Tarreau59234e92008-11-30 23:51:27 +01003791 /*
3792 * Right now, we know that we have processed the entire headers
3793 * and that unwanted requests have been filtered out. We can do
3794 * whatever we want with the remaining request. Also, now we
3795 * may have separate values for ->fe, ->be.
3796 */
Willy Tarreau06619262006-12-17 08:37:22 +01003797
Willy Tarreau59234e92008-11-30 23:51:27 +01003798 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003799 * If HTTP PROXY is set we simply get remote server address parsing
3800 * incoming request. Note that this requires that a connection is
3801 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003802 */
3803 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003804 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003805 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003806
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003807 /* Note that for now we don't reuse existing proxy connections */
3808 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003809 txn->req.msg_state = HTTP_MSG_ERROR;
3810 txn->status = 500;
3811 req->analysers = 0;
3812 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3813
3814 if (!(s->flags & SN_ERR_MASK))
3815 s->flags |= SN_ERR_RESOURCE;
3816 if (!(s->flags & SN_FINST_MASK))
3817 s->flags |= SN_FINST_R;
3818
3819 return 0;
3820 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003821
3822 path = http_get_path(txn);
3823 url2sa(req->buf->p + msg->sl.rq.u,
3824 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3825 &conn->addr.to);
3826 /* if the path was found, we have to remove everything between
3827 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3828 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3829 * u_l characters by a single "/".
3830 */
3831 if (path) {
3832 char *cur_ptr = req->buf->p;
3833 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3834 int delta;
3835
3836 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3837 http_msg_move_end(&txn->req, delta);
3838 cur_end += delta;
3839 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3840 goto return_bad_req;
3841 }
3842 else {
3843 char *cur_ptr = req->buf->p;
3844 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3845 int delta;
3846
3847 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3848 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3849 http_msg_move_end(&txn->req, delta);
3850 cur_end += delta;
3851 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3852 goto return_bad_req;
3853 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003854 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003855
Willy Tarreau59234e92008-11-30 23:51:27 +01003856 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003857 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003858 * Note that doing so might move headers in the request, but
3859 * the fields will stay coherent and the URI will not move.
3860 * This should only be performed in the backend.
3861 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003862 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003863 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3864 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003865
Willy Tarreau59234e92008-11-30 23:51:27 +01003866 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003867 * 8: the appsession cookie was looked up very early in 1.2,
3868 * so let's do the same now.
3869 */
3870
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003871 /* It needs to look into the URI unless persistence must be ignored */
3872 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003873 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 +01003874 }
3875
William Lallemanda73203e2012-03-12 12:48:57 +01003876 /* add unique-id if "header-unique-id" is specified */
3877
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003878 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3879 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3880 goto return_bad_req;
3881 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003882 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003883 }
William Lallemanda73203e2012-03-12 12:48:57 +01003884
3885 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003886 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3887 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003888 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003889 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003890 goto return_bad_req;
3891 }
3892
Cyril Bontéb21570a2009-11-29 20:04:48 +01003893 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003894 * 9: add X-Forwarded-For if either the frontend or the backend
3895 * asks for it.
3896 */
3897 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003898 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003899 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003900 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3901 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003902 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003903 /* The header is set to be added only if none is present
3904 * and we found it, so don't do anything.
3905 */
3906 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003907 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003908 /* Add an X-Forwarded-For header unless the source IP is
3909 * in the 'except' network range.
3910 */
3911 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003912 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003913 != s->fe->except_net.s_addr) &&
3914 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003915 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003916 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003917 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003918 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003919 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003920
3921 /* Note: we rely on the backend to get the header name to be used for
3922 * x-forwarded-for, because the header is really meant for the backends.
3923 * However, if the backend did not specify any option, we have to rely
3924 * on the frontend's header name.
3925 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003926 if (s->be->fwdfor_hdr_len) {
3927 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003928 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003929 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003930 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003931 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003932 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003933 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003934
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003935 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003936 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003937 }
3938 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003939 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003940 /* FIXME: for the sake of completeness, we should also support
3941 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003942 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003943 int len;
3944 char pn[INET6_ADDRSTRLEN];
3945 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003946 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003947 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003948
Willy Tarreau59234e92008-11-30 23:51:27 +01003949 /* Note: we rely on the backend to get the header name to be used for
3950 * x-forwarded-for, because the header is really meant for the backends.
3951 * However, if the backend did not specify any option, we have to rely
3952 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003953 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003954 if (s->be->fwdfor_hdr_len) {
3955 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003956 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003957 } else {
3958 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003959 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003960 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003961 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003962
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003963 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003964 goto return_bad_req;
3965 }
3966 }
3967
3968 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003969 * 10: add X-Original-To if either the frontend or the backend
3970 * asks for it.
3971 */
3972 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3973
3974 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003975 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003976 /* Add an X-Original-To header unless the destination IP is
3977 * in the 'except' network range.
3978 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003979 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003980
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003981 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003982 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003983 (((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 +02003984 != s->fe->except_to.s_addr) &&
3985 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003986 (((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 +02003987 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003988 int len;
3989 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003990 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003991
3992 /* Note: we rely on the backend to get the header name to be used for
3993 * x-original-to, because the header is really meant for the backends.
3994 * However, if the backend did not specify any option, we have to rely
3995 * on the frontend's header name.
3996 */
3997 if (s->be->orgto_hdr_len) {
3998 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003999 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004000 } else {
4001 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004002 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004003 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004004 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004005
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004006 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004007 goto return_bad_req;
4008 }
4009 }
4010 }
4011
Willy Tarreau50fc7772012-11-11 22:19:57 +01004012 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4013 * If an "Upgrade" token is found, the header is left untouched in order not to have
4014 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4015 * "Upgrade" is present in the Connection header.
4016 */
4017 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4018 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004019 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4020 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004021 unsigned int want_flags = 0;
4022
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004023 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004024 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004025 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4026 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004027 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004028 want_flags |= TX_CON_CLO_SET;
4029 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004030 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004031 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4032 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004033 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004034 want_flags |= TX_CON_KAL_SET;
4035 }
4036
4037 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004038 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004039 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004040
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004041
Willy Tarreau522d6c02009-12-06 18:49:18 +01004042 /* If we have no server assigned yet and we're balancing on url_param
4043 * with a POST request, we may be interested in checking the body for
4044 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004045 */
4046 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4047 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004048 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004049 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004050 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004051 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004052 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004053
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004054 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004055 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004056#ifdef TCP_QUICKACK
4057 /* We expect some data from the client. Unless we know for sure
4058 * we already have a full request, we have to re-enable quick-ack
4059 * in case we previously disabled it, otherwise we might cause
4060 * the client to delay further data.
4061 */
4062 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004063 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004064 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004065 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004066 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004067#endif
4068 }
Willy Tarreau03945942009-12-22 16:50:27 +01004069
Willy Tarreau59234e92008-11-30 23:51:27 +01004070 /*************************************************************
4071 * OK, that's finished for the headers. We have done what we *
4072 * could. Let's switch to the DATA state. *
4073 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004074 req->analyse_exp = TICK_ETERNITY;
4075 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004076
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004077 /* if the server closes the connection, we want to immediately react
4078 * and close the socket to save packets and syscalls.
4079 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004080 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4081 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004082
Willy Tarreau59234e92008-11-30 23:51:27 +01004083 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004084 /* OK let's go on with the BODY now */
4085 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004086
Willy Tarreau59234e92008-11-30 23:51:27 +01004087 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004088 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004089 /* we detected a parsing error. We want to archive this request
4090 * in the dedicated proxy area for later troubleshooting.
4091 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004092 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004093 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004094
Willy Tarreau59234e92008-11-30 23:51:27 +01004095 txn->req.msg_state = HTTP_MSG_ERROR;
4096 txn->status = 400;
4097 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004098 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004099
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004100 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004101 if (s->listener->counters)
4102 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004103
Willy Tarreau59234e92008-11-30 23:51:27 +01004104 if (!(s->flags & SN_ERR_MASK))
4105 s->flags |= SN_ERR_PRXCOND;
4106 if (!(s->flags & SN_FINST_MASK))
4107 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004108 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004109}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004110
Willy Tarreau60b85b02008-11-30 23:28:40 +01004111/* This function is an analyser which processes the HTTP tarpit. It always
4112 * returns zero, at the beginning because it prevents any other processing
4113 * from occurring, and at the end because it terminates the request.
4114 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004115int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004116{
4117 struct http_txn *txn = &s->txn;
4118
4119 /* This connection is being tarpitted. The CLIENT side has
4120 * already set the connect expiration date to the right
4121 * timeout. We just have to check that the client is still
4122 * there and that the timeout has not expired.
4123 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004124 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004125 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004126 !tick_is_expired(req->analyse_exp, now_ms))
4127 return 0;
4128
4129 /* We will set the queue timer to the time spent, just for
4130 * logging purposes. We fake a 500 server error, so that the
4131 * attacker will not suspect his connection has been tarpitted.
4132 * It will not cause trouble to the logs because we can exclude
4133 * the tarpitted connections by filtering on the 'PT' status flags.
4134 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004135 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4136
4137 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004138 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004139 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004140
4141 req->analysers = 0;
4142 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004143
Willy Tarreau60b85b02008-11-30 23:28:40 +01004144 if (!(s->flags & SN_ERR_MASK))
4145 s->flags |= SN_ERR_PRXCOND;
4146 if (!(s->flags & SN_FINST_MASK))
4147 s->flags |= SN_FINST_T;
4148 return 0;
4149}
4150
Willy Tarreaud34af782008-11-30 23:36:37 +01004151/* This function is an analyser which processes the HTTP request body. It looks
4152 * for parameters to be used for the load balancing algorithm (url_param). It
4153 * must only be called after the standard HTTP request processing has occurred,
4154 * because it expects the request to be parsed. It returns zero if it needs to
4155 * read more data, or 1 once it has completed its analysis.
4156 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004157int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004158{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004159 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004160 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004161 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004162
4163 /* We have to parse the HTTP request body to find any required data.
4164 * "balance url_param check_post" should have been the only way to get
4165 * into this. We were brought here after HTTP header analysis, so all
4166 * related structures are ready.
4167 */
4168
Willy Tarreau522d6c02009-12-06 18:49:18 +01004169 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4170 goto missing_data;
4171
4172 if (msg->msg_state < HTTP_MSG_100_SENT) {
4173 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4174 * send an HTTP/1.1 100 Continue intermediate response.
4175 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004176 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004177 struct hdr_ctx ctx;
4178 ctx.idx = 0;
4179 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004180 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004181 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004182 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004183 }
4184 }
4185 msg->msg_state = HTTP_MSG_100_SENT;
4186 }
4187
4188 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004189 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004190 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004191 * is still null. We must save the body in msg->next because it
4192 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004193 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004194 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004195
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004196 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004197 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4198 else
4199 msg->msg_state = HTTP_MSG_DATA;
4200 }
4201
4202 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004203 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004204 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004205 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004206 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004207 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004208
Willy Tarreau115acb92009-12-26 13:56:06 +01004209 if (!ret)
4210 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004211 else if (ret < 0) {
4212 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004213 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004214 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004215 }
4216
Willy Tarreaud98cf932009-12-27 22:54:55 +01004217 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004218 * We have the first data byte is in msg->sov. We're waiting for at
4219 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004220 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004221
Willy Tarreau124d9912011-03-01 20:30:48 +01004222 if (msg->body_len < limit)
4223 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004224
Willy Tarreau9b28e032012-10-12 23:49:43 +02004225 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004226 goto http_end;
4227
4228 missing_data:
4229 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004230 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004231 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004232 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004233 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004234
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004235 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004236 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004237 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004238
4239 if (!(s->flags & SN_ERR_MASK))
4240 s->flags |= SN_ERR_CLITO;
4241 if (!(s->flags & SN_FINST_MASK))
4242 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004243 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004244 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004245
4246 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004247 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004248 /* Not enough data. We'll re-use the http-request
4249 * timeout here. Ideally, we should set the timeout
4250 * relative to the accept() date. We just set the
4251 * request timeout once at the beginning of the
4252 * request.
4253 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004254 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004255 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004256 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004257 return 0;
4258 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004259
4260 http_end:
4261 /* The situation will not evolve, so let's give up on the analysis. */
4262 s->logs.tv_request = now; /* update the request timer to reflect full request */
4263 req->analysers &= ~an_bit;
4264 req->analyse_exp = TICK_ETERNITY;
4265 return 1;
4266
4267 return_bad_req: /* let's centralize all bad requests */
4268 txn->req.msg_state = HTTP_MSG_ERROR;
4269 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004270 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004271
Willy Tarreau79ebac62010-06-07 13:47:49 +02004272 if (!(s->flags & SN_ERR_MASK))
4273 s->flags |= SN_ERR_PRXCOND;
4274 if (!(s->flags & SN_FINST_MASK))
4275 s->flags |= SN_FINST_R;
4276
Willy Tarreau522d6c02009-12-06 18:49:18 +01004277 return_err_msg:
4278 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004279 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004280 if (s->listener->counters)
4281 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004282 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004283}
4284
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004285/* send a server's name with an outgoing request over an established connection.
4286 * Note: this function is designed to be called once the request has been scheduled
4287 * for being forwarded. This is the reason why it rewinds the buffer before
4288 * proceeding.
4289 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004290int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004291
4292 struct hdr_ctx ctx;
4293
Mark Lamourinec2247f02012-01-04 13:02:01 -05004294 char *hdr_name = be->server_id_hdr_name;
4295 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004296 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004297 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004298 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004299
William Lallemandd9e90662012-01-30 17:27:17 +01004300 ctx.idx = 0;
4301
Willy Tarreau9b28e032012-10-12 23:49:43 +02004302 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004303 if (old_o) {
4304 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004305 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004306 }
4307
Willy Tarreau9b28e032012-10-12 23:49:43 +02004308 old_i = chn->buf->i;
4309 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 -05004310 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004311 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004312 }
4313
4314 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004315 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004316 memcpy(hdr_val, hdr_name, hdr_name_len);
4317 hdr_val += hdr_name_len;
4318 *hdr_val++ = ':';
4319 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004320 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4321 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004322
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004323 if (old_o) {
4324 /* If this was a forwarded request, we must readjust the amount of
4325 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004326 * variations. Note that if the request was already scheduled for
4327 * forwarding, it had its req->sol pointing to the body, which
4328 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004329 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004330 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004331 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004332 }
4333
Mark Lamourinec2247f02012-01-04 13:02:01 -05004334 return 0;
4335}
4336
Willy Tarreau610ecce2010-01-04 21:15:02 +01004337/* Terminate current transaction and prepare a new one. This is very tricky
4338 * right now but it works.
4339 */
4340void http_end_txn_clean_session(struct session *s)
4341{
Willy Tarreau068621e2013-12-23 15:11:25 +01004342 int prev_status = s->txn.status;
4343
Willy Tarreau610ecce2010-01-04 21:15:02 +01004344 /* FIXME: We need a more portable way of releasing a backend's and a
4345 * server's connections. We need a safer way to reinitialize buffer
4346 * flags. We also need a more accurate method for computing per-request
4347 * data.
4348 */
4349 http_silent_debug(__LINE__, s);
4350
Willy Tarreau4213a112013-12-15 10:25:42 +01004351 /* unless we're doing keep-alive, we want to quickly close the connection
4352 * to the server.
4353 */
4354 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4355 !si_conn_ready(s->req->cons)) {
4356 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4357 si_shutr(s->req->cons);
4358 si_shutw(s->req->cons);
4359 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360
4361 http_silent_debug(__LINE__, s);
4362
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004363 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004364 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004365 if (unlikely(s->srv_conn))
4366 sess_change_server(s, NULL);
4367 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004368
4369 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4370 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004371 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372
4373 if (s->txn.status) {
4374 int n;
4375
4376 n = s->txn.status / 100;
4377 if (n < 1 || n > 5)
4378 n = 0;
4379
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004380 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004381 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004382 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004383 s->fe->fe_counters.p.http.comp_rsp++;
4384 }
Willy Tarreau24657792010-02-26 10:30:28 +01004385 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004386 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004387 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004388 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004389 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004390 s->be->be_counters.p.http.comp_rsp++;
4391 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 }
4393
4394 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004395 s->logs.bytes_in -= s->req->buf->i;
4396 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397
4398 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004399 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004400 !(s->flags & SN_MONITOR) &&
4401 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4402 s->do_log(s);
4403 }
4404
4405 s->logs.accept_date = date; /* user-visible date for logging */
4406 s->logs.tv_accept = now; /* corrected date for internal use */
4407 tv_zero(&s->logs.tv_request);
4408 s->logs.t_queue = -1;
4409 s->logs.t_connect = -1;
4410 s->logs.t_data = -1;
4411 s->logs.t_close = 0;
4412 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4413 s->logs.srv_queue_size = 0; /* we will get this number soon */
4414
Willy Tarreau9b28e032012-10-12 23:49:43 +02004415 s->logs.bytes_in = s->req->total = s->req->buf->i;
4416 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417
4418 if (s->pend_pos)
4419 pendconn_free(s->pend_pos);
4420
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004421 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004422 if (s->flags & SN_CURR_SESS) {
4423 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004424 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004425 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004426 if (may_dequeue_tasks(objt_server(s->target), s->be))
4427 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428 }
4429
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004430 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431
Willy Tarreau4213a112013-12-15 10:25:42 +01004432 /* only release our endpoint if we don't intend to reuse the
4433 * connection.
4434 */
4435 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4436 !si_conn_ready(s->req->cons)) {
4437 si_release_endpoint(s->req->cons);
4438 }
4439
Willy Tarreau610ecce2010-01-04 21:15:02 +01004440 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004442 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004443 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004444 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004445 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4446 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 +02004447 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004449
4450 if (s->flags & SN_COMP_READY)
4451 s->comp_algo->end(&s->comp_ctx);
4452 s->comp_algo = NULL;
4453 s->flags &= ~SN_COMP_READY;
4454
Willy Tarreau610ecce2010-01-04 21:15:02 +01004455 s->txn.meth = 0;
4456 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004457 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004458
4459 if (prev_status == 401 || prev_status == 407) {
4460 /* In HTTP keep-alive mode, if we receive a 401, we still have
4461 * a chance of being able to send the visitor again to the same
4462 * server over the same connection. This is required by some
4463 * broken protocols such as NTLM, and anyway whenever there is
4464 * an opportunity for sending the challenge to the proper place,
4465 * it's better to do it (at least it helps with debugging).
4466 */
4467 s->txn.flags |= TX_PREFER_LAST;
4468 }
4469
Willy Tarreauee55dc02010-06-01 10:56:34 +02004470 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004471 s->req->cons->flags |= SI_FL_INDEP_STR;
4472
Willy Tarreau96e31212011-05-30 18:10:30 +02004473 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004474 s->req->flags |= CF_NEVER_WAIT;
4475 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004476 }
4477
Willy Tarreau610ecce2010-01-04 21:15:02 +01004478 /* if the request buffer is not empty, it means we're
4479 * about to process another request, so send pending
4480 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004481 * Just don't do this if the buffer is close to be full,
4482 * because the request will wait for it to flush a little
4483 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004484 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004485 if (s->req->buf->i) {
4486 if (s->rep->buf->o &&
4487 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4488 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004489 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004490 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004491
4492 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004493 channel_auto_read(s->req);
4494 channel_auto_close(s->req);
4495 channel_auto_read(s->rep);
4496 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004497
Willy Tarreau27375622013-12-17 00:00:28 +01004498 /* we're in keep-alive with an idle connection, monitor it */
4499 si_idle_conn(s->req->cons);
4500
Willy Tarreau342b11c2010-11-24 16:22:09 +01004501 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004502 s->rep->analysers = 0;
4503
4504 http_silent_debug(__LINE__, s);
4505}
4506
4507
4508/* This function updates the request state machine according to the response
4509 * state machine and buffer flags. It returns 1 if it changes anything (flag
4510 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4511 * it is only used to find when a request/response couple is complete. Both
4512 * this function and its equivalent should loop until both return zero. It
4513 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4514 */
4515int http_sync_req_state(struct session *s)
4516{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004517 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004518 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004519 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520 unsigned int old_state = txn->req.msg_state;
4521
4522 http_silent_debug(__LINE__, s);
4523 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4524 return 0;
4525
4526 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004527 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004528 * We can shut the read side unless we want to abort_on_close,
4529 * or we have a POST request. The issue with POST requests is
4530 * that some browsers still send a CRLF after the request, and
4531 * this CRLF must be read so that it does not remain in the kernel
4532 * buffers, otherwise a close could cause an RST on some systems
4533 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004534 * Note that if we're using keep-alive on the client side, we'd
4535 * rather poll now and keep the polling enabled for the whole
4536 * session's life than enabling/disabling it between each
4537 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004538 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004539 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4540 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4541 !(s->be->options & PR_O_ABRT_CLOSE) &&
4542 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004543 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544
Willy Tarreau40f151a2012-12-20 12:10:09 +01004545 /* if the server closes the connection, we want to immediately react
4546 * and close the socket to save packets and syscalls.
4547 */
4548 chn->cons->flags |= SI_FL_NOHALF;
4549
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4551 goto wait_other_side;
4552
4553 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4554 /* The server has not finished to respond, so we
4555 * don't want to move in order not to upset it.
4556 */
4557 goto wait_other_side;
4558 }
4559
4560 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4561 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004562 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004564 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 goto wait_other_side;
4566 }
4567
4568 /* When we get here, it means that both the request and the
4569 * response have finished receiving. Depending on the connection
4570 * mode, we'll have to wait for the last bytes to leave in either
4571 * direction, and sometimes for a close to be effective.
4572 */
4573
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004574 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4575 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004576 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4577 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004578 }
4579 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4580 /* Option forceclose is set, or either side wants to close,
4581 * let's enforce it now that we're not expecting any new
4582 * data to come. The caller knows the session is complete
4583 * once both states are CLOSED.
4584 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004585 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4586 channel_shutr_now(chn);
4587 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004589 }
4590 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004591 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4592 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004593 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004594 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4595 channel_auto_read(chn);
4596 txn->req.msg_state = HTTP_MSG_TUNNEL;
4597 chn->flags |= CF_NEVER_WAIT;
4598 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004599 }
4600
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004603 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004604
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004605 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004606 txn->req.msg_state = HTTP_MSG_CLOSING;
4607 goto http_msg_closing;
4608 }
4609 else {
4610 txn->req.msg_state = HTTP_MSG_CLOSED;
4611 goto http_msg_closed;
4612 }
4613 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004614 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615 }
4616
4617 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4618 http_msg_closing:
4619 /* nothing else to forward, just waiting for the output buffer
4620 * to be empty and for the shutw_now to take effect.
4621 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004622 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004623 txn->req.msg_state = HTTP_MSG_CLOSED;
4624 goto http_msg_closed;
4625 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004626 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627 txn->req.msg_state = HTTP_MSG_ERROR;
4628 goto wait_other_side;
4629 }
4630 }
4631
4632 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4633 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004634 /* see above in MSG_DONE why we only do this in these states */
4635 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4636 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4637 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004638 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 goto wait_other_side;
4640 }
4641
4642 wait_other_side:
4643 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004644 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645}
4646
4647
4648/* This function updates the response state machine according to the request
4649 * state machine and buffer flags. It returns 1 if it changes anything (flag
4650 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4651 * it is only used to find when a request/response couple is complete. Both
4652 * this function and its equivalent should loop until both return zero. It
4653 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4654 */
4655int http_sync_res_state(struct session *s)
4656{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004657 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004659 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 unsigned int old_state = txn->rsp.msg_state;
4661
4662 http_silent_debug(__LINE__, s);
4663 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4664 return 0;
4665
4666 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4667 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004668 * still monitor the server connection for a possible close
4669 * while the request is being uploaded, so we don't disable
4670 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004672 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004673
4674 if (txn->req.msg_state == HTTP_MSG_ERROR)
4675 goto wait_other_side;
4676
4677 if (txn->req.msg_state < HTTP_MSG_DONE) {
4678 /* The client seems to still be sending data, probably
4679 * because we got an error response during an upload.
4680 * We have the choice of either breaking the connection
4681 * or letting it pass through. Let's do the later.
4682 */
4683 goto wait_other_side;
4684 }
4685
4686 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4687 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004688 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004689 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004690 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004691 goto wait_other_side;
4692 }
4693
4694 /* When we get here, it means that both the request and the
4695 * response have finished receiving. Depending on the connection
4696 * mode, we'll have to wait for the last bytes to leave in either
4697 * direction, and sometimes for a close to be effective.
4698 */
4699
4700 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4701 /* Server-close mode : shut read and wait for the request
4702 * side to close its output buffer. The caller will detect
4703 * when we're in DONE and the other is in CLOSED and will
4704 * catch that for the final cleanup.
4705 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004706 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4707 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004709 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4710 /* Option forceclose is set, or either side wants to close,
4711 * let's enforce it now that we're not expecting any new
4712 * data to come. The caller knows the session is complete
4713 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004715 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4716 channel_shutr_now(chn);
4717 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004718 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004719 }
4720 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004721 /* The last possible modes are keep-alive and tunnel. Tunnel will
4722 * need to forward remaining data. Keep-alive will need to monitor
4723 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004724 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004725 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004726 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004727 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4728 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 }
4730
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004731 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004733 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004734 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4735 goto http_msg_closing;
4736 }
4737 else {
4738 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4739 goto http_msg_closed;
4740 }
4741 }
4742 goto wait_other_side;
4743 }
4744
4745 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4746 http_msg_closing:
4747 /* nothing else to forward, just waiting for the output buffer
4748 * to be empty and for the shutw_now to take effect.
4749 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004750 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4752 goto http_msg_closed;
4753 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004754 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004755 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004756 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004757 if (objt_server(s->target))
4758 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004759 goto wait_other_side;
4760 }
4761 }
4762
4763 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4764 http_msg_closed:
4765 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004766 bi_erase(chn);
4767 channel_auto_close(chn);
4768 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 goto wait_other_side;
4770 }
4771
4772 wait_other_side:
4773 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004774 /* We force the response to leave immediately if we're waiting for the
4775 * other side, since there is no pending shutdown to push it out.
4776 */
4777 if (!channel_is_empty(chn))
4778 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004779 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004780}
4781
4782
4783/* Resync the request and response state machines. Return 1 if either state
4784 * changes.
4785 */
4786int http_resync_states(struct session *s)
4787{
4788 struct http_txn *txn = &s->txn;
4789 int old_req_state = txn->req.msg_state;
4790 int old_res_state = txn->rsp.msg_state;
4791
4792 http_silent_debug(__LINE__, s);
4793 http_sync_req_state(s);
4794 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004795 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004796 if (!http_sync_res_state(s))
4797 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004798 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004799 if (!http_sync_req_state(s))
4800 break;
4801 }
4802 http_silent_debug(__LINE__, s);
4803 /* OK, both state machines agree on a compatible state.
4804 * There are a few cases we're interested in :
4805 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4806 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4807 * directions, so let's simply disable both analysers.
4808 * - HTTP_MSG_CLOSED on the response only means we must abort the
4809 * request.
4810 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4811 * with server-close mode means we've completed one request and we
4812 * must re-initialize the server connection.
4813 */
4814
4815 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4816 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4817 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4818 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4819 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004820 channel_auto_close(s->req);
4821 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004822 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004823 channel_auto_close(s->rep);
4824 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004826 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4827 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004828 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004829 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004830 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004831 channel_auto_close(s->rep);
4832 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004833 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004834 channel_abort(s->req);
4835 channel_auto_close(s->req);
4836 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004837 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004839 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4840 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004841 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004842 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4843 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4844 /* server-close/keep-alive: terminate this transaction,
4845 * possibly killing the server connection and reinitialize
4846 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004847 */
4848 http_end_txn_clean_session(s);
4849 }
4850
4851 http_silent_debug(__LINE__, s);
4852 return txn->req.msg_state != old_req_state ||
4853 txn->rsp.msg_state != old_res_state;
4854}
4855
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856/* This function is an analyser which forwards request body (including chunk
4857 * sizes if any). It is called as soon as we must forward, even if we forward
4858 * zero byte. The only situation where it must not be called is when we're in
4859 * tunnel mode and we want to forward till the close. It's used both to forward
4860 * remaining data and to resync after end of body. It expects the msg_state to
4861 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4862 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004863 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004864 * bytes of pending data + the headers if not already done (between sol and sov).
4865 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004867int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004868{
4869 struct http_txn *txn = &s->txn;
4870 struct http_msg *msg = &s->txn.req;
4871
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004872 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4873 return 0;
4874
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004875 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004876 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004877 /* Output closed while we were sending data. We must abort and
4878 * wake the other side up.
4879 */
4880 msg->msg_state = HTTP_MSG_ERROR;
4881 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004882 return 1;
4883 }
4884
Willy Tarreau4fe41902010-06-07 22:27:41 +02004885 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004886 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004887
4888 /* Note that we don't have to send 100-continue back because we don't
4889 * need the data to complete our job, and it's up to the server to
4890 * decide whether to return 100, 417 or anything else in return of
4891 * an "Expect: 100-continue" header.
4892 */
4893
4894 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004895 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004896 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004897 * is still null. We must save the body in msg->next because it
4898 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004899 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004900 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004901
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004902 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004903 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004904 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004905 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004906 }
4907
Willy Tarreaud98cf932009-12-27 22:54:55 +01004908 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004909 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004910
Willy Tarreau610ecce2010-01-04 21:15:02 +01004911 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004912 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004913 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004914 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004915 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004916 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004917 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004918 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004919 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004920
Willy Tarreaucaabe412010-01-03 23:08:28 +01004921 if (msg->msg_state == HTTP_MSG_DATA) {
4922 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004923 if (req->to_forward) {
4924 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004925 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01004926 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01004927
4928 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004929 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004930 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004932 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004933 }
4934 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004935 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004936 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004937 * TRAILERS state.
4938 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004939 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004940
Willy Tarreau54d23df2012-10-25 19:04:45 +02004941 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004942 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004943 else if (ret < 0) {
4944 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004945 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004946 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004947 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004948 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004949 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004950 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004951 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004952 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004953 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004954
4955 if (ret == 0)
4956 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004957 else if (ret < 0) {
4958 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004959 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004960 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004961 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004962 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004963 /* we're in MSG_CHUNK_SIZE now */
4964 }
4965 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004966 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004967
4968 if (ret == 0)
4969 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004970 else if (ret < 0) {
4971 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004972 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004973 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004974 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004975 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004976 /* we're in HTTP_MSG_DONE now */
4977 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004978 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004979 int old_state = msg->msg_state;
4980
Willy Tarreau610ecce2010-01-04 21:15:02 +01004981 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004982 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004983 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4984 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004985 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986 if (http_resync_states(s)) {
4987 /* some state changes occurred, maybe the analyser
4988 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004989 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004990 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004991 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004992 /* request errors are most likely due to
4993 * the server aborting the transfer.
4994 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004995 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004996 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004997 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004998 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005000 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005001 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005002 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005003
5004 /* If "option abortonclose" is set on the backend, we
5005 * want to monitor the client's connection and forward
5006 * any shutdown notification to the server, which will
5007 * decide whether to close or to go on processing the
5008 * request.
5009 */
5010 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005011 channel_auto_read(req);
5012 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005013 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005014 else if (s->txn.meth == HTTP_METH_POST) {
5015 /* POST requests may require to read extra CRLF
5016 * sent by broken browsers and which could cause
5017 * an RST to be sent upon close on some systems
5018 * (eg: Linux).
5019 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005020 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005021 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005022
Willy Tarreau610ecce2010-01-04 21:15:02 +01005023 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005024 }
5025 }
5026
Willy Tarreaud98cf932009-12-27 22:54:55 +01005027 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005028 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005029 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005030 if (!(s->flags & SN_ERR_MASK))
5031 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005032 if (!(s->flags & SN_FINST_MASK)) {
5033 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5034 s->flags |= SN_FINST_H;
5035 else
5036 s->flags |= SN_FINST_D;
5037 }
5038
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005039 s->fe->fe_counters.cli_aborts++;
5040 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005041 if (objt_server(s->target))
5042 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005043
5044 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005045 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005046
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005047 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005048 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005049 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005050
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005051 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005052 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005053 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005054 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005055 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005056
Willy Tarreau5c620922011-05-11 19:56:11 +02005057 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005058 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005059 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005060 * modes are already handled by the stream sock layer. We must not do
5061 * this in content-length mode because it could present the MSG_MORE
5062 * flag with the last block of forwarded data, which would cause an
5063 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005064 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005065 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005066 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005067
Willy Tarreau610ecce2010-01-04 21:15:02 +01005068 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005069 return 0;
5070
Willy Tarreaud98cf932009-12-27 22:54:55 +01005071 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005072 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005073 if (s->listener->counters)
5074 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005075 return_bad_req_stats_ok:
5076 txn->req.msg_state = HTTP_MSG_ERROR;
5077 if (txn->status) {
5078 /* Note: we don't send any error if some data were already sent */
5079 stream_int_retnclose(req->prod, NULL);
5080 } else {
5081 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005082 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005083 }
5084 req->analysers = 0;
5085 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005086
5087 if (!(s->flags & SN_ERR_MASK))
5088 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005089 if (!(s->flags & SN_FINST_MASK)) {
5090 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5091 s->flags |= SN_FINST_H;
5092 else
5093 s->flags |= SN_FINST_D;
5094 }
5095 return 0;
5096
5097 aborted_xfer:
5098 txn->req.msg_state = HTTP_MSG_ERROR;
5099 if (txn->status) {
5100 /* Note: we don't send any error if some data were already sent */
5101 stream_int_retnclose(req->prod, NULL);
5102 } else {
5103 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005104 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005105 }
5106 req->analysers = 0;
5107 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5108
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005109 s->fe->fe_counters.srv_aborts++;
5110 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005111 if (objt_server(s->target))
5112 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005113
5114 if (!(s->flags & SN_ERR_MASK))
5115 s->flags |= SN_ERR_SRVCL;
5116 if (!(s->flags & SN_FINST_MASK)) {
5117 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5118 s->flags |= SN_FINST_H;
5119 else
5120 s->flags |= SN_FINST_D;
5121 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005122 return 0;
5123}
5124
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005125/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5126 * processing can continue on next analysers, or zero if it either needs more
5127 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5128 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5129 * when it has nothing left to do, and may remove any analyser when it wants to
5130 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005131 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005132int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005133{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 struct http_txn *txn = &s->txn;
5135 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005136 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005137 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005139 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005140
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005141 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 +02005142 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005144 rep,
5145 rep->rex, rep->wex,
5146 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005147 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005148 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005149
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005150 /*
5151 * Now parse the partial (or complete) lines.
5152 * We will check the response syntax, and also join multi-line
5153 * headers. An index of all the lines will be elaborated while
5154 * parsing.
5155 *
5156 * For the parsing, we use a 28 states FSM.
5157 *
5158 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005159 * rep->buf->p = beginning of response
5160 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5161 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005162 * msg->eol = end of current header or line (LF or CRLF)
5163 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005164 */
5165
Willy Tarreau83e3af02009-12-28 17:39:57 +01005166 /* There's a protected area at the end of the buffer for rewriting
5167 * purposes. We don't want to start to parse the request if the
5168 * protected area is affected, because we may have to move processed
5169 * data later, which is much more complicated.
5170 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005171 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005172 if (unlikely(!channel_reserved(rep))) {
5173 /* some data has still not left the buffer, wake us once that's done */
5174 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5175 goto abort_response;
5176 channel_dont_close(rep);
5177 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005178 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005179 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005180 }
5181
Willy Tarreau379357a2013-06-08 12:55:46 +02005182 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5183 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5184 buffer_slow_realign(rep->buf);
5185
Willy Tarreau9b28e032012-10-12 23:49:43 +02005186 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005187 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005188 }
5189
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005190 /* 1: we might have to print this header in debug mode */
5191 if (unlikely((global.mode & MODE_DEBUG) &&
5192 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005193 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005194 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195
Willy Tarreau9b28e032012-10-12 23:49:43 +02005196 sol = rep->buf->p;
5197 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005198 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005199
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005200 sol += hdr_idx_first_pos(&txn->hdr_idx);
5201 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005202
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005203 while (cur_idx) {
5204 eol = sol + txn->hdr_idx.v[cur_idx].len;
5205 debug_hdr("srvhdr", s, sol, eol);
5206 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5207 cur_idx = txn->hdr_idx.v[cur_idx].next;
5208 }
5209 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005210
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005211 /*
5212 * Now we quickly check if we have found a full valid response.
5213 * If not so, we check the FD and buffer states before leaving.
5214 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005215 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005216 * responses are checked first.
5217 *
5218 * Depending on whether the client is still there or not, we
5219 * may send an error response back or not. Note that normally
5220 * we should only check for HTTP status there, and check I/O
5221 * errors somewhere else.
5222 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005223
Willy Tarreau655dce92009-11-08 13:10:58 +01005224 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225 /* Invalid response */
5226 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5227 /* we detected a parsing error. We want to archive this response
5228 * in the dedicated proxy area for later troubleshooting.
5229 */
5230 hdr_response_bad:
5231 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005232 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005234 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005235 if (objt_server(s->target)) {
5236 objt_server(s->target)->counters.failed_resp++;
5237 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005238 }
Willy Tarreau64648412010-03-05 10:41:54 +01005239 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005240 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005241 rep->analysers = 0;
5242 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005243 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005244 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005245 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005246
5247 if (!(s->flags & SN_ERR_MASK))
5248 s->flags |= SN_ERR_PRXCOND;
5249 if (!(s->flags & SN_FINST_MASK))
5250 s->flags |= SN_FINST_H;
5251
5252 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005253 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005254
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005256 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005257 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005258 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005259 goto hdr_response_bad;
5260 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005261
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005262 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005263 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005265 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005266 else if (txn->flags & TX_NOT_FIRST)
5267 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005268
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005269 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005270 if (objt_server(s->target)) {
5271 objt_server(s->target)->counters.failed_resp++;
5272 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005273 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005274
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005275 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005276 rep->analysers = 0;
5277 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005278 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005279 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005280 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005281
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005282 if (!(s->flags & SN_ERR_MASK))
5283 s->flags |= SN_ERR_SRVCL;
5284 if (!(s->flags & SN_FINST_MASK))
5285 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005286 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005287 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005288
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005289 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005290 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005291 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005292 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005293 else if (txn->flags & TX_NOT_FIRST)
5294 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005295
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005296 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005297 if (objt_server(s->target)) {
5298 objt_server(s->target)->counters.failed_resp++;
5299 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005300 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005301
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005302 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005303 rep->analysers = 0;
5304 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005305 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005306 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005307 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005308
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005309 if (!(s->flags & SN_ERR_MASK))
5310 s->flags |= SN_ERR_SRVTO;
5311 if (!(s->flags & SN_FINST_MASK))
5312 s->flags |= SN_FINST_H;
5313 return 0;
5314 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005315
Willy Tarreauf003d372012-11-26 13:35:37 +01005316 /* client abort with an abortonclose */
5317 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5318 s->fe->fe_counters.cli_aborts++;
5319 s->be->be_counters.cli_aborts++;
5320 if (objt_server(s->target))
5321 objt_server(s->target)->counters.cli_aborts++;
5322
5323 rep->analysers = 0;
5324 channel_auto_close(rep);
5325
5326 txn->status = 400;
5327 bi_erase(rep);
5328 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5329
5330 if (!(s->flags & SN_ERR_MASK))
5331 s->flags |= SN_ERR_CLICL;
5332 if (!(s->flags & SN_FINST_MASK))
5333 s->flags |= SN_FINST_H;
5334
5335 /* process_session() will take care of the error */
5336 return 0;
5337 }
5338
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005339 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005340 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005341 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005342 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005343 else if (txn->flags & TX_NOT_FIRST)
5344 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005345
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005346 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005347 if (objt_server(s->target)) {
5348 objt_server(s->target)->counters.failed_resp++;
5349 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005350 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005351
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005352 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005353 rep->analysers = 0;
5354 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005355 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005356 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005357 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005358
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005359 if (!(s->flags & SN_ERR_MASK))
5360 s->flags |= SN_ERR_SRVCL;
5361 if (!(s->flags & SN_FINST_MASK))
5362 s->flags |= SN_FINST_H;
5363 return 0;
5364 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005365
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005366 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005367 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005368 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005369 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005370 else if (txn->flags & TX_NOT_FIRST)
5371 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005372
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005373 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005375 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005376
5377 if (!(s->flags & SN_ERR_MASK))
5378 s->flags |= SN_ERR_CLICL;
5379 if (!(s->flags & SN_FINST_MASK))
5380 s->flags |= SN_FINST_H;
5381
5382 /* process_session() will take care of the error */
5383 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005384 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005385
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005386 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005387 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005388 return 0;
5389 }
5390
5391 /* More interesting part now : we know that we have a complete
5392 * response which at least looks like HTTP. We have an indicator
5393 * of each header's length, so we can parse them quickly.
5394 */
5395
5396 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005397 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005399 /*
5400 * 1: get the status code
5401 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005402 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005403 if (n < 1 || n > 5)
5404 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005405 /* when the client triggers a 4xx from the server, it's most often due
5406 * to a missing object or permission. These events should be tracked
5407 * because if they happen often, it may indicate a brute force or a
5408 * vulnerability scan.
5409 */
5410 if (n == 4)
5411 session_inc_http_err_ctr(s);
5412
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005413 if (objt_server(s->target))
5414 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415
Willy Tarreau5b154472009-12-21 20:11:07 +01005416 /* check if the response is HTTP/1.1 or above */
5417 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005418 ((rep->buf->p[5] > '1') ||
5419 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005420 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005421
5422 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005423 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005424
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005425 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005426 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005427
Willy Tarreau9b28e032012-10-12 23:49:43 +02005428 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429
Willy Tarreau39650402010-03-15 19:44:39 +01005430 /* Adjust server's health based on status code. Note: status codes 501
5431 * and 505 are triggered on demand by client request, so we must not
5432 * count them as server failures.
5433 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005434 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005435 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005436 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005437 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005438 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005439 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005440
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005441 /*
5442 * 2: check for cacheability.
5443 */
5444
5445 switch (txn->status) {
5446 case 200:
5447 case 203:
5448 case 206:
5449 case 300:
5450 case 301:
5451 case 410:
5452 /* RFC2616 @13.4:
5453 * "A response received with a status code of
5454 * 200, 203, 206, 300, 301 or 410 MAY be stored
5455 * by a cache (...) unless a cache-control
5456 * directive prohibits caching."
5457 *
5458 * RFC2616 @9.5: POST method :
5459 * "Responses to this method are not cacheable,
5460 * unless the response includes appropriate
5461 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005462 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005464 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005465 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5466 break;
5467 default:
5468 break;
5469 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005470
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005471 /*
5472 * 3: we may need to capture headers
5473 */
5474 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005475 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005476 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005477 txn->rsp.cap, s->fe->rsp_cap);
5478
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005479 /* 4: determine the transfer-length.
5480 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5481 * the presence of a message-body in a RESPONSE and its transfer length
5482 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005483 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005484 * All responses to the HEAD request method MUST NOT include a
5485 * message-body, even though the presence of entity-header fields
5486 * might lead one to believe they do. All 1xx (informational), 204
5487 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5488 * message-body. All other responses do include a message-body,
5489 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005490 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005491 * 1. Any response which "MUST NOT" include a message-body (such as the
5492 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5493 * always terminated by the first empty line after the header fields,
5494 * regardless of the entity-header fields present in the message.
5495 *
5496 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5497 * the "chunked" transfer-coding (Section 6.2) is used, the
5498 * transfer-length is defined by the use of this transfer-coding.
5499 * If a Transfer-Encoding header field is present and the "chunked"
5500 * transfer-coding is not present, the transfer-length is defined by
5501 * the sender closing the connection.
5502 *
5503 * 3. If a Content-Length header field is present, its decimal value in
5504 * OCTETs represents both the entity-length and the transfer-length.
5505 * If a message is received with both a Transfer-Encoding header
5506 * field and a Content-Length header field, the latter MUST be ignored.
5507 *
5508 * 4. If the message uses the media type "multipart/byteranges", and
5509 * the transfer-length is not otherwise specified, then this self-
5510 * delimiting media type defines the transfer-length. This media
5511 * type MUST NOT be used unless the sender knows that the recipient
5512 * can parse it; the presence in a request of a Range header with
5513 * multiple byte-range specifiers from a 1.1 client implies that the
5514 * client can parse multipart/byteranges responses.
5515 *
5516 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005517 */
5518
5519 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005520 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005521 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005522 * FIXME: should we parse anyway and return an error on chunked encoding ?
5523 */
5524 if (txn->meth == HTTP_METH_HEAD ||
5525 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005526 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005527 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005528 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005529 goto skip_content_length;
5530 }
5531
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005532 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005533 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005534 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005535 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005536 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005537 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5538 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005539 /* bad transfer-encoding (chunked followed by something else) */
5540 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005541 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005542 break;
5543 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005544 }
5545
5546 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5547 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005548 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005549 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005550 signed long long cl;
5551
Willy Tarreauad14f752011-09-02 20:33:27 +02005552 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005553 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005554 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005555 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005556
Willy Tarreauad14f752011-09-02 20:33:27 +02005557 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005558 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005559 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005560 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005561
Willy Tarreauad14f752011-09-02 20:33:27 +02005562 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005563 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005564 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005565 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005566
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005567 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005568 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005569 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005570 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005571
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005572 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005573 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574 }
5575
William Lallemand82fe75c2012-10-23 10:25:10 +02005576 if (s->fe->comp || s->be->comp)
5577 select_compression_response_header(s, rep->buf);
5578
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005579 /* FIXME: we should also implement the multipart/byterange method.
5580 * For now on, we resort to close mode in this case (unknown length).
5581 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005582skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005583
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005584 /* end of job, return OK */
5585 rep->analysers &= ~an_bit;
5586 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005587 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005588 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005589
5590 abort_keep_alive:
5591 /* A keep-alive request to the server failed on a network error.
5592 * The client is required to retry. We need to close without returning
5593 * any other information so that the client retries.
5594 */
5595 txn->status = 0;
5596 rep->analysers = 0;
5597 s->req->analysers = 0;
5598 channel_auto_close(rep);
5599 s->logs.logwait = 0;
5600 s->logs.level = 0;
5601 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5602 bi_erase(rep);
5603 stream_int_retnclose(rep->cons, NULL);
5604 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005605}
5606
5607/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005608 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5609 * and updates t->rep->analysers. It might make sense to explode it into several
5610 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005611 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005612int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005613{
5614 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005615 struct http_msg *msg = &txn->rsp;
5616 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005617 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005618 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005619
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005620 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 +02005621 now_ms, __FUNCTION__,
5622 t,
5623 rep,
5624 rep->rex, rep->wex,
5625 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005626 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005627 rep->analysers);
5628
Willy Tarreau655dce92009-11-08 13:10:58 +01005629 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005630 return 0;
5631
5632 rep->analysers &= ~an_bit;
5633 rep->analyse_exp = TICK_ETERNITY;
5634
Willy Tarreau5b154472009-12-21 20:11:07 +01005635 /* Now we have to check if we need to modify the Connection header.
5636 * This is more difficult on the response than it is on the request,
5637 * because we can have two different HTTP versions and we don't know
5638 * how the client will interprete a response. For instance, let's say
5639 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5640 * HTTP/1.1 response without any header. Maybe it will bound itself to
5641 * HTTP/1.0 because it only knows about it, and will consider the lack
5642 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5643 * the lack of header as a keep-alive. Thus we will use two flags
5644 * indicating how a request MAY be understood by the client. In case
5645 * of multiple possibilities, we'll fix the header to be explicit. If
5646 * ambiguous cases such as both close and keepalive are seen, then we
5647 * will fall back to explicit close. Note that we won't take risks with
5648 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005649 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005650 */
5651
Willy Tarreaudc008c52010-02-01 16:20:08 +01005652 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5653 txn->status == 101)) {
5654 /* Either we've established an explicit tunnel, or we're
5655 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005656 * to understand the next protocols. We have to switch to tunnel
5657 * mode, so that we transfer the request and responses then let
5658 * this protocol pass unmodified. When we later implement specific
5659 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005660 * header which contains information about that protocol for
5661 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005662 */
5663 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5664 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005665 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5666 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005667 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5668 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005669 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005670
Willy Tarreau70dffda2014-01-30 03:07:23 +01005671 /* this situation happens when combining pretend-keepalive with httpclose. */
5672 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5673 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5674 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5675 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5676
Willy Tarreau60466522010-01-18 19:08:45 +01005677 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005678 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005679 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5680 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005681
Willy Tarreau60466522010-01-18 19:08:45 +01005682 /* now adjust header transformations depending on current state */
5683 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5684 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5685 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005686 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005687 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005688 }
Willy Tarreau60466522010-01-18 19:08:45 +01005689 else { /* SCL / KAL */
5690 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005691 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005692 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005693 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005694
Willy Tarreau60466522010-01-18 19:08:45 +01005695 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005696 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005697
Willy Tarreau60466522010-01-18 19:08:45 +01005698 /* Some keep-alive responses are converted to Server-close if
5699 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005700 */
Willy Tarreau60466522010-01-18 19:08:45 +01005701 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5702 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005703 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005704 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005705 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005706 }
5707
Willy Tarreau7959a552013-09-23 16:44:27 +02005708 /* we want to have the response time before we start processing it */
5709 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5710
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005711 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005712 /*
5713 * 3: we will have to evaluate the filters.
5714 * As opposed to version 1.2, now they will be evaluated in the
5715 * filters order and not in the header order. This means that
5716 * each filter has to be validated among all headers.
5717 *
5718 * Filters are tried with ->be first, then with ->fe if it is
5719 * different from ->be.
5720 */
5721
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005722 cur_proxy = t->be;
5723 while (1) {
5724 struct proxy *rule_set = cur_proxy;
5725
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005726 /* evaluate http-response rules */
5727 if (!http_res_last_rule)
5728 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5729
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005730 /* try headers filters */
5731 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005732 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005733 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005734 if (objt_server(t->target)) {
5735 objt_server(t->target)->counters.failed_resp++;
5736 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005737 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005738 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005739 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005740 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005741 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005742 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005743 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005744 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005745 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005746 if (!(t->flags & SN_ERR_MASK))
5747 t->flags |= SN_ERR_PRXCOND;
5748 if (!(t->flags & SN_FINST_MASK))
5749 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005750 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005751 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005752 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005753
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005754 /* has the response been denied ? */
5755 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005756 if (objt_server(t->target))
5757 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005758
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005759 t->be->be_counters.denied_resp++;
5760 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005761 if (t->listener->counters)
5762 t->listener->counters->denied_resp++;
5763
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005764 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005765 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005767 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005768 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005769 if (txn->status < 200)
5770 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005771 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005772 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005773 ret = acl_pass(ret);
5774 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5775 ret = !ret;
5776 if (!ret)
5777 continue;
5778 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005779 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005780 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005781 }
5782
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005783 /* check whether we're already working on the frontend */
5784 if (cur_proxy == t->fe)
5785 break;
5786 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005787 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005788
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005789 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005790 * We may be facing a 100-continue response, in which case this
5791 * is not the right response, and we're waiting for the next one.
5792 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005793 * next one.
5794 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005795 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005796 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005797 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005798 msg->msg_state = HTTP_MSG_RPBEFORE;
5799 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005800 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005801 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5802 return 1;
5803 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005804 else if (unlikely(txn->status < 200))
5805 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005806
5807 /* we don't have any 1xx status code now */
5808
5809 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005810 * 4: check for server cookie.
5811 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005812 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5813 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005814 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005815
Willy Tarreaubaaee002006-06-26 02:48:02 +02005816
Willy Tarreaua15645d2007-03-18 16:22:39 +01005817 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005818 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005819 */
Willy Tarreau67402132012-05-31 20:40:20 +02005820 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005821 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005822
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005823 /*
5824 * 6: add server cookie in the response if needed
5825 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005826 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005827 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005828 (!(t->flags & SN_DIRECT) ||
5829 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5830 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5831 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5832 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005833 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005834 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005835 /* the server is known, it's not the one the client requested, or the
5836 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005837 * insert a set-cookie here, except if we want to insert only on POST
5838 * requests and this one isn't. Note that servers which don't have cookies
5839 * (eg: some backup servers) will return a full cookie removal request.
5840 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005841 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005842 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005843 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5844 t->be->cookie_name);
5845 }
5846 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005847 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005848
5849 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5850 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005851 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5852 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5853 trash.len += 5;
5854
Willy Tarreauef4f3912010-10-07 21:00:29 +02005855 if (t->be->cookie_maxlife) {
5856 /* emit first_date, which is either the original one or
5857 * the current date.
5858 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005859 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005860 s30tob64(txn->cookie_first_date ?
5861 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005862 (date.tv_sec+3) >> 2, trash.str + trash.len);
5863 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005864 }
5865 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005866 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005867 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005868
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005870 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005871
Willy Tarreau4992dd22012-05-31 21:02:17 +02005872 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005873 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005874
5875 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005876 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005877
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005878 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005879 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005880
Willy Tarreauf1348312010-10-07 15:54:11 +02005881 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005882 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005883 /* the server did not change, only the date was updated */
5884 txn->flags |= TX_SCK_UPDATED;
5885 else
5886 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005887
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005888 /* Here, we will tell an eventual cache on the client side that we don't
5889 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5890 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5891 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5892 */
Willy Tarreau67402132012-05-31 20:40:20 +02005893 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005894
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005895 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5896
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005897 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005898 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005899 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005900 }
5901 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005902
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005903 /*
5904 * 7: check if result will be cacheable with a cookie.
5905 * We'll block the response if security checks have caught
5906 * nasty things such as a cacheable cookie.
5907 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005908 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5909 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005910 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005911
5912 /* we're in presence of a cacheable response containing
5913 * a set-cookie header. We'll block it as requested by
5914 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005915 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005916 if (objt_server(t->target))
5917 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005918
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005919 t->be->be_counters.denied_resp++;
5920 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005921 if (t->listener->counters)
5922 t->listener->counters->denied_resp++;
5923
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005924 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005925 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005926 send_log(t->be, LOG_ALERT,
5927 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005928 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005929 goto return_srv_prx_502;
5930 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005931
5932 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005933 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005934 * If an "Upgrade" token is found, the header is left untouched in order
5935 * not to have to deal with some client bugs : some of them fail an upgrade
5936 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005937 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005938 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5939 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005940 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5941 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005942 unsigned int want_flags = 0;
5943
5944 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5945 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5946 /* we want a keep-alive response here. Keep-alive header
5947 * required if either side is not 1.1.
5948 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005949 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005950 want_flags |= TX_CON_KAL_SET;
5951 }
5952 else {
5953 /* we want a close response here. Close header required if
5954 * the server is 1.1, regardless of the client.
5955 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005956 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005957 want_flags |= TX_CON_CLO_SET;
5958 }
5959
5960 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005961 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005963
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005964 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005965 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005966 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005967 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005968
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005969 /*************************************************************
5970 * OK, that's finished for the headers. We have done what we *
5971 * could. Let's switch to the DATA state. *
5972 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005973
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005974 /* if the user wants to log as soon as possible, without counting
5975 * bytes from the server, then this is the right moment. We have
5976 * to temporarily assign bytes_out to log what we currently have.
5977 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005978 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005979 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5980 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005981 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005982 t->logs.bytes_out = 0;
5983 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005984
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005985 /* Note: we must not try to cheat by jumping directly to DATA,
5986 * otherwise we would not let the client side wake up.
5987 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005988
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005989 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005990 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005991 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005992}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005993
Willy Tarreaud98cf932009-12-27 22:54:55 +01005994/* This function is an analyser which forwards response body (including chunk
5995 * sizes if any). It is called as soon as we must forward, even if we forward
5996 * zero byte. The only situation where it must not be called is when we're in
5997 * tunnel mode and we want to forward till the close. It's used both to forward
5998 * remaining data and to resync after end of body. It expects the msg_state to
5999 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6000 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006001 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02006002 * bytes of pending data + the headers if not already done (between sol and sov).
6003 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006004 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006005int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006006{
6007 struct http_txn *txn = &s->txn;
6008 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02006009 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02006010 static struct buffer *tmpbuf = NULL;
6011 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006012 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006013 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006014
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006015 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6016 return 0;
6017
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006018 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006019 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006020 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006021 /* Output closed while we were sending data. We must abort and
6022 * wake the other side up.
6023 */
6024 msg->msg_state = HTTP_MSG_ERROR;
6025 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006026 return 1;
6027 }
6028
Willy Tarreau4fe41902010-06-07 22:27:41 +02006029 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006030 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006031
William Lallemand82fe75c2012-10-23 10:25:10 +02006032 /* this is the first time we need the compression buffer */
6033 if (s->comp_algo != NULL && tmpbuf == NULL) {
6034 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6035 goto aborted_xfer; /* no memory */
6036 }
6037
Willy Tarreaud98cf932009-12-27 22:54:55 +01006038 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01006039 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02006040 * rep->buf.p still points to the beginning of the message and msg->sol
6041 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006042 */
William Lallemand82fe75c2012-10-23 10:25:10 +02006043 channel_forward(res, msg->sov);
6044 msg->next = 0;
6045 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006046
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006047 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006048 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02006049 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01006050 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006051 }
6052
William Lallemand82fe75c2012-10-23 10:25:10 +02006053 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006054 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006055 if (ret < 0) {
6056 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006057 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006058 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006059 compressing = 1;
6060 }
6061
Willy Tarreaud98cf932009-12-27 22:54:55 +01006062 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006063 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02006064 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006065 if (s->comp_algo == NULL) {
6066 bytes = msg->sov - msg->sol;
6067 if (msg->chunk_len || bytes) {
6068 msg->sol = msg->sov;
6069 msg->next -= bytes; /* will be forwarded */
6070 msg->chunk_len += bytes;
6071 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6072 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006073 }
6074
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006075 switch (msg->msg_state - HTTP_MSG_DATA) {
6076 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006077 if (compressing) {
6078 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6079 if (ret < 0)
6080 goto aborted_xfer;
6081 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006082
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006083 if (res->to_forward || msg->chunk_len) {
6084 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006085 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006086 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006087
6088 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006089 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006090 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006091 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006092 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006093 if (compressing && consumed_data) {
6094 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6095 compressing = 0;
6096 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006097 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006098 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006099 /* fall through for HTTP_MSG_CHUNK_CRLF */
6100
6101 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6102 /* we want the CRLF after the data */
6103
6104 ret = http_skip_chunk_crlf(msg);
6105 if (ret == 0)
6106 goto missing_data;
6107 else if (ret < 0) {
6108 if (msg->err_pos >= 0)
6109 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6110 goto return_bad_res;
6111 }
6112 /* skipping data in buffer for compression */
6113 if (compressing) {
6114 b_adv(res->buf, msg->next);
6115 msg->next = 0;
6116 msg->sov = 0;
6117 msg->sol = 0;
6118 }
6119 /* we're in MSG_CHUNK_SIZE now, fall through */
6120
6121 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006122 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01006123 * set ->sov and ->next to point to the body and switch to DATA or
6124 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006125 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006126
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006127 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006128 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006129 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006130 else if (ret < 0) {
6131 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006132 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006133 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006134 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006135 if (compressing) {
6136 if (likely(msg->chunk_len > 0)) {
6137 /* skipping data if we are in compression mode */
6138 b_adv(res->buf, msg->next);
6139 msg->next = 0;
6140 msg->sov = 0;
6141 msg->sol = 0;
6142 } else {
6143 if (consumed_data) {
6144 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6145 compressing = 0;
6146 }
6147 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006148 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006149 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006150 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006151
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006152 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6153 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006154 if (ret == 0)
6155 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006156 else if (ret < 0) {
6157 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006158 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006159 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006160 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006161 if (s->comp_algo != NULL) {
6162 /* forwarding trailers */
6163 channel_forward(res, msg->next);
6164 msg->next = 0;
6165 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006166 /* we're in HTTP_MSG_DONE now, but we might still have
6167 * some data pending, so let's loop over once.
6168 */
6169 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006170
6171 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006172 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006173
6174 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006175 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006176 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6177 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006178 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006179 if (http_resync_states(s)) {
6180 http_silent_debug(__LINE__, s);
6181 /* some state changes occurred, maybe the analyser
6182 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006183 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006184 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006185 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006186 /* response errors are most likely due to
6187 * the client aborting the transfer.
6188 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006189 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006190 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006191 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006192 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006193 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006194 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006195 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006196 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006197 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006198 }
6199 }
6200
Willy Tarreaud98cf932009-12-27 22:54:55 +01006201 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006202 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006203 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6204 compressing = 0;
6205 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006206
6207 if (res->flags & CF_SHUTW)
6208 goto aborted_xfer;
6209
6210 /* stop waiting for data if the input is closed before the end. If the
6211 * client side was already closed, it means that the client has aborted,
6212 * so we don't want to count this as a server abort. Otherwise it's a
6213 * server abort.
6214 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006215 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006216 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6217 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006218 if (!(s->flags & SN_ERR_MASK))
6219 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006220 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006221 if (objt_server(s->target))
6222 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006223 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006224 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006225
Willy Tarreau40dba092010-03-04 18:14:51 +01006226 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006227 if (!s->req->analysers)
6228 goto return_bad_res;
6229
Willy Tarreauea953162012-05-18 23:41:28 +02006230 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006231 if (s->comp_algo == NULL) {
6232 bytes = msg->sov - msg->sol;
6233 if (msg->chunk_len || bytes) {
6234 msg->sol = msg->sov;
6235 msg->next -= bytes; /* will be forwarded */
6236 msg->chunk_len += bytes;
6237 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6238 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006239 }
6240
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006241 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006242 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006243 * Similarly, with keep-alive on the client side, we don't want to forward a
6244 * close.
6245 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006246 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006247 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6248 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006249 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006250
Willy Tarreau5c620922011-05-11 19:56:11 +02006251 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006252 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006253 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006254 * modes are already handled by the stream sock layer. We must not do
6255 * this in content-length mode because it could present the MSG_MORE
6256 * flag with the last block of forwarded data, which would cause an
6257 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006258 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006259 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006260 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006261
Willy Tarreaud98cf932009-12-27 22:54:55 +01006262 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006263 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006264 return 0;
6265
Willy Tarreau40dba092010-03-04 18:14:51 +01006266 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006267 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006268 if (objt_server(s->target))
6269 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006270
6271 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006272 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006273 /* don't send any error message as we're in the body */
6274 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006275 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006276 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006277 if (objt_server(s->target))
6278 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006279
6280 if (!(s->flags & SN_ERR_MASK))
6281 s->flags |= SN_ERR_PRXCOND;
6282 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006283 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006284 return 0;
6285
6286 aborted_xfer:
6287 txn->rsp.msg_state = HTTP_MSG_ERROR;
6288 /* don't send any error message as we're in the body */
6289 stream_int_retnclose(res->cons, NULL);
6290 res->analysers = 0;
6291 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6292
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006293 s->fe->fe_counters.cli_aborts++;
6294 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006295 if (objt_server(s->target))
6296 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006297
6298 if (!(s->flags & SN_ERR_MASK))
6299 s->flags |= SN_ERR_CLICL;
6300 if (!(s->flags & SN_FINST_MASK))
6301 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006302 return 0;
6303}
6304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006305/* Iterate the same filter through all request headers.
6306 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006307 * Since it can manage the switch to another backend, it updates the per-proxy
6308 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006310int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006311{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006312 char term;
6313 char *cur_ptr, *cur_end, *cur_next;
6314 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006315 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006316 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006317 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006319 last_hdr = 0;
6320
Willy Tarreau9b28e032012-10-12 23:49:43 +02006321 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006322 old_idx = 0;
6323
6324 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006325 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006326 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006327 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006328 (exp->action == ACT_ALLOW ||
6329 exp->action == ACT_DENY ||
6330 exp->action == ACT_TARPIT))
6331 return 0;
6332
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006333 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006334 if (!cur_idx)
6335 break;
6336
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006337 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006338 cur_ptr = cur_next;
6339 cur_end = cur_ptr + cur_hdr->len;
6340 cur_next = cur_end + cur_hdr->cr + 1;
6341
6342 /* Now we have one header between cur_ptr and cur_end,
6343 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344 */
6345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006346 /* The annoying part is that pattern matching needs
6347 * that we modify the contents to null-terminate all
6348 * strings before testing them.
6349 */
6350
6351 term = *cur_end;
6352 *cur_end = '\0';
6353
6354 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6355 switch (exp->action) {
6356 case ACT_SETBE:
6357 /* It is not possible to jump a second time.
6358 * FIXME: should we return an HTTP/500 here so that
6359 * the admin knows there's a problem ?
6360 */
6361 if (t->be != t->fe)
6362 break;
6363
6364 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006365 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006366 last_hdr = 1;
6367 break;
6368
6369 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006370 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006371 last_hdr = 1;
6372 break;
6373
6374 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006375 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006376 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006377 break;
6378
6379 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006380 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006381 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382 break;
6383
6384 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006385 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6386 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006387 /* FIXME: if the user adds a newline in the replacement, the
6388 * index will not be recalculated for now, and the new line
6389 * will not be counted as a new header.
6390 */
6391
6392 cur_end += delta;
6393 cur_next += delta;
6394 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006395 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006396 break;
6397
6398 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006399 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006400 cur_next += delta;
6401
Willy Tarreaufa355d42009-11-29 18:12:29 +01006402 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006403 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6404 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006405 cur_hdr->len = 0;
6406 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006407 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006408 break;
6409
6410 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006412 if (cur_end)
6413 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006415 /* keep the link from this header to next one in case of later
6416 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006417 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006418 old_idx = cur_idx;
6419 }
6420 return 0;
6421}
6422
6423
6424/* Apply the filter to the request line.
6425 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6426 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006427 * Since it can manage the switch to another backend, it updates the per-proxy
6428 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006430int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006431{
6432 char term;
6433 char *cur_ptr, *cur_end;
6434 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006435 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006436 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006437
Willy Tarreau3d300592007-03-18 18:34:41 +01006438 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006439 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006440 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 (exp->action == ACT_ALLOW ||
6442 exp->action == ACT_DENY ||
6443 exp->action == ACT_TARPIT))
6444 return 0;
6445 else if (exp->action == ACT_REMOVE)
6446 return 0;
6447
6448 done = 0;
6449
Willy Tarreau9b28e032012-10-12 23:49:43 +02006450 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006451 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006452
6453 /* Now we have the request line between cur_ptr and cur_end */
6454
6455 /* The annoying part is that pattern matching needs
6456 * that we modify the contents to null-terminate all
6457 * strings before testing them.
6458 */
6459
6460 term = *cur_end;
6461 *cur_end = '\0';
6462
6463 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6464 switch (exp->action) {
6465 case ACT_SETBE:
6466 /* It is not possible to jump a second time.
6467 * FIXME: should we return an HTTP/500 here so that
6468 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006469 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006470 if (t->be != t->fe)
6471 break;
6472
6473 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006474 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 done = 1;
6476 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 done = 1;
6481 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006484 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 done = 1;
6486 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006489 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 done = 1;
6491 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493 case ACT_REPLACE:
6494 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006495 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6496 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 /* FIXME: if the user adds a newline in the replacement, the
6498 * index will not be recalculated for now, and the new line
6499 * will not be counted as a new header.
6500 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006501
Willy Tarreaufa355d42009-11-29 18:12:29 +01006502 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006504 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 HTTP_MSG_RQMETH,
6506 cur_ptr, cur_end + 1,
6507 NULL, NULL);
6508 if (unlikely(!cur_end))
6509 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 /* we have a full request and we know that we have either a CR
6512 * or an LF at <ptr>.
6513 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006514 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6515 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006516 /* there is no point trying this regex on headers */
6517 return 1;
6518 }
6519 }
6520 *cur_end = term; /* restore the string terminator */
6521 return done;
6522}
Willy Tarreau97de6242006-12-27 17:18:38 +01006523
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006527 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529 * unparsable request. Since it can manage the switch to another backend, it
6530 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006532int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006534 struct http_txn *txn = &s->txn;
6535 struct hdr_exp *exp;
6536
6537 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006538 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 /*
6541 * The interleaving of transformations and verdicts
6542 * makes it difficult to decide to continue or stop
6543 * the evaluation.
6544 */
6545
Willy Tarreau6c123b12010-01-28 20:22:06 +01006546 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6547 break;
6548
Willy Tarreau3d300592007-03-18 18:34:41 +01006549 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006551 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006553
6554 /* if this filter had a condition, evaluate it now and skip to
6555 * next filter if the condition does not match.
6556 */
6557 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006558 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006559 ret = acl_pass(ret);
6560 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6561 ret = !ret;
6562
6563 if (!ret)
6564 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006565 }
6566
6567 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006568 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 if (unlikely(ret < 0))
6570 return -1;
6571
6572 if (likely(ret == 0)) {
6573 /* The filter did not match the request, it can be
6574 * iterated through all headers.
6575 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006576 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006577 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006578 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580}
6581
6582
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006585 * Try to retrieve the server associated to the appsession.
6586 * If the server is found, it's assigned to the session.
6587 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006588void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006589 struct http_txn *txn = &t->txn;
6590 appsess *asession = NULL;
6591 char *sessid_temp = NULL;
6592
Cyril Bontéb21570a2009-11-29 20:04:48 +01006593 if (len > t->be->appsession_len) {
6594 len = t->be->appsession_len;
6595 }
6596
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006597 if (t->be->options2 & PR_O2_AS_REQL) {
6598 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006599 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006600 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006601 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006602 }
6603
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006604 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006605 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6606 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6607 return;
6608 }
6609
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006610 memcpy(txn->sessid, buf, len);
6611 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006612 }
6613
6614 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6615 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6616 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6617 return;
6618 }
6619
Cyril Bontéb21570a2009-11-29 20:04:48 +01006620 memcpy(sessid_temp, buf, len);
6621 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006622
6623 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6624 /* free previously allocated memory */
6625 pool_free2(apools.sessid, sessid_temp);
6626
6627 if (asession != NULL) {
6628 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6629 if (!(t->be->options2 & PR_O2_AS_REQL))
6630 asession->request_count++;
6631
6632 if (asession->serverid != NULL) {
6633 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006634
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006635 while (srv) {
6636 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006637 if ((srv->state & SRV_RUNNING) ||
6638 (t->be->options & PR_O_PERSIST) ||
6639 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006640 /* we found the server and it's usable */
6641 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006642 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006643 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006644 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006645
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006646 break;
6647 } else {
6648 txn->flags &= ~TX_CK_MASK;
6649 txn->flags |= TX_CK_DOWN;
6650 }
6651 }
6652 srv = srv->next;
6653 }
6654 }
6655 }
6656}
6657
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006658/* Find the end of a cookie value contained between <s> and <e>. It works the
6659 * same way as with headers above except that the semi-colon also ends a token.
6660 * See RFC2965 for more information. Note that it requires a valid header to
6661 * return a valid result.
6662 */
6663char *find_cookie_value_end(char *s, const char *e)
6664{
6665 int quoted, qdpair;
6666
6667 quoted = qdpair = 0;
6668 for (; s < e; s++) {
6669 if (qdpair) qdpair = 0;
6670 else if (quoted) {
6671 if (*s == '\\') qdpair = 1;
6672 else if (*s == '"') quoted = 0;
6673 }
6674 else if (*s == '"') quoted = 1;
6675 else if (*s == ',' || *s == ';') return s;
6676 }
6677 return s;
6678}
6679
6680/* Delete a value in a header between delimiters <from> and <next> in buffer
6681 * <buf>. The number of characters displaced is returned, and the pointer to
6682 * the first delimiter is updated if required. The function tries as much as
6683 * possible to respect the following principles :
6684 * - replace <from> delimiter by the <next> one unless <from> points to a
6685 * colon, in which case <next> is simply removed
6686 * - set exactly one space character after the new first delimiter, unless
6687 * there are not enough characters in the block being moved to do so.
6688 * - remove unneeded spaces before the previous delimiter and after the new
6689 * one.
6690 *
6691 * It is the caller's responsibility to ensure that :
6692 * - <from> points to a valid delimiter or the colon ;
6693 * - <next> points to a valid delimiter or the final CR/LF ;
6694 * - there are non-space chars before <from> ;
6695 * - there is a CR/LF at or after <next>.
6696 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006697int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006698{
6699 char *prev = *from;
6700
6701 if (*prev == ':') {
6702 /* We're removing the first value, preserve the colon and add a
6703 * space if possible.
6704 */
6705 if (!http_is_crlf[(unsigned char)*next])
6706 next++;
6707 prev++;
6708 if (prev < next)
6709 *prev++ = ' ';
6710
6711 while (http_is_spht[(unsigned char)*next])
6712 next++;
6713 } else {
6714 /* Remove useless spaces before the old delimiter. */
6715 while (http_is_spht[(unsigned char)*(prev-1)])
6716 prev--;
6717 *from = prev;
6718
6719 /* copy the delimiter and if possible a space if we're
6720 * not at the end of the line.
6721 */
6722 if (!http_is_crlf[(unsigned char)*next]) {
6723 *prev++ = *next++;
6724 if (prev + 1 < next)
6725 *prev++ = ' ';
6726 while (http_is_spht[(unsigned char)*next])
6727 next++;
6728 }
6729 }
6730 return buffer_replace2(buf, prev, next, NULL, 0);
6731}
6732
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006733/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006734 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006735 * desirable to call it only when needed. This code is quite complex because
6736 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6737 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006739void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006740{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006741 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006742 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006743 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006744 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6745 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006747 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006748 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006749 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006750
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006751 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006753 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006755 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006756 hdr_beg = hdr_next;
6757 hdr_end = hdr_beg + cur_hdr->len;
6758 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006759
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006760 /* We have one full header between hdr_beg and hdr_end, and the
6761 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006762 * "Cookie:" headers.
6763 */
6764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006765 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006766 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767 old_idx = cur_idx;
6768 continue;
6769 }
6770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 del_from = NULL; /* nothing to be deleted */
6772 preserve_hdr = 0; /* assume we may kill the whole header */
6773
Willy Tarreau58f10d72006-12-04 02:26:12 +01006774 /* Now look for cookies. Conforming to RFC2109, we have to support
6775 * attributes whose name begin with a '$', and associate them with
6776 * the right cookie, if we want to delete this cookie.
6777 * So there are 3 cases for each cookie read :
6778 * 1) it's a special attribute, beginning with a '$' : ignore it.
6779 * 2) it's a server id cookie that we *MAY* want to delete : save
6780 * some pointers on it (last semi-colon, beginning of cookie...)
6781 * 3) it's an application cookie : we *MAY* have to delete a previous
6782 * "special" cookie.
6783 * At the end of loop, if a "special" cookie remains, we may have to
6784 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006785 * *MUST* delete it.
6786 *
6787 * Note: RFC2965 is unclear about the processing of spaces around
6788 * the equal sign in the ATTR=VALUE form. A careful inspection of
6789 * the RFC explicitly allows spaces before it, and not within the
6790 * tokens (attrs or values). An inspection of RFC2109 allows that
6791 * too but section 10.1.3 lets one think that spaces may be allowed
6792 * after the equal sign too, resulting in some (rare) buggy
6793 * implementations trying to do that. So let's do what servers do.
6794 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6795 * allowed quoted strings in values, with any possible character
6796 * after a backslash, including control chars and delimitors, which
6797 * causes parsing to become ambiguous. Browsers also allow spaces
6798 * within values even without quotes.
6799 *
6800 * We have to keep multiple pointers in order to support cookie
6801 * removal at the beginning, middle or end of header without
6802 * corrupting the header. All of these headers are valid :
6803 *
6804 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6805 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6806 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6807 * | | | | | | | | |
6808 * | | | | | | | | hdr_end <--+
6809 * | | | | | | | +--> next
6810 * | | | | | | +----> val_end
6811 * | | | | | +-----------> val_beg
6812 * | | | | +--------------> equal
6813 * | | | +----------------> att_end
6814 * | | +---------------------> att_beg
6815 * | +--------------------------> prev
6816 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006817 */
6818
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006819 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6820 /* Iterate through all cookies on this line */
6821
6822 /* find att_beg */
6823 att_beg = prev + 1;
6824 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6825 att_beg++;
6826
6827 /* find att_end : this is the first character after the last non
6828 * space before the equal. It may be equal to hdr_end.
6829 */
6830 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006831
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006832 while (equal < hdr_end) {
6833 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006834 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006835 if (http_is_spht[(unsigned char)*equal++])
6836 continue;
6837 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838 }
6839
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6841 * is between <att_beg> and <equal>, both may be identical.
6842 */
6843
6844 /* look for end of cookie if there is an equal sign */
6845 if (equal < hdr_end && *equal == '=') {
6846 /* look for the beginning of the value */
6847 val_beg = equal + 1;
6848 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6849 val_beg++;
6850
6851 /* find the end of the value, respecting quotes */
6852 next = find_cookie_value_end(val_beg, hdr_end);
6853
6854 /* make val_end point to the first white space or delimitor after the value */
6855 val_end = next;
6856 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6857 val_end--;
6858 } else {
6859 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006861
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 /* We have nothing to do with attributes beginning with '$'. However,
6863 * they will automatically be removed if a header before them is removed,
6864 * since they're supposed to be linked together.
6865 */
6866 if (*att_beg == '$')
6867 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006869 /* Ignore cookies with no equal sign */
6870 if (equal == next) {
6871 /* This is not our cookie, so we must preserve it. But if we already
6872 * scheduled another cookie for removal, we cannot remove the
6873 * complete header, but we can remove the previous block itself.
6874 */
6875 preserve_hdr = 1;
6876 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006877 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 val_end += delta;
6879 next += delta;
6880 hdr_end += delta;
6881 hdr_next += delta;
6882 cur_hdr->len += delta;
6883 http_msg_move_end(&txn->req, delta);
6884 prev = del_from;
6885 del_from = NULL;
6886 }
6887 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006888 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 /* if there are spaces around the equal sign, we need to
6891 * strip them otherwise we'll get trouble for cookie captures,
6892 * or even for rewrites. Since this happens extremely rarely,
6893 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6896 int stripped_before = 0;
6897 int stripped_after = 0;
6898
6899 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006900 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 equal += stripped_before;
6902 val_beg += stripped_before;
6903 }
6904
6905 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006906 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 val_beg += stripped_after;
6908 stripped_before += stripped_after;
6909 }
6910
6911 val_end += stripped_before;
6912 next += stripped_before;
6913 hdr_end += stripped_before;
6914 hdr_next += stripped_before;
6915 cur_hdr->len += stripped_before;
6916 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006917 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006918 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 /* First, let's see if we want to capture this cookie. We check
6921 * that we don't already have a client side cookie, because we
6922 * can only capture one. Also as an optimisation, we ignore
6923 * cookies shorter than the declared name.
6924 */
6925 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6926 (val_end - att_beg >= t->fe->capture_namelen) &&
6927 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6928 int log_len = val_end - att_beg;
6929
6930 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6931 Alert("HTTP logging : out of memory.\n");
6932 } else {
6933 if (log_len > t->fe->capture_len)
6934 log_len = t->fe->capture_len;
6935 memcpy(txn->cli_cookie, att_beg, log_len);
6936 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006937 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006938 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006939
Willy Tarreaubca99692010-10-06 19:25:55 +02006940 /* Persistence cookies in passive, rewrite or insert mode have the
6941 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006942 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006943 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006945 * For cookies in prefix mode, the form is :
6946 *
6947 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 */
6949 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6950 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6951 struct server *srv = t->be->srv;
6952 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006954 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6955 * have the server ID between val_beg and delim, and the original cookie between
6956 * delim+1 and val_end. Otherwise, delim==val_end :
6957 *
6958 * Cookie: NAME=SRV; # in all but prefix modes
6959 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6960 * | || || | |+-> next
6961 * | || || | +--> val_end
6962 * | || || +---------> delim
6963 * | || |+------------> val_beg
6964 * | || +-------------> att_end = equal
6965 * | |+-----------------> att_beg
6966 * | +------------------> prev
6967 * +-------------------------> hdr_beg
6968 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006969
Willy Tarreau67402132012-05-31 20:40:20 +02006970 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006971 for (delim = val_beg; delim < val_end; delim++)
6972 if (*delim == COOKIE_DELIM)
6973 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006974 } else {
6975 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006977 /* Now check if the cookie contains a date field, which would
6978 * appear after a vertical bar ('|') just after the server name
6979 * and before the delimiter.
6980 */
6981 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6982 if (vbar1) {
6983 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006984 * right is the last seen date. It is a base64 encoded
6985 * 30-bit value representing the UNIX date since the
6986 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006987 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006988 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006989 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006990 if (val_end - vbar1 >= 5) {
6991 val = b64tos30(vbar1);
6992 if (val > 0)
6993 txn->cookie_last_date = val << 2;
6994 }
6995 /* look for a second vertical bar */
6996 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6997 if (vbar1 && (val_end - vbar1 > 5)) {
6998 val = b64tos30(vbar1 + 1);
6999 if (val > 0)
7000 txn->cookie_first_date = val << 2;
7001 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007002 }
7003 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004
Willy Tarreauf64d1412010-10-07 20:06:11 +02007005 /* if the cookie has an expiration date and the proxy wants to check
7006 * it, then we do that now. We first check if the cookie is too old,
7007 * then only if it has expired. We detect strict overflow because the
7008 * time resolution here is not great (4 seconds). Cookies with dates
7009 * in the future are ignored if their offset is beyond one day. This
7010 * allows an admin to fix timezone issues without expiring everyone
7011 * and at the same time avoids keeping unwanted side effects for too
7012 * long.
7013 */
7014 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007015 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7016 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007017 txn->flags &= ~TX_CK_MASK;
7018 txn->flags |= TX_CK_OLD;
7019 delim = val_beg; // let's pretend we have not found the cookie
7020 txn->cookie_first_date = 0;
7021 txn->cookie_last_date = 0;
7022 }
7023 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007024 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7025 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007026 txn->flags &= ~TX_CK_MASK;
7027 txn->flags |= TX_CK_EXPIRED;
7028 delim = val_beg; // let's pretend we have not found the cookie
7029 txn->cookie_first_date = 0;
7030 txn->cookie_last_date = 0;
7031 }
7032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007033 /* Here, we'll look for the first running server which supports the cookie.
7034 * This allows to share a same cookie between several servers, for example
7035 * to dedicate backup servers to specific servers only.
7036 * However, to prevent clients from sticking to cookie-less backup server
7037 * when they have incidentely learned an empty cookie, we simply ignore
7038 * empty cookies and mark them as invalid.
7039 * The same behaviour is applied when persistence must be ignored.
7040 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007041 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007044 while (srv) {
7045 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7046 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7047 if ((srv->state & SRV_RUNNING) ||
7048 (t->be->options & PR_O_PERSIST) ||
7049 (t->flags & SN_FORCE_PRST)) {
7050 /* we found the server and we can use it */
7051 txn->flags &= ~TX_CK_MASK;
7052 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7053 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007054 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 break;
7056 } else {
7057 /* we found a server, but it's down,
7058 * mark it as such and go on in case
7059 * another one is available.
7060 */
7061 txn->flags &= ~TX_CK_MASK;
7062 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007063 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007065 srv = srv->next;
7066 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067
Willy Tarreauf64d1412010-10-07 20:06:11 +02007068 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007069 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007070 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007071 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7072 txn->flags |= TX_CK_UNUSED;
7073 else
7074 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007075 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007077 /* depending on the cookie mode, we may have to either :
7078 * - delete the complete cookie if we're in insert+indirect mode, so that
7079 * the server never sees it ;
7080 * - remove the server id from the cookie value, and tag the cookie as an
7081 * application cookie so that it does not get accidentely removed later,
7082 * if we're in cookie prefix mode
7083 */
Willy Tarreau67402132012-05-31 20:40:20 +02007084 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086
Willy Tarreau9b28e032012-10-12 23:49:43 +02007087 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 val_end += delta;
7089 next += delta;
7090 hdr_end += delta;
7091 hdr_next += delta;
7092 cur_hdr->len += delta;
7093 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007095 del_from = NULL;
7096 preserve_hdr = 1; /* we want to keep this cookie */
7097 }
7098 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007099 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007100 del_from = prev;
7101 }
7102 } else {
7103 /* This is not our cookie, so we must preserve it. But if we already
7104 * scheduled another cookie for removal, we cannot remove the
7105 * complete header, but we can remove the previous block itself.
7106 */
7107 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007109 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007110 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007111 if (att_beg >= del_from)
7112 att_beg += delta;
7113 if (att_end >= del_from)
7114 att_end += delta;
7115 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 val_end += delta;
7117 next += delta;
7118 hdr_end += delta;
7119 hdr_next += delta;
7120 cur_hdr->len += delta;
7121 http_msg_move_end(&txn->req, delta);
7122 prev = del_from;
7123 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007125 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007126
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007127 /* Look for the appsession cookie unless persistence must be ignored */
7128 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7129 int cmp_len, value_len;
7130 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007131
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007132 if (t->be->options2 & PR_O2_AS_PFX) {
7133 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7134 value_begin = att_beg + t->be->appsession_name_len;
7135 value_len = val_end - att_beg - t->be->appsession_name_len;
7136 } else {
7137 cmp_len = att_end - att_beg;
7138 value_begin = val_beg;
7139 value_len = val_end - val_beg;
7140 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007141
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 /* let's see if the cookie is our appcookie */
7143 if (cmp_len == t->be->appsession_name_len &&
7144 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7145 manage_client_side_appsession(t, value_begin, value_len);
7146 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147 }
7148
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007149 /* continue with next cookie on this header line */
7150 att_beg = next;
7151 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007153 /* There are no more cookies on this line.
7154 * We may still have one (or several) marked for deletion at the
7155 * end of the line. We must do this now in two ways :
7156 * - if some cookies must be preserved, we only delete from the
7157 * mark to the end of line ;
7158 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007160 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007162 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007163 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007164 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007165 cur_hdr->len += delta;
7166 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007167 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007168
7169 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007170 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7171 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007173 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007174 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007175 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007176 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177 }
7178
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007179 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007180 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007181 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007182}
7183
7184
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185/* Iterate the same filter through all response headers contained in <rtr>.
7186 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7187 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007188int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189{
7190 char term;
7191 char *cur_ptr, *cur_end, *cur_next;
7192 int cur_idx, old_idx, last_hdr;
7193 struct http_txn *txn = &t->txn;
7194 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007195 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196
7197 last_hdr = 0;
7198
Willy Tarreau9b28e032012-10-12 23:49:43 +02007199 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 old_idx = 0;
7201
7202 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007203 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007205 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 (exp->action == ACT_ALLOW ||
7207 exp->action == ACT_DENY))
7208 return 0;
7209
7210 cur_idx = txn->hdr_idx.v[old_idx].next;
7211 if (!cur_idx)
7212 break;
7213
7214 cur_hdr = &txn->hdr_idx.v[cur_idx];
7215 cur_ptr = cur_next;
7216 cur_end = cur_ptr + cur_hdr->len;
7217 cur_next = cur_end + cur_hdr->cr + 1;
7218
7219 /* Now we have one header between cur_ptr and cur_end,
7220 * and the next header starts at cur_next.
7221 */
7222
7223 /* The annoying part is that pattern matching needs
7224 * that we modify the contents to null-terminate all
7225 * strings before testing them.
7226 */
7227
7228 term = *cur_end;
7229 *cur_end = '\0';
7230
7231 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7232 switch (exp->action) {
7233 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007234 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 last_hdr = 1;
7236 break;
7237
7238 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007239 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 last_hdr = 1;
7241 break;
7242
7243 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007244 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7245 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 /* FIXME: if the user adds a newline in the replacement, the
7247 * index will not be recalculated for now, and the new line
7248 * will not be counted as a new header.
7249 */
7250
7251 cur_end += delta;
7252 cur_next += delta;
7253 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007254 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 break;
7256
7257 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007258 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007259 cur_next += delta;
7260
Willy Tarreaufa355d42009-11-29 18:12:29 +01007261 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7263 txn->hdr_idx.used--;
7264 cur_hdr->len = 0;
7265 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007266 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267 break;
7268
7269 }
7270 }
7271 if (cur_end)
7272 *cur_end = term; /* restore the string terminator */
7273
7274 /* keep the link from this header to next one in case of later
7275 * removal of next header.
7276 */
7277 old_idx = cur_idx;
7278 }
7279 return 0;
7280}
7281
7282
7283/* Apply the filter to the status line in the response buffer <rtr>.
7284 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7285 * or -1 if a replacement resulted in an invalid status line.
7286 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007287int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288{
7289 char term;
7290 char *cur_ptr, *cur_end;
7291 int done;
7292 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007293 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294
7295
Willy Tarreau3d300592007-03-18 18:34:41 +01007296 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007298 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 (exp->action == ACT_ALLOW ||
7300 exp->action == ACT_DENY))
7301 return 0;
7302 else if (exp->action == ACT_REMOVE)
7303 return 0;
7304
7305 done = 0;
7306
Willy Tarreau9b28e032012-10-12 23:49:43 +02007307 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007308 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309
7310 /* Now we have the status line between cur_ptr and cur_end */
7311
7312 /* The annoying part is that pattern matching needs
7313 * that we modify the contents to null-terminate all
7314 * strings before testing them.
7315 */
7316
7317 term = *cur_end;
7318 *cur_end = '\0';
7319
7320 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7321 switch (exp->action) {
7322 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007323 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 done = 1;
7325 break;
7326
7327 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007328 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007329 done = 1;
7330 break;
7331
7332 case ACT_REPLACE:
7333 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007334 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7335 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 /* FIXME: if the user adds a newline in the replacement, the
7337 * index will not be recalculated for now, and the new line
7338 * will not be counted as a new header.
7339 */
7340
Willy Tarreaufa355d42009-11-29 18:12:29 +01007341 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007343 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007344 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345 cur_ptr, cur_end + 1,
7346 NULL, NULL);
7347 if (unlikely(!cur_end))
7348 return -1;
7349
7350 /* we have a full respnse and we know that we have either a CR
7351 * or an LF at <ptr>.
7352 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007353 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007354 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 /* there is no point trying this regex on headers */
7356 return 1;
7357 }
7358 }
7359 *cur_end = term; /* restore the string terminator */
7360 return done;
7361}
7362
7363
7364
7365/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007366 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7368 * unparsable response.
7369 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007370int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007372 struct http_txn *txn = &s->txn;
7373 struct hdr_exp *exp;
7374
7375 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 int ret;
7377
7378 /*
7379 * The interleaving of transformations and verdicts
7380 * makes it difficult to decide to continue or stop
7381 * the evaluation.
7382 */
7383
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007384 if (txn->flags & TX_SVDENY)
7385 break;
7386
Willy Tarreau3d300592007-03-18 18:34:41 +01007387 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7389 exp->action == ACT_PASS)) {
7390 exp = exp->next;
7391 continue;
7392 }
7393
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007394 /* if this filter had a condition, evaluate it now and skip to
7395 * next filter if the condition does not match.
7396 */
7397 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007398 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007399 ret = acl_pass(ret);
7400 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7401 ret = !ret;
7402 if (!ret)
7403 continue;
7404 }
7405
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007407 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 if (unlikely(ret < 0))
7409 return -1;
7410
7411 if (likely(ret == 0)) {
7412 /* The filter did not match the response, it can be
7413 * iterated through all headers.
7414 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007415 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417 }
7418 return 0;
7419}
7420
7421
Willy Tarreaua15645d2007-03-18 16:22:39 +01007422/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007423 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007424 * desirable to call it only when needed. This function is also used when we
7425 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007427void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007428{
7429 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007430 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007432 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007433 char *hdr_beg, *hdr_end, *hdr_next;
7434 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007435
Willy Tarreaua15645d2007-03-18 16:22:39 +01007436 /* Iterate through the headers.
7437 * we start with the start line.
7438 */
7439 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007440 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441
7442 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7443 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007444 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007445
7446 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007447 hdr_beg = hdr_next;
7448 hdr_end = hdr_beg + cur_hdr->len;
7449 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007450
Willy Tarreau24581ba2010-08-31 22:39:35 +02007451 /* We have one full header between hdr_beg and hdr_end, and the
7452 * next header starts at hdr_next. We're only interested in
7453 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007454 */
7455
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 is_cookie2 = 0;
7457 prev = hdr_beg + 10;
7458 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007459 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007460 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7461 if (!val) {
7462 old_idx = cur_idx;
7463 continue;
7464 }
7465 is_cookie2 = 1;
7466 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467 }
7468
Willy Tarreau24581ba2010-08-31 22:39:35 +02007469 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7470 * <prev> points to the colon.
7471 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007472 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7475 * check-cache is enabled) and we are not interested in checking
7476 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007477 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007478 if (t->be->cookie_name == NULL &&
7479 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007480 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007481 return;
7482
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 /* OK so now we know we have to process this response cookie.
7484 * The format of the Set-Cookie header is slightly different
7485 * from the format of the Cookie header in that it does not
7486 * support the comma as a cookie delimiter (thus the header
7487 * cannot be folded) because the Expires attribute described in
7488 * the original Netscape's spec may contain an unquoted date
7489 * with a comma inside. We have to live with this because
7490 * many browsers don't support Max-Age and some browsers don't
7491 * support quoted strings. However the Set-Cookie2 header is
7492 * clean.
7493 *
7494 * We have to keep multiple pointers in order to support cookie
7495 * removal at the beginning, middle or end of header without
7496 * corrupting the header (in case of set-cookie2). A special
7497 * pointer, <scav> points to the beginning of the set-cookie-av
7498 * fields after the first semi-colon. The <next> pointer points
7499 * either to the end of line (set-cookie) or next unquoted comma
7500 * (set-cookie2). All of these headers are valid :
7501 *
7502 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7503 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7504 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7505 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7506 * | | | | | | | | | |
7507 * | | | | | | | | +-> next hdr_end <--+
7508 * | | | | | | | +------------> scav
7509 * | | | | | | +--------------> val_end
7510 * | | | | | +--------------------> val_beg
7511 * | | | | +----------------------> equal
7512 * | | | +------------------------> att_end
7513 * | | +----------------------------> att_beg
7514 * | +------------------------------> prev
7515 * +-----------------------------------------> hdr_beg
7516 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007517
Willy Tarreau24581ba2010-08-31 22:39:35 +02007518 for (; prev < hdr_end; prev = next) {
7519 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 /* find att_beg */
7522 att_beg = prev + 1;
7523 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7524 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525
Willy Tarreau24581ba2010-08-31 22:39:35 +02007526 /* find att_end : this is the first character after the last non
7527 * space before the equal. It may be equal to hdr_end.
7528 */
7529 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530
Willy Tarreau24581ba2010-08-31 22:39:35 +02007531 while (equal < hdr_end) {
7532 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7533 break;
7534 if (http_is_spht[(unsigned char)*equal++])
7535 continue;
7536 att_end = equal;
7537 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007538
Willy Tarreau24581ba2010-08-31 22:39:35 +02007539 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7540 * is between <att_beg> and <equal>, both may be identical.
7541 */
7542
7543 /* look for end of cookie if there is an equal sign */
7544 if (equal < hdr_end && *equal == '=') {
7545 /* look for the beginning of the value */
7546 val_beg = equal + 1;
7547 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7548 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549
Willy Tarreau24581ba2010-08-31 22:39:35 +02007550 /* find the end of the value, respecting quotes */
7551 next = find_cookie_value_end(val_beg, hdr_end);
7552
7553 /* make val_end point to the first white space or delimitor after the value */
7554 val_end = next;
7555 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7556 val_end--;
7557 } else {
7558 /* <equal> points to next comma, semi-colon or EOL */
7559 val_beg = val_end = next = equal;
7560 }
7561
7562 if (next < hdr_end) {
7563 /* Set-Cookie2 supports multiple cookies, and <next> points to
7564 * a colon or semi-colon before the end. So skip all attr-value
7565 * pairs and look for the next comma. For Set-Cookie, since
7566 * commas are permitted in values, skip to the end.
7567 */
7568 if (is_cookie2)
7569 next = find_hdr_value_end(next, hdr_end);
7570 else
7571 next = hdr_end;
7572 }
7573
7574 /* Now everything is as on the diagram above */
7575
7576 /* Ignore cookies with no equal sign */
7577 if (equal == val_end)
7578 continue;
7579
7580 /* If there are spaces around the equal sign, we need to
7581 * strip them otherwise we'll get trouble for cookie captures,
7582 * or even for rewrites. Since this happens extremely rarely,
7583 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7586 int stripped_before = 0;
7587 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588
Willy Tarreau24581ba2010-08-31 22:39:35 +02007589 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007590 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007591 equal += stripped_before;
7592 val_beg += stripped_before;
7593 }
7594
7595 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007596 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007597 val_beg += stripped_after;
7598 stripped_before += stripped_after;
7599 }
7600
7601 val_end += stripped_before;
7602 next += stripped_before;
7603 hdr_end += stripped_before;
7604 hdr_next += stripped_before;
7605 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007606 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007607 }
7608
7609 /* First, let's see if we want to capture this cookie. We check
7610 * that we don't already have a server side cookie, because we
7611 * can only capture one. Also as an optimisation, we ignore
7612 * cookies shorter than the declared name.
7613 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007614 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007615 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007616 (val_end - att_beg >= t->fe->capture_namelen) &&
7617 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7618 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007619 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620 Alert("HTTP logging : out of memory.\n");
7621 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007622 else {
7623 if (log_len > t->fe->capture_len)
7624 log_len = t->fe->capture_len;
7625 memcpy(txn->srv_cookie, att_beg, log_len);
7626 txn->srv_cookie[log_len] = 0;
7627 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628 }
7629
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007630 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007632 if (!(t->flags & SN_IGNORE_PRST) &&
7633 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7634 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007635 /* assume passive cookie by default */
7636 txn->flags &= ~TX_SCK_MASK;
7637 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638
7639 /* If the cookie is in insert mode on a known server, we'll delete
7640 * this occurrence because we'll insert another one later.
7641 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 * a direct access.
7643 */
Willy Tarreau67402132012-05-31 20:40:20 +02007644 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007645 /* The "preserve" flag was set, we don't want to touch the
7646 * server's cookie.
7647 */
7648 }
Willy Tarreau67402132012-05-31 20:40:20 +02007649 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7650 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007651 /* this cookie must be deleted */
7652 if (*prev == ':' && next == hdr_end) {
7653 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007654 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007655 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7656 txn->hdr_idx.used--;
7657 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007658 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007659 hdr_next += delta;
7660 http_msg_move_end(&txn->rsp, delta);
7661 /* note: while both invalid now, <next> and <hdr_end>
7662 * are still equal, so the for() will stop as expected.
7663 */
7664 } else {
7665 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007666 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 next = prev;
7668 hdr_end += delta;
7669 hdr_next += delta;
7670 cur_hdr->len += delta;
7671 http_msg_move_end(&txn->rsp, delta);
7672 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007673 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007674 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007675 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 }
Willy Tarreau67402132012-05-31 20:40:20 +02007677 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007678 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 * with this server since we know it.
7680 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007681 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007682 next += delta;
7683 hdr_end += delta;
7684 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007686 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687
Willy Tarreauf1348312010-10-07 15:54:11 +02007688 txn->flags &= ~TX_SCK_MASK;
7689 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007691 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007693 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007695 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007696 next += delta;
7697 hdr_end += delta;
7698 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007699 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007700 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701
Willy Tarreau827aee92011-03-10 16:55:02 +01007702 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007703 txn->flags &= ~TX_SCK_MASK;
7704 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705 }
7706 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007707 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7708 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007709 int cmp_len, value_len;
7710 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007711
Cyril Bontéb21570a2009-11-29 20:04:48 +01007712 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007713 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7714 value_begin = att_beg + t->be->appsession_name_len;
7715 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007716 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007717 cmp_len = att_end - att_beg;
7718 value_begin = val_beg;
7719 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007720 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007721
Cyril Bonté17530c32010-04-06 21:11:10 +02007722 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007723 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7724 /* free a possibly previously allocated memory */
7725 pool_free2(apools.sessid, txn->sessid);
7726
Cyril Bontéb21570a2009-11-29 20:04:48 +01007727 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007728 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007729 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7730 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7731 return;
7732 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007733 memcpy(txn->sessid, value_begin, value_len);
7734 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007735 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007736 }
7737 /* that's done for this cookie, check the next one on the same
7738 * line when next != hdr_end (only if is_cookie2).
7739 */
7740 }
7741 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007742 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007743 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007744
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007745 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007746 appsess *asession = NULL;
7747 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007748 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007749 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007750 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007751 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7752 Alert("Not enough Memory process_srv():asession:calloc().\n");
7753 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7754 return;
7755 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007756 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7757
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007758 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7759 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7760 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007761 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007762 return;
7763 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007764 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007765 asession->sessid[t->be->appsession_len] = 0;
7766
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007767 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007768 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007769 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007770 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007771 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007772 return;
7773 }
7774 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007775 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007776
7777 asession->request_count = 0;
7778 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7779 }
7780
7781 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7782 asession->request_count++;
7783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784}
7785
7786
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787/*
7788 * Check if response is cacheable or not. Updates t->flags.
7789 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007790void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791{
7792 struct http_txn *txn = &t->txn;
7793 char *p1, *p2;
7794
7795 char *cur_ptr, *cur_end, *cur_next;
7796 int cur_idx;
7797
Willy Tarreau5df51872007-11-25 16:20:08 +01007798 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 return;
7800
7801 /* Iterate through the headers.
7802 * we start with the start line.
7803 */
7804 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007805 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007806
7807 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7808 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007809 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810
7811 cur_hdr = &txn->hdr_idx.v[cur_idx];
7812 cur_ptr = cur_next;
7813 cur_end = cur_ptr + cur_hdr->len;
7814 cur_next = cur_end + cur_hdr->cr + 1;
7815
7816 /* We have one full header between cur_ptr and cur_end, and the
7817 * next header starts at cur_next. We're only interested in
7818 * "Cookie:" headers.
7819 */
7820
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007821 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7822 if (val) {
7823 if ((cur_end - (cur_ptr + val) >= 8) &&
7824 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7825 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7826 return;
7827 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828 }
7829
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007830 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7831 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 continue;
7833
7834 /* OK, right now we know we have a cache-control header at cur_ptr */
7835
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007836 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837
7838 if (p1 >= cur_end) /* no more info */
7839 continue;
7840
7841 /* p1 is at the beginning of the value */
7842 p2 = p1;
7843
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007844 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 p2++;
7846
7847 /* we have a complete value between p1 and p2 */
7848 if (p2 < cur_end && *p2 == '=') {
7849 /* we have something of the form no-cache="set-cookie" */
7850 if ((cur_end - p1 >= 21) &&
7851 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7852 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007853 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007854 continue;
7855 }
7856
7857 /* OK, so we know that either p2 points to the end of string or to a comma */
7858 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007859 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7861 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7862 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007863 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864 return;
7865 }
7866
7867 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007868 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869 continue;
7870 }
7871 }
7872}
7873
7874
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875/*
7876 * Try to retrieve a known appsession in the URI, then the associated server.
7877 * If the server is found, it's assigned to the session.
7878 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007879void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007880{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007881 char *end_params, *first_param, *cur_param, *next_param;
7882 char separator;
7883 int value_len;
7884
7885 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007886
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007887 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007888 (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 +01007889 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007890 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007891
Cyril Bontéb21570a2009-11-29 20:04:48 +01007892 first_param = NULL;
7893 switch (mode) {
7894 case PR_O2_AS_M_PP:
7895 first_param = memchr(begin, ';', len);
7896 break;
7897 case PR_O2_AS_M_QS:
7898 first_param = memchr(begin, '?', len);
7899 break;
7900 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007901
Cyril Bontéb21570a2009-11-29 20:04:48 +01007902 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007904 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905
Cyril Bontéb21570a2009-11-29 20:04:48 +01007906 switch (mode) {
7907 case PR_O2_AS_M_PP:
7908 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7909 end_params = (char *) begin + len;
7910 }
7911 separator = ';';
7912 break;
7913 case PR_O2_AS_M_QS:
7914 end_params = (char *) begin + len;
7915 separator = '&';
7916 break;
7917 default:
7918 /* unknown mode, shouldn't happen */
7919 return;
7920 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007921
Cyril Bontéb21570a2009-11-29 20:04:48 +01007922 cur_param = next_param = end_params;
7923 while (cur_param > first_param) {
7924 cur_param--;
7925 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7926 /* let's see if this is the appsession parameter */
7927 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7928 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7929 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7930 /* Cool... it's the right one */
7931 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7932 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7933 if (value_len > 0) {
7934 manage_client_side_appsession(t, cur_param, value_len);
7935 }
7936 break;
7937 }
7938 next_param = cur_param;
7939 }
7940 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007942 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007943 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007944#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007945}
7946
Willy Tarreaub2513902006-12-17 14:52:38 +01007947/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007948 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007949 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007950 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007951 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007952 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007953 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007954 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007955 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007956int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007957{
7958 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007959 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007960 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007961
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007962 if (!uri_auth)
7963 return 0;
7964
Cyril Bonté70be45d2010-10-12 00:14:35 +02007965 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007966 return 0;
7967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007968 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007969 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007970 return 0;
7971
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007972 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007973 return 0;
7974
Willy Tarreaub2513902006-12-17 14:52:38 +01007975 return 1;
7976}
7977
Willy Tarreau4076a152009-04-02 15:18:36 +02007978/*
7979 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007980 * By default it tries to report the error position as msg->err_pos. However if
7981 * this one is not set, it will then report msg->next, which is the last known
7982 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007983 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007984 */
7985void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007986 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01007987 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007988{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007989 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007990 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007991
Willy Tarreau9b28e032012-10-12 23:49:43 +02007992 es->len = MIN(chn->buf->i, sizeof(es->buf));
7993 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007994 len1 = MIN(len1, es->len);
7995 len2 = es->len - len1; /* remaining data if buffer wraps */
7996
Willy Tarreau9b28e032012-10-12 23:49:43 +02007997 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007998 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007999 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008000
Willy Tarreau4076a152009-04-02 15:18:36 +02008001 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008002 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008003 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008004 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008005
Willy Tarreau4076a152009-04-02 15:18:36 +02008006 es->when = date; // user-visible date
8007 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008008 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008009 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008010 if (objt_conn(s->req->prod->end))
8011 es->src = __objt_conn(s->req->prod->end)->addr.from;
8012 else
8013 memset(&es->src, 0, sizeof(es->src));
8014
Willy Tarreau078272e2010-12-12 12:46:33 +01008015 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008016 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008017 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008018 es->s_flags = s->flags;
8019 es->t_flags = s->txn.flags;
8020 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008021 es->b_out = chn->buf->o;
8022 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008023 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008024 es->m_clen = msg->chunk_len;
8025 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008026}
Willy Tarreaub2513902006-12-17 14:52:38 +01008027
Willy Tarreau294c4732011-12-16 21:35:50 +01008028/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8029 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8030 * performed over the whole headers. Otherwise it must contain a valid header
8031 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8032 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8033 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8034 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008035 * -1. The value fetch stops at commas, so this function is suited for use with
8036 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008037 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008038 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008039unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008040 struct hdr_idx *idx, int occ,
8041 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008042{
Willy Tarreau294c4732011-12-16 21:35:50 +01008043 struct hdr_ctx local_ctx;
8044 char *ptr_hist[MAX_HDR_HISTORY];
8045 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008046 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008047 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008048
Willy Tarreau294c4732011-12-16 21:35:50 +01008049 if (!ctx) {
8050 local_ctx.idx = 0;
8051 ctx = &local_ctx;
8052 }
8053
Willy Tarreaubce70882009-09-07 11:51:47 +02008054 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008055 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008056 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008057 occ--;
8058 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008059 *vptr = ctx->line + ctx->val;
8060 *vlen = ctx->vlen;
8061 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008062 }
8063 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008064 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008065 }
8066
8067 /* negative occurrence, we scan all the list then walk back */
8068 if (-occ > MAX_HDR_HISTORY)
8069 return 0;
8070
Willy Tarreau294c4732011-12-16 21:35:50 +01008071 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008072 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008073 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8074 len_hist[hist_ptr] = ctx->vlen;
8075 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008076 hist_ptr = 0;
8077 found++;
8078 }
8079 if (-occ > found)
8080 return 0;
8081 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008082 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8083 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8084 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008085 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008086 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008087 if (hist_ptr >= MAX_HDR_HISTORY)
8088 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008089 *vptr = ptr_hist[hist_ptr];
8090 *vlen = len_hist[hist_ptr];
8091 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008092}
8093
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008094/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8095 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8096 * performed over the whole headers. Otherwise it must contain a valid header
8097 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8098 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8099 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8100 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8101 * -1. This function differs from http_get_hdr() in that it only returns full
8102 * line header values and does not stop at commas.
8103 * The return value is 0 if nothing was found, or non-zero otherwise.
8104 */
8105unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8106 struct hdr_idx *idx, int occ,
8107 struct hdr_ctx *ctx, char **vptr, int *vlen)
8108{
8109 struct hdr_ctx local_ctx;
8110 char *ptr_hist[MAX_HDR_HISTORY];
8111 int len_hist[MAX_HDR_HISTORY];
8112 unsigned int hist_ptr;
8113 int found;
8114
8115 if (!ctx) {
8116 local_ctx.idx = 0;
8117 ctx = &local_ctx;
8118 }
8119
8120 if (occ >= 0) {
8121 /* search from the beginning */
8122 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8123 occ--;
8124 if (occ <= 0) {
8125 *vptr = ctx->line + ctx->val;
8126 *vlen = ctx->vlen;
8127 return 1;
8128 }
8129 }
8130 return 0;
8131 }
8132
8133 /* negative occurrence, we scan all the list then walk back */
8134 if (-occ > MAX_HDR_HISTORY)
8135 return 0;
8136
8137 found = hist_ptr = 0;
8138 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8139 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8140 len_hist[hist_ptr] = ctx->vlen;
8141 if (++hist_ptr >= MAX_HDR_HISTORY)
8142 hist_ptr = 0;
8143 found++;
8144 }
8145 if (-occ > found)
8146 return 0;
8147 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8148 * find occurrence -occ, so we have to check [hist_ptr+occ].
8149 */
8150 hist_ptr += occ;
8151 if (hist_ptr >= MAX_HDR_HISTORY)
8152 hist_ptr -= MAX_HDR_HISTORY;
8153 *vptr = ptr_hist[hist_ptr];
8154 *vlen = len_hist[hist_ptr];
8155 return 1;
8156}
8157
Willy Tarreaubaaee002006-06-26 02:48:02 +02008158/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008159 * Print a debug line with a header. Always stop at the first CR or LF char,
8160 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8161 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008162 */
8163void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8164{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008165 int max;
8166 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008167 dir,
8168 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8169 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 +02008170
8171 for (max = 0; start + max < end; max++)
8172 if (start[max] == '\r' || start[max] == '\n')
8173 break;
8174
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008175 UBOUND(max, trash.size - trash.len - 3);
8176 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8177 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008178 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008179}
8180
Willy Tarreau0937bc42009-12-22 15:03:09 +01008181/*
8182 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8183 * the required fields are properly allocated and that we only need to (re)init
8184 * them. This should be used before processing any new request.
8185 */
8186void http_init_txn(struct session *s)
8187{
8188 struct http_txn *txn = &s->txn;
8189 struct proxy *fe = s->fe;
8190
8191 txn->flags = 0;
8192 txn->status = -1;
8193
Willy Tarreauf64d1412010-10-07 20:06:11 +02008194 txn->cookie_first_date = 0;
8195 txn->cookie_last_date = 0;
8196
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008197 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008198 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008199 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008200 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008201 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008202 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008203 txn->req.chunk_len = 0LL;
8204 txn->req.body_len = 0LL;
8205 txn->rsp.chunk_len = 0LL;
8206 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008207 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8208 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008209 txn->req.chn = s->req;
8210 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008211
8212 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008213
8214 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8215 if (fe->options2 & PR_O2_REQBUG_OK)
8216 txn->req.err_pos = -1; /* let buggy requests pass */
8217
Willy Tarreau46023632010-01-07 22:51:47 +01008218 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008219 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8220
Willy Tarreau46023632010-01-07 22:51:47 +01008221 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008222 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8223
8224 if (txn->hdr_idx.v)
8225 hdr_idx_init(&txn->hdr_idx);
8226}
8227
8228/* to be used at the end of a transaction */
8229void http_end_txn(struct session *s)
8230{
8231 struct http_txn *txn = &s->txn;
8232
8233 /* these ones will have been dynamically allocated */
8234 pool_free2(pool2_requri, txn->uri);
8235 pool_free2(pool2_capture, txn->cli_cookie);
8236 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008237 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008238 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008239
William Lallemanda73203e2012-03-12 12:48:57 +01008240 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008241 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008242 txn->uri = NULL;
8243 txn->srv_cookie = NULL;
8244 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008245
8246 if (txn->req.cap) {
8247 struct cap_hdr *h;
8248 for (h = s->fe->req_cap; h; h = h->next)
8249 pool_free2(h->pool, txn->req.cap[h->index]);
8250 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8251 }
8252
8253 if (txn->rsp.cap) {
8254 struct cap_hdr *h;
8255 for (h = s->fe->rsp_cap; h; h = h->next)
8256 pool_free2(h->pool, txn->rsp.cap[h->index]);
8257 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8258 }
8259
Willy Tarreau0937bc42009-12-22 15:03:09 +01008260}
8261
8262/* to be used at the end of a transaction to prepare a new one */
8263void http_reset_txn(struct session *s)
8264{
8265 http_end_txn(s);
8266 http_init_txn(s);
8267
8268 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008269 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008270 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008271 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008272 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008273 /* re-init store persistence */
8274 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008275 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008276
Willy Tarreau0937bc42009-12-22 15:03:09 +01008277 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008278
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008279 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008280
Willy Tarreau739cfba2010-01-25 23:11:14 +01008281 /* We must trim any excess data from the response buffer, because we
8282 * may have blocked an invalid response from a server that we don't
8283 * want to accidentely forward once we disable the analysers, nor do
8284 * we want those data to come along with next response. A typical
8285 * example of such data would be from a buggy server responding to
8286 * a HEAD with some data, or sending more than the advertised
8287 * content-length.
8288 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008289 if (unlikely(s->rep->buf->i))
8290 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008291
Willy Tarreau0937bc42009-12-22 15:03:09 +01008292 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008293 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008294
Willy Tarreaud04e8582010-05-31 12:31:35 +02008295 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008296 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008297
8298 s->req->rex = TICK_ETERNITY;
8299 s->req->wex = TICK_ETERNITY;
8300 s->req->analyse_exp = TICK_ETERNITY;
8301 s->rep->rex = TICK_ETERNITY;
8302 s->rep->wex = TICK_ETERNITY;
8303 s->rep->analyse_exp = TICK_ETERNITY;
8304}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008305
Willy Tarreauff011f22011-01-06 17:51:27 +01008306void free_http_req_rules(struct list *r) {
8307 struct http_req_rule *tr, *pr;
8308
8309 list_for_each_entry_safe(pr, tr, r, list) {
8310 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008311 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008312 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008313
8314 free(pr);
8315 }
8316}
8317
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008318/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008319struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8320{
8321 struct http_req_rule *rule;
8322 int cur_arg;
8323
8324 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8325 if (!rule) {
8326 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008327 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008328 }
8329
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008330 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008331 rule->action = HTTP_REQ_ACT_ALLOW;
8332 cur_arg = 1;
8333 } else if (!strcmp(args[0], "deny")) {
8334 rule->action = HTTP_REQ_ACT_DENY;
8335 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008336 } else if (!strcmp(args[0], "tarpit")) {
8337 rule->action = HTTP_REQ_ACT_TARPIT;
8338 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008339 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008340 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008341 cur_arg = 1;
8342
8343 while(*args[cur_arg]) {
8344 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008345 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008346 cur_arg+=2;
8347 continue;
8348 } else
8349 break;
8350 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008351 } else if (!strcmp(args[0], "set-nice")) {
8352 rule->action = HTTP_REQ_ACT_SET_NICE;
8353 cur_arg = 1;
8354
8355 if (!*args[cur_arg] ||
8356 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8357 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8358 file, linenum, args[0]);
8359 goto out_err;
8360 }
8361 rule->arg.nice = atoi(args[cur_arg]);
8362 if (rule->arg.nice < -1024)
8363 rule->arg.nice = -1024;
8364 else if (rule->arg.nice > 1024)
8365 rule->arg.nice = 1024;
8366 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008367 } else if (!strcmp(args[0], "set-tos")) {
8368#ifdef IP_TOS
8369 char *err;
8370 rule->action = HTTP_REQ_ACT_SET_TOS;
8371 cur_arg = 1;
8372
8373 if (!*args[cur_arg] ||
8374 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8375 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8376 file, linenum, args[0]);
8377 goto out_err;
8378 }
8379
8380 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8381 if (err && *err != '\0') {
8382 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8383 file, linenum, err, args[0]);
8384 goto out_err;
8385 }
8386 cur_arg++;
8387#else
8388 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8389 goto out_err;
8390#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008391 } else if (!strcmp(args[0], "set-mark")) {
8392#ifdef SO_MARK
8393 char *err;
8394 rule->action = HTTP_REQ_ACT_SET_MARK;
8395 cur_arg = 1;
8396
8397 if (!*args[cur_arg] ||
8398 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8399 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8400 file, linenum, args[0]);
8401 goto out_err;
8402 }
8403
8404 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8405 if (err && *err != '\0') {
8406 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8407 file, linenum, err, args[0]);
8408 goto out_err;
8409 }
8410 cur_arg++;
8411 global.last_checks |= LSTCHK_NETADM;
8412#else
8413 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8414 goto out_err;
8415#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008416 } else if (!strcmp(args[0], "set-log-level")) {
8417 rule->action = HTTP_REQ_ACT_SET_LOGL;
8418 cur_arg = 1;
8419
8420 if (!*args[cur_arg] ||
8421 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8422 bad_log_level:
8423 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8424 file, linenum, args[0]);
8425 goto out_err;
8426 }
8427 if (strcmp(args[cur_arg], "silent") == 0)
8428 rule->arg.loglevel = -1;
8429 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8430 goto bad_log_level;
8431 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008432 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8433 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8434 cur_arg = 1;
8435
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008436 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8437 (*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 +01008438 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8439 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008440 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008441 }
8442
8443 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8444 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8445 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008446
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008447 proxy->conf.args.ctx = ARGC_HRQ;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008448 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8449 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008450 free(proxy->conf.lfs_file);
8451 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8452 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008453 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008454 } else if (strcmp(args[0], "redirect") == 0) {
8455 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008456 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008457
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008458 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008459 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8460 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8461 goto out_err;
8462 }
8463
8464 /* this redirect rule might already contain a parsed condition which
8465 * we'll pass to the http-request rule.
8466 */
8467 rule->action = HTTP_REQ_ACT_REDIR;
8468 rule->arg.redir = redir;
8469 rule->cond = redir->cond;
8470 redir->cond = NULL;
8471 cur_arg = 2;
8472 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008473 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008474 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 +01008475 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008476 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008477 }
8478
8479 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8480 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008481 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008482
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008483 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8484 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8485 file, linenum, args[0], errmsg);
8486 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008487 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008488 }
8489 rule->cond = cond;
8490 }
8491 else if (*args[cur_arg]) {
8492 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8493 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8494 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008495 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008496 }
8497
8498 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008499 out_err:
8500 free(rule);
8501 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008502}
8503
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008504/* parse an "http-respose" rule */
8505struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8506{
8507 struct http_res_rule *rule;
8508 int cur_arg;
8509
8510 rule = calloc(1, sizeof(*rule));
8511 if (!rule) {
8512 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8513 goto out_err;
8514 }
8515
8516 if (!strcmp(args[0], "allow")) {
8517 rule->action = HTTP_RES_ACT_ALLOW;
8518 cur_arg = 1;
8519 } else if (!strcmp(args[0], "deny")) {
8520 rule->action = HTTP_RES_ACT_DENY;
8521 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008522 } else if (!strcmp(args[0], "set-nice")) {
8523 rule->action = HTTP_RES_ACT_SET_NICE;
8524 cur_arg = 1;
8525
8526 if (!*args[cur_arg] ||
8527 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8528 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8529 file, linenum, args[0]);
8530 goto out_err;
8531 }
8532 rule->arg.nice = atoi(args[cur_arg]);
8533 if (rule->arg.nice < -1024)
8534 rule->arg.nice = -1024;
8535 else if (rule->arg.nice > 1024)
8536 rule->arg.nice = 1024;
8537 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008538 } else if (!strcmp(args[0], "set-tos")) {
8539#ifdef IP_TOS
8540 char *err;
8541 rule->action = HTTP_RES_ACT_SET_TOS;
8542 cur_arg = 1;
8543
8544 if (!*args[cur_arg] ||
8545 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8546 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8547 file, linenum, args[0]);
8548 goto out_err;
8549 }
8550
8551 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8552 if (err && *err != '\0') {
8553 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8554 file, linenum, err, args[0]);
8555 goto out_err;
8556 }
8557 cur_arg++;
8558#else
8559 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8560 goto out_err;
8561#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008562 } else if (!strcmp(args[0], "set-mark")) {
8563#ifdef SO_MARK
8564 char *err;
8565 rule->action = HTTP_RES_ACT_SET_MARK;
8566 cur_arg = 1;
8567
8568 if (!*args[cur_arg] ||
8569 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8570 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8571 file, linenum, args[0]);
8572 goto out_err;
8573 }
8574
8575 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8576 if (err && *err != '\0') {
8577 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8578 file, linenum, err, args[0]);
8579 goto out_err;
8580 }
8581 cur_arg++;
8582 global.last_checks |= LSTCHK_NETADM;
8583#else
8584 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8585 goto out_err;
8586#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008587 } else if (!strcmp(args[0], "set-log-level")) {
8588 rule->action = HTTP_RES_ACT_SET_LOGL;
8589 cur_arg = 1;
8590
8591 if (!*args[cur_arg] ||
8592 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8593 bad_log_level:
8594 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8595 file, linenum, args[0]);
8596 goto out_err;
8597 }
8598 if (strcmp(args[cur_arg], "silent") == 0)
8599 rule->arg.loglevel = -1;
8600 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8601 goto bad_log_level;
8602 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008603 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8604 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8605 cur_arg = 1;
8606
8607 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8608 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8609 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8610 file, linenum, args[0]);
8611 goto out_err;
8612 }
8613
8614 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8615 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8616 LIST_INIT(&rule->arg.hdr_add.fmt);
8617
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008618 proxy->conf.args.ctx = ARGC_HRS;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008619 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8620 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008621 free(proxy->conf.lfs_file);
8622 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8623 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008624 cur_arg += 2;
8625 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008626 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 +02008627 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8628 goto out_err;
8629 }
8630
8631 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8632 struct acl_cond *cond;
8633 char *errmsg = NULL;
8634
8635 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8636 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8637 file, linenum, args[0], errmsg);
8638 free(errmsg);
8639 goto out_err;
8640 }
8641 rule->cond = cond;
8642 }
8643 else if (*args[cur_arg]) {
8644 Alert("parsing [%s:%d]: 'http-response %s' expects"
8645 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8646 file, linenum, args[0], args[cur_arg]);
8647 goto out_err;
8648 }
8649
8650 return rule;
8651 out_err:
8652 free(rule);
8653 return NULL;
8654}
8655
Willy Tarreau4baae242012-12-27 12:00:31 +01008656/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008657 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8658 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008659 */
8660struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008661 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008662{
8663 struct redirect_rule *rule;
8664 int cur_arg;
8665 int type = REDIRECT_TYPE_NONE;
8666 int code = 302;
8667 const char *destination = NULL;
8668 const char *cookie = NULL;
8669 int cookie_set = 0;
8670 unsigned int flags = REDIRECT_FLAG_NONE;
8671 struct acl_cond *cond = NULL;
8672
8673 cur_arg = 0;
8674 while (*(args[cur_arg])) {
8675 if (strcmp(args[cur_arg], "location") == 0) {
8676 if (!*args[cur_arg + 1])
8677 goto missing_arg;
8678
8679 type = REDIRECT_TYPE_LOCATION;
8680 cur_arg++;
8681 destination = args[cur_arg];
8682 }
8683 else if (strcmp(args[cur_arg], "prefix") == 0) {
8684 if (!*args[cur_arg + 1])
8685 goto missing_arg;
8686
8687 type = REDIRECT_TYPE_PREFIX;
8688 cur_arg++;
8689 destination = args[cur_arg];
8690 }
8691 else if (strcmp(args[cur_arg], "scheme") == 0) {
8692 if (!*args[cur_arg + 1])
8693 goto missing_arg;
8694
8695 type = REDIRECT_TYPE_SCHEME;
8696 cur_arg++;
8697 destination = args[cur_arg];
8698 }
8699 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8700 if (!*args[cur_arg + 1])
8701 goto missing_arg;
8702
8703 cur_arg++;
8704 cookie = args[cur_arg];
8705 cookie_set = 1;
8706 }
8707 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8708 if (!*args[cur_arg + 1])
8709 goto missing_arg;
8710
8711 cur_arg++;
8712 cookie = args[cur_arg];
8713 cookie_set = 0;
8714 }
8715 else if (strcmp(args[cur_arg], "code") == 0) {
8716 if (!*args[cur_arg + 1])
8717 goto missing_arg;
8718
8719 cur_arg++;
8720 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008721 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008722 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008723 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008724 args[cur_arg - 1], args[cur_arg]);
8725 return NULL;
8726 }
8727 }
8728 else if (!strcmp(args[cur_arg],"drop-query")) {
8729 flags |= REDIRECT_FLAG_DROP_QS;
8730 }
8731 else if (!strcmp(args[cur_arg],"append-slash")) {
8732 flags |= REDIRECT_FLAG_APPEND_SLASH;
8733 }
8734 else if (strcmp(args[cur_arg], "if") == 0 ||
8735 strcmp(args[cur_arg], "unless") == 0) {
8736 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8737 if (!cond) {
8738 memprintf(errmsg, "error in condition: %s", *errmsg);
8739 return NULL;
8740 }
8741 break;
8742 }
8743 else {
8744 memprintf(errmsg,
8745 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8746 args[cur_arg]);
8747 return NULL;
8748 }
8749 cur_arg++;
8750 }
8751
8752 if (type == REDIRECT_TYPE_NONE) {
8753 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8754 return NULL;
8755 }
8756
8757 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8758 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008759 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008760
8761 if (!use_fmt) {
8762 /* old-style static redirect rule */
8763 rule->rdr_str = strdup(destination);
8764 rule->rdr_len = strlen(destination);
8765 }
8766 else {
8767 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008768
8769 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8770 * if prefix == "/", we don't want to add anything, otherwise it
8771 * makes it hard for the user to configure a self-redirection.
8772 */
8773 proxy->conf.args.ctx = ARGC_RDR;
8774 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
8775 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, 0,
8776 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008777 free(curproxy->conf.lfs_file);
8778 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8779 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008780 }
8781 }
8782
Willy Tarreau4baae242012-12-27 12:00:31 +01008783 if (cookie) {
8784 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8785 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8786 */
8787 rule->cookie_len = strlen(cookie);
8788 if (cookie_set) {
8789 rule->cookie_str = malloc(rule->cookie_len + 10);
8790 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8791 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8792 rule->cookie_len += 9;
8793 } else {
8794 rule->cookie_str = malloc(rule->cookie_len + 21);
8795 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8796 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8797 rule->cookie_len += 20;
8798 }
8799 }
8800 rule->type = type;
8801 rule->code = code;
8802 rule->flags = flags;
8803 LIST_INIT(&rule->list);
8804 return rule;
8805
8806 missing_arg:
8807 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8808 return NULL;
8809}
8810
Willy Tarreau8797c062007-05-07 00:55:35 +02008811/************************************************************************/
8812/* The code below is dedicated to ACL parsing and matching */
8813/************************************************************************/
8814
8815
Willy Tarreau14174bc2012-04-16 14:34:04 +02008816/* This function ensures that the prerequisites for an L7 fetch are ready,
8817 * which means that a request or response is ready. If some data is missing,
8818 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008819 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8820 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008821 *
8822 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008823 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8824 * decide whether or not an HTTP message is present ;
8825 * 0 if the requested data cannot be fetched or if it is certain that
8826 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008827 * 1 if an HTTP message is ready
8828 */
8829static int
Willy Tarreau506d0502013-07-06 13:29:24 +02008830smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008831 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008832{
8833 struct http_txn *txn = l7;
8834 struct http_msg *msg = &txn->req;
8835
8836 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8837 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8838 */
8839
8840 if (unlikely(!s || !txn))
8841 return 0;
8842
8843 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008844 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008845
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008846 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008847 if (unlikely(!s->req))
8848 return 0;
8849
Willy Tarreauaae75e32013-03-29 12:31:49 +01008850 /* If the buffer does not leave enough free space at the end,
8851 * we must first realign it.
8852 */
8853 if (s->req->buf->p > s->req->buf->data &&
8854 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8855 buffer_slow_realign(s->req->buf);
8856
Willy Tarreau14174bc2012-04-16 14:34:04 +02008857 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008858 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008859 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008860
8861 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008862 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008863 http_msg_analyzer(msg, &txn->hdr_idx);
8864
8865 /* Still no valid request ? */
8866 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008867 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008868 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008869 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008870 }
8871 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008872 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008873 return 0;
8874 }
8875
8876 /* OK we just got a valid HTTP request. We have some minor
8877 * preparation to perform so that further checks can rely
8878 * on HTTP tests.
8879 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008880
8881 /* If the request was parsed but was too large, we must absolutely
8882 * return an error so that it is not processed. At the moment this
8883 * cannot happen, but if the parsers are to change in the future,
8884 * we want this check to be maintained.
8885 */
8886 if (unlikely(s->req->buf->i + s->req->buf->p >
8887 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8888 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02008889 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008890 return 1;
8891 }
8892
Willy Tarreau9b28e032012-10-12 23:49:43 +02008893 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008894 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8895 s->flags |= SN_REDIRECTABLE;
8896
Willy Tarreau506d0502013-07-06 13:29:24 +02008897 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8898 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008899 }
8900
Willy Tarreau506d0502013-07-06 13:29:24 +02008901 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008902 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008903 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008904
8905 /* otherwise everything's ready for the request */
8906 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008907 else {
8908 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008909 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8910 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008911 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008912 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008913 }
8914
8915 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02008916 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008917 return 1;
8918}
Willy Tarreau8797c062007-05-07 00:55:35 +02008919
Willy Tarreauc0239e02012-04-16 14:42:55 +02008920#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008921 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 +02008922
Willy Tarreau24e32d82012-04-23 23:55:44 +02008923#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02008924 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 +02008925
Willy Tarreau8797c062007-05-07 00:55:35 +02008926
8927/* 1. Check on METHOD
8928 * We use the pre-parsed method if it is known, and store its number as an
8929 * integer. If it is unknown, we use the pointer and the length.
8930 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008931static 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 +02008932{
8933 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008934 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02008935
Willy Tarreauae8b7962007-06-09 23:10:04 +02008936 len = strlen(*text);
8937 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008938
8939 pattern->val.i = meth;
8940 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008941 if (usage == PAT_U_COMPILE) {
8942 pattern->ptr.str = strdup(*text);
8943 if (!pattern->ptr.str) {
8944 memprintf(err, "out of memory while loading pattern");
8945 return 0;
8946 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008947 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01008948 else {
8949 trash = get_trash_chunk();
8950 if (trash->size < len) {
8951 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
8952 len, trash->size);
8953 return 0;
8954 }
8955 pattern->ptr.str = trash->str;
8956 }
8957 pattern->expect_type = SMP_T_CSTR;
Willy Tarreau8797c062007-05-07 00:55:35 +02008958 pattern->len = len;
8959 }
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +01008960 else
8961 pattern->expect_type = SMP_T_UINT;
Willy Tarreau8797c062007-05-07 00:55:35 +02008962 return 1;
8963}
8964
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008965/* This function fetches the method of current HTTP request and stores
8966 * it in the global pattern struct as a chunk. There are two possibilities :
8967 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8968 * in <len> and <ptr> is NULL ;
8969 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8970 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01008971 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008972 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008973static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008974smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02008975 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02008976{
8977 int meth;
8978 struct http_txn *txn = l7;
8979
Willy Tarreau24e32d82012-04-23 23:55:44 +02008980 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008981
Willy Tarreau8797c062007-05-07 00:55:35 +02008982 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008983 smp->type = SMP_T_UINT;
8984 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008985 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008986 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8987 /* ensure the indexes are not affected */
8988 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008989 smp->type = SMP_T_CSTR;
8990 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008991 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008992 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008993 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008994 return 1;
8995}
8996
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008997/* See above how the method is stored in the global pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +01008998static enum pat_match_res pat_match_meth(struct sample *smp, struct pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008999{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009000 int icase;
9001
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009002
Willy Tarreauf853c462012-04-23 18:53:56 +02009003 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009004 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02009005 if (smp->data.uint == pattern->val.i)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009006 return PAT_MATCH;
9007 return PAT_NOMATCH;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009008 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009009
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009010 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
9011 if (pattern->val.i != HTTP_METH_OTHER)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009012 return PAT_NOMATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009013
9014 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02009015 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009016 return PAT_NOMATCH;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009017
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009018 icase = pattern->flags & PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02009019 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
9020 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009021 return PAT_NOMATCH;
9022 return PAT_MATCH;
Willy Tarreau8797c062007-05-07 00:55:35 +02009023}
9024
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009025static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009026smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009027 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009028{
9029 struct http_txn *txn = l7;
9030 char *ptr;
9031 int len;
9032
Willy Tarreauc0239e02012-04-16 14:42:55 +02009033 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009034
Willy Tarreau8797c062007-05-07 00:55:35 +02009035 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009036 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009037
9038 while ((len-- > 0) && (*ptr++ != '/'));
9039 if (len <= 0)
9040 return 0;
9041
Willy Tarreauf853c462012-04-23 18:53:56 +02009042 smp->type = SMP_T_CSTR;
9043 smp->data.str.str = ptr;
9044 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009045
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009046 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009047 return 1;
9048}
9049
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009050static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009051smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009052 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009053{
9054 struct http_txn *txn = l7;
9055 char *ptr;
9056 int len;
9057
Willy Tarreauc0239e02012-04-16 14:42:55 +02009058 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009059
Willy Tarreauf26b2522012-12-14 08:33:14 +01009060 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9061 return 0;
9062
Willy Tarreau8797c062007-05-07 00:55:35 +02009063 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009064 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009065
9066 while ((len-- > 0) && (*ptr++ != '/'));
9067 if (len <= 0)
9068 return 0;
9069
Willy Tarreauf853c462012-04-23 18:53:56 +02009070 smp->type = SMP_T_CSTR;
9071 smp->data.str.str = ptr;
9072 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009073
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009074 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009075 return 1;
9076}
9077
9078/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009079static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009080smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009081 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009082{
9083 struct http_txn *txn = l7;
9084 char *ptr;
9085 int len;
9086
Willy Tarreauc0239e02012-04-16 14:42:55 +02009087 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009088
Willy Tarreauf26b2522012-12-14 08:33:14 +01009089 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9090 return 0;
9091
Willy Tarreau8797c062007-05-07 00:55:35 +02009092 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009093 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009094
Willy Tarreauf853c462012-04-23 18:53:56 +02009095 smp->type = SMP_T_UINT;
9096 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009097 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009098 return 1;
9099}
9100
9101/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009102static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009103smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009104 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009105{
9106 struct http_txn *txn = l7;
9107
Willy Tarreauc0239e02012-04-16 14:42:55 +02009108 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009109
Willy Tarreauf853c462012-04-23 18:53:56 +02009110 smp->type = SMP_T_CSTR;
9111 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009112 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02009113 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009114 return 1;
9115}
9116
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009117static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009118smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009119 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009120{
9121 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009122 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009123
Willy Tarreauc0239e02012-04-16 14:42:55 +02009124 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009125
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009126 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9127 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009128 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009129
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009130 smp->type = SMP_T_IPV4;
9131 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009132 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009133 return 1;
9134}
9135
9136static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009137smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009138 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009139{
9140 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009141 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009142
Willy Tarreauc0239e02012-04-16 14:42:55 +02009143 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009144
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009145 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr);
9146 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9147 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009148
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009149 smp->type = SMP_T_UINT;
9150 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009151 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009152 return 1;
9153}
9154
Willy Tarreau185b5c42012-04-26 15:11:51 +02009155/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9156 * Accepts an optional argument of type string containing the header field name,
9157 * and an optional argument of type signed or unsigned integer to request an
9158 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009159 * headers are considered from the first one. It does not stop on commas and
9160 * returns full lines instead (useful for User-Agent or Date for example).
9161 */
9162static int
9163smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009164 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009165{
9166 struct http_txn *txn = l7;
9167 struct hdr_idx *idx = &txn->hdr_idx;
9168 struct hdr_ctx *ctx = smp->ctx.a[0];
9169 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9170 int occ = 0;
9171 const char *name_str = NULL;
9172 int name_len = 0;
9173
9174 if (!ctx) {
9175 /* first call */
9176 ctx = &static_hdr_ctx;
9177 ctx->idx = 0;
9178 smp->ctx.a[0] = ctx;
9179 }
9180
9181 if (args) {
9182 if (args[0].type != ARGT_STR)
9183 return 0;
9184 name_str = args[0].data.str.str;
9185 name_len = args[0].data.str.len;
9186
9187 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9188 occ = args[1].data.uint;
9189 }
9190
9191 CHECK_HTTP_MESSAGE_FIRST();
9192
9193 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9194 /* search for header from the beginning */
9195 ctx->idx = 0;
9196
9197 if (!occ && !(opt & SMP_OPT_ITERATE))
9198 /* no explicit occurrence and single fetch => last header by default */
9199 occ = -1;
9200
9201 if (!occ)
9202 /* prepare to report multiple occurrences for ACL fetches */
9203 smp->flags |= SMP_F_NOT_LAST;
9204
9205 smp->type = SMP_T_CSTR;
9206 smp->flags |= SMP_F_VOL_HDR;
9207 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9208 return 1;
9209
9210 smp->flags &= ~SMP_F_NOT_LAST;
9211 return 0;
9212}
9213
9214/* 6. Check on HTTP header count. The number of occurrences is returned.
9215 * Accepts exactly 1 argument of type string. It does not stop on commas and
9216 * returns full lines instead (useful for User-Agent or Date for example).
9217 */
9218static int
9219smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009220 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009221{
9222 struct http_txn *txn = l7;
9223 struct hdr_idx *idx = &txn->hdr_idx;
9224 struct hdr_ctx ctx;
9225 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9226 int cnt;
9227
9228 if (!args || args->type != ARGT_STR)
9229 return 0;
9230
9231 CHECK_HTTP_MESSAGE_FIRST();
9232
9233 ctx.idx = 0;
9234 cnt = 0;
9235 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9236 cnt++;
9237
9238 smp->type = SMP_T_UINT;
9239 smp->data.uint = cnt;
9240 smp->flags = SMP_F_VOL_HDR;
9241 return 1;
9242}
9243
9244/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9245 * Accepts an optional argument of type string containing the header field name,
9246 * and an optional argument of type signed or unsigned integer to request an
9247 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009248 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009249 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009250static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009251smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009252 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009253{
9254 struct http_txn *txn = l7;
9255 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009256 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009257 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009258 int occ = 0;
9259 const char *name_str = NULL;
9260 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009261
Willy Tarreaua890d072013-04-02 12:01:06 +02009262 if (!ctx) {
9263 /* first call */
9264 ctx = &static_hdr_ctx;
9265 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009266 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009267 }
9268
Willy Tarreau185b5c42012-04-26 15:11:51 +02009269 if (args) {
9270 if (args[0].type != ARGT_STR)
9271 return 0;
9272 name_str = args[0].data.str.str;
9273 name_len = args[0].data.str.len;
9274
9275 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9276 occ = args[1].data.uint;
9277 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009278
Willy Tarreaue333ec92012-04-16 16:26:40 +02009279 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009280
Willy Tarreau185b5c42012-04-26 15:11:51 +02009281 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009282 /* search for header from the beginning */
9283 ctx->idx = 0;
9284
Willy Tarreau185b5c42012-04-26 15:11:51 +02009285 if (!occ && !(opt & SMP_OPT_ITERATE))
9286 /* no explicit occurrence and single fetch => last header by default */
9287 occ = -1;
9288
9289 if (!occ)
9290 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009291 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009292
Willy Tarreau185b5c42012-04-26 15:11:51 +02009293 smp->type = SMP_T_CSTR;
9294 smp->flags |= SMP_F_VOL_HDR;
9295 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 +02009296 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009297
Willy Tarreau37406352012-04-23 16:16:37 +02009298 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009299 return 0;
9300}
9301
Willy Tarreauc11416f2007-06-17 16:58:38 +02009302/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009303 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009304 */
9305static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009306smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009307 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009308{
9309 struct http_txn *txn = l7;
9310 struct hdr_idx *idx = &txn->hdr_idx;
9311 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009312 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009313 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009314
Willy Tarreau24e32d82012-04-23 23:55:44 +02009315 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009316 return 0;
9317
Willy Tarreaue333ec92012-04-16 16:26:40 +02009318 CHECK_HTTP_MESSAGE_FIRST();
9319
Willy Tarreau33a7e692007-06-10 19:45:56 +02009320 ctx.idx = 0;
9321 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009322 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 +02009323 cnt++;
9324
Willy Tarreauf853c462012-04-23 18:53:56 +02009325 smp->type = SMP_T_UINT;
9326 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009327 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009328 return 1;
9329}
9330
Willy Tarreau185b5c42012-04-26 15:11:51 +02009331/* Fetch an HTTP header's integer value. The integer value is returned. It
9332 * takes a mandatory argument of type string and an optional one of type int
9333 * to designate a specific occurrence. It returns an unsigned integer, which
9334 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009335 */
9336static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009337smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009338 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009339{
Willy Tarreauef38c392013-07-22 16:29:32 +02009340 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009341
Willy Tarreauf853c462012-04-23 18:53:56 +02009342 if (ret > 0) {
9343 smp->type = SMP_T_UINT;
9344 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9345 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009346
Willy Tarreaud53e2422012-04-16 17:21:11 +02009347 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009348}
9349
Cyril Bonté69fa9922012-10-25 00:01:06 +02009350/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9351 * and an optional one of type int to designate a specific occurrence.
9352 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009353 */
9354static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009355smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009356 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009357{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009358 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009359
Willy Tarreauef38c392013-07-22 16:29:32 +02009360 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009361 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9362 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009363 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009364 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009365 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009366 if (smp->data.str.len < temp->size - 1) {
9367 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9368 temp->str[smp->data.str.len] = '\0';
9369 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9370 smp->type = SMP_T_IPV6;
9371 break;
9372 }
9373 }
9374 }
9375
Willy Tarreaud53e2422012-04-16 17:21:11 +02009376 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009377 if (!(smp->flags & SMP_F_NOT_LAST))
9378 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009379 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009380 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009381}
9382
Willy Tarreau737b0c12007-06-10 21:28:46 +02009383/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9384 * the first '/' after the possible hostname, and ends before the possible '?'.
9385 */
9386static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009387smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009388 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009389{
9390 struct http_txn *txn = l7;
9391 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009392
Willy Tarreauc0239e02012-04-16 14:42:55 +02009393 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009394
Willy Tarreau9b28e032012-10-12 23:49:43 +02009395 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009396 ptr = http_get_path(txn);
9397 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009398 return 0;
9399
9400 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02009401 smp->type = SMP_T_CSTR;
9402 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009403
9404 while (ptr < end && *ptr != '?')
9405 ptr++;
9406
Willy Tarreauf853c462012-04-23 18:53:56 +02009407 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02009408 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009409 return 1;
9410}
9411
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009412/* This produces a concatenation of the first occurrence of the Host header
9413 * followed by the path component if it begins with a slash ('/'). This means
9414 * that '*' will not be added, resulting in exactly the first Host entry.
9415 * If no Host header is found, then the path is returned as-is. The returned
9416 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009417 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009418 */
9419static int
9420smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009421 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009422{
9423 struct http_txn *txn = l7;
9424 char *ptr, *end, *beg;
9425 struct hdr_ctx ctx;
9426
9427 CHECK_HTTP_MESSAGE_FIRST();
9428
9429 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009430 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 +02009431 !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009432 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009433
9434 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009435 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009436 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009437 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009438 smp->data.str.len = ctx.vlen;
9439
9440 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009441 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 +02009442 beg = http_get_path(txn);
9443 if (!beg)
9444 beg = end;
9445
9446 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9447
9448 if (beg < ptr && *beg == '/') {
9449 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9450 smp->data.str.len += ptr - beg;
9451 }
9452
9453 smp->flags = SMP_F_VOL_1ST;
9454 return 1;
9455}
9456
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009457/* This produces a 32-bit hash of the concatenation of the first occurrence of
9458 * the Host header followed by the path component if it begins with a slash ('/').
9459 * This means that '*' will not be added, resulting in exactly the first Host
9460 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009461 * is hashed using the path hash followed by a full avalanche hash and provides a
9462 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009463 * high-traffic sites without having to store whole paths.
9464 */
9465static int
9466smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009467 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009468{
9469 struct http_txn *txn = l7;
9470 struct hdr_ctx ctx;
9471 unsigned int hash = 0;
9472 char *ptr, *beg, *end;
9473 int len;
9474
9475 CHECK_HTTP_MESSAGE_FIRST();
9476
9477 ctx.idx = 0;
9478 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
9479 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9480 ptr = ctx.line + ctx.val;
9481 len = ctx.vlen;
9482 while (len--)
9483 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9484 }
9485
9486 /* now retrieve the path */
9487 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
9488 beg = http_get_path(txn);
9489 if (!beg)
9490 beg = end;
9491
9492 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9493
9494 if (beg < ptr && *beg == '/') {
9495 while (beg < ptr)
9496 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9497 }
9498 hash = full_hash(hash);
9499
9500 smp->type = SMP_T_UINT;
9501 smp->data.uint = hash;
9502 smp->flags = SMP_F_VOL_1ST;
9503 return 1;
9504}
9505
Willy Tarreau4a550602012-12-09 14:53:32 +01009506/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009507 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9508 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9509 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009510 * that in environments where IPv6 is insignificant, truncating the output to
9511 * 8 bytes would still work.
9512 */
9513static int
9514smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009515 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009516{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009517 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009518 struct connection *cli_conn = objt_conn(l4->si[0].end);
9519
9520 if (!cli_conn)
9521 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009522
Willy Tarreauef38c392013-07-22 16:29:32 +02009523 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009524 return 0;
9525
Willy Tarreau47ca5452012-12-23 20:22:19 +01009526 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009527 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9528 temp->len += sizeof(smp->data.uint);
9529
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009530 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009531 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009532 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009533 temp->len += 4;
9534 break;
9535 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009536 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009537 temp->len += 16;
9538 break;
9539 default:
9540 return 0;
9541 }
9542
9543 smp->data.str = *temp;
9544 smp->type = SMP_T_BIN;
9545 return 1;
9546}
9547
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009548static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009549smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009550 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009551{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009552 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9553 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9554 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009555
Willy Tarreau24e32d82012-04-23 23:55:44 +02009556 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009557
Willy Tarreauf853c462012-04-23 18:53:56 +02009558 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009559 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009560 return 1;
9561}
9562
Willy Tarreau7f18e522010-10-22 20:04:13 +02009563/* return a valid test if the current request is the first one on the connection */
9564static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009565smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009566 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009567{
9568 if (!s)
9569 return 0;
9570
Willy Tarreauf853c462012-04-23 18:53:56 +02009571 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009572 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009573 return 1;
9574}
9575
Willy Tarreau34db1082012-04-19 17:16:54 +02009576/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009577static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009578smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009579 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009580{
9581
Willy Tarreau24e32d82012-04-23 23:55:44 +02009582 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009583 return 0;
9584
Willy Tarreauc0239e02012-04-16 14:42:55 +02009585 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009586
Willy Tarreauc0239e02012-04-16 14:42:55 +02009587 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009588 return 0;
9589
Willy Tarreauf853c462012-04-23 18:53:56 +02009590 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009591 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 +01009592 return 1;
9593}
Willy Tarreau8797c062007-05-07 00:55:35 +02009594
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009595/* Accepts exactly 1 argument of type userlist */
9596static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009597smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009598 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009599{
9600
9601 if (!args || args->type != ARGT_USR)
9602 return 0;
9603
9604 CHECK_HTTP_MESSAGE_FIRST();
9605
9606 if (!get_http_auth(l4))
9607 return 0;
9608
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009609 /* pat_match_auth() will need several information at once */
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009610 smp->ctx.a[0] = args->data.usr; /* user list */
9611 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9612 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9613
9614 /* if the user does not belong to the userlist or has a wrong password,
9615 * report that it unconditionally does not match. Otherwise we return
9616 * a non-zero integer which will be ignored anyway since all the params
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009617 * that pat_match_auth() will use are in test->ctx.a[0,1,2].
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009618 */
9619 smp->type = SMP_T_BOOL;
9620 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9621 if (smp->data.uint)
9622 smp->type = SMP_T_UINT;
9623
9624 return 1;
9625}
9626
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009627/* Try to find the next occurrence of a cookie name in a cookie header value.
9628 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9629 * the cookie value is returned into *value and *value_l, and the function
9630 * returns a pointer to the next pointer to search from if the value was found.
9631 * Otherwise if the cookie was not found, NULL is returned and neither value
9632 * nor value_l are touched. The input <hdr> string should first point to the
9633 * header's value, and the <hdr_end> pointer must point to the first character
9634 * not part of the value. <list> must be non-zero if value may represent a list
9635 * of values (cookie headers). This makes it faster to abort parsing when no
9636 * list is expected.
9637 */
9638static char *
9639extract_cookie_value(char *hdr, const char *hdr_end,
9640 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009641 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009642{
9643 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9644 char *next;
9645
9646 /* we search at least a cookie name followed by an equal, and more
9647 * generally something like this :
9648 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9649 */
9650 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9651 /* Iterate through all cookies on this line */
9652
9653 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9654 att_beg++;
9655
9656 /* find att_end : this is the first character after the last non
9657 * space before the equal. It may be equal to hdr_end.
9658 */
9659 equal = att_end = att_beg;
9660
9661 while (equal < hdr_end) {
9662 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9663 break;
9664 if (http_is_spht[(unsigned char)*equal++])
9665 continue;
9666 att_end = equal;
9667 }
9668
9669 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9670 * is between <att_beg> and <equal>, both may be identical.
9671 */
9672
9673 /* look for end of cookie if there is an equal sign */
9674 if (equal < hdr_end && *equal == '=') {
9675 /* look for the beginning of the value */
9676 val_beg = equal + 1;
9677 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9678 val_beg++;
9679
9680 /* find the end of the value, respecting quotes */
9681 next = find_cookie_value_end(val_beg, hdr_end);
9682
9683 /* make val_end point to the first white space or delimitor after the value */
9684 val_end = next;
9685 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9686 val_end--;
9687 } else {
9688 val_beg = val_end = next = equal;
9689 }
9690
9691 /* We have nothing to do with attributes beginning with '$'. However,
9692 * they will automatically be removed if a header before them is removed,
9693 * since they're supposed to be linked together.
9694 */
9695 if (*att_beg == '$')
9696 continue;
9697
9698 /* Ignore cookies with no equal sign */
9699 if (equal == next)
9700 continue;
9701
9702 /* Now we have the cookie name between att_beg and att_end, and
9703 * its value between val_beg and val_end.
9704 */
9705
9706 if (att_end - att_beg == cookie_name_l &&
9707 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9708 /* let's return this value and indicate where to go on from */
9709 *value = val_beg;
9710 *value_l = val_end - val_beg;
9711 return next + 1;
9712 }
9713
9714 /* Set-Cookie headers only have the name in the first attr=value part */
9715 if (!list)
9716 break;
9717 }
9718
9719 return NULL;
9720}
9721
William Lallemanda43ba4e2014-01-28 18:14:25 +01009722/* Fetch a captured HTTP request header. The index is the position of
9723 * the "capture" option in the configuration file
9724 */
9725static int
9726smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9727 const struct arg *args, struct sample *smp, const char *kw)
9728{
9729 struct proxy *fe = l4->fe;
9730 struct http_txn *txn = l7;
9731 int idx;
9732
9733 if (!args || args->type != ARGT_UINT)
9734 return 0;
9735
9736 idx = args->data.uint;
9737
9738 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9739 return 0;
9740
9741 smp->type = SMP_T_CSTR;
9742 smp->data.str.str = txn->req.cap[idx];
9743 smp->data.str.len = strlen(txn->req.cap[idx]);
9744
9745 return 1;
9746}
9747
9748/* Fetch a captured HTTP response header. The index is the position of
9749 * the "capture" option in the configuration file
9750 */
9751static int
9752smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9753 const struct arg *args, struct sample *smp, const char *kw)
9754{
9755 struct proxy *fe = l4->fe;
9756 struct http_txn *txn = l7;
9757 int idx;
9758
9759 if (!args || args->type != ARGT_UINT)
9760 return 0;
9761
9762 idx = args->data.uint;
9763
9764 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9765 return 0;
9766
9767 smp->type = SMP_T_CSTR;
9768 smp->data.str.str = txn->rsp.cap[idx];
9769 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9770
9771 return 1;
9772}
9773
William Lallemand65ad6e12014-01-31 15:08:02 +01009774/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9775static int
9776smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9777 const struct arg *args, struct sample *smp, const char *kw)
9778{
9779 struct chunk *temp;
9780 struct http_txn *txn = l7;
9781 char *spc;
9782 int len;
9783
9784 if (!txn->uri)
9785 return 0;
9786
9787 spc = strchr(txn->uri, ' '); /* first space before URI */
9788 if (likely(spc))
9789 len = spc - txn->uri;
9790 else
9791 len = strlen(txn->uri);
9792
9793 temp = get_trash_chunk();
9794 len = MIN(len, temp->size - 1);
9795 strncpy(temp->str, txn->uri, len);
9796 temp->str[len] = '\0';
9797
9798 smp->data.str = *temp;
9799 smp->data.str.len = len;
9800 smp->type = SMP_T_STR;
9801
9802 return 1;
9803
9804}
9805
9806/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9807static int
9808smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9809 const struct arg *args, struct sample *smp, const char *kw)
9810{
9811 struct chunk *temp;
9812 struct http_txn *txn = l7;
9813 char *ptr;
9814 char *ret;
9815
9816 if (!txn->uri)
9817 return 0;
9818 ptr = txn->uri;
9819
9820 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9821 ptr++;
9822 if (!*ptr)
9823 return 0;
9824
9825 ptr++; /* skip the space */
9826
9827 temp = get_trash_chunk();
9828 ret = encode_string(temp->str, temp->str + temp->size, '#', url_encode_map, ptr);
9829 if (ret == NULL || *ret != '\0')
9830 return 0;
9831 ptr = temp->str = http_get_path_from_string(temp->str);
9832 if (!ptr)
9833 return 0;
9834 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
9835 ptr++;
9836 *ptr = '\0';
9837
9838 smp->data.str = *temp;
9839 smp->data.str.len = strlen(smp->data.str.str);
9840 smp->type = SMP_T_STR;
9841
9842 return 1;
9843}
9844
9845
Willy Tarreaue333ec92012-04-16 16:26:40 +02009846/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009847 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009848 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009849 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009850 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009851 * Accepts exactly 1 argument of type string. If the input options indicate
9852 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009853 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009854 */
9855static int
Willy Tarreau51539362012-05-08 12:46:28 +02009856smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009857 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009858{
9859 struct http_txn *txn = l7;
9860 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009861 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009862 const struct http_msg *msg;
9863 const char *hdr_name;
9864 int hdr_name_len;
9865 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009866 int occ = 0;
9867 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009868
Willy Tarreau24e32d82012-04-23 23:55:44 +02009869 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009870 return 0;
9871
Willy Tarreaua890d072013-04-02 12:01:06 +02009872 if (!ctx) {
9873 /* first call */
9874 ctx = &static_hdr_ctx;
9875 ctx->idx = 0;
9876 smp->ctx.a[2] = ctx;
9877 }
9878
Willy Tarreaue333ec92012-04-16 16:26:40 +02009879 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009880
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009881 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009882 msg = &txn->req;
9883 hdr_name = "Cookie";
9884 hdr_name_len = 6;
9885 } else {
9886 msg = &txn->rsp;
9887 hdr_name = "Set-Cookie";
9888 hdr_name_len = 10;
9889 }
9890
Willy Tarreau28376d62012-04-26 21:26:10 +02009891 if (!occ && !(opt & SMP_OPT_ITERATE))
9892 /* no explicit occurrence and single fetch => last cookie by default */
9893 occ = -1;
9894
9895 /* OK so basically here, either we want only one value and it's the
9896 * last one, or we want to iterate over all of them and we fetch the
9897 * next one.
9898 */
9899
Willy Tarreau9b28e032012-10-12 23:49:43 +02009900 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009901 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009902 /* search for the header from the beginning, we must first initialize
9903 * the search parameters.
9904 */
Willy Tarreau37406352012-04-23 16:16:37 +02009905 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009906 ctx->idx = 0;
9907 }
9908
Willy Tarreau28376d62012-04-26 21:26:10 +02009909 smp->flags |= SMP_F_VOL_HDR;
9910
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009911 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009912 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9913 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009914 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9915 goto out;
9916
Willy Tarreau24e32d82012-04-23 23:55:44 +02009917 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009918 continue;
9919
Willy Tarreau37406352012-04-23 16:16:37 +02009920 smp->ctx.a[0] = ctx->line + ctx->val;
9921 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009922 }
9923
Willy Tarreauf853c462012-04-23 18:53:56 +02009924 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009925 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009926 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009927 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009928 &smp->data.str.str,
9929 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009930 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009931 found = 1;
9932 if (occ >= 0) {
9933 /* one value was returned into smp->data.str.{str,len} */
9934 smp->flags |= SMP_F_NOT_LAST;
9935 return 1;
9936 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009937 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009938 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009939 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009940 /* all cookie headers and values were scanned. If we're looking for the
9941 * last occurrence, we may return it now.
9942 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009943 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009944 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009945 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009946}
9947
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009948/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009949 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +01009950 * multiple cookies may be parsed on the same line. The returned sample is of
9951 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009952 */
9953static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009954smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009955 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009956{
9957 struct http_txn *txn = l7;
9958 struct hdr_idx *idx = &txn->hdr_idx;
9959 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009960 const struct http_msg *msg;
9961 const char *hdr_name;
9962 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009963 int cnt;
9964 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009965 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009966
Willy Tarreau24e32d82012-04-23 23:55:44 +02009967 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009968 return 0;
9969
Willy Tarreaue333ec92012-04-16 16:26:40 +02009970 CHECK_HTTP_MESSAGE_FIRST();
9971
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009972 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009973 msg = &txn->req;
9974 hdr_name = "Cookie";
9975 hdr_name_len = 6;
9976 } else {
9977 msg = &txn->rsp;
9978 hdr_name = "Set-Cookie";
9979 hdr_name_len = 10;
9980 }
9981
Willy Tarreau9b28e032012-10-12 23:49:43 +02009982 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009983 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009984 ctx.idx = 0;
9985 cnt = 0;
9986
9987 while (1) {
9988 /* Note: val_beg == NULL every time we need to fetch a new header */
9989 if (!val_beg) {
9990 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9991 break;
9992
Willy Tarreau24e32d82012-04-23 23:55:44 +02009993 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009994 continue;
9995
9996 val_beg = ctx.line + ctx.val;
9997 val_end = val_beg + ctx.vlen;
9998 }
9999
Willy Tarreauf853c462012-04-23 18:53:56 +020010000 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010001 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010002 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010003 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010004 &smp->data.str.str,
10005 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010006 cnt++;
10007 }
10008 }
10009
Willy Tarreaub169eba2013-12-16 15:14:43 +010010010 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010011 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010012 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010013 return 1;
10014}
10015
Willy Tarreau51539362012-05-08 12:46:28 +020010016/* Fetch an cookie's integer value. The integer value is returned. It
10017 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10018 */
10019static int
10020smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010021 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010022{
Willy Tarreauef38c392013-07-22 16:29:32 +020010023 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010024
10025 if (ret > 0) {
10026 smp->type = SMP_T_UINT;
10027 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10028 }
10029
10030 return ret;
10031}
10032
Willy Tarreau8797c062007-05-07 00:55:35 +020010033/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010034/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010035/************************************************************************/
10036
David Cournapeau16023ee2010-12-23 20:55:41 +090010037/*
10038 * Given a path string and its length, find the position of beginning of the
10039 * query string. Returns NULL if no query string is found in the path.
10040 *
10041 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10042 *
10043 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10044 */
bedis4c75cca2012-10-05 08:38:24 +020010045static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010046{
10047 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010048
bedis4c75cca2012-10-05 08:38:24 +020010049 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010050 return p ? p + 1 : NULL;
10051}
10052
bedis4c75cca2012-10-05 08:38:24 +020010053static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010054{
bedis4c75cca2012-10-05 08:38:24 +020010055 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010056}
10057
10058/*
10059 * Given a url parameter, find the starting position of the first occurence,
10060 * or NULL if the parameter is not found.
10061 *
10062 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10063 * the function will return query_string+8.
10064 */
10065static char*
10066find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010067 char* url_param_name, size_t url_param_name_l,
10068 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010069{
10070 char *pos, *last;
10071
10072 pos = query_string;
10073 last = query_string + query_string_l - url_param_name_l - 1;
10074
10075 while (pos <= last) {
10076 if (pos[url_param_name_l] == '=') {
10077 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10078 return pos;
10079 pos += url_param_name_l + 1;
10080 }
bedis4c75cca2012-10-05 08:38:24 +020010081 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010082 pos++;
10083 pos++;
10084 }
10085 return NULL;
10086}
10087
10088/*
10089 * Given a url parameter name, returns its value and size into *value and
10090 * *value_l respectively, and returns non-zero. If the parameter is not found,
10091 * zero is returned and value/value_l are not touched.
10092 */
10093static int
10094find_url_param_value(char* path, size_t path_l,
10095 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010096 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010097{
10098 char *query_string, *qs_end;
10099 char *arg_start;
10100 char *value_start, *value_end;
10101
bedis4c75cca2012-10-05 08:38:24 +020010102 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010103 if (!query_string)
10104 return 0;
10105
10106 qs_end = path + path_l;
10107 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010108 url_param_name, url_param_name_l,
10109 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010110 if (!arg_start)
10111 return 0;
10112
10113 value_start = arg_start + url_param_name_l + 1;
10114 value_end = value_start;
10115
bedis4c75cca2012-10-05 08:38:24 +020010116 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010117 value_end++;
10118
10119 *value = value_start;
10120 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010121 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010122}
10123
10124static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010125smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010126 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010127{
bedis4c75cca2012-10-05 08:38:24 +020010128 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010129 struct http_txn *txn = l7;
10130 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010131
bedis4c75cca2012-10-05 08:38:24 +020010132 if (!args || args[0].type != ARGT_STR ||
10133 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010134 return 0;
10135
10136 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010137
bedis4c75cca2012-10-05 08:38:24 +020010138 if (args[1].type)
10139 delim = *args[1].data.str.str;
10140
Willy Tarreau9b28e032012-10-12 23:49:43 +020010141 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010142 args->data.str.str, args->data.str.len,
10143 &smp->data.str.str, &smp->data.str.len,
10144 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010145 return 0;
10146
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +020010147 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010148 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010149 return 1;
10150}
10151
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010152/* Return the signed integer value for the specified url parameter (see url_param
10153 * above).
10154 */
10155static int
10156smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010157 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010158{
Willy Tarreauef38c392013-07-22 16:29:32 +020010159 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010160
10161 if (ret > 0) {
10162 smp->type = SMP_T_UINT;
10163 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10164 }
10165
10166 return ret;
10167}
10168
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010169/* This produces a 32-bit hash of the concatenation of the first occurrence of
10170 * the Host header followed by the path component if it begins with a slash ('/').
10171 * This means that '*' will not be added, resulting in exactly the first Host
10172 * entry. If no Host header is found, then the path is used. The resulting value
10173 * is hashed using the url hash followed by a full avalanche hash and provides a
10174 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10175 * high-traffic sites without having to store whole paths.
10176 * this differs from the base32 functions in that it includes the url parameters
10177 * as well as the path
10178 */
10179static int
10180smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010181 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010182{
10183 struct http_txn *txn = l7;
10184 struct hdr_ctx ctx;
10185 unsigned int hash = 0;
10186 char *ptr, *beg, *end;
10187 int len;
10188
10189 CHECK_HTTP_MESSAGE_FIRST();
10190
10191 ctx.idx = 0;
10192 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
10193 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10194 ptr = ctx.line + ctx.val;
10195 len = ctx.vlen;
10196 while (len--)
10197 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10198 }
10199
10200 /* now retrieve the path */
10201 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
10202 beg = http_get_path(txn);
10203 if (!beg)
10204 beg = end;
10205
10206 for (ptr = beg; ptr < end ; ptr++);
10207
10208 if (beg < ptr && *beg == '/') {
10209 while (beg < ptr)
10210 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10211 }
10212 hash = full_hash(hash);
10213
10214 smp->type = SMP_T_UINT;
10215 smp->data.uint = hash;
10216 smp->flags = SMP_F_VOL_1ST;
10217 return 1;
10218}
10219
10220/* This concatenates the source address with the 32-bit hash of the Host and
10221 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10222 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10223 * on the source address length. The URL hash is stored before the address so
10224 * that in environments where IPv6 is insignificant, truncating the output to
10225 * 8 bytes would still work.
10226 */
10227static int
10228smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010229 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010230{
10231 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010232 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010233
Willy Tarreaue155ec22013-11-18 18:33:22 +010010234 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010235 return 0;
10236
10237 temp = get_trash_chunk();
10238 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10239 temp->len += sizeof(smp->data.uint);
10240
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010241 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010242 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010243 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010244 temp->len += 4;
10245 break;
10246 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010247 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010248 temp->len += 16;
10249 break;
10250 default:
10251 return 0;
10252 }
10253
10254 smp->data.str = *temp;
10255 smp->type = SMP_T_BIN;
10256 return 1;
10257}
10258
Willy Tarreau185b5c42012-04-26 15:11:51 +020010259/* This function is used to validate the arguments passed to any "hdr" fetch
10260 * keyword. These keywords support an optional positive or negative occurrence
10261 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10262 * is assumed that the types are already the correct ones. Returns 0 on error,
10263 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10264 * error message in case of error, that the caller is responsible for freeing.
10265 * The initial location must either be freeable or NULL.
10266 */
10267static int val_hdr(struct arg *arg, char **err_msg)
10268{
10269 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010270 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010271 return 0;
10272 }
10273 return 1;
10274}
10275
Willy Tarreau276fae92013-07-25 14:36:01 +020010276/* takes an UINT value on input supposed to represent the time since EPOCH,
10277 * adds an optional offset found in args[0] and emits a string representing
10278 * the date in RFC-1123/5322 format.
10279 */
10280static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10281{
10282 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10283 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10284 struct chunk *temp;
10285 struct tm *tm;
10286 time_t curr_date = smp->data.uint;
10287
10288 /* add offset */
10289 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10290 curr_date += args[0].data.sint;
10291
10292 tm = gmtime(&curr_date);
10293
10294 temp = get_trash_chunk();
10295 temp->len = snprintf(temp->str, temp->size - temp->len,
10296 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10297 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10298 tm->tm_hour, tm->tm_min, tm->tm_sec);
10299
10300 smp->data.str = *temp;
10301 smp->type = SMP_T_STR;
10302 return 1;
10303}
10304
Willy Tarreau4a568972010-05-12 08:08:50 +020010305/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010306/* All supported ACL keywords must be declared here. */
10307/************************************************************************/
10308
10309/* Note: must not be declared <const> as its list will be overwritten.
10310 * Please take care of keeping this list alphabetically sorted.
10311 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010312static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010313 { "base", "base", pat_parse_str, pat_match_str },
10314 { "base_beg", "base", pat_parse_str, pat_match_beg },
10315 { "base_dir", "base", pat_parse_str, pat_match_dir },
10316 { "base_dom", "base", pat_parse_str, pat_match_dom },
10317 { "base_end", "base", pat_parse_str, pat_match_end },
10318 { "base_len", "base", pat_parse_int, pat_match_len },
10319 { "base_reg", "base", pat_parse_reg, pat_match_reg },
10320 { "base_sub", "base", pat_parse_str, pat_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010321
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010322 { "cook", "req.cook", pat_parse_str, pat_match_str },
10323 { "cook_beg", "req.cook", pat_parse_str, pat_match_beg },
10324 { "cook_dir", "req.cook", pat_parse_str, pat_match_dir },
10325 { "cook_dom", "req.cook", pat_parse_str, pat_match_dom },
10326 { "cook_end", "req.cook", pat_parse_str, pat_match_end },
10327 { "cook_len", "req.cook", pat_parse_int, pat_match_len },
10328 { "cook_reg", "req.cook", pat_parse_reg, pat_match_reg },
10329 { "cook_sub", "req.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010330
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010331 { "hdr", "req.hdr", pat_parse_str, pat_match_str },
10332 { "hdr_beg", "req.hdr", pat_parse_str, pat_match_beg },
10333 { "hdr_dir", "req.hdr", pat_parse_str, pat_match_dir },
10334 { "hdr_dom", "req.hdr", pat_parse_str, pat_match_dom },
10335 { "hdr_end", "req.hdr", pat_parse_str, pat_match_end },
10336 { "hdr_len", "req.hdr", pat_parse_int, pat_match_len },
10337 { "hdr_reg", "req.hdr", pat_parse_reg, pat_match_reg },
10338 { "hdr_sub", "req.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010339
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010340 { "http_auth_group", NULL, pat_parse_strcat, pat_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010341
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010342 { "method", NULL, pat_parse_meth, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010343
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010344 { "path", "path", pat_parse_str, pat_match_str },
10345 { "path_beg", "path", pat_parse_str, pat_match_beg },
10346 { "path_dir", "path", pat_parse_str, pat_match_dir },
10347 { "path_dom", "path", pat_parse_str, pat_match_dom },
10348 { "path_end", "path", pat_parse_str, pat_match_end },
10349 { "path_len", "path", pat_parse_int, pat_match_len },
10350 { "path_reg", "path", pat_parse_reg, pat_match_reg },
10351 { "path_sub", "path", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010352
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010353 { "req_ver", "req.ver", pat_parse_str, pat_match_str },
10354 { "resp_ver", "res.ver", pat_parse_str, pat_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010355
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010356 { "scook", "res.cook", pat_parse_str, pat_match_str },
10357 { "scook_beg", "res.cook", pat_parse_str, pat_match_beg },
10358 { "scook_dir", "res.cook", pat_parse_str, pat_match_dir },
10359 { "scook_dom", "res.cook", pat_parse_str, pat_match_dom },
10360 { "scook_end", "res.cook", pat_parse_str, pat_match_end },
10361 { "scook_len", "res.cook", pat_parse_int, pat_match_len },
10362 { "scook_reg", "res.cook", pat_parse_reg, pat_match_reg },
10363 { "scook_sub", "res.cook", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010364
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010365 { "shdr", "res.hdr", pat_parse_str, pat_match_str },
10366 { "shdr_beg", "res.hdr", pat_parse_str, pat_match_beg },
10367 { "shdr_dir", "res.hdr", pat_parse_str, pat_match_dir },
10368 { "shdr_dom", "res.hdr", pat_parse_str, pat_match_dom },
10369 { "shdr_end", "res.hdr", pat_parse_str, pat_match_end },
10370 { "shdr_len", "res.hdr", pat_parse_int, pat_match_len },
10371 { "shdr_reg", "res.hdr", pat_parse_reg, pat_match_reg },
10372 { "shdr_sub", "res.hdr", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010373
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010374 { "url", "url", pat_parse_str, pat_match_str },
10375 { "url_beg", "url", pat_parse_str, pat_match_beg },
10376 { "url_dir", "url", pat_parse_str, pat_match_dir },
10377 { "url_dom", "url", pat_parse_str, pat_match_dom },
10378 { "url_end", "url", pat_parse_str, pat_match_end },
10379 { "url_len", "url", pat_parse_int, pat_match_len },
10380 { "url_reg", "url", pat_parse_reg, pat_match_reg },
10381 { "url_sub", "url", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010382
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010383 { "urlp", "urlp", pat_parse_str, pat_match_str },
10384 { "urlp_beg", "urlp", pat_parse_str, pat_match_beg },
10385 { "urlp_dir", "urlp", pat_parse_str, pat_match_dir },
10386 { "urlp_dom", "urlp", pat_parse_str, pat_match_dom },
10387 { "urlp_end", "urlp", pat_parse_str, pat_match_end },
10388 { "urlp_len", "urlp", pat_parse_int, pat_match_len },
10389 { "urlp_reg", "urlp", pat_parse_reg, pat_match_reg },
10390 { "urlp_sub", "urlp", pat_parse_str, pat_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010391
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010392 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010393}};
10394
10395/************************************************************************/
10396/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010397/************************************************************************/
10398/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010399static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau409bcde2013-01-08 00:31:00 +010010400 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10401 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10402 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10403
William Lallemand65ad6e12014-01-31 15:08:02 +010010404 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10405 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10406
William Lallemanda43ba4e2014-01-28 18:14:25 +010010407 /* capture are allocated and are permanent in the session */
10408 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRQHP },
10409 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_CSTR, SMP_USE_HRSHP },
10410
Willy Tarreau409bcde2013-01-08 00:31:00 +010010411 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10412 * are only here to match the ACL's name, are request-only and are used
10413 * for ACL compatibility only.
10414 */
10415 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10416 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10417 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10418 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10419
10420 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10421 * only here to match the ACL's name, are request-only and are used for
10422 * ACL compatibility only.
10423 */
10424 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
10425 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10426 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10427 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10428
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010429 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
10430 { "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 +010010431 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
10432 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
10433 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010434
10435 /* HTTP protocol on the request path */
10436 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010437 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010438
10439 /* HTTP version on the request path */
10440 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010441 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010442
10443 /* HTTP version on the response path */
10444 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010445 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10446
Willy Tarreau18ed2562013-01-14 15:56:36 +010010447 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
10448 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10449 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10450 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10451
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010452 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10453 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010454 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
10455 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10456 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10457 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10458
10459 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
10460 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10461 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10462 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10463
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010464 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10465 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010466 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10467 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10468 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10469 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10470
Willy Tarreau409bcde2013-01-08 00:31:00 +010010471 /* scook is valid only on the response and is used for ACL compatibility */
10472 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
10473 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10474 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10475 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
10476
10477 /* shdr is valid only on the response and is used for ACL compatibility */
10478 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
10479 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10480 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10481 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10482
10483 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
10484 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010485 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10486 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010487 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10488 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10489 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10490 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
10491 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10492 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010493}};
10494
Willy Tarreau8797c062007-05-07 00:55:35 +020010495
Willy Tarreau276fae92013-07-25 14:36:01 +020010496/* Note: must not be declared <const> as its list will be overwritten */
10497static struct sample_conv_kw_list sample_conv_kws = {ILH, {
10498 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR },
10499 { NULL, NULL, 0, 0, 0 },
10500}};
10501
Willy Tarreau8797c062007-05-07 00:55:35 +020010502__attribute__((constructor))
10503static void __http_protocol_init(void)
10504{
10505 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010506 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010507 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010508}
10509
10510
Willy Tarreau58f10d72006-12-04 02:26:12 +010010511/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010512 * Local variables:
10513 * c-indent-level: 8
10514 * c-basic-offset: 8
10515 * End:
10516 */